Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Control/Toggle.hpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.
7 : : All rights reserved. See the LICENSE file for details.
8 : : \brief Toggle is the base for an Option, doing generic searches
9 : : \details Toggle is the base for an Option, doing generic searches.
10 : : */
11 : : // *****************************************************************************
12 : : #ifndef Toggle_h
13 : : #define Toggle_h
14 : :
15 : : #include <map>
16 : :
17 : : #include "Exception.hpp"
18 : : #include "PrintUtil.hpp"
19 : :
20 : : namespace tk {
21 : :
22 : : //! \brief Toggle is the base for an Option, doing generic searches
23 : : //! \details Toggle is templated on an enum type (a strongly typed enum), whose
24 : : //! values are used as keys in maps of associated option values.
25 : : //! \see Control/Option for client code examples
26 : : template< typename Enum >
27 : : class Toggle {
28 : :
29 : : public:
30 : : using EnumType = Enum; //! Used to access template typename from outside
31 : :
32 : : //! Lookup group name
33 [ + - ]: 2 : const std::string& group() const { return groupname; }
34 : :
35 : : //! Lookup Enum value based on keyword, Enum must exist
36 : : //! \param[in] keyword Keyword to search for
37 : : //! \return Strongly-typed enum value associated to keyword if found
38 : : Enum value( const std::string& keyword ) const {
39 : : auto it = values.find( keyword );
40 : : Assert( it != end(values),
41 : : std::string("Cannot find value for keyword \"") + keyword + "\"" );
42 [ + + ][ + - ]: 811 : return it->second;
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - - ]
[ + - ][ - - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ + - ]
[ - - ][ + - ]
[ + - ][ + - ]
[ - + ][ - - ]
[ - - ][ - - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
[ - + ][ - - ]
[ - - ][ - - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ - + ]
[ + - ][ + + ]
[ - + ][ + + ]
[ - + ][ + + ]
[ + + ][ + + ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
43 : : }
44 : :
45 : : //! Check if keyword exists
46 : : //! \param[in] keyword Keyword to search for
47 : : //! \return True if keyword if found
48 : : bool exist( const std::string& keyword ) const {
49 : : return values.find( keyword ) != end( values ) ? true : false;
50 : : }
51 : :
52 : : //! Lookup option name based on Enum
53 : : //! \param[in] val Enum value to search for
54 : : //! \return Keyword if enum value is found
55 : : const std::string& name( Enum val ) const {
56 : : auto it = names.find( val );
57 : : Assert( it != end(names),
58 : : std::string("Cannot find name for value \"") << val << "\"" );
59 [ + - ][ + - ]: 718 : return it->second;
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
60 : : }
61 : :
62 : : protected:
63 : : //! Constructor protected: designed to be used only as a base class
64 : : //! \param[in] g Group name of the option set
65 : : //! \param[in] n Map associating enum values to option names
66 : : //! \param[in] v Map associating keyword strings to enum values
67 : : //! \details Note that all arguments are rvalues, thus the objects gutted
68 : : //! and this is the only constructor provided.
69 [ + + ]: 24144 : explicit Toggle( std::string&& g,
70 : : std::map< Enum, std::string >&& n,
71 : : std::map< std::string, Enum >&& v ) :
72 : : groupname( std::move(g) ), names( std::move(n) ), values( std::move(v) )
73 : 24144 : { Assert( names.size() == values.size(), "map sizes differ in Toggle" ); }
74 : :
75 : : private:
76 : : std::string groupname; //!< Group name of the option set
77 : : std::map< Enum, std::string > names; //!< Map of enums -> names
78 : : std::map< std::string, Enum > values; //!< Map of keywords -> enums
79 : : };
80 : :
81 : : } // tk::
82 : :
83 : : #endif // Toggle_h
|