1 /////////////////////////////////////////////////////////////////////////////// 2 // BSD 3-Clause License 3 // 4 // Copyright (c) 2020, The Regents of the University of California 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions are met: 9 // 10 // * Redistributions of source code must retain the above copyright notice, this 11 // list of conditions and the following disclaimer. 12 // 13 // * Redistributions in binary form must reproduce the above copyright notice, 14 // this list of conditions and the following disclaimer in the documentation 15 // and/or other materials provided with the distribution. 16 // 17 // * Neither the name of the copyright holder nor the names of its 18 // contributors may be used to endorse or promote products derived from 19 // this software without specific prior written permission. 20 // 21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 // POSSIBILITY OF SUCH DAMAGE. 32 33 // Generator Code Begin Cpp 34 #include "{{klass.name}}.h" 35 #include "db.h" 36 #include "dbDiff.hpp" 37 #include "dbDatabase.h" 38 #include "dbTable.h" 39 #include "dbTable.hpp" 40 41 {% for include in klass.cpp_includes %} 42 #include "{{include}}" 43 {% endfor %} 44 // User Code Begin Includes 45 // User Code End Includes 46 namespace odb { 47 48 template class dbTable<_{{klass.name}}>; 49 50 bool _{{klass.name}}::operator==(const _{{klass.name}}& rhs) const 51 { 52 {% for field in klass.fields %} 53 {% if field.bitFields %} 54 {% for innerField in klass.structs[0].fields %} 55 {% for component in innerField.components %} 56 {% if 'no-cmp' not in innerField.flags %} 57 {% if innerField.table %} 58 if({{field.name}}->{{component}}!=rhs.{{field.name}}->{{component}}) 59 {% else %} 60 if({{field.name}}.{{component}}!=rhs.{{field.name}}.{{component}}) 61 {% endif %} 62 return false; 63 64 {% endif %} 65 {% endfor %} 66 {% endfor %} 67 68 69 70 {% else %} 71 72 {% for component in field.components %} 73 {% if 'no-cmp' not in field.flags %} 74 {% if field.table %} 75 if(*{{component}}!=*rhs.{{component}}) 76 {% else %} 77 if({{component}}!=rhs.{{component}}) 78 {% endif %} 79 return false; 80 {% endif %} 81 {% endfor %} 82 {% endif %} 83 {% endfor %} 84 85 86 87 88 //User Code Begin == 89 //User Code End == 90 return true; 91 } 92 bool _{{klass.name}}::operator<(const _{{klass.name}}& rhs) const 93 { 94 95 96 97 98 {% for field in klass.fields %} 99 {% if field.bitFields %} 100 {% for innerField in klass.structs[0].fields %} 101 {% for component in innerField.components %} 102 {% if 'cmpgt' in innerField.flags %} 103 104 if({{field.name}}.{{component}}>=rhs.{{field.name}}.{{component}}) 105 return false; 106 107 {% endif %} 108 {% endfor %} 109 {% endfor %} 110 111 {% else %} 112 113 {% for component in field.components %} 114 {% if 'cmpgt' in field.flags %} 115 if({{component}}>=rhs.{{component}}) 116 return false; 117 {% endif %} 118 {% endfor %} 119 {% endif %} 120 {% endfor %} 121 122 //User Code Begin < 123 //User Code End < 124 return true; 125 } 126 void _{{klass.name}}::differences(dbDiff& diff, const char* field, const _{{klass.name}}& rhs) const 127 { 128 129 DIFF_BEGIN 130 131 {% for field in klass.fields %} 132 {% if field.bitFields %} 133 {% for innerField in klass.structs[0].fields %} 134 {% for component in innerField.components %} 135 {% if 'no-diff' not in innerField.flags %} 136 DIFF_FIELD({{field.name}}.{{component}}); 137 {% endif %} 138 {% endfor %} 139 {% endfor %} 140 {% else %} 141 {% for component in field.components %} 142 {% if 'no-diff' not in field.flags %} 143 {% if field.table %} 144 DIFF_TABLE{%if 'no-deep' in field.flags%}_NO_DEEP{%endif%}({{component}}); 145 {% elif 'isHashTable' in field and field.isHashTable %} 146 DIFF_HASH_TABLE{% if 'no-deep' in field.flags %}_NO_DEEP{% endif %}({{component}}); 147 {% else %} 148 DIFF_FIELD{% if 'no-deep' in field.flags %}_NO_DEEP{% endif %}({{component}}); 149 {% endif %} 150 {% endif %} 151 {% endfor %} 152 {% endif %} 153 {% endfor %} 154 // User Code Begin Differences 155 // User Code End Differences 156 DIFF_END 157 } 158 void _{{klass.name}}::out(dbDiff& diff, char side, const char* field) const 159 { 160 DIFF_OUT_BEGIN 161 {% for field in klass.fields %} 162 {% if field.bitFields %} 163 {% for innerField in klass.structs[0].fields %} 164 {% for component in innerField.components %} 165 {% if 'no-diff' not in innerField.flags %} 166 DIFF_OUT_FIELD({{field.name}}.{{component}}); 167 {% endif %} 168 {% endfor %} 169 {% endfor %} 170 {% else %} 171 {% for component in field.components %} 172 {% if 'no-diff' not in field.flags %} 173 {% if field.table %} 174 DIFF_OUT_TABLE{% if 'no-deep' in field.flags %}_NO_DEEP{% endif %}({{component}}); 175 {% elif field.isHashTable %} 176 DIFF_OUT_HASH_TABLE{% if 'no-deep' in field.flags %}_NO_DEEP{% endif %}({{component}}); 177 {% else %} 178 DIFF_OUT_FIELD{% if 'no-deep' in field.flags %}_NO_DEEP{% endif %}({{component}}); 179 {% endif %} 180 {% endif %} 181 {% endfor %} 182 {% endif %} 183 {% endfor %} 184 185 // User Code Begin Out 186 // User Code End Out 187 DIFF_END 188 } 189 _{{klass.name}}::_{{klass.name}}(_dbDatabase* db) 190 { 191 {% for field in klass.fields %} 192 {% if field.bitFields %} 193 {% if field.numBits == 32 %} 194 uint32_t* {{field.name}}_bit_field = (uint32_t*) &{{field.name}}; 195 {% else %} 196 uint64_t* {{field.name}}_bit_field = (uint64_t*) &{{field.name}}; 197 {% endif %} 198 *{{field.name}}_bit_field = 0; 199 {% elif field.table %} 200 {{field.name}} = new dbTable<_{{field.type}}>(db, this, (GetObjTbl_t) &_{{klass.name}}::getObjectTable, {{field.type}}Obj); 201 ZALLOCATED({{field.name}}); 202 {% elif field.isHashTable %} 203 {{field.name}}.setTable({{field.table_name}}); 204 {% elif 'default' in field%} 205 {{field.name}} = {{field.default}}; 206 {% endif %} 207 {% endfor %} 208 // User Code Begin Constructor 209 // User Code End Constructor 210 } 211 _{{klass.name}}::_{{klass.name}}(_dbDatabase* db, const _{{klass.name}}& r) 212 { 213 {% for field in klass.fields %} 214 {% for component in field.components %} 215 {% if field.table %} 216 {{field.name}} = new dbTable<_{{field.type}}>(db, this, *r.{{field.name}}); 217 ZALLOCATED({{field.name}}); 218 {% elif field.isHashTable %} 219 {{field.name}}.setTable({{field.table_name}}); 220 {% else %} 221 {{component}}=r.{{component}}; 222 {% endif %} 223 {% endfor %} 224 {% endfor %} 225 //User Code Begin CopyConstructor 226 //User Code End CopyConstructor 227 } 228 229 {% for i in range(klass.constructors|length) %} 230 _{{klass.name}}::_{{klass.name}}(_dbDatabase* db{% for arg in klass.constructors[i].args %},{{arg.type}} {{arg.name}}{% endfor %}) 231 { 232 {% for arg in klass.constructors[i].args %} 233 {% if arg.get('field') is not none %} 234 this->{{arg.field}}={{arg.name}}; 235 {% endif %} 236 {% endfor %} 237 //User Code Begin CustomConstructor{{i}} 238 //User Code End CustomConstructor{{i}} 239 } 240 {% endfor %} 241 dbIStream& operator>>(dbIStream& stream, _{{klass.name}}& obj) 242 { 243 {% for field in klass.fields %} 244 {% if field.bitFields %} 245 {% if field.numBits == 32 %} 246 uint32_t* {{field.name}}_bit_field = (uint32_t*) &obj.{{field.name}}; 247 {% else %} 248 uint64_t* {{field.name}}_bit_field = (uint64_t*) &obj.{{field.name}}; 249 {% endif %} 250 stream >> *{{field.name}}_bit_field; 251 {% else %} 252 {% if 'no-serial' not in field.flags %} 253 stream >> {% if field.table %}*{% endif %}obj.{{field.name}}; 254 {% endif %} 255 {% endif %} 256 {% endfor %} 257 //User Code Begin >> 258 //User Code End >> 259 return stream; 260 } 261 dbOStream& operator<<(dbOStream& stream, const _{{klass.name}}& obj) 262 { 263 {% for field in klass.fields %} 264 {% if field.bitFields %} 265 {% if field.numBits == 32 %} 266 uint32_t* {{field.name}}_bit_field = (uint32_t*) &obj.{{field.name}}; 267 {% else %} 268 uint64_t* {{field.name}}_bit_field = (uint64_t*) &obj.{{field.name}}; 269 {% endif %} 270 stream << *{{field.name}}_bit_field; 271 {% else %} 272 {% if 'no-serial' not in field.flags %} 273 stream << {% if field.table %}*{% endif %}obj.{{field.name}}; 274 {% endif %} 275 {% endif %} 276 {% endfor %} 277 //User Code Begin << 278 //User Code End << 279 return stream; 280 } 281 282 {% if klass.hasTables %} 283 dbObjectTable* _{{klass.name}}::getObjectTable(dbObjectType type) 284 { 285 switch (type) { 286 {% for field in klass.fields %} 287 {% if field.table %} 288 case {{field.type}}Obj: 289 return {{field.name}}; 290 {% endif %} 291 {% endfor %} 292 //User Code Begin getObjectTable 293 //User Code End getObjectTable 294 default: 295 break; 296 } 297 return getTable()->getObjectTable(type); 298 } 299 {% endif %} 300 _{{klass.name}}::~_{{klass.name}}() 301 { 302 {% for field in klass.fields %} 303 {% if field.name == '_name' %} 304 if(_name) 305 free((void*) _name); 306 {% endif %} 307 {% if field.table %} 308 delete {{field.name}}; 309 {% endif %} 310 {% endfor %} 311 //User Code Begin Destructor 312 //User Code End Destructor 313 } 314 315 //User Code Begin PrivateMethods 316 //User Code End PrivateMethods 317 318 //////////////////////////////////////////////////////////////////// 319 // 320 // {{klass.name}} - Methods 321 // 322 //////////////////////////////////////////////////////////////////// 323 {% for field in klass.fields %} 324 325 {% if 'no-set' not in field.flags %} 326 void {{klass.name}}::{{field.setterFunctionName}}( {{field.setterArgumentType}} {{field.argument}} ) 327 { 328 329 _{{klass.name}}* obj = (_{{klass.name}}*)this; 330 331 {% if field.isRef %} 332 333 obj->{{field.name}}={{field.argument}}->getImpl()->getOID(); 334 335 {% else %} 336 obj->{{field.name}}={{field.argument}}; 337 338 {% endif %} 339 } 340 {% endif %} 341 342 {% if 'no-get' not in field.flags %} 343 {% if field.dbSetGetter %} 344 dbSet<{{field.type}}> {{klass.name}}::get{{field.functional_name}}() const 345 { 346 _{{klass.name}}* obj = (_{{klass.name}}*)this; 347 return dbSet<{{field.type}}>(obj, obj->{{field.name}}); 348 } 349 {% elif field.isPassByRef %} 350 void {{klass.name}}::{{field.getterFunctionName}}({{field.getterReturnType}}& tbl) const 351 { 352 _{{klass.name}}* obj = (_{{klass.name}}*)this; 353 tbl = obj->{{field.name}}; 354 } 355 {% elif field.isHashTable %} 356 {{field.getterReturnType}} {{klass.name}}::{{field.getterFunctionName}}(const char* name) const 357 { 358 _{{klass.name}}* obj = (_{{klass.name}}*)this; 359 return ({{field.getterReturnType}}) obj->{{field.name}}.find(name); 360 } 361 {% else %} 362 {{field.getterReturnType}} {{klass.name}}::{{field.getterFunctionName}}({% if field.isHashTable %}const char* name{% endif %}) const 363 { 364 _{{klass.name}}* obj = (_{{klass.name}}*)this; 365 {% if field.isRef %} 366 if(obj->{{field.name}} == 0) 367 return NULL; 368 _{{field.parent}}* par = (_{{field.parent}}*) obj->getOwner(); 369 return ({{field.refType}}) par->{{field.refTable}}->getPtr(obj->{{field.name}}); 370 {% elif field.isHashTable %} 371 return {{field.getterReturnType}} obj->{{field.name}}.find(name); 372 {% else %} 373 return obj->{{field.name}}; 374 {% endif %} 375 } 376 {% endif %} 377 {% endif %} 378 {% endfor %} 379 380 381 382 {% for _struct in klass.structs %} 383 384 {% if _struct.in_class %} 385 {% for field in _struct.fields %} 386 387 {% if 'no-set' not in field.flags %} 388 void {{klass.name}}::{{field.setterFunctionName}}( {{field.setterArgumentType}} {{field.argument}} ) 389 { 390 391 _{{klass.name}}* obj = (_{{klass.name}}*)this; 392 393 obj->{{_struct.in_class_name}}.{{field.name}}={{field.argument}}; 394 395 } 396 {% endif %} 397 398 {% if 'no-get' not in field.flags %} 399 {{field.getterReturnType}} {{klass.name}}::{{field.getterFunctionName}}() const 400 { 401 _{{klass.name}}* obj = (_{{klass.name}}*)this; 402 403 return obj->{{_struct.in_class_name}}.{{field.name}}; 404 } 405 {% endif %} 406 407 408 {% endfor %} 409 {% endif %} 410 {% endfor %} 411 412 413 //User Code Begin {{klass.name}}PublicMethods 414 //User Code End {{klass.name}}PublicMethods 415 } 416 // Generator Code End Cpp 417