00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _UT_OPTIONS_H_
00025 #define _UT_OPTIONS_H_
00026
00027 #include "UT_API.h"
00028 #include <iostream.h>
00029 #include "UT_SmallObject.h"
00030 #include "UT_PtrArraySorted.h"
00031 #include "UT_SymbolTable.h"
00032 #include "UT_DeepString.h"
00033 #include <string>
00034
00035 class UT_WorkBuffer;
00036 class UT_String;
00037
00038 #include "UT_VectorTypes.h"
00039
00040 class UT_OptionEntry;
00041 class UT_JSONParser;
00042 class UT_JSONWriter;
00043 class UT_JSONIStream;
00044
00045
00046
00047
00048 typedef enum {
00049 UT_OPTION_INVALID = -1,
00050 UT_OPTION_INT = 0,
00051 UT_OPTION_BOOL = 1,
00052 UT_OPTION_FPREAL = 2,
00053 UT_OPTION_STRING = 3,
00054 UT_OPTION_VECTOR2 = 4,
00055 UT_OPTION_VECTOR3 = 5,
00056 UT_OPTION_VECTOR4 = 6,
00057 UT_OPTION_QUATERNION = 7,
00058 UT_OPTION_MATRIX3 = 8,
00059 UT_OPTION_MATRIX4 = 9,
00060 UT_OPTION_UV = 10,
00061 UT_OPTION_UVW = 11,
00062 UT_OPTION_STRINGRAW = 12,
00063 UT_OPTION_NUM_TYPES = 13
00064 } UT_OptionType;
00065
00066 class UT_API UT_Options
00067 {
00068 public:
00069 UT_Options();
00070 UT_Options(const UT_Options &src);
00071 virtual ~UT_Options();
00072
00073
00074 void clear();
00075
00076
00077
00078
00079
00080
00081 bool load(const char *filename);
00082 bool save(const char *filename) const;
00083 bool load(const char *filename, UT_IStream &is);
00084 bool save(const char *filename, ostream &os) const;
00085 bool saveAsJSON(const char *filename) const;
00086 bool saveAsJSON(const char *filename, ostream &os) const;
00087
00088
00089 void removeOption(const char *name);
00090
00091
00092
00093 void getOptionString(const char *name,
00094 UT_WorkBuffer &result) const;
00095 void getOptionString(const char *name,
00096 UT_String &result) const;
00097 static void getOptionString(const UT_OptionEntry *option,
00098 UT_WorkBuffer &result);
00099 static void getOptionString(const UT_OptionEntry *option,
00100 UT_String &result);
00101
00102
00103
00104 bool appendPyDictionary(UT_WorkBuffer &result) const;
00105
00106
00107
00108 bool getPyOptionString(const char *name,
00109 UT_WorkBuffer &result) const;
00110 bool getPyOptionString(const char *name,
00111 UT_String &result) const;
00112
00113 static bool getPyOptionString(const UT_OptionEntry *option,
00114 UT_WorkBuffer &result);
00115 static bool getPyOptionString(const UT_OptionEntry *option,
00116 UT_String &result);
00117
00118
00119 bool save(UT_JSONWriter &w) const;
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 bool load(UT_JSONParser &parser,
00130 bool clear_self,
00131 UT_JSONIStream *is = 0);
00132
00133 bool load(UT_JSONParser &parser,
00134 bool clear_self,
00135 UT_IStream *is);
00136
00137
00138 bool hasOption(const char *name) const;
00139 UT_OptionType getOptionType(const char *name) const;
00140 const UT_OptionEntry*getOptionEntry(const char *name) const;
00141
00142
00143 int getNumOptions() const;
00144 const char *getOptionName(int optionnum) const;
00145 UT_OptionType getOptionType(int optionnum) const;
00146 const UT_OptionEntry*getOptionEntry(int optionnum) const;
00147
00148
00149 int64 getOptionI(const char *name) const;
00150 bool getOptionB(const char *name) const;
00151 fpreal64 getOptionF(const char *name) const;
00152 const UT_Vector2D getOptionV2(const char *name) const;
00153 const UT_Vector3D getOptionV3(const char *name) const;
00154 const UT_Vector4D getOptionV4(const char *name) const;
00155 const UT_QuaternionD getOptionQ(const char *name) const;
00156 const UT_Matrix3D getOptionM3(const char *name) const;
00157 const UT_Matrix4D getOptionM4(const char *name) const;
00158 const UT_Vector2D getOptionUV(const char *name) const;
00159 const UT_Vector3D getOptionUVW(const char *name) const;
00160
00161
00162
00163
00164 std::string getOptionS(const char *name) const;
00165 void getOptionS(const char *name, std::string &value) const;
00166 void getOptionS(const char *name, UT_String &value) const;
00167 void getOptionS(
00168 const char *name, UT_WorkBuffer &value) const;
00169
00170
00171
00172 fpreal64 getOptionNum(const char *name) const;
00173
00174
00175
00176
00177
00178
00179
00180
00181 bool importOption(const char *name, int &value) const;
00182 bool importOption(const char *name, int64 &value) const;
00183 bool importOption(const char *name, bool &value) const;
00184 bool importOption(const char *name, fpreal32 &value) const;
00185 bool importOption(const char *name, fpreal64 &value) const;
00186 bool importOption(const char *name, std::string &value) const;
00187 bool importOption(const char *name, UT_String &value) const;
00188 bool importOption(const char *name, UT_Vector2F &value) const;
00189 bool importOption(const char *name, UT_Vector3F &value) const;
00190 bool importOption(const char *name, UT_Vector4F &value) const;
00191 bool importOption(const char *name, UT_QuaternionF &value) const;
00192 bool importOption(const char *name, UT_Matrix3F &value) const;
00193 bool importOption(const char *name, UT_Matrix4F &value) const;
00194 bool importOption(const char *name, UT_Vector2D &value) const;
00195 bool importOption(const char *name, UT_Vector3D &value) const;
00196 bool importOption(const char *name, UT_Vector4D &value) const;
00197 bool importOption(const char *name, UT_QuaternionD &value) const;
00198 bool importOption(const char *name, UT_Matrix3D &value) const;
00199 bool importOption(const char *name, UT_Matrix4D &value) const;
00200
00201
00202 bool importOption(const char *name, UT_IntArray &value) const;
00203 bool importOption(const char *name, UT_FloatArray &value) const;
00204
00205
00206 void setOptionI(const char *name, int64 value);
00207 void setOptionB(const char *name, bool value);
00208 void setOptionF(const char *name, fpreal64 value);
00209
00210
00211
00212 void setOptionS(
00213 const char *name, const char *value, int64 size=-1);
00214 void setOptionS(const char *name, const std::string &value);
00215
00216
00217
00218
00219 void setOptionSRaw(
00220 const char *name, const char *value, int64 size=-1);
00221 void setOptionSRaw(
00222 const char *name, const std::string &value);
00223 void setOptionV2(const char *name,
00224 const UT_Vector2F &value);
00225 void setOptionV2(const char *name,
00226 const UT_Vector2D &value);
00227 void setOptionV2(const char *name,
00228 fpreal64 x, fpreal64 y);
00229 void setOptionV3(const char *name,
00230 const UT_Vector3F &value);
00231 void setOptionV3(const char *name,
00232 const UT_Vector3D &value);
00233 void setOptionV3(const char *name,
00234 fpreal64 x, fpreal64 y, fpreal64 z);
00235 void setOptionV4(const char *name,
00236 const UT_Vector4F &value);
00237 void setOptionV4(const char *name,
00238 const UT_Vector4D &value);
00239 void setOptionV4(const char *name,
00240 fpreal64 x, fpreal64 y, fpreal64 z, fpreal64 w);
00241 void setOptionQ(const char *name,
00242 const UT_QuaternionF &value);
00243 void setOptionQ(const char *name,
00244 const UT_QuaternionD &value);
00245 void setOptionM3(const char *name,
00246 const UT_Matrix3F &value);
00247 void setOptionM3(const char *name,
00248 const UT_Matrix3D &value);
00249 void setOptionM4(const char *name,
00250 const UT_Matrix4F &value);
00251 void setOptionM4(const char *name,
00252 const UT_Matrix4D &value);
00253 void setOptionUV(const char *name,
00254 const UT_Vector2F &value);
00255 void setOptionUV(const char *name,
00256 const UT_Vector2D &value);
00257 void setOptionUVW(const char *name,
00258 const UT_Vector3F &value);
00259 void setOptionUVW(const char *name,
00260 const UT_Vector3D &value);
00261
00262
00263 unsigned hash() const;
00264
00265
00266 bool operator==(const UT_Options &src) const;
00267 bool operator!=(const UT_Options &src) const
00268 { return !(*this == src); }
00269
00270
00271 const UT_Options &operator=(const UT_Options &src);
00272 void merge(const UT_Options &src);
00273 int64 getMemoryUsage() const;
00274
00275 static UT_OptionType getOptionTypeFromString(const char *optiontypestr);
00276 static const char *getStringFromOptionType(UT_OptionType optiontype);
00277
00278 protected:
00279 const std::string *findOptionS(const char *name) const;
00280 void addError(const char *filename, int linecount,
00281 const char *error_str) const;
00282 void setOption(const char *name, UT_OptionEntry *value);
00283 virtual void optionChanged(const char *name);
00284
00285 static int compareStrings(const char * const *str1,
00286 const char * const *str2);
00287 static const char *getStringReference(const char *symbol);
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 static bool saveJSON(UT_JSONWriter &w, const UT_OptionEntry *opt);
00309
00310
00311 static unsigned hash(const UT_OptionEntry *option);
00312 static unsigned isEqual(const UT_OptionEntry *o1,
00313 const UT_OptionEntry *o2,
00314 fpreal64 tolerance=0);
00315
00316
00317 UT_PtrArraySorted<const char *> myOptionNames;
00318 UT_PtrArray<UT_OptionEntry *> myOptionValues;
00319 bool mySendOptionChanges;
00320 };
00321
00322
00323 class UT_API UT_OptionEntry : public UT_SmallObject<UT_OptionEntry,
00324 UT_SMALLOBJECT_CLEANPAGES_DEFAULT,
00325 UT_SMALLOBJECT_PAGESIZE_DEFAULT,
00326 UT_SMALLOBJECT_THREADSAFE_ON>
00327 {
00328 public:
00329 UT_OptionEntry()
00330 { }
00331 virtual ~UT_OptionEntry()
00332 { }
00333
00334 virtual UT_OptionType getType() const = 0;
00335 virtual UT_OptionEntry *clone() const = 0;
00336 };
00337
00338
00339 template<class T, UT_OptionType ID>
00340 class UT_OptionEntryImpl : public UT_OptionEntry
00341 {
00342 public:
00343 UT_OptionEntryImpl(const T &value)
00344 : myValue(value)
00345 { }
00346 virtual ~UT_OptionEntryImpl()
00347 { }
00348
00349 const T &getValue() const
00350 { return myValue; }
00351 virtual UT_OptionType getType() const
00352 { return ID; }
00353 virtual UT_OptionEntry *clone() const
00354 { return new UT_OptionEntryImpl(myValue); }
00355
00356 private:
00357 T myValue;
00358 };
00359
00360 typedef UT_OptionEntryImpl<int64, UT_OPTION_INT> UT_OptionInt;
00361 typedef UT_OptionEntryImpl<bool, UT_OPTION_BOOL> UT_OptionBool;
00362 typedef UT_OptionEntryImpl<fpreal64, UT_OPTION_FPREAL> UT_OptionFpreal;
00363 typedef UT_OptionEntryImpl<std::string, UT_OPTION_STRING> UT_OptionString;
00364 typedef UT_OptionEntryImpl<UT_Vector2D, UT_OPTION_VECTOR2> UT_OptionVector2;
00365 typedef UT_OptionEntryImpl<UT_Vector3D, UT_OPTION_VECTOR3> UT_OptionVector3;
00366 typedef UT_OptionEntryImpl<UT_Vector4D, UT_OPTION_VECTOR4> UT_OptionVector4;
00367 typedef UT_OptionEntryImpl<UT_QuaternionD, UT_OPTION_QUATERNION> UT_OptionQuaternion;
00368 typedef UT_OptionEntryImpl<UT_Matrix3D, UT_OPTION_MATRIX3> UT_OptionMatrix3;
00369 typedef UT_OptionEntryImpl<UT_Matrix4D, UT_OPTION_MATRIX4> UT_OptionMatrix4;
00370 typedef UT_OptionEntryImpl<UT_Vector2D, UT_OPTION_UV> UT_OptionUV;
00371 typedef UT_OptionEntryImpl<UT_Vector3D, UT_OPTION_UVW> UT_OptionUVW;
00372
00373 #endif
00374