1#ifndef COIN_SOSUBFIELD_H
2#define COIN_SOSUBFIELD_H
36#include <Inventor/SbBasic.h>
37#include <Inventor/SbName.h>
38#include <Inventor/C/tidbits.h>
43#include <Inventor/fields/SoField.h>
44#include <Inventor/SoInput.h>
45#include <Inventor/SoOutput.h>
54#define SO_SFIELD_CONSTRUCTOR_HEADER(_class_) \
60#define SO_SFIELD_REQUIRED_HEADER(_class_) \
62 static SoType classTypeId; \
63 static void atexit_cleanup(void) { SoType::removeType(_class_::classTypeId.getName()); _class_::classTypeId STATIC_SOTYPE_INIT; } \
65 static void * createInstance(void); \
66 static SoType getClassTypeId(void); \
67 virtual SoType getTypeId(void) const; \
69 virtual void copyFrom(const SoField & field); \
70 const _class_ & operator=(const _class_ & field); \
71 virtual SbBool isSame(const SoField & field) const
74#define PRIVATE_SFIELD_IO_HEADER() \
76 virtual SbBool readValue(SoInput * in); \
77 virtual void writeValue(SoOutput * out) const
80#define SO_SFIELD_VALUE_HEADER(_class_, _valtype_, _valref_) \
81 PRIVATE_SFIELD_IO_HEADER(); \
86 _valref_ getValue(void) const { this->evaluate(); return this->value; } \
87 void setValue(_valref_ newvalue); \
88 _valref_ operator=(_valref_ newvalue) { this->setValue(newvalue); return this->value; } \
90 int operator==(const _class_ & field) const; \
91 int operator!=(const _class_ & field) const { return ! operator==(field); }
96#define SO_SFIELD_DERIVED_VALUE_HEADER(_class_, _valtype_, _valref_) \
97 PRIVATE_SFIELD_IO_HEADER(); \
99 _valref_ operator=(_valref_ newvalue) { this->setValue(newvalue); return this->value; }
103#define SO_SFIELD_HEADER(_class_, _valtype_, _valref_) \
104 SO_SFIELD_CONSTRUCTOR_HEADER(_class_); \
105 SO_SFIELD_REQUIRED_HEADER(_class_); \
106 SO_SFIELD_VALUE_HEADER(_class_, _valtype_, _valref_)
109#define SO_SFIELD_DERIVED_HEADER(_class_, _valtype_, _valref_) \
110 SO_SFIELD_CONSTRUCTOR_HEADER(_class_); \
111 SO_SFIELD_REQUIRED_HEADER(_class_); \
112 SO_SFIELD_DERIVED_VALUE_HEADER(_class_, _valtype_, _valref_)
122#define PRIVATE_FIELD_INIT_CLASS(_class_, _classname_, _parent_, _createfunc_) \
125 assert(_parent_::getClassTypeId() != SoType::badType()); \
127 assert(_class_::classTypeId == SoType::badType()); \
128 _class_::classTypeId = \
129 SoType::createType(_parent_::getClassTypeId(), _classname_, _createfunc_); \
130 cc_coin_atexit_static_internal \
132 _class_::atexit_cleanup \
138#define SO_SFIELD_INIT_CLASS(_class_, _parent_) \
140 const char * classname = SO__QUOTE(_class_); \
141 PRIVATE_FIELD_INIT_CLASS(_class_, classname, _parent_, &_class_::createInstance); \
144#define SO_SFIELD_CONSTRUCTOR_SOURCE(_class_) \
145_class_::_class_(void) { assert(_class_::classTypeId != SoType::badType()); } \
146_class_::~_class_() { }
150#define SO_SFIELD_VALUE_SOURCE(_class_, _valtype_, _valref_) \
152_class_::setValue(_valref_ valuearg) { \
153 this->value = valuearg; \
154 this->valueChanged(); \
158_class_::operator==(const _class_ & field) const \
160 return (this->getValue() == field.getValue()); \
164#define PRIVATE_TYPEID_SOURCE(_class_) \
171SoType _class_::getTypeId(void) const { return _class_::classTypeId; } \
177SoType _class_::getClassTypeId(void) { return _class_::classTypeId; } \
182void * _class_::createInstance(void) { return new _class_; } \
183SoType _class_::classTypeId STATIC_SOTYPE_INIT
186#define PRIVATE_EQUALITY_SOURCE(_class_) \
188_class_::copyFrom(const SoField & field) \
190 this->operator=(static_cast<const _class_ &>(field)); \
194_class_::isSame(const SoField & field) const \
196 if (field.getTypeId() != this->getTypeId()) return FALSE; \
197 return this->operator==(static_cast<const _class_ &>(field)); \
202#define SO_SFIELD_REQUIRED_SOURCE(_class_) \
203PRIVATE_TYPEID_SOURCE(_class_); \
204PRIVATE_EQUALITY_SOURCE(_class_); \
207_class_::operator=(const _class_ & field) \
209 this->setValue(field.getValue()); \
215#define SO_SFIELD_SOURCE(_class_, _valtype_, _valref_) \
216 SO_SFIELD_CONSTRUCTOR_SOURCE(_class_); \
217 SO_SFIELD_VALUE_SOURCE(_class_, _valtype_, _valref_); \
218 SO_SFIELD_REQUIRED_SOURCE(_class_)
222#define SO_SFIELD_DERIVED_SOURCE(_class_, _valtype_, _valref_) \
223 SO_SFIELD_CONSTRUCTOR_SOURCE(_class_); \
224 SO_SFIELD_REQUIRED_SOURCE(_class_)
233#define PRIVATE_MFIELD_IO_HEADER() \
235 virtual SbBool read1Value(SoInput * in, int idx); \
236 virtual void write1Value(SoOutput * out, int idx) const
240#define SO_MFIELD_VALUE_HEADER(_class_, _valtype_, _valref_) \
241 PRIVATE_MFIELD_IO_HEADER(); \
243 virtual void deleteAllValues(void); \
244 virtual void copyValue(int to, int from); \
245 virtual int fieldSizeof(void) const; \
246 virtual void * valuesPtr(void); \
247 virtual void setValuesPtr(void * ptr); \
248 virtual void allocValues(int num); \
250 _valtype_ * values; \
252 _valref_ operator[](const int idx) const \
253 { this->evaluate(); return this->values[idx]; } \
257 const _valtype_ * getValues(const int start) const \
258 { this->evaluate(); return const_cast<const _valtype_ *>(this->values + start); } \
259 int find(_valref_ value, SbBool addifnotfound = FALSE); \
260 void setValues(const int start, const int num, const _valtype_ * newvals); \
261 void set1Value(const int idx, _valref_ value); \
262 void setValue(_valref_ value); \
263 _valref_ operator=(_valref_ val) { this->setValue(val); return val; } \
264 SbBool operator==(const _class_ & field) const; \
265 SbBool operator!=(const _class_ & field) const { return !operator==(field); } \
266 _valtype_ * startEditing(void) { this->evaluate(); return this->values; } \
267 void finishEditing(void) { this->valueChanged(); }
269#define SO_MFIELD_DERIVED_VALUE_HEADER(_class_, _valtype_, _valref_) \
270 PRIVATE_MFIELD_IO_HEADER(); \
272 _valref_ operator=(_valref_ val) { this->setValue(val); return val; }
276#define SO_MFIELD_HEADER(_class_, _valtype_, _valref_) \
277 SO_SFIELD_CONSTRUCTOR_HEADER(_class_); \
278 SO_SFIELD_REQUIRED_HEADER(_class_); \
279 SO_MFIELD_VALUE_HEADER(_class_, _valtype_, _valref_)
283#define SO_MFIELD_DERIVED_HEADER(_class_, _valtype_, _valref_) \
284 SO_SFIELD_CONSTRUCTOR_HEADER(_class_); \
285 SO_SFIELD_REQUIRED_HEADER(_class_); \
286 SO_MFIELD_DERIVED_VALUE_HEADER(_class_, _valtype_, _valref_)
288#define SO_MFIELD_SETVALUESPOINTER_HEADER(_valtype_) \
289 void setValuesPointer(const int num, const _valtype_ * userdata); \
290 void setValuesPointer(const int num, _valtype_ * userdata)
300#define SO_MFIELD_INIT_CLASS(_class_, _parent_) \
301 SO_SFIELD_INIT_CLASS(_class_, _parent_)
305#define SO_MFIELD_CONSTRUCTOR_SOURCE(_class_) \
306_class_::_class_(void) \
308 assert(_class_::classTypeId != SoType::badType()); \
309 this->values = NULL; \
312_class_::~_class_(void) \
314 this->enableNotify(FALSE); \
315 this->deleteAllValues(); \
320#define SO_MFIELD_DERIVED_CONSTRUCTOR_SOURCE(_class_) \
321_class_::_class_(void) { } \
322_class_::~_class_(void) { }
326#define SO_MFIELD_REQUIRED_SOURCE(_class_) \
327PRIVATE_TYPEID_SOURCE(_class_); \
328PRIVATE_EQUALITY_SOURCE(_class_); \
330_class_::operator=(const _class_ & field) \
335 this->allocValues(field.getNum()); \
337 this->setValues(0, field.getNum(), field.getValues(0)); \
343#define SO_MFIELD_VALUE_SOURCE(_class_, _valtype_, _valref_) \
345_class_::fieldSizeof(void) const \
347 return sizeof(_valtype_); \
351_class_::valuesPtr(void) \
353 return static_cast<void *>(this->values); \
357_class_::setValuesPtr(void * ptr) \
359 this->values = static_cast<_valtype_ *>(ptr); \
363_class_::find(_valref_ value, SbBool addifnotfound) \
366 for (int i=0; i < this->num; i++) if (this->values[i] == value) return i; \
368 if (addifnotfound) this->set1Value(this->num, value); \
373_class_::setValues(const int start, const int numarg, const _valtype_ * newvals) \
375 if (start+numarg > this->maxNum) this->allocValues(start+numarg); \
376 else if (start+numarg > this->num) this->num = start+numarg; \
378 for (int i=0; i < numarg; i++) \
379 this->values[i+start] = static_cast<const _valtype_>(newvals[i]); \
380 this->setChangedIndices(start, numarg); \
381 this->valueChanged(); \
382 this->setChangedIndices(); \
386_class_::set1Value(const int idx, _valref_ value) \
388 if (idx+1 > this->maxNum) this->allocValues(idx+1); \
389 else if (idx+1 > this->num) this->num = idx+1; \
390 this->values[idx] = value; \
391 this->setChangedIndex(idx); \
392 this->valueChanged(); \
393 this->setChangedIndices(); \
397_class_::setValue(_valref_ value) \
399 this->allocValues(1); \
400 this->values[0] = value; \
401 this->setChangedIndex(0); \
402 this->valueChanged(); \
403 this->setChangedIndices(); \
407_class_::operator==(const _class_ & field) const \
409 if (this == &field) return TRUE; \
410 if (this->getNum() != field.getNum()) return FALSE; \
412 const _valtype_ * const lhs = this->getValues(0); \
413 const _valtype_ * const rhs = field.getValues(0); \
414 for (int i = 0; i < this->num; i++) if (lhs[i] != rhs[i]) return FALSE; \
420_class_::deleteAllValues(void) \
427_class_::copyValue(int to, int from) \
429 this->values[to] = this->values[from]; \
433#define SO_MFIELD_ALLOC_SOURCE(_class_, _valtype_) \
435_class_::allocValues(int newnum) \
445 _valtype_ * newblock; \
446 assert(newnum >= 0); \
448 this->setChangedIndices(); \
450 if (!this->userDataIsUsed) delete[] this->values; \
451 this->setValuesPtr(NULL); \
453 this->userDataIsUsed = FALSE; \
455 else if (newnum > this->maxNum || newnum < this->num) { \
456 if (this->valuesPtr()) { \
465 oldmaxnum = this->maxNum; \
466 while (newnum > this->maxNum) this->maxNum *= 2; \
467 while ((this->maxNum / 2) >= newnum) this->maxNum /= 2; \
469 if (oldmaxnum != this->maxNum) { \
470 newblock = new _valtype_[this->maxNum]; \
472 for (i=0; i < SbMin(this->num, newnum); i++) \
473 newblock[i] = this->values[i]; \
475 delete[] this->values; \
476 this->setValuesPtr(newblock); \
477 this->userDataIsUsed = FALSE; \
481 this->setValuesPtr(new _valtype_[newnum]); \
482 this->userDataIsUsed = FALSE; \
483 this->maxNum = newnum; \
487 this->num = newnum; \
492#define SO_MFIELD_MALLOC_SOURCE(_class_, _valtype_) \
494_class_::allocValues(int number) \
496 SoMField::allocValues(number); \
501#define SO_MFIELD_SOURCE_MALLOC(_class_, _valtype_, _valref_) \
502 SO_MFIELD_REQUIRED_SOURCE(_class_); \
503 SO_MFIELD_CONSTRUCTOR_SOURCE(_class_); \
504 SO_MFIELD_MALLOC_SOURCE(_class_, _valtype_); \
505 SO_MFIELD_VALUE_SOURCE(_class_, _valtype_, _valref_)
509#define SO_MFIELD_SOURCE(_class_, _valtype_, _valref_) \
510 SO_MFIELD_REQUIRED_SOURCE(_class_); \
511 SO_MFIELD_CONSTRUCTOR_SOURCE(_class_); \
512 SO_MFIELD_ALLOC_SOURCE(_class_, _valtype_); \
513 SO_MFIELD_VALUE_SOURCE(_class_, _valtype_, _valref_)
516#define SO_MFIELD_DERIVED_SOURCE(_class_, _valtype_, _valref_) \
517 SO_MFIELD_REQUIRED_SOURCE(_class_); \
518 SO_MFIELD_DERIVED_CONSTRUCTOR_SOURCE(_class_)
520#define SO_MFIELD_SETVALUESPOINTER_SOURCE(_class_, _valtype_, _usertype_) \
522_class_::setValuesPointer(const int numarg, _usertype_ * userdata) \
525 if (numarg > 0 && userdata) { \
526 this->values = reinterpret_cast<_valtype_*>(userdata); \
527 this->userDataIsUsed = TRUE; \
528 this->num = this->maxNum = numarg; \
529 this->valueChanged(); \
533_class_::setValuesPointer(const int numarg, const _usertype_ * userdata) \
535 this->setValuesPointer(numarg, const_cast<_usertype_*>(userdata)); \