📄 keyval.h
字号:
char* Va_pcharvalue(const char* key,int nindex,...); std::string Va_stringvalue(const char* key,int nindex,...); Ref<DescribedClass> Va_describedclassvalue(const char* key,int nindex,...); //@} /// Return the current error condition. KeyValError error(); /// Return a textual representation of err. const char* errormsg(KeyValError err); /// Return a textual representation of the current error. const char* errormsg(); /// Write a message to fp describing the error. virtual void errortrace(std::ostream&fp=ExEnv::err0()); /// Write a message to fp describing the error. virtual void dump(std::ostream&fp=ExEnv::err0()); /// Print keywords that were never looked at, if possible. virtual void print_unseen(std::ostream&fp=ExEnv::out0()); /** Return 1 if there were unseen keywords, 0 if there are none, or -1 this keyval doesn't keep track of unseen keywords. */ virtual int have_unseen(); /// Control printing of assignments. void verbose(int v) { verbose_ = v; } /// Returns nonzero if assignments are printed. int verbose() const { return verbose_; }};/** This class allows keyval associations to be set up by the program, rather than determined by an external file. */class AssignedKeyVal: public KeyVal { private: std::map<std::string,Ref<KeyValValue> > _map; // do not allow a copy constructor or assignment AssignedKeyVal(const AssignedKeyVal&); void operator=(const AssignedKeyVal&); protected: int key_exists(const char*); Ref<KeyValValue> key_value(const char*, const KeyValValue& def); public: AssignedKeyVal(); ~AssignedKeyVal(); /** Each of this routines assigns key to val. */ //@{ void assign(const char* key, const Ref<KeyValValue>& val); void assign(const char* key, double val); void assignboolean(const char* key, int val); void assign(const char* key, float val); void assign(const char* key, char val); void assign(const char* key, int val); void assign(const char* key, const char* val); void assign(const char* key, const Ref<DescribedClass>& val); //@} /// Erase all of the stored assignments. void clear();};/** StringKeyVal is a base class for KeyVal implementations that store all values in a string format. These are converted to other data types through KeyValValue.*/class StringKeyVal: public KeyVal { private: // once a described class is found it is kept here so // multiple references to it return the same instance std::map<std::string,Ref<KeyValValue> > _map; // do not allow a copy constructor or assignment StringKeyVal(const StringKeyVal&); void operator=(const StringKeyVal&); protected: StringKeyVal(); int key_exists(const char*); Ref<KeyValValue> key_value(const char*, const KeyValValue& def); public: virtual ~StringKeyVal(); /// Returns the string representation of the value assigned to key. virtual const char* stringrep(const char *key) = 0; /** Returns the name of the exact class of the object at the keyword. If no classname is assigned then 0 is returned. */ virtual const char* classname(const char*); /** Returns a string which is the actual keyword if some sort of variable substitution takes place (needed to make multiple references to the same object work in input files). */ virtual const char* truekeyword(const char*); /// See the parent class documentation for descriptions of these functions. //@{ virtual void errortrace(std::ostream&fp=ExEnv::err0()); virtual void dump(std::ostream&fp=ExEnv::err0()); //@}};/** This takes several KeyVal objects and makes them look like one KeyVal object. When a key is sought first KeyVal, then the next, and so on is searched until the keyword is found.*/class AggregateKeyVal : public KeyVal { private: enum { MaxKeyVal = 4 }; Ref<KeyVal> kv[MaxKeyVal]; Ref<KeyVal> getkeyval(const char*key); // do not allow a copy constructor or assignment AggregateKeyVal(const AggregateKeyVal&); void operator=(const AggregateKeyVal&); protected: int key_exists(const char*); Ref<KeyValValue> key_value(const char*, const KeyValValue& def); public: /** These contructors create an AggregateKeyVal that is formed from several other KeyVal objects. The search order is keyval1, keyval2, and so on. */ //@{ AggregateKeyVal(const Ref<KeyVal>& keyval1); AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2); AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2, const Ref<KeyVal>& keyval3); AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2, const Ref<KeyVal>& keyval3, const Ref<KeyVal>& keyval4); //@} ~AggregateKeyVal(); void errortrace(std::ostream&fp=ExEnv::err0()); void dump(std::ostream&fp=ExEnv::err0());};/** PrefixKeyVal is a KeyVal that searches a different KeyVal using modified keys. This is convenient for reading keys grouped together with a common prefix. Consider the following code: <pre> sc::Ref<sc::KeyVal> keyval = new sc::PrefixKeyVal("A",original_keyval); int r = keyval->intvalue("x"); </pre> This code will assign to r the value associated with "x" in keyval. keyval will search for "x" by searching for "A::x" in original_keyval. This class is important for implementing constructors that take KeyVal arguments. When an object is being constructed from a KeyVal, it may contain another object that must be constructed from a KeyVal. In order to let the sub-object read the correct keywords from the KeyVal, without knowledge of the containing objects keyword prefix, a PrefixKeyVal can be constructed. For example, the code <pre> class A: public DescribedClass { double f0_; public: A(const Ref<KeyVal> &keyval): f0_(keyval->doublevalue("f0")) {} } class B: public DescribedClass { double f1_; Ref<A> a_; public: B(const Ref<KeyVal> &keyval): f1_(keyval->doublevalue("f1")), a_(new PrefixKeyVal(keyval,"a")) {} }; </pre> can be used to read ParsedKeyVal input that looks like <pre> b<B>: ( f1 = 1.0 a<A>: ( f0 = 2.0 ) ) </pre> */class PrefixKeyVal : public KeyVal { private: char* prefix; Ref<KeyVal> keyval; void setup(const char*,int,int,int,int,int); int getnewprefixkey(const char*key,char*newkey); // do not allow a copy constructor or assignment PrefixKeyVal(const PrefixKeyVal&); void operator=(const PrefixKeyVal&); int key_exists(const char*); Ref<KeyValValue> key_value(const char*, const KeyValValue& def); public: /** Construct a PrefixKeyVal, using the given prefix and indices. */ //@{ PrefixKeyVal(const Ref<KeyVal>&,int i); PrefixKeyVal(const Ref<KeyVal>&,int i,int j); PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k); PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k,int l); PrefixKeyVal(const Ref<KeyVal>&,const char*prefix); PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i); PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j); PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k); PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k,int l); //@} /// These routines are deprecated and will be removed in a future release. //@{ PrefixKeyVal(const char*,const Ref<KeyVal>&); PrefixKeyVal(const char*,const Ref<KeyVal>&,int); PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int); PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int,int); PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int,int,int); //@} ~PrefixKeyVal(); void errortrace(std::ostream&fp=ExEnv::err0()); void dump(std::ostream&fp=ExEnv::err0());};class IPV2;/** Converts textual information into keyword/value assocations. The parsing is done with an IPV2 object. The \ref keyval for more information on the input format. */class ParsedKeyVal : public StringKeyVal { private: int nfile; char**file; int nfp; IPV2* ipv2; // do not allow a copy constructor or assignment ParsedKeyVal(const ParsedKeyVal&); void operator=(const ParsedKeyVal&); public: /// Create an empty ParsedKeyVal. ParsedKeyVal(); /// Parse the given input file. ParsedKeyVal(const char*file); /// Read input from s. ParsedKeyVal(std::istream&s); /** Use the given IPV2* object. The new ParsedKeyVal takes wnership of the passed IPV2 object. */ ParsedKeyVal(IPV2*); /** This ctor is given a string which is used to form keywords that are sought in the keyval argument. The associated values are used to construct file names that are used to initialize the ParsedKeyVal. The keywords sought are string'dir' for the directory prefix and string'files' for an array of file names. */ ParsedKeyVal(const char*,const Ref<KeyVal>&); /// Cleanup, deleting the IPV2 object. ~ParsedKeyVal(); /** This is like the ParsedKeyVal(const char*,const Ref<KeyVal>&) ctor, but writes the contents of the files to the given ostream. */ static void cat_files(const char*,const Ref<KeyVal>&,std::ostream &o); /// Read input data from the given filename void read(const char*); /// Read input data from the given stream. void read(std::istream&); /// Read input data from the given string. void parse_string(const char *); /// Overrides of parent members. //@{ const char* stringrep(const char*); const char* classname(const char*); const char* truekeyword(const char*); void errortrace(std::ostream&fp=ExEnv::err0()); void dump(std::ostream&fp=ExEnv::err0()); void print_unseen(std::ostream&fp=ExEnv::out0()); int have_unseen(); //@}};}#endif /* _KeyVal_h */// Local Variables:// mode: c++// c-file-style: "CLJ"// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -