HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_OptionFile.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_OptionFile.h (C++, Utility Library)
7  *
8  * COMMENTS: General class for dealing with option files
9  *
10  */
11 
12 #ifndef _UT_OPTION_FILE_H_INCLUDED_
13 #define _UT_OPTION_FILE_H_INCLUDED_
14 
15 #include "UT_API.h"
16 #include "UT_Color.h"
17 #include "UT_NonCopyable.h"
18 #include "UT_String.h"
19 #include "UT_SymbolTable.h"
20 #include "UT_VectorTypes.h"
21 #include <SYS/SYS_Types.h>
22 
23 #include <iosfwd>
24 
26 
27 // This abstract class defines the parser used. If you pass NULL for the parser
28 // option into UT_OptionFile, then it will use a default parser
30 {
31 public:
33  virtual ~UT_OptionParser() {}
34 
36 
37  // load the options into the given hash table. the user must do a strdup
38  // of the string value when adding to the symbol table
39  virtual bool load(const char *filename, UT_IStream &is,
40  UT_OptionValueMap &options) = 0;
41  virtual bool save(const char *filename, std::ostream &os,
42  const UT_OptionValueMap &options) const = 0;
43 };
44 
45 // Default parser used
47 {
48 public:
49  bool load(const char *filename, UT_IStream &is,
50  UT_OptionValueMap &options) override;
51  bool save(const char *filename, std::ostream &os,
52  const UT_OptionValueMap &options) const override;
53 
54 protected:
55  virtual bool parseLine( const char *line, UT_OptionValueMap &options );
56  void addError( const char *error_str ) const;
57 
58 private:
59  int myLineCount; //Used to output the line a load failed on
60  UT_String myFilename;
61 };
62 
63 // Same idea as UT_UIOptionParser, except we output delimiters around what we
64 // save so we can nest our output in a .hip file
66 {
67 public:
68  bool load(const char *filename, UT_IStream &is,
69  UT_OptionValueMap &options) override;
70 
71  bool save(const char *filename, std::ostream &os,
72  const UT_OptionValueMap &options) const override;
73 
74 protected:
75  bool parseLine(const char *line,
76  UT_OptionValueMap &options) override;
77 };
78 
80 {
81 public:
82  /// Construct a UT_OptionFile objects. If no parser is specified,
83  /// it defaults to UT_UIOptionParser.
84  UT_OptionFile( UT_OptionParser *parser = NULL );
86  ~UT_OptionFile();
87 
88  void clear();
89 
90  // only returns false if the file encounters an error. true is returned on
91  // success or if the file didn't exist.
92  bool load( const char *filename );
93  bool save( const char *filename ) const;
94  bool load( const char *filename, UT_IStream &is);
95  bool save( const char *filename, std::ostream &os ) const;
96  bool save( const char *filename, FILE *file ) const;
97 
98  bool hasOption( const char *name ) const;
99  void removeOption( const char *name );
100 
101  void getOption( const char *name, int &value, int defvalue ) const;
102  void getOption( const char *name, exint &value, exint defvalue ) const;
103  void getOption( const char *name, bool &value, bool defvalue ) const;
104  void getOption( const char *name, fpreal32 &value, fpreal32 defvalue ) const;
105  void getOption( const char *name, fpreal64 &value, fpreal64 defvalue ) const;
106  void getOption( const char *name, UT_String &value,
107  const UT_String &defvalue ) const;
108  void getOption( const char *name, UT_String &value,
109  const char *defvalue ) const;
110  void getOption( const char *name, UT_StringHolder &value,
111  const UT_StringHolder &defvalue ) const;
112  void getOption( const char *name, UT_StringHolder &value,
113  const char *defvalue ) const;
114  void getOption( const char *name, UT_Vector2 &value,
115  const UT_Vector2 &defvalue ) const;
116  void getOption( const char *name, UT_Vector3 &value,
117  const UT_Vector3 &defvalue ) const;
118  void getOption( const char *name, UT_Vector4 &value,
119  const UT_Vector4 &defvalue ) const;
120  void getOption( const char *name, UT_Quaternion &value,
121  const UT_Quaternion &defvalue ) const;
122  void getOption( const char *name, UT_Matrix3 &value,
123  const UT_Matrix3 &defvalue ) const;
124  void getOption( const char *name, UT_Matrix4 &value,
125  const UT_Matrix4 &defvalue ) const;
126  void getOption( const char *name, UT_Color &value,
127  const UT_Color &defvalue ) const;
128 
129  void setOption( const char *name, int value );
130  void setOption( const char *name, exint value );
131  void setOption( const char *name, bool value );
132  void setOption( const char *name, fpreal32 value );
133  void setOption( const char *name, fpreal64 value );
134  // Null strings are not allowed.
135  void setOption( const char *name, const UT_String &value );
136  void setOption( const char *name, const UT_StringHolder &value );
137  void setOption( const char *name, const char *value );
138  void setOption( const char *name, const UT_Vector2 &value );
139  void setOption( const char *name, const UT_Vector3 &value );
140  void setOption( const char *name, const UT_Vector4 &value );
141  void setOption( const char *name, const UT_Quaternion &value );
142  void setOption( const char *name, const UT_Matrix3 &value );
143  void setOption( const char *name, const UT_Matrix4 &value );
144  void setOption( const char *name, const UT_Color &value);
145 
146  const UT_OptionFile &operator=(const UT_OptionFile &src);
147  void merge(const UT_OptionFile &src);
148 
149  // Iterators
151  {
152  public:
153  const_iterator(const UT_OptionFile &option_file)
154  : myOptionFile(option_file)
155  {
156  }
157  const_iterator(const UT_OptionFile &option_file,
159  : myOptionFile(option_file), myIter(iter)
160  {
161  }
162 
163  const char *name() const
164  {
165  return myIter.name();
166  }
167 
168  template <typename _T>
169  void getValue(_T &value, const _T &defvalue) const
170  {
171  myOptionFile.getOption(myIter.name(), value, defvalue);
172  }
173 
175  {
176  return (&myOptionFile == &cmp.myOptionFile
177  && myIter == cmp.myIter);
178  }
180  {
181  return !(*this == cmp);
182  }
183 
184  const_iterator& operator++() // preincrement
185  {
186  ++myIter;
187  return (*this);
188  }
189  const_iterator operator++(int) // postincrement
190  {
191  const_iterator tmp = *this;
192  ++*this;
193  return (tmp);
194  }
195 
196  protected:
197  // right now, we don't have to define any copy/assignment operators
198  // because element-wise assignment works fine.
201  };
203  {
204  public:
205  iterator(UT_OptionFile &option_file)
206  : const_iterator(option_file)
207  {
208  }
209  iterator(UT_OptionFile &option_file,
211  : const_iterator(option_file, iter)
212  {
213  }
214 
215  template <typename _T>
216  void setValue(_T &value)
217  {
218  const_cast<UT_OptionFile *>(&myOptionFile)->setOption(
219  myIter.name(), value);
220  }
221 
222  bool operator==(const iterator &cmp) const
223  {
224  return (&myOptionFile == &cmp.myOptionFile
225  && myIter == cmp.myIter);
226  }
227  bool operator!=(const iterator &cmp) const
228  {
229  return !(*this == cmp);
230  }
231 
232  private:
233  };
234 
235 
237  {
238  return const_iterator(*this, myOptions.begin());
239  }
241  {
242  return const_iterator(*this);
243  }
245  {
246  return iterator(*this, myOptions.begin());
247  }
249  {
250  return iterator(*this);
251  }
252 
253 
254 private:
255  UT_OptionParser * myParser;
256  UT_OptionValueMap myOptions;
257 };
258 
259 #endif // _UT_OPTION_FILE_H_INCLUDED_
260 
bool operator!=(const const_iterator &cmp)
bool load(const char *filename, UT_IStream &is, UT_OptionValueMap &options) override
GT_API const UT_StringHolder filename
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
GLsizei const GLfloat * value
Definition: glcorearb.h:824
const_iterator end() const
UT_OptionValueMap::const_iterator myIter
int64 exint
Definition: SYS_Types.h:125
const_iterator begin() const
iterator end()
bool save(const char *filename, std::ostream &os, const UT_OptionValueMap &options) const override
#define UT_API
Definition: UT_API.h:14
void getValue(_T &value, const _T &defvalue) const
void setOption(const char *name, int value)
const_iterator & operator++()
bool operator!=(const iterator &cmp) const
float fpreal32
Definition: SYS_Types.h:200
const UT_OptionFile & myOptionFile
bool operator==(const iterator &cmp) const
base_iterator< const ITEM_T, const_map_iterator > const_iterator
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
double fpreal64
Definition: SYS_Types.h:201
iterator(UT_OptionFile &option_file, UT_OptionValueMap::const_iterator iter)
const_iterator operator++(int)
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
iterator begin()
GLuint const GLchar * name
Definition: glcorearb.h:786
const_iterator(const UT_OptionFile &option_file)
iterator(UT_OptionFile &option_file)
virtual bool parseLine(const char *line, UT_OptionValueMap &options)
UT_SymbolMap< UT_StringHolder > UT_OptionValueMap
Definition: UT_OptionFile.h:25
const_iterator(const UT_OptionFile &option_file, UT_OptionValueMap::const_iterator iter)
bool operator==(const const_iterator &cmp)
void setValue(_T &value)
const char * name() const
Definition: core.h:1131
#define const
Definition: zconf.h:214
virtual ~UT_OptionParser()
Definition: UT_OptionFile.h:33
GLenum src
Definition: glcorearb.h:1793