1 /* 2 * Copyright 2006 Sony Computer Entertainment Inc. 3 * 4 * Licensed under the MIT Open Source License, for details please see license.txt or the website 5 * http://www.opensource.org/licenses/mit-license.php 6 * 7 */ 8 9 #ifndef __DAE_ATOMIC_TYPE_H__ 10 #define __DAE_ATOMIC_TYPE_H__ 11 12 #include <sstream> 13 #include <dae/daeTypes.h> 14 #include <dae/daeStringRef.h> 15 #include <dae/daeArray.h> 16 #include <dae/daeElement.h> 17 18 #ifndef _WIN32 19 #include <stdint.h> 20 #endif 21 22 class DAE; 23 class daeAtomicType; 24 class daeMetaElement; 25 26 typedef daeTArray<daeAtomicType*> daeAtomicTypeArray; 27 class daeMetaAttribute; 28 typedef daeSmartRef<daeMetaAttribute> daeMetaAttributeRef; 29 30 /** 31 * The @c daeAtomicType class implements a standard interface for 32 * data elements in the reflective object system. 33 * 34 * @c daeAtomicType provides a central virtual interface that can be 35 * used by the rest of the reflective object system. 36 * 37 * The atomic type system if very useful for file IO and building 38 * automatic tools for data inspection and manipulation, 39 * such as hierarchy examiners and object editors. 40 * 41 * Types provide the following symantic operations: 42 * - @c print() 43 * - @c memoryToString() 44 * - @c stringToMemory() 45 * 46 * Types are also able to align data pointers appropriately. 47 */ 48 class DLLSPEC daeAtomicType 49 { 50 public: 51 /** 52 * destructor 53 */ 54 virtual ~daeAtomicType() {} 55 56 /** 57 * constructor 58 */ 59 daeAtomicType(DAE& dae); 60 61 public: 62 /** 63 * Prints an atomic typed element into a destination string. 64 * @param src Source of the raw data from which to get the typed items. 65 * @param dst Destination to output the string version of the elements to. 66 * @return Returns true if the operation was successful, false if not successful. 67 */ 68 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst) = 0; 69 70 /** 71 * Reads an atomic typed item into the destination runtime memory. 72 * @param src Source string. 73 * @param dst Raw binary location to store the resulting value. 74 * @return Returns true if the operation was successful, false if not successful. 75 */ 76 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 77 78 /** 79 * Converts an array of atomic items into a whitespace separated string. 80 * @param array The array of data. 81 * @param buffer The buffer to write into. 82 */ 83 virtual void arrayToString(daeArray& array, std::ostringstream& buffer); 84 85 /** 86 * Reads a whitespace separated list of atomic items into an array. The array is 87 * cleared before writing into it. 88 * @param src Whitespace separated list of items. 89 * @param array The output array of data. 90 * @return Returns true if the operation was successful, false otherwise. 91 */ 92 virtual daeBool stringToArray(daeChar* src, daeArray& array); 93 94 /** 95 * Creates a new object of the appropriate type for this daeAtomicType and returns it 96 * as a pointer. The return value must be freed by calling destroy. 97 * @return Returns a pointer to a new value. The memory must be freed by calling destroy. 98 */ 99 virtual daeMemoryRef create() = 0; 100 101 /** 102 * Deletes an object previously allocated with create. 103 * @param obj The object previously allocated with create. 104 */ 105 virtual void destroy(daeMemoryRef obj) = 0; 106 107 /** 108 * Creates a daeTArray of the appropriate type (e.g. daeTArray<int>, daeTArray<daeIDRef>) 109 * and returns it as a daeArray*. 110 * @return Returns a daeArray*. This array should be freed by the caller with 111 * operator delete. 112 */ 113 virtual daeArray* createArray() = 0; 114 115 /** 116 * Performs a virtual comparison operation between two values of the same atomic type. 117 * @param value1 Memory location of the first value. 118 * @param value2 Memory location of the second value. 119 * @return Returns a positive integer if value1 > value2, a negative integer if 120 * value1 < value2, and 0 if value1 == value2. 121 */ 122 virtual daeInt compare(daeChar* value1, daeChar* value2); 123 124 /** 125 * Array version of the compare function. 126 * @param value1 First array to compare. 127 * @param value2 Second array to compare. 128 * @return Returns a positive integer if value1 > value2, a negative integer if 129 * value1 < value2, and 0 if value1 == value2. 130 */ 131 virtual daeInt compareArray(daeArray& value1, daeArray& value2); 132 133 /** 134 * Performs a virtual copy operation. 135 * @param src Memory location of the value to copy from. 136 * @param dst Memory location of the value to copy to. 137 */ 138 virtual void copy(daeChar* src, daeChar* dst) = 0; 139 140 /** 141 * Array version of the copy function. 142 * @param src Array to copy from. 143 * @param dst Array to copy to. 144 */ 145 virtual void copyArray(daeArray& src, daeArray& dst); 146 147 /** 148 * Gets the array of strings as name bindings for this type. 149 * @return Returns the array of strings. 150 */ 151 daeStringRefArray& getNameBindings() { return _nameBindings; } 152 153 /** 154 * Gets the enum associated with this atomic type. This is not scalable and only 155 * works for base types, otherwise 'extension' is used. 156 * @return Returns the enum associated with this atomic type. 157 */ 158 daeEnum getTypeEnum() { return _typeEnum; } 159 160 /** 161 * Gets the size in bytes for this atomic type. 162 * @return Returns the size of the atomic type in bytes. 163 */ 164 daeInt getSize() { return _size; } 165 166 /** 167 * Gets the scanf format used for this type. 168 * @return Returns the scanf format. 169 * @note 170 * Warning - this field is only for convenience and may not always work. 171 * It is used only when the read functions are left to the base 172 * implementation. 173 */ 174 daeStringRef getScanFormat() { return _scanFormat; } 175 176 /** 177 * Gets the printf format used for this type. 178 * @return Returns the printf format. 179 * @note 180 * Warning - this field is only for convenience and may not always work. 181 * It is used only when the print functions are left to the base 182 * implementation. 183 */ 184 daeStringRef getPrintFormat() { return _printFormat; } 185 186 /** 187 * Gets the alignment in bytes necessary for this type on this 188 * platform. 189 * @return Returns the alignment in bytes. 190 */ 191 daeInt getAlignment() { return _alignment; } 192 193 /** 194 * Gets the string associated with this type. 195 * @return Returns the string associated with this type. 196 */ 197 daeStringRef getTypeString() { return _typeString; } 198 199 /** 200 * Performs an alignment based on the alignment for this type. 201 * @param ptr Pointer to be aligned. 202 * @return Returns the aligned pointer computed via 203 * <tt> (ptr+alignment-1)&(~(alignment-1). </tt> 204 * 205 */ 206 daeChar* align(daeChar* ptr) { 207 return (daeChar*)(((intptr_t)(ptr+_alignment-1))&(~(_alignment - 1))); } 208 209 /** 210 * Notifies an object when the containing document changes. 211 * @param value Memory location of the atomic type value. 212 * @param doc The new document. 213 */ 214 virtual void setDocument(daeChar* value, daeDocument* doc) { } 215 216 /** 217 * Same as the previous method, but works on an array of objects. 218 * @param values Array of the atomic type values. 219 * @param doc The new document. 220 */ 221 virtual void setDocument(daeArray& array, daeDocument* doc) { } 222 223 protected: 224 DAE* _dae; 225 daeInt _size; 226 daeInt _alignment; 227 daeEnum _typeEnum; 228 daeStringRef _typeString; 229 daeStringRef _printFormat; 230 daeStringRef _scanFormat; 231 daeInt _maxStringLength; 232 233 public: 234 /** 235 * An array of strings as name bindings for this type. 236 */ 237 daeStringRefArray _nameBindings; 238 239 public: // Static Interface 240 /** An enum for identifying the different atomic types */ 241 enum daeAtomicTypes { 242 /** bool atomic type */ 243 BoolType, 244 /** enum atomic type */ 245 EnumType, 246 /** character atomic type */ 247 CharType, 248 /** short integer atomic type */ 249 ShortType, 250 /** integer atomic type */ 251 IntType, 252 /** unsigned integer atomic type */ 253 UIntType, 254 /** long integer atomic type */ 255 LongType, 256 /** unsigned long integer atomic type */ 257 ULongType, 258 /** floating point atomic type */ 259 FloatType, 260 /** double precision floating point atomic type */ 261 DoubleType, 262 /** string reference atomic type */ 263 StringRefType, 264 /** element reference atomic type */ 265 ElementRefType, 266 /** memory reference atomic type */ 267 MemoryRefType, 268 /** void reference atomic type */ 269 RawRefType, 270 /** resolver atomic type */ 271 ResolverType, 272 /** ID resolver atomic type */ 273 IDResolverType, 274 /** string token atomic type */ 275 TokenType, 276 /** extension atomic type */ 277 ExtensionType 278 }; 279 }; 280 281 282 // This is a container class for storing a modifiable list of daeAtomicType objects. 283 class DLLSPEC daeAtomicTypeList { 284 public: 285 daeAtomicTypeList(DAE& dae); 286 ~daeAtomicTypeList(); 287 288 /** 289 * Appends a new type to the list. 290 * @param t Type to append. 291 * @return Returns the index of the type in the list. 292 */ 293 daeInt append(daeAtomicType* t); 294 295 /** 296 * Gets a type from the list of types, based on its index. 297 * @param index Index of the type to retrieve. 298 * @return Returns the @c daeAtomicType indicated by index. 299 */ 300 const daeAtomicType* getByIndex(daeInt index); 301 302 /** 303 * Gets the number of atomic types in the list. 304 * @return Returns the number of atomic types in the list. 305 */ 306 daeInt getCount(); 307 308 /** 309 * Finds a type by its string name. 310 * @param type String name of the type. 311 * @return Returns the type with the corresponding name. 312 */ 313 daeAtomicType* get(daeStringRef type); 314 315 /** 316 * Finds a type by its enum. 317 * @param type Enum representing the desired type. 318 * @return Returns the type with the corresponding enum. 319 */ 320 daeAtomicType* get(daeEnum type); 321 322 private: 323 daeAtomicTypeArray types; 324 }; 325 326 327 /** 328 * The @c daeBoolType class is derived from @c daeAtomicType, and implements 329 * the reflective system for objects of type @c daeBool. 330 */ 331 class DLLSPEC daeBoolType : public daeAtomicType 332 { 333 public: 334 /** 335 * Constructor 336 */ 337 daeBoolType(DAE& dae); 338 public: 339 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 340 341 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 342 343 virtual daeMemoryRef create(); 344 345 virtual void destroy(daeMemoryRef obj); 346 347 virtual void copy(daeChar* src, daeChar* dst); 348 349 virtual daeArray* createArray(); 350 }; 351 352 /** 353 * The @c daeIntType class is derived from @c daeAtomicType, and implements 354 * the reflective system for objects of type @c daeInt. 355 */ 356 class DLLSPEC daeIntType : public daeAtomicType 357 { 358 public: 359 /** 360 * Constructor 361 */ 362 daeIntType(DAE& dae); 363 public: 364 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 365 366 virtual daeMemoryRef create(); 367 368 virtual void destroy(daeMemoryRef obj); 369 370 virtual void copy(daeChar* src, daeChar* dst); 371 372 virtual daeArray* createArray(); 373 }; 374 375 /** 376 * The @c daeLongType class is derived from @c daeAtomicType, and implements 377 * the reflective system for objects of type @c daeLong. 378 */ 379 class DLLSPEC daeLongType : public daeAtomicType 380 { 381 public: 382 /** 383 * Constructor 384 */ 385 daeLongType(DAE& dae); 386 public: 387 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 388 389 virtual daeMemoryRef create(); 390 391 virtual void destroy(daeMemoryRef obj); 392 393 virtual void copy(daeChar* src, daeChar* dst); 394 395 virtual daeArray* createArray(); 396 }; 397 398 /** 399 * The @c daeUIntType class is derived from @c daeAtomicType, and implements 400 * the reflective system for objects of type @c daeUInt. 401 */ 402 class DLLSPEC daeUIntType : public daeAtomicType 403 { 404 public: 405 /** 406 * Constructor 407 */ 408 daeUIntType(DAE& dae); 409 public: 410 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 411 412 virtual daeMemoryRef create(); 413 414 virtual void destroy(daeMemoryRef obj); 415 416 virtual void copy(daeChar* src, daeChar* dst); 417 418 virtual daeArray* createArray(); 419 }; 420 421 /** 422 * The @c daeUIntType class is derived from @c daeAtomicType, and implements 423 * the reflective system for objects of type @c daeUInt. 424 */ 425 class DLLSPEC daeULongType : public daeAtomicType 426 { 427 public: 428 /** 429 * Constructor 430 */ 431 daeULongType(DAE& dae); 432 public: 433 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 434 435 virtual daeMemoryRef create(); 436 437 virtual void destroy(daeMemoryRef obj); 438 439 virtual void copy(daeChar* src, daeChar* dst); 440 441 virtual daeArray* createArray(); 442 }; 443 444 /** 445 * The @c daeShortType is derived from @c daeAtomicType, and implements 446 * the reflective system for objects of type @c daeShort. 447 */ 448 class DLLSPEC daeShortType : public daeAtomicType 449 { 450 public: 451 /** 452 * Constructor 453 */ 454 daeShortType(DAE& dae); 455 public: 456 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 457 458 virtual daeMemoryRef create(); 459 460 virtual void destroy(daeMemoryRef obj); 461 462 virtual void copy(daeChar* src, daeChar* dst); 463 464 virtual daeArray* createArray(); 465 }; 466 467 /** 468 * The @c daeFloatType is derived from @c daeAtomicType, and implements 469 * the reflective system for objects of type @c daeFloat. 470 */ 471 class DLLSPEC daeFloatType : public daeAtomicType 472 { 473 public: 474 /** 475 * Constructor 476 */ 477 daeFloatType(DAE& dae); 478 public: 479 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 480 481 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 482 483 virtual daeMemoryRef create(); 484 485 virtual void destroy(daeMemoryRef obj); 486 487 virtual void copy(daeChar* src, daeChar* dst); 488 489 virtual daeArray* createArray(); 490 }; 491 492 /** 493 * The @c daeDoubleType is derived from @c daeAtomicType, and implements 494 * the reflective system for objects of type @c daeDouble. 495 */ 496 class DLLSPEC daeDoubleType : public daeAtomicType 497 { 498 public: 499 /** 500 * Constructor 501 */ 502 daeDoubleType(DAE& dae); 503 public: 504 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 505 506 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 507 508 virtual daeMemoryRef create(); 509 510 virtual void destroy(daeMemoryRef obj); 511 512 virtual void copy(daeChar* src, daeChar* dst); 513 514 virtual daeArray* createArray(); 515 }; 516 517 /** 518 * The @c daeStringRefType class is derived from @c daeAtomicType, and implements 519 * the reflective system for objects of type @c daeStringRef. 520 */ 521 class DLLSPEC daeStringRefType : public daeAtomicType 522 { 523 public: 524 /** 525 * Constructor 526 */ 527 daeStringRefType(DAE& dae); 528 public: 529 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 530 531 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 532 533 virtual daeInt compare(daeChar* value1, daeChar* value2); 534 535 virtual daeMemoryRef create(); 536 537 virtual void destroy(daeMemoryRef obj); 538 539 virtual void copy(daeChar* src, daeChar* dst); 540 541 virtual daeArray* createArray(); 542 }; 543 544 /** 545 * The @c daeTokenType class is derived from @c daeStringRefType, and implements 546 * the reflective system for objects of type daeStringRef, with specialized 547 * treatment from the parser. 548 */ 549 class DLLSPEC daeTokenType : public daeStringRefType 550 { 551 public: 552 /** 553 * Constructor 554 */ 555 daeTokenType(DAE& dae); 556 557 public: 558 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 559 560 virtual daeMemoryRef create(); 561 562 virtual void destroy(daeMemoryRef obj); 563 564 virtual void copy(daeChar* src, daeChar* dst); 565 566 virtual daeArray* createArray(); 567 }; 568 569 /** 570 * The @c daeElementRefType class is derived from @c daeAtomicType, and implements 571 * the reflective system for objects of type @c daeElementRef. 572 */ 573 class DLLSPEC daeElementRefType : public daeAtomicType 574 { 575 public: 576 /** 577 * The @c daeMetaElement for the type this @c daeElementRefType represents. 578 */ 579 daeMetaElement* _elementType; 580 581 public: 582 /** 583 * Constructor 584 */ 585 daeElementRefType(DAE& dae); 586 public: 587 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 588 589 virtual daeMemoryRef create(); 590 591 virtual void destroy(daeMemoryRef obj); 592 593 virtual void copy(daeChar* src, daeChar* dst); 594 595 virtual daeArray* createArray(); 596 }; 597 598 /** 599 * The @c daeEnumType class is derived from @c daeAtomicType, and implements 600 * the reflective system for objects of type daeEnum. 601 */ 602 class DLLSPEC daeEnumType: public daeAtomicType 603 { 604 public: 605 /** 606 * The array which contains the values used in this enum. 607 */ 608 daeEnumArray* _values; 609 /** 610 * The array which contains the strings to associate with the values used in this enum. 611 */ 612 daeStringRefArray* _strings; 613 614 public: 615 /** 616 * Constructor 617 */ 618 daeEnumType(DAE& dae); 619 620 /** 621 * Destructor 622 */ 623 ~daeEnumType(); 624 625 public: 626 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 627 628 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 629 630 virtual daeMemoryRef create(); 631 632 virtual void destroy(daeMemoryRef obj); 633 634 virtual void copy(daeChar* src, daeChar* dst); 635 636 virtual daeArray* createArray(); 637 }; 638 639 /** 640 * The @c daeRawRefType class is derived from @c daeAtomicType, and implements 641 * the reflective system for objects of type @c daeRawRef. 642 */ 643 class DLLSPEC daeRawRefType: public daeAtomicType 644 { 645 public: 646 /** 647 * Constructor. 648 */ 649 daeRawRefType(DAE& dae); 650 651 public: 652 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 653 654 virtual daeMemoryRef create(); 655 656 virtual void destroy(daeMemoryRef obj); 657 658 virtual void copy(daeChar* src, daeChar* dst); 659 660 virtual daeArray* createArray(); 661 }; 662 663 /** 664 * The @c daeResolverType class is derived from @c daeAtomicType, and implements 665 * the reflective system for objects of type @c daeResolver. 666 */ 667 class DLLSPEC daeResolverType : public daeAtomicType 668 { 669 public: 670 /** 671 * Constructor. 672 */ 673 daeResolverType(DAE& dae); 674 public: 675 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 676 677 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 678 679 virtual daeInt compare(daeChar* value1, daeChar* value2); 680 681 virtual daeMemoryRef create(); 682 683 virtual void destroy(daeMemoryRef obj); 684 685 virtual void copy(daeChar* src, daeChar* dst); 686 687 virtual daeArray* createArray(); 688 689 virtual void setDocument(daeChar* value, daeDocument* doc); 690 691 virtual void setDocument(daeArray& array, daeDocument* doc); 692 }; 693 694 /** 695 * The @c daeIDResolverType class is derived from @c daeAtomicType, and implements 696 * the reflective system for objects of type @c daeIDResolver. 697 */ 698 class DLLSPEC daeIDResolverType : public daeAtomicType 699 { 700 public: 701 /** 702 * Constructor. 703 */ 704 daeIDResolverType(DAE& dae); 705 public: 706 virtual daeBool memoryToString(daeChar* src, std::ostringstream& dst); 707 708 virtual daeBool stringToMemory(daeChar* src, daeChar* dst); 709 710 virtual daeInt compare(daeChar* value1, daeChar* value2); 711 712 virtual daeMemoryRef create(); 713 714 virtual void destroy(daeMemoryRef obj); 715 716 virtual void copy(daeChar* src, daeChar* dst); 717 718 virtual daeArray* createArray(); 719 }; 720 721 722 723 #endif // __DAE_ATOMIC_TYPE_H__ 724 725 726 727