Home | History | Annotate | Download | only in dae
      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_META_ATTRIBUTE_H__
     10 #define __DAE_META_ATTRIBUTE_H__
     11 
     12 #include <string>
     13 #include <sstream>
     14 #include <dae/daeTypes.h>
     15 #include <dae/daeStringRef.h>
     16 #include <dae/daeAtomicType.h>
     17 #include <dae/daeElement.h>
     18 #include <dae/daeArray.h>
     19 
     20 class daeElement;
     21 class daeMetaElement;
     22 class daeMetaAttribute;
     23 class daeMetaElementAttribute;
     24 
     25 /**
     26  * The @c daeMetaAttribute class describes one attribute in a C++ COLLADA dom element.
     27  *
     28  * In the case of the C++ object model a conceptual attribute can be
     29  * either a dom attribute, a dom element, or a dom value.
     30  * Essentially, the meta attribute describes fields on the C++ class.
     31  * However these attributes are stored separately in the containing meta
     32  * @c daeMetaElement.
     33  * @c daeMetaAttributes always exist inside of @c daeMetaElements.
     34  * Each @c daeMetaAttribute has certain semantic operations it is capable of
     35  * including @c set(), @c get(), and @c print().
     36  * @c daeMetaAttributes use the @c daeAtomicType system as their underlying semantic
     37  * implementation, but contain additional information about the packaging
     38  * of the atomic types into the C++ dom classes such as offset, and
     39  * array information.
     40  */
     41 class DLLSPEC daeMetaAttribute : public daeRefCountedObj
     42 {
     43 protected:
     44 	daeStringRef    _name;
     45 	daeInt          _offset;
     46 	daeAtomicType*  _type;
     47 	daeMetaElement*	_container;
     48 	std::string     _defaultString;
     49 	daeMemoryRef    _defaultValue;
     50 	daeBool         _isRequired;
     51 
     52 public:
     53 	/**
     54 	 * Constructor
     55 	 */
     56 	daeMetaAttribute();
     57 
     58 	/**
     59 	 * Destructor
     60 	 */
     61 	virtual ~daeMetaAttribute();
     62 public:
     63 	/**
     64 	 * Determines if the schema indicates that this is a required attribute.
     65 	 * @return Returns true if this is a required attribute, false if not.
     66 	 */
     67 	daeBool getIsRequired() {return _isRequired; }
     68 	/**
     69 	 * Sets the value that indicates that this attribute is required by the schema.  If set, the attribute
     70 	 * will always be exported by the API regardless of its value.
     71 	 * @param isRequired Indicates if the schema says this attribute is required, true if it is, false if not.
     72 	 */
     73 	void setIsRequired(daeBool isRequired) {_isRequired = isRequired;}
     74 	/**
     75 	 * Sets the byte offset (from @c this) where this attribute's storage is
     76 	 * found in its container element class.
     77 	 * @param offset Integer byte offset from @c this pointer.
     78 	 */
     79 	void setOffset(daeInt offset) { _offset = offset; }
     80 
     81 	/**
     82 	 * Gets the byte offset (from @ this) where this attribute's storage is
     83 	 * found in its container element class.
     84 	 * @return Returns the integer byte offset from @c this pointer for this attribute.
     85 	 */
     86 	daeInt getOffset() { return _offset; }
     87 
     88 	/**
     89 	 * Sets the name of the attribute.
     90 	 * @param name @c daeString that is directly stored as a pointer
     91 	 * without being copied.
     92 	 */
     93 	void setName(daeString name) { _name = name; }
     94 
     95 	/**
     96 	 * Gets the name of this attribute.
     97 	 * @return Returnsthe name of this attribute.
     98 	 */
     99 	daeStringRef getName() { return _name; }
    100 
    101 	/**
    102 	 * Sets the type of the attribute.
    103 	 * @param type @c daeAtomicType to use for interacting with this
    104 	 * attribute in a containing @c daeElement.
    105 	 */
    106 	void setType(daeAtomicType* type) { _type = type; }
    107 
    108 	/**
    109 	 * Gets the @c daeAtomicType used by this attribute.
    110 	 * @return Returns the @c daeAtomicType that this attribute uses for its
    111 	 * implementation.
    112 	 */
    113 	daeAtomicType* getType() { return _type; }
    114 
    115 	/**
    116 	 * Sets the default for this attribute via a string.
    117 	 * @param defaultVal @c daeString representing the default value.
    118 	 */
    119 	virtual void setDefaultString(daeString defaultVal);
    120 
    121 	/**
    122 	 * Sets the default for this attribute via a memory pointer.
    123 	 * @param defaultVal @c daeMemoryRef representing the default value.
    124 	 */
    125 	virtual void setDefaultValue(daeMemoryRef defaultVal);
    126 
    127 	/**
    128 	 * Gets the default for this attribute as a string.
    129 	 * @return Returns a @c daeString representing the default value.
    130 	 */
    131 	daeString getDefaultString();
    132 
    133 	/**
    134 	 * Gets the default for this attribute as a memory value.
    135 	 * @return Returns a @c daeMemoryRef representing the default value.
    136 	 */
    137 	daeMemoryRef getDefaultValue();
    138 
    139 	/**
    140 	 * Sets the containing @c daeMetaElement for this attribute.
    141 	 * @param container Element on which this @c daeMetaAttribute belongs.
    142 	 */
    143 	void setContainer(daeMetaElement* container) { _container = container; }
    144 
    145 	/**
    146 	 * Gets the containing @c daeMetaElement for this attribute.
    147 	 * @return Returns the @c daeMetaElement to which this @c daeAttribute belongs.
    148 	 */
    149 	daeMetaElement* getContainer() { return _container; }
    150 
    151 	/**
    152 	 * Notifies an attribute when the containing document changes.
    153 	 */
    154 	virtual void setDocument(daeElement* e, daeDocument* doc);
    155 
    156 	/**
    157 	 * Converts an element's attribute value to a string.
    158 	 */
    159 	virtual void memoryToString(daeElement* e, std::ostringstream& buffer);
    160 
    161 	/**
    162 	 * Converts a string to a memory value in the specified element.
    163 	 */
    164 	virtual void stringToMemory(daeElement* e, daeString s);
    165 
    166 	/**
    167 	 * Gets the attribute's memory pointer from containing element <tt><i>e</i></tt>.
    168 	 * @param e Containing element from which to get the value.
    169 	 * @return Returns the memory pointer corresponding to this attribute  out of parent element e.
    170 	 */
    171 	virtual daeMemoryRef get(daeElement* e);
    172 
    173 	/**
    174 	 * Gets if this attribute is an array attribute.
    175 	 * @return Returns true if this attribute is an array type.
    176 	 */
    177 	virtual daeBool isArrayAttribute()		{ return false; }
    178 
    179 public:
    180 	/**
    181 	 * Gets the number of bytes for this attribute.
    182 	 * @return Returns the number of bytes in the C++ COLLADA dom element for this
    183 	 * attribute.
    184 	 */
    185 	virtual daeInt getSize();
    186 
    187 	/**
    188 	 * Gets the alignment in bytes on the class of this meta attribute type.
    189 	 * @return Returns the alignment in bytes.
    190 	 */
    191 	virtual daeInt getAlignment();
    192 
    193 	/**
    194 	 * Copies the value of this attribute from fromElement into toElement.
    195 	 * @param toElement Pointer to a @c daeElement to copy this attribute to.
    196 	 * @param fromElement Pointer to a @c daeElement to copy this attribute from.
    197 	 */
    198 	virtual void copy(daeElement* toElement, daeElement* fromElement);
    199 
    200 	/**
    201 	 * Copies the default value of this attribute to the element
    202 	 * @param element Pointer to a @c daeElement to copy the default value to.
    203 	 */
    204 	virtual void copyDefault(daeElement* element);
    205 
    206 	/**
    207 	 * Compares the value of this attribute in the given elements.
    208 	 * @param elt1 The first element whose attribute value should be compared.
    209 	 * @param elt2 The second element whose attribute value should be compared.
    210 	 * @return Returns a positive integer if value1 > value2, a negative integer if
    211 	 * value1 < value2, and 0 if value1 == value2.
    212 	 */
    213 	virtual daeInt compare(daeElement* elt1, daeElement* elt2);
    214 
    215 	/**
    216 	 * Compares the value of this attribute from the given element to the default value
    217 	 * of this attribute (if one exists).
    218 	 * @param e The element whose value should be compared to the default value.
    219 	 * @return Returns a positive integer if value > default, a negative integer if
    220 	 * value < default, and 0 if value == default.
    221 	 */
    222 	virtual daeInt compareToDefault(daeElement* e);
    223 
    224 public:
    225 	// These methods are deprecated.
    226 	virtual daeChar* getWritableMemory(daeElement* e);  // Use get instead.
    227 	virtual void set(daeElement* element, daeString s); // Use stringToMemory instead.
    228 };
    229 
    230 
    231 /**
    232  * The @c daeMetaArrayAttribute class is simple a wrapper that implements
    233  * an array of atomic types rather than a singleton.
    234  * The corresponding storage is an array
    235  * and the corresponding operations are implemented on the array
    236  * data structure rather than on inlined storage in elements.
    237  */
    238 class DLLSPEC daeMetaArrayAttribute : public daeMetaAttribute
    239 {
    240 public:
    241 	virtual ~daeMetaArrayAttribute();
    242 
    243 	/**
    244 	 * Defines the override version of this method from @c daeMetaAttribute.
    245 	 * @param toElement Pointer to a @c daeElement to copy this attribute to.
    246 	 * @param fromElement Pointer to a @c daeElement to copy this attribute from.
    247 	 */
    248 	virtual void copy(daeElement* toElement, daeElement* fromElement);
    249 
    250 	/**
    251 	 * Copies the default value of this attribute to the element
    252 	 * @param element Pointer to a @c daeElement to copy the default value to.
    253 	 */
    254 	virtual void copyDefault(daeElement* element);
    255 
    256 	/**
    257 	 * Compares the value of this attribute in the given elements.
    258 	 * @param elt1 The first element whose attribute value should be compared.
    259 	 * @param elt2 The second element whose attribute value should be compared.
    260 	 * @return Returns a positive integer if value1 > value2, a negative integer if
    261 	 * value1 < value2, and 0 if value1 == value2.
    262 	 */
    263 	virtual daeInt compare(daeElement* elt1, daeElement* elt2);
    264 
    265 	/**
    266 	 * Compares the value of this attribute from the given element to the default value
    267 	 * of this attribute (if one exists).
    268 	 * @param e The element whose value should be compared to the default value.
    269 	 * @return Returns a positive integer if value > default, a negative integer if
    270 	 * value < default, and 0 if value == default.
    271 	 */
    272 	virtual daeInt compareToDefault(daeElement* e);
    273 
    274 	/**
    275 	 * Converts an element's attribute value to a string.
    276 	 */
    277 	virtual void memoryToString(daeElement* e, std::ostringstream& buffer);
    278 
    279 	/**
    280 	 * Converts a string to a memory value in the specified element.
    281 	 */
    282 	virtual void stringToMemory(daeElement* e, daeString s);
    283 
    284 	/**
    285 	 * Sets the default for this attribute via a string.
    286 	 * @param defaultVal @c daeString representing the default value.
    287 	 */
    288 	virtual void setDefaultString(daeString defaultVal);
    289 
    290 	/**
    291 	 * Sets the default for this attribute via a memory pointer.
    292 	 * @param defaultVal @c daeMemoryRef representing the default value.
    293 	 */
    294 	virtual void setDefaultValue(daeMemoryRef defaultVal);
    295 
    296 	/**
    297 	 * Gets if this attribute is an array attribute.
    298 	 * @return Returns true if this attribute is an array type.
    299 	 */
    300 	virtual daeBool isArrayAttribute() { return true; }
    301 
    302 	/**
    303 	 * Notifies an attribute when the containing document changes.
    304 	 */
    305 	virtual void setDocument(daeElement* e, daeDocument* doc);
    306 };
    307 
    308 
    309 typedef daeSmartRef<daeMetaAttribute> daeMetaAttributeRef;
    310 
    311 typedef daeTArray<daeMetaAttributeRef> daeMetaAttributeRefArray;
    312 typedef daeTArray<daeMetaAttribute*> daeMetaAttributePtrArray;
    313 
    314 #endif //__DAE_META_ATTRIBUTE_H__
    315 
    316 
    317 
    318 
    319 
    320 
    321