39 #ifndef RERANKER_ENVIRONMENT_H_
40 #define RERANKER_ENVIRONMENT_H_
42 #define VAR_MAP_DEBUG 1
55 using std::ostringstream;
56 using std::shared_ptr;
57 using std::unordered_map;
58 using std::unordered_set;
85 virtual const string &
Name()
const {
return name_; }
89 virtual bool Defined(
const string &varname)
const = 0;
100 virtual void Print(ostream &os)
const = 0;
131 virtual bool Defined(
const string &varname)
const = 0;
136 const string type =
"") = 0;
139 virtual const string &
GetType(
const string &varname)
const = 0;
166 virtual void Print(ostream &os)
const = 0;
184 template <
typename T>
200 return "\"" + value +
"\"";
210 return value ?
"true" :
"false";
220 string ToString(
const shared_ptr<T> &value)
const {
222 oss <<
"<" <<
typeid(shared_ptr<T>).name() <<
":" << value.get() <<
">";
231 template <
typename T>
237 typename vector<T>::const_iterator it = value.begin();
239 if (it != value.end()) {
243 for (; it != value.end(); ++it) {
244 oss <<
", " << value_string.
ToString(*it);
255 template <
typename T>
277 bool Get(
const string &varname, T *value)
const {
278 typename unordered_map<string, T>::const_iterator it = vars_.find(varname);
279 if (it == vars_.end()) {
288 virtual bool Defined(
const string &varname)
const {
289 return vars_.find(varname) != vars_.end();
293 void Set(
const string &varname, T value) {
294 vars_[varname] = value;
300 cerr <<
"VarMap<" <<
Name() <<
">::ReadAndSet: about to set varname "
301 << varname <<
" of type " <<
typeid(T).name() << endl;
309 if (typed_var_map != NULL) {
311 string rhs_variable = st.
Next();
314 bool success = typed_var_map->
Get(rhs_variable, &value);
317 cerr <<
"VarMap<" <<
Name() <<
">::ReadAndSet: setting variable "
318 << varname <<
" to same value as rhs variable " << rhs_variable
338 cerr <<
"VarMap<" <<
Name() <<
">::ReadAndSet: set varname "
339 << varname <<
" to value " << value_string.
ToString(value)<< endl;
345 virtual void Print(ostream &os)
const {
347 for (
typename unordered_map<string, T>::const_iterator it = vars_.begin();
348 it != vars_.end(); ++it) {
349 const T& value = it->second;
366 unordered_map<string, T> vars_;
string ToString(const T &value) const
virtual Environment * Copy() const =0
Returns a copy of this environment.
A container to hold the mapping between named variables of a specific type and their values...
virtual VarMapBase * GetVarMapForType(const string &type)=0
Retrieves the VarMap instance for the specified type, or NULL if there is no such VarMap...
virtual bool Defined(const string &varname) const
Returns whether the specified variable has a definition in this environment.
void SetMembers(const string &name, Environment *env, bool is_primitive)
To allow proper implementation of Copy in VarMapBase implementation, since we don't get copying of ba...
virtual VarMapBase * Copy(Environment *env) const =0
Returns a newly constructed copy of this VarMap.
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
virtual bool Defined(const string &varname) const =0
Returns whether the specified variable has been defined in this environment.
bool Get(const string &varname, T *value) const
Assigns the value of the specified variable to the object pointed to by the value parameter...
virtual void ReadAndSet(const string &varname, StreamTokenizer &st)=0
Reads the next value (primitive or spec for constructing a Factory-constructible object) from the spe...
A base class for a mapping from variables of a specific type to their values.
VarMap(const string &name, Environment *env, bool is_primitive=true)
Constructs a mapping from variables of a particular type to their values.
void Set(const string &varname, T value)
Sets the specified variable to the specified value.
VarMapBase(const string &name, Environment *env, bool is_primitive)
Constructs a base class for a concrete implementation providing a mapping from variables of a particu...
virtual bool IsPrimitive() const
Returns whether this instance contains primitive or primtive vector variables.
string ToString(const string &value) const
Provides the StreamTokenizer class.
string ToString(const bool &value) const
string Next()
Returns the next token in the token stream.
Provides a generic dynamic object factory.
virtual void ReadAndSet(const string &varname, StreamTokenizer &st, const string type="")=0
Sets the specified variable to the value obtained from the following tokens available from the specif...
A template class that helps print out values with ostream& operator support and vectors of those valu...
virtual const string & GetType(const string &varname) const =0
Retrieves the type name of the specified variable.
An interface for an environment in which variables of various types are mapped to their values...
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
virtual bool Defined(const string &varname) const =0
Returns whether the specified variable has a definition in this environment.
Environment * env_
The Environment that holds this VarMap instance.
A class to initialize a Factory-constructible object.
virtual VarMapBase * Copy(Environment *env) const
Returns a newly constructed copy of this VarMap.
string Peek() const
Returns the next token that would be returned by the Next method.
string name_
The type name of this VarMap.
virtual void Print(ostream &os) const
Prints out a human-readable string to the specified output stream containing the variables, their type and, if primitive, their values.
virtual const string & Name() const
Returns the type name of the variables of this instance.
virtual void ReadAndSet(const string &varname, StreamTokenizer &st)
Reads the next value (primitive or spec for constructing a Factory-constructible object) from the spe...
static Environment * CreateEmpty()
A static factory method to create a new, empty Environment instance.
virtual void Print(ostream &os) const =0
Prints a human-readable string of all the variables in this environment, their types and...
string ToString(const shared_ptr< T > &value) const
virtual VarMapBase * GetVarMap(const string &varname)=0
Retrieves the VarMap instance for the specified variable.
string ToString(const vector< T > &value) const
virtual void PrintFactories(ostream &os) const =0
Prints out a human-readable string with the names of all abstract base types and their concrete imple...
bool is_primitive_
Whether this VarMap instance holds variables of primitive type or vector of primitives.
virtual void Print(ostream &os) const =0
Prints out a human-readable string to the specified output stream containing the variables, their type and, if primitive, their values.