37 #ifndef INFACT_ENVIRONMENT_IMPL_H_
38 #define INFACT_ENVIRONMENT_IMPL_H_
42 #include <unordered_map>
43 #include <unordered_set>
51 using std::ostringstream;
53 using std::unordered_map;
54 using std::unordered_set;
70 for (unordered_map<string, VarMapBase *>::iterator it = var_map_.begin();
71 it != var_map_.end(); ++it) {
78 virtual bool Defined(
const string &varname)
const {
79 unordered_map<string, string>::const_iterator it = types_.find(varname);
80 return it != types_.end();
88 virtual const string &
GetType(
const string &varname)
const {
89 unordered_map<string, string>::const_iterator type_it =
91 if (type_it == types_.end()) {
94 return type_it->second;
103 string lookup_type = type;
106 unordered_map<string, string>::const_iterator factory_type_it =
107 concrete_to_factory_type_.find(type);
108 if (factory_type_it != concrete_to_factory_type_.end()) {
109 lookup_type = factory_type_it->second;
112 unordered_map<string, VarMapBase *>::const_iterator var_map_it =
113 var_map_.find(lookup_type);
114 if (var_map_it == var_map_.end()) {
117 return var_map_it->second;
121 virtual void Print(ostream &os)
const {
122 for (unordered_map<string, VarMapBase *>::const_iterator var_map_it =
124 var_map_it != var_map_.end(); ++var_map_it) {
125 var_map_it->second->Print(os);
136 for (unordered_map<string, VarMapBase *>::iterator new_env_var_map_it =
137 new_env->var_map_.begin();
138 new_env_var_map_it != new_env->var_map_.end(); ++new_env_var_map_it) {
139 new_env_var_map_it->second = new_env_var_map_it->second->Copy(new_env);
153 bool Get(
const string &varname, T *value)
const;
157 string InferType(
const string &varname,
159 bool *is_object_type);
162 unordered_map<string, string> types_;
166 unordered_map<string, VarMapBase *> var_map_;
170 unordered_map<string, string> concrete_to_factory_type_;
178 unordered_map<string, string>::const_iterator type_it =
179 types_.find(varname);
180 if (type_it == types_.end()) {
182 ostringstream err_ss;
183 err_ss <<
"Environment::Get: error: no value for variable "
185 cerr << err_ss.str() << endl;
191 const string &type = type_it->second;
192 unordered_map<string, VarMapBase*>::const_iterator var_map_it =
195 if (var_map_it == var_map_.end()) {
196 ostringstream err_ss;
197 err_ss <<
"Environment::Get: error: types_ and var_map_ data members "
198 <<
"are out of sync";
206 if (typed_var_map ==
nullptr) {
207 ostringstream err_ss;
208 err_ss <<
"Environment::Get: error: no value for variable "
209 << varname <<
" of type " <<
typeid(*value).name()
210 <<
"; perhaps you meant " << type <<
"?";
211 cerr << err_ss.str() << endl;
214 bool success = typed_var_map->
Get(varname, value);
216 ostringstream err_ss;
217 err_ss <<
"Environment::Get: error: no value for variable "
218 << varname <<
" of type " <<
typeid(*value).name()
219 <<
"; types_ and var_map_ data members are out of sync";
virtual ~EnvironmentImpl()
Destroys this environment.
virtual const string & GetType(const string &varname) const
Retrieves the type name of the specified variable.
bool Get(const string &varname, T *value) const
Retrieves the value of the variable with the specified name and puts into into the object pointed to ...
virtual bool Defined(const string &varname) const
Returns whether the specified variable has been defined in this environment.
Provides an error handling function that optionally throws an exception.
virtual Environment * Copy() const
Returns a copy of this environment.
virtual void ReadAndSet(const string &varname, StreamTokenizer &st, const string type)
Sets the specified variable to the value obtained from the following tokens available from the specif...
EnvironmentImpl(int debug=0)
Constructs a new, empty environment.
virtual void Print(ostream &os) const
Prints a human-readable string of all the variables in this environment, their types and...
bool Get(const string &varname, T *value) const
Assigns the value of the specified variable to the object pointed to by the value parameter...
A container to hold the mapping between named variables of a specific type and their values...
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
An interface for an environment in which variables of various types are mapped to their values...
virtual void PrintFactories(ostream &os) const
Prints out a human-readable string with the names of all abstract base types and their concrete imple...
Provides an interface for an Environment, which contains a map from variables of a specific type (pri...
void Error(const std::string &message)
Reports an error encountered during parsing and/or construction of an object.
Provides a set of named variables and their types, as well as the values for those variables...
virtual VarMapBase * GetVarMap(const string &varname)
Retrieves the VarMap instance for the specified variable.
A base class for a mapping from variables of a specific type to their values.
virtual VarMapBase * GetVarMapForType(const string &type)
Retrieves the VarMap instance for the specified type.