Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #ifndef VLXValue_INCLUDE_ONCE 00033 #define VLXValue_INCLUDE_ONCE 00034 00035 #include <vlCore/VLXVisitor.hpp> 00036 #include <vector> 00037 00038 namespace vl 00039 { 00041 class VLXTaggedValue: public Object 00042 { 00043 VL_INSTRUMENT_ABSTRACT_CLASS(vl::VLXTaggedValue, Object) 00044 00045 public: 00046 VLXTaggedValue(const char* tag=NULL): mLineNumber(0) 00047 { 00048 if (tag) 00049 mTag = tag; 00050 } 00051 00052 virtual ~VLXTaggedValue() {} 00053 00054 int lineNumber() const { return mLineNumber; } 00055 00056 void setLineNumber(int line) { mLineNumber = line; } 00057 00058 virtual void acceptVisitor(VLXVisitor*) = 0; 00059 00060 void setTag(const char* tag) { mTag = tag; } 00061 00062 const std::string& tag() const { return mTag; } 00063 00064 private: 00065 std::string mTag; 00066 int mLineNumber; // the line number coming from the tokenizer 00067 }; 00068 //----------------------------------------------------------------------------- 00070 class VLXRawtextBlock: public VLXTaggedValue 00071 { 00072 VL_INSTRUMENT_CLASS(vl::VLXRawtextBlock, VLXTaggedValue) 00073 00074 public: 00075 VLXRawtextBlock(const char* tag=NULL, const char* value=NULL): VLXTaggedValue(tag) 00076 { 00077 if (value) 00078 mValue = value; 00079 } 00080 00081 virtual void acceptVisitor(VLXVisitor* v) { v->visitRawtextBlock(this); } 00082 00083 std::string& value() { return mValue; } 00084 00085 const std::string& value() const { return mValue; } 00086 00087 void setValue(const char* value) { mValue = value; } 00088 00089 private: 00090 std::string mValue; 00091 }; 00092 //----------------------------------------------------------------------------- 00094 class VLXArray: public VLXTaggedValue 00095 { 00096 VL_INSTRUMENT_ABSTRACT_CLASS(vl::VLXArray, VLXTaggedValue) 00097 00098 public: 00099 VLXArray(const char* tag=NULL): VLXTaggedValue(tag) {} 00100 00101 }; 00102 //----------------------------------------------------------------------------- 00104 template<typename T> 00105 class VLXArrayTemplate: public VLXArray 00106 { 00107 VL_INSTRUMENT_ABSTRACT_CLASS(vl::VLXArrayTemplate<T>, VLXArray) 00108 00109 public: 00110 typedef T scalar_type; 00111 00112 public: 00113 VLXArrayTemplate(const char* tag=NULL): VLXArray(tag) { } 00114 00115 std::vector<T>& value() { return mValue; } 00116 00117 const std::vector<T>& value() const { return mValue; } 00118 00119 T* ptr() { if (mValue.empty()) return NULL; else return &mValue[0]; } 00120 00121 const T* ptr() const { if (mValue.empty()) return NULL; else return &mValue[0]; } 00122 00123 template<typename T2> void copyTo(T2*ptr) const { for(size_t i=0; i<mValue.size(); ++i, ++ptr) *ptr = (T2)mValue[i]; } 00124 00125 template<typename T2> void copyFrom(const T2*ptr) { for(size_t i=0; i<mValue.size(); ++i, ++ptr) mValue[i] = (scalar_type)*ptr; } 00126 00127 private: 00128 std::vector<T> mValue; 00129 }; 00130 //----------------------------------------------------------------------------- 00132 class VLXArrayInteger: public VLXArrayTemplate<long long> 00133 { 00134 VL_INSTRUMENT_CLASS(vl::VLXArrayInteger, VLXArrayTemplate<long long>) 00135 00136 public: 00137 VLXArrayInteger(const char* tag=NULL): VLXArrayTemplate<long long>(tag) { } 00138 00139 virtual void acceptVisitor(VLXVisitor* v) { v->visitArray(this); } 00140 }; 00141 //----------------------------------------------------------------------------- 00143 class VLXArrayReal: public VLXArrayTemplate<double> 00144 { 00145 VL_INSTRUMENT_CLASS(vl::VLXArrayReal, VLXArrayTemplate<double>) 00146 00147 public: 00148 VLXArrayReal(const char* tag=NULL): VLXArrayTemplate<double>(tag) { } 00149 00150 virtual void acceptVisitor(VLXVisitor* v) { v->visitArray(this); } 00151 }; 00152 //----------------------------------------------------------------------------- 00153 /* 00154 class VLXArrayString: public VLXArray 00155 { 00156 VL_INSTRUMENT_CLASS(vl::VLXArrayString, VLXArray) 00157 00158 public: 00159 VLXArrayString(const char* tag=NULL): VLXArray(tag) { } 00160 00161 virtual void acceptVisitor(VLXVisitor* v) { v->visitArray(this); } 00162 00163 std::vector<std::string>& value() { return mValue; } 00164 00165 const std::vector<std::string>& value() const { return mValue; } 00166 00167 std::string* ptr() { if (mValue.empty()) return NULL; else return &mValue[0]; } 00168 00169 const std::string* ptr() const { if (mValue.empty()) return NULL; else return &mValue[0]; } 00170 00171 public: 00172 std::vector<std::string> mValue; 00173 }; 00174 //----------------------------------------------------------------------------- 00175 class VLXArrayIdentifier: public VLXArray 00176 { 00177 VL_INSTRUMENT_CLASS(vl::VLXArrayIdentifier, VLXArray) 00178 00179 public: 00180 VLXArrayIdentifier(const char* tag=NULL): VLXArray(tag) { } 00181 00182 virtual void acceptVisitor(VLXVisitor* v) { v->visitArray(this); } 00183 00184 std::vector<std::string>& value() { return mValue; } 00185 00186 const std::vector<std::string>& value() const { return mValue; } 00187 00188 std::string* ptr() { if (mValue.empty()) return NULL; else return &mValue[0]; } 00189 00190 const std::string* ptr() const { if (mValue.empty()) return NULL; else return &mValue[0]; } 00191 00192 public: 00193 std::vector<std::string> mValue; 00194 }; 00195 //----------------------------------------------------------------------------- 00196 class VLXArrayID: public VLXArray 00197 { 00198 VL_INSTRUMENT_CLASS(vl::VLXArrayID, VLXArray) 00199 00200 public: 00201 VLXArrayID(const char* tag=NULL): VLXArray(tag) { } 00202 00203 virtual void acceptVisitor(VLXVisitor* v) { v->visitArray(this); } 00204 00205 class Value 00206 { 00207 public: 00208 Value(const char* uid): mID(uid) {} 00209 00210 void setID(const char* uid) { mID = uid; } 00211 00212 const char* uid() const { return mID.c_str(); } 00213 00214 void setStructure(VLXStructure* obj) { mObj = obj; } 00215 00216 VLXStructure* object() { return mObj.get(); } 00217 00218 const VLXStructure* object() const { return mObj.get(); } 00219 00220 private: 00221 std::string mID; // the ID string 00222 ref<VLXStructure> mObj; // the linked object 00223 }; 00224 00225 std::vector<Value>& value() { return mValue; } 00226 00227 const std::vector<Value>& value() const { return mValue; } 00228 00229 Value* ptr() { if (mValue.empty()) return NULL; else return &mValue[0]; } 00230 00231 const Value* ptr() const { if (mValue.empty()) return NULL; else return &mValue[0]; } 00232 00233 public: 00234 std::vector<Value> mValue; 00235 }; 00236 */ 00237 //----------------------------------------------------------------------------- 00239 class VLXValue 00240 { 00241 public: 00242 enum EType 00243 { 00244 Bool, 00245 Integer, 00246 Real, 00247 String, 00248 Identifier, 00249 ID, 00250 RawtextBlock, 00251 List, 00252 Structure, 00253 ArrayInteger, 00254 ArrayReal 00255 /* 00256 ArrayString, 00257 ArrayIdentifier, 00258 ArrayID, 00259 */ 00260 }; 00261 00262 private: 00263 VLCORE_EXPORT void release(); 00264 00265 public: 00266 VLXValue() 00267 { 00268 mLineNumber = 0; 00269 mType = Integer; 00270 mUnion.mInteger = 0; 00271 } 00272 00273 VLXValue(VLXStructure* obj) 00274 { 00275 mLineNumber = 0; 00276 mType = Integer; 00277 mUnion.mInteger = 0; 00278 00279 setStructure(obj); 00280 } 00281 00282 VLXValue(VLXList* list) 00283 { 00284 mLineNumber = 0; 00285 mType = Integer; 00286 mUnion.mInteger = 0; 00287 00288 setList(list); 00289 } 00290 00291 VLXValue(VLXRawtextBlock* rawtext) 00292 { 00293 mLineNumber = 0; 00294 mType = Integer; 00295 mUnion.mInteger = 0; 00296 00297 setRawtextBlock(rawtext); 00298 } 00299 00300 VLXValue(VLXArrayInteger* arr) 00301 { 00302 mLineNumber = 0; 00303 mType = Integer; 00304 mUnion.mInteger = 0; 00305 setArrayInteger(arr); 00306 } 00307 00308 VLXValue(VLXArrayReal* arr) 00309 { 00310 mLineNumber = 0; 00311 mType = Integer; 00312 mUnion.mInteger = 0; 00313 setArrayReal(arr); 00314 } 00315 00316 /* 00317 VLXValue(VLXArrayString* arr) 00318 { 00319 mLineNumber = 0; 00320 mType = Integer; 00321 mUnion.mInteger = 0; 00322 setArrayString(arr); 00323 } 00324 00325 VLXValue(VLXArrayIdentifier* arr) 00326 { 00327 mLineNumber = 0; 00328 mType = Integer; 00329 mUnion.mInteger = 0; 00330 setArrayIdentifier(arr); 00331 } 00332 00333 VLXValue(VLXArrayID* arr) 00334 { 00335 mLineNumber = 0; 00336 mType = Integer; 00337 mUnion.mInteger = 0; 00338 setArrayID(arr); 00339 } 00340 */ 00341 00342 VLXValue(long long i) 00343 { 00344 mLineNumber = 0; 00345 mType = Integer; 00346 mUnion.mInteger = i; 00347 } 00348 00349 VLXValue(double d) 00350 { 00351 mLineNumber = 0; 00352 mType = Real; 00353 mUnion.mReal = d; 00354 } 00355 00356 VLXValue(const char* str, EType type) 00357 { 00358 mLineNumber = 0; 00359 mType = Integer; 00360 mUnion.mInteger = 0; 00361 00362 switch(type) 00363 { 00364 case String: setString(str); break; 00365 case Identifier: setIdentifier(str); break; 00366 case ID: setID(str); break; 00367 default: 00368 VL_TRAP(); 00369 break; 00370 } 00371 } 00372 00373 VLXValue(bool boolean) 00374 { 00375 mLineNumber = 0; 00376 mType = Integer; 00377 mUnion.mInteger = 0; 00378 00379 setBool(boolean); 00380 } 00381 00382 VLXValue(const VLXValue& other) 00383 { 00384 mType = Integer; 00385 mUnion.mInteger = 0; 00386 mLineNumber = 0; 00387 00388 *this = other; 00389 } 00390 00391 ~VLXValue() { release(); } 00392 00393 VLCORE_EXPORT VLXValue& operator=(const VLXValue& other); 00394 00395 EType type() const { return mType; } 00396 00397 // object 00398 00399 VLCORE_EXPORT VLXStructure* setStructure(VLXStructure*); 00400 00401 VLXStructure* getStructure() { VL_CHECK(mType == Structure); return mUnion.mStructure; } 00402 00403 const VLXStructure* getStructure() const { VL_CHECK(mType == Structure); return mUnion.mStructure; } 00404 00405 // list 00406 00407 VLCORE_EXPORT VLXList* setList(VLXList*); 00408 00409 VLXList* getList() { VL_CHECK(mType == List); return mUnion.mList; } 00410 00411 const VLXList* getList() const { VL_CHECK(mType == List); return mUnion.mList; } 00412 00413 // rawtext block 00414 00415 VLCORE_EXPORT VLXRawtextBlock* setRawtextBlock(VLXRawtextBlock*); 00416 00417 VLXRawtextBlock* getRawtextBlock() { VL_CHECK(mType == RawtextBlock); return mUnion.mRawtextBlock; } 00418 00419 const VLXRawtextBlock* getRawtextBlock() const { VL_CHECK(mType == RawtextBlock); return mUnion.mRawtextBlock; } 00420 00421 // array 00422 00423 VLCORE_EXPORT VLXArray* setArray(VLXArray*); 00424 VLCORE_EXPORT VLXArrayInteger* setArrayInteger(VLXArrayInteger*); 00425 VLCORE_EXPORT VLXArrayReal* setArrayReal(VLXArrayReal*); 00426 /* 00427 VLCORE_EXPORT VLXArrayString* setArrayString(VLXArrayString*); 00428 VLCORE_EXPORT VLXArrayIdentifier* setArrayIdentifier(VLXArrayIdentifier*); 00429 VLCORE_EXPORT VLXArrayID* setArrayID(VLXArrayID*); 00430 */ 00431 00432 /* 00433 VLXArrayString* getArrayString() { VL_CHECK(mType == ArrayString); return mUnion.mArray->as<VLXArrayString>(); } 00434 const VLXArrayString* getArrayString() const { VL_CHECK(mType == ArrayString); return mUnion.mArray->as<VLXArrayString>(); } 00435 00436 VLXArrayIdentifier* getArrayIdentifier() { VL_CHECK(mType == ArrayIdentifier); return mUnion.mArray->as<VLXArrayIdentifier>(); } 00437 const VLXArrayIdentifier* getArrayIdentifier() const { VL_CHECK(mType == ArrayIdentifier); return mUnion.mArray->as<VLXArrayIdentifier>(); } 00438 00439 VLXArrayID* getArrayID() { VL_CHECK(mType == ArrayID); return mUnion.mArray->as<VLXArrayID>(); } 00440 const VLXArrayID* getArrayID() const { VL_CHECK(mType == ArrayID); return mUnion.mArray->as<VLXArrayID>(); } 00441 */ 00442 00443 VLXArrayInteger* getArrayInteger() { VL_CHECK(mType == ArrayInteger); return mUnion.mArray->as<VLXArrayInteger>(); } 00444 const VLXArrayInteger* getArrayInteger() const { VL_CHECK(mType == ArrayInteger); return mUnion.mArray->as<VLXArrayInteger>(); } 00445 00446 VLXArrayReal* getArrayReal() { VL_CHECK(mType == ArrayReal); return mUnion.mArray->as<VLXArrayReal>(); } 00447 const VLXArrayReal* getArrayReal() const { VL_CHECK(mType == ArrayReal); return mUnion.mArray->as<VLXArrayReal>(); } 00448 00449 // string 00450 00451 const std::string& setString(const char* str) 00452 { 00453 release(); 00454 mType = String; 00455 mUnion.mString = new std::string(str); 00456 return *mUnion.mString; 00457 } 00458 00459 const std::string& getString() const { VL_CHECK(mType == String); return *mUnion.mString; } 00460 00461 // identifier 00462 00463 const std::string& setIdentifier(const char* str) 00464 { 00465 release(); 00466 mType = Identifier; 00467 mUnion.mString = new std::string(str); 00468 return *mUnion.mString; 00469 } 00470 00471 const std::string& getIdentifier() const { VL_CHECK(mType == Identifier); return *mUnion.mString; } 00472 00473 // uid 00474 00475 const std::string& setID(const char* str) 00476 { 00477 release(); 00478 mType = ID; 00479 mUnion.mString = new std::string(str); 00480 return *mUnion.mString; 00481 } 00482 00483 const std::string& getID() const { VL_CHECK(mType == ID); return *mUnion.mString; } 00484 00485 // integer 00486 00487 long long setInteger(long long val) 00488 { 00489 release(); 00490 mType = Integer; 00491 return mUnion.mInteger = val; 00492 } 00493 00494 long long getInteger() const { VL_CHECK(mType == Integer); return mUnion.mInteger; } 00495 00496 // floating point 00497 00498 double setReal(double val) 00499 { 00500 release(); 00501 mType = Real; 00502 return mUnion.mReal = val; 00503 } 00504 00505 double getReal() const { VL_CHECK(mType == Real); return mUnion.mReal; } 00506 00507 // bool 00508 00509 bool setBool(bool val) 00510 { 00511 release(); 00512 mType = Bool; 00513 return mUnion.mBool = val; 00514 } 00515 00516 bool getBool() const { VL_CHECK(mType == Bool); return mUnion.mBool; } 00517 00518 int lineNumber() const { return mLineNumber; } 00519 00520 void setLineNumber(int line) { mLineNumber = line; } 00521 00522 private: 00523 union 00524 { 00525 bool mBool; 00526 long long mInteger; 00527 double mReal; 00528 std::string* mString; 00529 VLXStructure* mStructure; 00530 VLXList* mList; 00531 VLXArray* mArray; 00532 VLXRawtextBlock* mRawtextBlock; 00533 } mUnion; 00534 00535 EType mType; 00536 int mLineNumber; // the line number coming from the tokenizer 00537 }; 00538 //----------------------------------------------------------------------------- 00540 class VLXStructure: public VLXTaggedValue 00541 { 00542 VL_INSTRUMENT_CLASS(vl::VLXStructure, VLXTaggedValue) 00543 00544 public: 00545 VLXStructure() 00546 { 00547 mKeyValue.reserve(16); 00548 setID("#NULL"); 00549 } 00550 00551 VLXStructure(const char* tag) 00552 { 00553 mKeyValue.reserve(16); 00554 setID("#NULL"); 00555 setTag(tag); 00556 } 00557 00558 VLXStructure(const char* tag, const std::string& uid) 00559 { 00560 mKeyValue.reserve(16); 00561 setID(uid.c_str()); 00562 setTag(tag); 00563 } 00564 00565 virtual void acceptVisitor(VLXVisitor* v) { v->visitStructure(this); } 00566 00567 VLXStructure& operator<<(const char* str) 00568 { 00569 value().resize( value().size() + 1 ); 00570 value().back().setKey(str); 00571 return *this; 00572 } 00573 00574 VLXStructure& operator<<(const VLXValue& val) 00575 { 00576 value().back().setValue(val); 00577 return *this; 00578 } 00579 00581 class Value 00582 { 00583 friend class VLXStructure; 00584 00585 public: 00586 Value() {} 00587 Value(const char* key, VLXValue value): mKey(key), mValue(value) {} 00588 00589 std::string& key() { return mKey; } 00590 const std::string& key() const { return mKey; } 00591 void setKey(const char* key) { mKey = key; } 00592 00593 VLXValue& value() { return mValue; } 00594 const VLXValue& value() const { return mValue; } 00595 void setValue(const VLXValue& value) { mValue = value; } 00596 00597 private: 00598 std::string mKey; 00599 VLXValue mValue; 00600 }; 00601 00602 void setID(const char* uid) { mID = uid; } 00603 00604 const std::string& uid() const { return mID; } 00605 00606 std::vector<Value>& value() { return mKeyValue; } 00607 00608 const std::vector<Value>& value() const { return mKeyValue; } 00609 00610 // mic fixme: we can speed this guys up with multimaps if we really want 00611 VLXValue* getValue(const char* key) 00612 { 00613 for(size_t i=0; i<mKeyValue.size(); ++i) 00614 if (mKeyValue[i].key() == key) 00615 return &mKeyValue[i].value(); 00616 return NULL; 00617 } 00618 00619 const VLXValue* getValue(const char* key) const 00620 { 00621 for(size_t i=0; i<mKeyValue.size(); ++i) 00622 if (mKeyValue[i].key() == key) 00623 return &mKeyValue[i].value(); 00624 return NULL; 00625 } 00626 00627 private: 00628 std::string mID; 00629 std::vector<Value> mKeyValue; 00630 }; 00631 //----------------------------------------------------------------------------- 00633 class VLXList: public VLXTaggedValue 00634 { 00635 VL_INSTRUMENT_CLASS(vl::VLXList, VLXTaggedValue) 00636 00637 public: 00638 VLXList(const char* tag=NULL): VLXTaggedValue(tag) 00639 { 00640 mValue.reserve(16); 00641 } 00642 00643 VLXList& operator<<(const VLXValue& val) 00644 { 00645 value().push_back( val ); 00646 return *this; 00647 } 00648 00649 virtual void acceptVisitor(VLXVisitor* v) { v->visitList(this); } 00650 00651 std::vector< VLXValue >& value() { return mValue; } 00652 00653 const std::vector< VLXValue >& value() const { return mValue; } 00654 00655 private: 00656 std::vector< VLXValue > mValue; 00657 }; 00658 } 00659 00660 #endif