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 #include <dae/daeMetaElementAttribute.h> 10 #include <dae/daeMetaElement.h> 11 12 daeMetaElementAttribute::daeMetaElementAttribute( daeMetaElement *container, daeMetaCMPolicy *parent, daeUInt ordinal, 13 daeInt minO, daeInt maxO) : daeMetaCMPolicy( container, parent, ordinal, minO, maxO ) 14 { 15 _elementType = NULL; 16 } 17 18 daeMetaElementAttribute::~daeMetaElementAttribute() 19 {} 20 21 daeMetaElementArrayAttribute::daeMetaElementArrayAttribute( daeMetaElement *container, daeMetaCMPolicy *parent, daeUInt ordinal, 22 daeInt minO, daeInt maxO) : daeMetaElementAttribute( container, parent, ordinal, minO, maxO ) 23 { 24 } 25 26 daeMetaElementArrayAttribute::~daeMetaElementArrayAttribute() 27 {} 28 29 30 void daeMetaElementAttribute::set(daeElement* e, daeString s) 31 { 32 //_type->stringToMemory((char*)s, getWritableMemory(e)); 33 daeElementRef *ref = (daeElementRef*)(getWritableMemory(e)); 34 if ((*ref) == NULL) { 35 (*ref) = _elementType->create(); 36 } 37 (*ref)->getMeta()->getValueAttribute()->stringToMemory((*ref), s); 38 } 39 40 void daeMetaElementAttribute::copy(daeElement* to, daeElement *from) { 41 daeElement *cpy = (*(daeElementRef*)(getWritableMemory(from)))->clone(); 42 (*(daeElementRef*)(getWritableMemory(to))) = cpy; 43 } 44 45 void daeMetaElementArrayAttribute::copy(daeElement* to, daeElement *from) { 46 (void)to; 47 (void)from; 48 } 49 50 void 51 daeMetaElementAttribute::setDocument( daeElement * parent, daeDocument* c ) 52 { 53 daeElementRef* er = (daeElementRef*)getWritableMemory( parent ); 54 if ( ((daeElement*)(*er)) != NULL ) { 55 (*er)->setDocument( c ); 56 } 57 } 58 59 void 60 daeMetaElementArrayAttribute::setDocument( daeElement * parent, daeDocument* c ) 61 { 62 daeElementRefArray* era = (daeElementRefArray*)getWritableMemory( parent ); 63 for ( unsigned int i = 0; i < era->getCount(); i++ ) { 64 era->get(i)->setDocument( c ); 65 } 66 } 67 68 daeInt 69 daeMetaElementAttribute::getCount(daeElement* e) 70 { 71 if (e == NULL) 72 return 0; 73 return ((*((daeElementRef*)getWritableMemory(e))) != NULL); 74 } 75 76 daeMemoryRef 77 daeMetaElementAttribute::get(daeElement *e, daeInt index) 78 { 79 (void)index; 80 return getWritableMemory(e); 81 } 82 83 daeInt 84 daeMetaElementArrayAttribute::getCount(daeElement *e) 85 { 86 if (e == NULL) 87 return 0; 88 daeElementRefArray* era = (daeElementRefArray*)getWritableMemory(e); 89 if (era == NULL) 90 return 0; 91 return (daeInt)era->getCount(); 92 } 93 94 daeMemoryRef 95 daeMetaElementArrayAttribute::get(daeElement* e, daeInt index) 96 { 97 if (e == NULL) 98 return NULL; 99 daeElementRefArray* era = (daeElementRefArray*)getWritableMemory(e); 100 if (era == NULL || index >= (daeInt)era->getCount() ) 101 return NULL; 102 return (daeMemoryRef)&(era->get(index)); 103 } 104 105 daeElement * 106 daeMetaElementAttribute::placeElement(daeElement* parent, daeElement* child, daeUInt &ordinal, daeInt offset, daeElement* before, daeElement *after ) 107 { 108 (void)offset; 109 (void)before; 110 (void)after; 111 if ((parent == NULL)||(child == NULL)) 112 return NULL; 113 if ( child->getMeta() != _elementType || strcmp( child->getElementName(), _name ) != 0 ) { 114 return NULL; 115 } 116 if (child->getParentElement() == parent) { 117 //I Don't know why this gets called when the child already has this as parent. 118 return child; 119 } 120 daeElementRef* er = (daeElementRef*)getWritableMemory(parent); 121 122 if ( *er != NULL ) 123 { 124 return NULL; 125 } 126 127 daeElement::removeFromParent( child ); 128 child->setParentElement( parent ); 129 130 *er = child; 131 ordinal = _ordinalOffset; 132 133 return child; 134 } 135 136 daeElement * 137 daeMetaElementArrayAttribute::placeElement(daeElement* parent, daeElement* child, daeUInt &ordinal, daeInt offset, daeElement* before, daeElement *after ) 138 { 139 if ((parent == NULL)||(child == NULL)) 140 return NULL; 141 if ( child->getMeta() != _elementType) 142 return NULL; 143 144 daeElementRefArray* era = (daeElementRefArray*)getWritableMemory(parent); 145 if ( _maxOccurs != -1 && (daeInt)era->getCount()-offset >= _maxOccurs ) { 146 return NULL; 147 } 148 149 if (strcmp( child->getElementName(), _name ) != 0 ) 150 return NULL; 151 152 daeElement *p = child->getParentElement(); 153 removeElement( p, child ); 154 child->setParentElement( parent ); 155 156 if ( before != NULL && before->getMeta() == _elementType ) { 157 size_t idx(0); 158 if ( era->find( before, idx ) == DAE_OK ) { 159 era->insertAt( idx, child ); 160 } 161 } 162 else if ( after != NULL && after->getMeta() == _elementType ) { 163 size_t idx(0); 164 if ( era->find( after, idx ) == DAE_OK ) { 165 era->insertAt( idx+1, child ); 166 } 167 } 168 else { 169 era->append(child); 170 } 171 ordinal = _ordinalOffset; 172 173 return child; 174 } 175 176 // These are the opposite of the placeElement functions above 177 daeBool 178 daeMetaElementAttribute::removeElement(daeElement* parent, daeElement* child) 179 { 180 (void)child; // silence unused variable warning 181 182 if ((parent == NULL)||(child == NULL )) 183 return false; 184 185 daeElementRef* er = (daeElementRef*)getWritableMemory(parent); 186 if ( *er != child ) { 187 return false; 188 } 189 *er = NULL; 190 return true; 191 } 192 193 daeBool 194 daeMetaElementArrayAttribute::removeElement(daeElement* parent, 195 daeElement* child) 196 { 197 if ((parent == NULL)||(child == NULL)) 198 return false ; 199 200 daeElementRefArray* era = (daeElementRefArray*)getWritableMemory(parent); 201 /* if ( (daeInt)era->getCount() <= _minOccurs ) { 202 return false; 203 }*/ 204 daeInt error = era->remove(child); 205 if ( error != DAE_OK ) { 206 return false; 207 } 208 return true; 209 } 210 211 daeMetaElement *daeMetaElementAttribute::findChild( daeString elementName ) { 212 if ( strcmp( elementName, _name ) == 0 ) { 213 return _elementType; 214 } 215 return NULL; 216 } 217 218 void daeMetaElementAttribute::getChildren( daeElement *parent, daeElementRefArray &array ) { 219 daeElementRef* er = (daeElementRef*)getWritableMemory(parent); 220 if ( *er != NULL ) { 221 array.appendUnique( *er ); 222 } 223 } 224 225 void daeMetaElementArrayAttribute::getChildren( daeElement *parent, daeElementRefArray &array ) { 226 daeElementRefArray* era = (daeElementRefArray*)getWritableMemory(parent); 227 size_t cnt = era->getCount(); 228 for ( size_t x = 0; x < cnt; x++ ) { 229 array.appendUnique( era->get(x) ); 230 } 231 } 232