Debian Codegraph @

FaCT++.Python/lib/Kernel/ifOptions.h

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef IFOPTIONS_H
21 #define IFOPTIONS_H
22
23 /********************************************************\
24 |* Interface for the options management for the FaCT++ *|
25 \********************************************************/
26
27 #include <string>
28 #include <map>
29 #include <iosfwd>
30
31 #include "fpp_assert.h"
32
33 class Configuration;
34
35 /// class for working with general options with boolean, integer or text values
36 class ifOption
37 {
38 public: // type interface
39 /// type of an option
40 enum ioType { iotBool, iotInt, iotText };
41
42 protected: // members
43 /// option name
44 std::string optionName;
45 /// informal description
46 std::string optionDescription;
47 /// default value (name of type)
48 std::string defaultValue;
49 /// textual value [relevant iff (type == iotText)]
50 std::string tValue;
51 /// type of value: bool, int or text
52 ioType type;
53 /// integer value [relevant iff (type == iotInt)]
54 int iValue = 0;
55 /// boolean value [relevant iff (type == iotBool)]
56 bool bValue = 0;
57
58 public: // interface
59 /// c'tor (init all values including proper ?Value)
60 ifOption ( const std::string& name, const std::string& desc, ioType t, const std::string& defVal );
61 /// no empty c'tor
62 ifOption ( void ) = delete;
63 /// no copy c'tor
64 ifOption ( const ifOption& ) = delete;
65 /// no assignment
66 ifOption& operator = ( const ifOption& ) = delete;
67
68 // write methods
69
70 /// set boolean value; @return false in case of error
71 bool setValue ( bool b ) { bValue = b; return (type != iotBool); }
72 /// set integer value; @return false in case of error
73 bool setValue ( int i ) { iValue = i; return (type != iotInt); }
74 /// set string value; @return false in case of error
75 bool setValue ( const std::string& t ) { tValue = t; return (type != iotText); }
76 /// set textually given value of current type; @return false in case of error
77 bool setAValue ( const std::string& s );
78
79 // access methods
80 /// get value of a Boolean option
81 bool getBool ( void ) const { fpp_assert ( type == iotBool ); return bValue; }
82 /// get value of an integer option
83 int getInt ( void ) const { fpp_assert ( type == iotInt ); return iValue; }
84 /// get value of a string option
85 const std::string& getText ( void ) const { fpp_assert ( type == iotText ); return tValue; }
86
87 /// output in the form of config file
88 void printConfString ( std::ostream& o ) const;
89 }; // ifOption
90
91 // implementation of class ifOption
92
93 inline ifOption :: ifOption ( const std::string& name, const std::string& desc, ioType t, const std::string& defVal )
94 : optionName(name)
95 , optionDescription(desc)
96 , defaultValue(defVal)
97 , type(t)
98 {
99 setAValue (defVal);
100 }
101
102 /// set of options with access by name
103 class ifOptionSet
104 {
105 protected: // internal type definitions
106 /// base internal type
107 typedef std::map<std::string,ifOption*> OptionSet;
108
109 protected: // members
110 /// set of all available (given) options
111 OptionSet Base;
112
113 protected: // methods
114 /// get access option structure by name; @return NULL if no such option was registered
115 const ifOption* locateOption ( const std::string& name ) const
116 {
117 auto p = Base.find(name);
118 return p == Base.end() ? nullptr : p->second;
119 }
120
121 public: // interface
122 /// empty c'tor
123 ifOptionSet() = default;
124 /// d'tor (delete all registered options)
125 ~ifOptionSet()
126 {
127 for ( auto& p: Base )
128 delete p.second;
129 }
130
131 /// register an option with given name, description, type and default. @return true iff such option exists
132 bool RegisterOption (
133 const std::string& name,
134 const std::string& desc,
135 ifOption::ioType t,
136 const std::string& defVal )
137 {
138 if ( locateOption (name) != nullptr )
139 return true;
140 Base[name] = new ifOption ( name, desc, t, defVal );
141 return false;
142 }
143 /// init all registered option using given section of given configuration
144 bool initByConfigure ( Configuration& conf, const std::string& Section );
145
146 bool setOption( const std::string& optionName, const std::string& optionValue)
147 {
148 auto p = Base.find(optionName);
149 if ( p == Base.end() )
150 return true;
151 if ( p->second->setAValue(optionValue)) // ... set up its value
152 return true; // can't set value
153 return false;
154 }
155
156 // read access
157
158 /// get Boolean value of given option
159 bool getBool ( const std::string& optionName ) const
160 {
161 const ifOption* p = locateOption ( optionName );
162 fpp_assert ( p != nullptr );
163 return p->getBool ();
164 }
165 /// get integral value of given option
166 int getInt ( const std::string& optionName ) const
167 {
168 const ifOption* p = locateOption ( optionName );
169 fpp_assert ( p != nullptr );
170 return p->getInt ();
171 }
172 /// get string value of given option
173 const std::string& getText ( const std::string& optionName ) const
174 {
175 const ifOption* p = locateOption ( optionName );
176 fpp_assert ( p != nullptr );
177 return p->getText ();
178 }
179
180 /// output option set in the form of config file
181 void printConfig ( std::ostream& o ) const;
182 }; // ifOptionSet
183
184 #endif