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