cartobase  4.5.0
object_d.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef CARTOBASE_OBJECT_OBJECT_D_H
35 #define CARTOBASE_OBJECT_OBJECT_D_H
36 
38 
39 #define INSTANTIATE_GENERIC_OBJECT_TYPE( T ) \
40  template class carto::TypedObject< T >; \
41  template class carto::ValueObject< T >; \
42  template class carto::ReferenceObject< T >; \
43  template class carto::PointerObject< T >; \
44  template \
45  T const &GenericObject::value< T >() const; \
46  template \
47  T &GenericObject::value< T >(); \
48  template \
49  bool GenericObject::value( T &dest ) const; \
50  template \
51  void GenericObject::setValue( T const & x ); \
52  template bool DictionaryInterface:: \
53  getProperty( const std::string &, T & ) const; \
54  template void DictionaryInterface:: \
55  setProperty( const std::string &, T const & ); \
56  template <> \
57  void DictionaryInterface::setProperty( const std::string &key, \
58  T const &value ) \
59  { \
60  this->setProperty( key, Object::value( value ) ); \
61  } \
62  template <> \
63  bool DictionaryInterface::getProperty( const std::string &key, T &value ) const \
64  { \
65  Object o; \
66  if( getProperty( key, o ) && o.get() ) \
67  { \
68  try \
69  { \
70  value = o->GenericObject::value< T >(); \
71  return true; \
72  } \
73  catch( ... ) \
74  { \
75  } \
76  } \
77  return false; \
78  } \
79 
80 
81 namespace carto
82 {
83 
84 // For lisibility, it has been decided to put a part of object
85 // implementation in a separate header.
87 
88 
89  //------------------------//
90  // DictionaryInterface //
91 //------------------------//
92 /*
93 
94 //---------------------------------------------------------------------------
95 template <typename T>
96 void DictionaryInterface::setProperty( const std::string &key,
97  const T &value )
98 {
99  this->setProperty( key, Object::value( value ) );
100 }
101 
102 
103 //---------------------------------------------------------------------------
104 template <typename T>
105 bool DictionaryInterface::getProperty( const std::string &key, T &value ) const
106 {
107  Object o;
108  if( getProperty( key, o ) && o.get() )
109  {
110  try
111  {
112  value = o->GenericObject::value<T>();
113  return true;
114  }
115  catch( ... )
116  {
117  }
118  }
119  return false;
120 }
121 */
123 
124  //-----------------//
125  // GenericObject //
126 //-----------------//
128 //-----------------------------------------------------------------------------
129 template<typename T>
130 const T &GenericObject::value() const
131 {
132  const TypedObject<T> *u = dynamic_cast<const TypedObject<T> *>( this );
133  if( u ) return( u->getValue() );
134  throw std::invalid_argument( std::string( "cannot convert object from " )
135  + type() + " to " + DataTypeCode<T>::name()
136  + " (dynamic cast failed from "
137  + typeid(*this).name() + " to "
138  + typeid(u).name() + ")" );
139 }
140 
141 
142 //-----------------------------------------------------------------------------
143 template<typename T>
145 {
146  TypedObject<T> *u = dynamic_cast<TypedObject<T> *>( this );
147  if( u ) return( u->getValue() );
148  throw std::invalid_argument( std::string( "cannot convert object from " )
149  + type() + " to " + DataTypeCode<T>::name()
150  + " (dynamic cast failed from "
151  + typeid(*this).name() + " to "
152  + typeid(u).name() + ")" );
153 }
154 
156 //-----------------------------------------------------------------------------
157 template <typename T>
158 bool GenericObject::value( T &dest ) const
159 {
160  const TypedObject<T> *u = dynamic_cast<const TypedObject<T> *>( this );
161  if( u ) {
162  dest = u->getValue();
163  return true;
164  }
165  return false;
166 }
167 
168 
169 //-----------------------------------------------------------------------------
170 template<typename T>
171 void GenericObject::setValue( const T & x )
172 {
173  TypedObject<T> *u = dynamic_cast<TypedObject<T> *>( this );
174  if( u ) {
175  u->getValue() = x;
176  } else {
177  throw std::invalid_argument( std::string( "cannot write value of type " )
179  + " to object of type " + type() );
180  }
181 }
182 
184  //------------------//
185  // TypedObject<T> //
186 //------------------//
187 
188 //-----------------------------------------------------------------------------
189 template <typename T>
191 {
193 #ifdef CARTO_DEBUG
194  _debugInstantiation = _debugInstantiation;
195 #endif
196 }
197 
198 
199 //-----------------------------------------------------------------------------
200 template <typename T>
202 {
203 }
204 
205 
206 //-----------------------------------------------------------------------------
207 template <typename T>
208 const T &TypedObject<T>::getValue() const
209 {
210  return const_cast< TypedObject<T> & >( *this ).getValue();
211 }
212 
214 //-----------------------------------------------------------------------------
215 template <typename T>
217 {
218  object_to( o, getValue() );
219 }
220 
221 
222 //-----------------------------------------------------------------------------
223 template<typename T>
224 std::string TypedObject<T>::type() const
225 {
227 }
228 
230 //-----------------------------------------------------------------------------
231 template <typename T>
233 {
234  return interface_internal::
236 }
237 
238 
239 //-----------------------------------------------------------------------------
240 template <typename T>
241 const void *TypedObject<T>::_getAddressOfValue() const
242 {
243  return &getValue();
244 }
245 
246 //-----------------------------------------------------------------------------
247 template <typename T>
249 {
250  throw std::runtime_error( "pure virtual function called" );
251 }
252 
253 
254 //-----------------------------------------------------------------------------
255 template <typename T>
257 {
258  throw std::runtime_error( "pure virtual function called" );
259 }
260 
261 
262 #ifdef CARTO_DEBUG
263 //-----------------------------------------------------------------------------
264 template <typename T>
266  PrintInstantiation<T>::doIt( &TypedObject<T>::_debugInstantiation );
267 
268 
269 //-----------------------------------------------------------------------------
270 template <typename T>
271 bool PrintInstantiation<T>::doIt( bool *address )
272 {
273  if ( verbose ) {
274  std::cerr << "!instantiation! " << typeid(T).name() << " "
275  << "mutable "
276  << address << " "
277  << DataTypeCode<T>::name() << std::endl;
278  }
279  return true;
280 }
282 //-----------------------------------------------------------------------------
283 template <typename T>
284 bool PrintInstantiation<const T>::doIt( bool *address)
285 {
286  if ( verbose ) {
287  std::cerr << "!instantiation! " << typeid(T).name() << " "
288  << "const "
289  << static_cast<void *>( address ) << " "
290  << DataTypeCode<T>::name() << std::endl;
291  }
292  return true;
293 }
294 
295 #endif // ifdef CARTO_DEBUG
297 
298 //-----------------------------------------------------------------------------
299 // ScalarInterface methods
300 //-----------------------------------------------------------------------------
301 
302 
303 //-----------------------------------------------------------------------------
304 template <typename T>
306 {
307  return interface_internal::
309 }
310 
311 
312 //-----------------------------------------------------------------------------
313 template <typename T>
315 {
316  return interface_internal::
318 }
319 
321 //-----------------------------------------------------------------------------
322 template <typename T>
324 {
325  return interface_internal::
327 }
329 
330 //-----------------------------------------------------------------------------
331 // StringInterface methods
332 //-----------------------------------------------------------------------------
333 
334 
335 //-----------------------------------------------------------------------------
336 template <typename T>
338 {
339  return interface_internal::
341 }
343 
344 //-----------------------------------------------------------------------------
345 template <typename T>
346 std::string TypedObject<T>::getString() const
347 {
348  return interface_internal::
350 }
351 
352 
353 //-----------------------------------------------------------------------------
354 template <typename T>
355 void TypedObject<T>::setString( const std::string &v )
356 {
357  return interface_internal::
359 }
361 
362 //-----------------------------------------------------------------------------
363 // ArrayInterface methods
364 //-----------------------------------------------------------------------------
365 
366 
367 //-----------------------------------------------------------------------------
368 template <typename T>
370 {
371  return interface_internal::
373 }
374 
375 
376 //-----------------------------------------------------------------------------
377 template <typename T>
379 {
380  return interface_internal::
382  getArrayItem( *this, index );
383 }
384 
385 
386 //-----------------------------------------------------------------------------
387 template <typename T>
388 void TypedObject<T>::setArrayItem( int index, Object value )
389 {
392  setArrayItem( *this, index, value );
393 }
394 
395 
396 //-----------------------------------------------------------------------------
397 // DynArrayInterface methods
398 //-----------------------------------------------------------------------------
399 
400 
401 //-----------------------------------------------------------------------------
402 template <typename T>
404 {
405  return interface_internal::
407 }
408 
410 //-----------------------------------------------------------------------------
411 template <typename T>
412 void TypedObject<T>::reserveArray( size_t size )
413 {
416  reserveArray( *this, size );
417 }
418 
419 
420 //-----------------------------------------------------------------------------
421 template <typename T>
422 void TypedObject<T>::resizeArray( size_t size )
423 {
426  resizeArray( *this, size );
427 }
428 
429 
430 //-----------------------------------------------------------------------------
431 template <typename T>
433 {
436  removeArrayItem( *this, index );
437 }
438 
439 
440 //-----------------------------------------------------------------------------
441 template <typename T>
442 void TypedObject<T>::insertArrayItem( int index, Object value )
443 {
446  insertArrayItem( *this, index, value );
447 }
448 
449 
450 
451 //-----------------------------------------------------------------------------
452 // SizeInterface methods
453 //-----------------------------------------------------------------------------
454 
455 
456 //-----------------------------------------------------------------------------
457 template <typename T>
458 size_t TypedObject<T>::size() const
459 {
461  size( *this );
462 }
463 
464 
465 //-----------------------------------------------------------------------------
466 // DictionaryInterface methods
467 //-----------------------------------------------------------------------------
468 
469 
470 //-----------------------------------------------------------------------------
471 template <typename T>
473 {
474  return interface_internal::
476  ::isDictionary( *this );
477 }
478 
479 
480 //-----------------------------------------------------------------------------
481 template <typename T>
482 bool TypedObject<T>::getProperty( const std::string &key,
483  Object & result ) const
484 {
485  return interface_internal::
487  getProperty( *this, key, result );
488 }
489 
490 
491 //-----------------------------------------------------------------------------
492 template <typename T>
493 void TypedObject<T>::setProperty( const std::string &key, Object value )
494 {
497  setProperty( *this, key, value );
498 }
499 
500 
501 //-----------------------------------------------------------------------------
502 template <typename T>
503 bool TypedObject<T>::removeProperty( const std::string &key )
504 {
505  return interface_internal::
507  removeProperty( *this,key );
508 }
509 
510 
511 //-----------------------------------------------------------------------------
512 template <typename T>
514 {
517  clearProperties( *this );
518 }
519 
520 
521 //-----------------------------------------------------------------------------
522 template <typename T>
523 bool TypedObject<T>::hasProperty( const std::string &key ) const
524 {
525  return interface_internal::
527  hasProperty( *this,key );
528 }
529 
530 
531 //-----------------------------------------------------------------------------
532 // IterableInterface methods
533 //-----------------------------------------------------------------------------
535 
536 //-----------------------------------------------------------------------------
537 template <typename T>
539 {
540  return interface_internal::
542 }
543 
545 //-----------------------------------------------------------------------------
546 template <typename T>
548 {
549  return interface_internal::
551  objectIterator( *this );
552 }
554 
555 //-----------------------------------------------------------------------------
556 // IteratorInterface methods
557 //-----------------------------------------------------------------------------
559 
560 //-----------------------------------------------------------------------------
561 template <typename T>
563 {
564  return interface_internal::
566 }
567 
568 
569 //-----------------------------------------------------------------------------
570 template <typename T>
572 {
573  return interface_internal::
575  isValid( *this );
576 }
577 
578 
579 //-----------------------------------------------------------------------------
580 template <typename T>
582 {
583  return interface_internal::
585  currentValue( *this );
586 }
587 
588 
589 //-----------------------------------------------------------------------------
590 template <typename T>
592 {
595  next( *this );
596 }
597 
598 
599 //-----------------------------------------------------------------------------
600 // DictionaryIteratorInterface methods
601 //-----------------------------------------------------------------------------
602 
604 //-----------------------------------------------------------------------------
605 template <typename T>
607 {
608  return interface_internal::
610  ::isDictionaryIterator( *this );
611 }
612 
613 
614 //-----------------------------------------------------------------------------
615 template <typename T>
616 std::string TypedObject<T>::key() const
617 {
618  return interface_internal::
620  key( *this );
621 }
622 
623 
624 //-----------------------------------------------------------------------------
625 // NoneInterface methods
626 //-----------------------------------------------------------------------------
627 
628 
629 //-----------------------------------------------------------------------------
630 template <typename T>
632 {
633  return interface_internal::
635  ::isNone( *this );
636 }
637 
638 
639 //-----------------------------------------------------------------------------
640 template <typename T>
641 bool TypedObject<T>::operator == ( const GenericObject & other ) const
642 {
643  if( this == &other )
644  return true;
645  if( isScalar() )
646  return interface_internal::
648  ::equals( *this, other );
649  if( isString() )
650  return interface_internal::
652  ::equals( *this, other );
653  if( isDictionary() )
654  return interface_internal::
656  ::equals( *this, other );
657  if( isIterable() )
658  return interface_internal::
660  ::equals( *this, other );
661  return false;
662 }
663 
664 
665  //------------------//
666  // ValueObject<T> //
667 //------------------//
668 
669 //-----------------------------------------------------------------------------
670 template <typename T>
672 {
673 }
675 
676 //-----------------------------------------------------------------------------
677 template <typename T>
679 {
680  return _value;
681 }
682 
683 
684 //-----------------------------------------------------------------------------
685 template <typename T>
687 {
688  return Object::value( _value );
689 }
690 
691 
692  //----------------------//
693  // ReferenceObject<T> //
694 //----------------------//
696 //-----------------------------------------------------------------------------
697 template <typename T>
699 {
700 }
701 
702 
703 //-----------------------------------------------------------------------------
704 template <typename T>
706 {
707  return _value;
708 }
709 
710 //-----------------------------------------------------------------------------
711 template <typename T>
713 {
714  return Object::reference( _value );
715 }
716 
717 
718  //--------------------//
719  // PointerObject<T> //
720 //--------------------//
721 
722 //-----------------------------------------------------------------------------
723 template <typename T>
725 {
726  if ( _owner ) delete _pvalue;
727 }
728 
729 
730 //-----------------------------------------------------------------------------
731 template <typename T>
733 {
734  return *_pvalue;
735 }
736 
737 //-----------------------------------------------------------------------------
738 template <typename T>
740 {
741  return Object( new PointerObject<T>( *_pvalue, false ) );
742 }
743 
744 
745 } // namespace carto
746 
747 #endif // ifndef CARTOBASE_OBJECT_OBJECT_D_H
virtual ~ValueObject()
Definition: object_d.h:671
virtual bool isNone() const
Returns false if the stored object doesn't actually implement the NoneInterface API (needed since all...
Definition: object_d.h:631
virtual Object getArrayItem(int index) const
Get the element of index index.
Definition: object_d.h:378
virtual bool isDictionary() const
Returns false if the stored object doesn't actually implement the DictionaryInterface API (needed sin...
Definition: object_d.h:472
virtual std::string key() const
Access the key of the current dictionary element.
Definition: object_d.h:616
base abstract generic object class.
Definition: object.h:475
virtual size_t size() const
Number of sub-elements.
Definition: object_d.h:458
virtual bool isValid() const
true if the iterator points to a valid value, false when the end of the iterable container has been r...
Definition: object_d.h:571
virtual void reserveArray(size_t)
like the STL vector::reserve(), memory is reserved but no element is stored
Definition: object_d.h:412
virtual bool hasProperty(const std::string &) const
check if an element exists under the key key
Definition: object_d.h:523
virtual T & getValue()
Definition: object_d.h:678
This class is just a hint to convert an actual data type to an identifier string used in input/output...
Definition: types.h:110
virtual T & getValue()
Definition: object_d.h:248
virtual void next()
Point to the next element of the iterable container.
Definition: object_d.h:591
virtual ~PointerObject()
Definition: object_d.h:724
virtual Object clone() const
cloning copy
Definition: object_d.h:256
static void check(T *x=NULL)
Definition: object.h:699
virtual Object clone() const
cloning copy
Definition: object_d.h:712
static Object reference(T &value)
factory function: builds an Object by referencing the value from a ReferenceObject storage wrapper...
Definition: object.h:1470
const T & value() const
Retreive value in object, const reference.
Definition: object_d.h:130
virtual Object currentValue() const
Access the value of the element pointed to by the iterator.
Definition: object_d.h:581
virtual bool removeProperty(const std::string &)
remove an element.
Definition: object_d.h:503
virtual void insertArrayItem(int, Object)
inserts an element into the array.
Definition: object_d.h:442
virtual Object clone() const
cloning copy
Definition: object_d.h:686
virtual ~TypedObject()
Definition: object_d.h:201
int verbose
virtual std::string type() const
type() returns the DataTypeCode::name() of the underlying object type
Definition: object_d.h:224
storage wrapper, derived but still abstract template class
Definition: object.h:87
virtual void resizeArray(size_t)
resize the array.
Definition: object_d.h:422
virtual double getScalar() const
Obtain a scalar value, possibly after a conversion.
Definition: object_d.h:314
virtual void setScalar(double)
The double value will be converted to the actual storage type before it is set in the contained objec...
Definition: object_d.h:323
void object_to(Object o, T &r)
Definition: object.h:1243
virtual std::string getString() const
Obtain a string value, possibly after a conversion.
Definition: object_d.h:346
virtual bool isIterator() const
Returns false if the stored object doesn't actually implement the IteratorInterface API (needed since...
Definition: object_d.h:562
virtual void clearProperties()
clear the dictionary
Definition: object_d.h:513
static std::string name()
Definition: types.h:243
void setValue(const T &val)
Store value in object by copying it.
Definition: object_d.h:171
virtual std::string type() const =0
type() returns the DataTypeCode::name() of the underlying object type
virtual bool isDynArray() const
Returns false if the stored object doesn't actually implement the DynArrayInterface API (needed since...
Definition: object_d.h:403
virtual Object clone() const
cloning copy
Definition: object_d.h:739
virtual T & getValue()
Definition: object_d.h:732
static Object value()
factory function: builds an Object by using the default constructor
Definition: object.h:1420
virtual ~ReferenceObject()
Definition: object_d.h:698
virtual bool getProperty(const std::string &, Object &) const
Access the element ok key key.
Definition: object_d.h:482
virtual bool isDictionaryIterator() const
Returns false if the stored object doesn't actually implement the DictionaryIteratorInterface API (ne...
Definition: object_d.h:606
virtual void setValue(Object val)
Definition: object_d.h:216
virtual bool isArray() const
Returns false if the stored object doesn't actually implement the ArrayInterface API (needed since al...
Definition: object_d.h:369
virtual void setProperty(const std::string &, Object)
Set (insert or replace) the element of key key with the value object.
Definition: object_d.h:493
virtual bool isIterable() const
Returns false if the stored object doesn't actually implement the IterableInterface API (needed since...
Definition: object_d.h:538
virtual bool operator==(const GenericObject &other) const
Definition: object_d.h:641
virtual Object objectIterator() const
returns an object implementing the IteratorIntrerface
Definition: object_d.h:547
virtual bool isString() const
Returns false if the stored object doesn't actually implement the StringInterface API (needed since a...
Definition: object_d.h:337
virtual void removeArrayItem(int)
removes an element from the array.
Definition: object_d.h:432
virtual T & getValue()
Definition: object_d.h:705
virtual bool isScalar() const
Returns false if the stored object doesn't actually implement the ScalarInterface API (needed since a...
Definition: object_d.h:305
virtual void setString(const std::string &)
The string value may be converted to the actual storage type before it is set in the contained object...
Definition: object_d.h:355
virtual void setArrayItem(int, Object)
Definition: object_d.h:388