cartobase  5.0.5
object.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_H
35 #define CARTOBASE_OBJECT_OBJECT_H
36 
38 #include <cartobase/type/types.h>
41 #include <cartobase/smart/rcptr.h>
43 #include <stdexcept>
44 #include <string>
45 #include <set>
46 #include <vector>
47 #include <list>
48 #include <typeinfo>
49 #ifdef CARTO_DEBUG
51 #include <iostream>
52 #endif // #ifdef CARTO_DEBUG
54 
55 
56 #define DECLARE_GENERIC_OBJECT_TYPE( T ) \
57 template <> \
58 struct GenericObjectTypeDeclared< T > \
59 { \
60  static inline void check() {} \
61 }; \
62 template <> \
63 void DictionaryInterface::setProperty( const std::string &key, \
64  T const &value ); \
65 template <> \
66 bool DictionaryInterface::getProperty( const std::string &key, T &value ) const; \
67 extern template class TypedObject< T >; \
68 extern template class ValueObject< T >; \
69 extern template class ReferenceObject< T >; \
70 extern template class PointerObject< T >; \
71 extern template \
72 T const &GenericObject::value< T >() const; \
73 extern template \
74 T &GenericObject::value< T >(); \
75 extern template \
76 bool GenericObject::value( T &dest ) const; \
77 extern template \
78 void GenericObject::setValue( T const & x ); \
79 extern template bool DictionaryInterface:: \
80 getProperty( const std::string &, T & ) const; \
81 extern template void DictionaryInterface:: \
82 setProperty( const std::string &, T const & ); \
83 
84 
85 namespace carto
86 {
87 class GenericObject;
88 template <typename T> class TypedObject;
89 class Object;
90 
91 
92  //-------------//
93  // Interface //
94  //-------------//
95 
96  class Interface
97  {
98  public:
99  virtual ~Interface();
100  };
101 
102 
103  //-------------------//
104  // ScalarInterface //
105  //-------------------//
106 
109  class ScalarInterface : public virtual Interface
110  {
111  public:
112 
113  virtual ~ScalarInterface();
114 
119  virtual bool isScalar() const;
124  virtual double getScalar() const = 0;
130  virtual void setScalar( double ) = 0;
132  virtual bool operator == ( const ScalarInterface & other ) const;
133  virtual bool operator != ( const ScalarInterface & other ) const
134  { return ! operator == ( other ); }
135  };
136 
137 
138  //-------------------//
139  // StringInterface //
140  //-------------------//
141 
143  class StringInterface : public virtual Interface
144  {
145  public:
146 
147  virtual ~StringInterface();
148 
153  virtual bool isString() const;
158  virtual std::string getString() const = 0;
164  virtual void setString( const std::string & ) = 0;
166  virtual bool operator == ( const StringInterface & other ) const;
167  virtual bool operator != ( const StringInterface & other ) const
168  { return ! operator == ( other ); }
169  };
170 
171 
172  //------------------//
173  // SizeInterface //
174  //------------------//
175 
177  class SizeInterface : public virtual Interface
178  {
179  public:
180 
181  virtual ~SizeInterface();
182 
187  virtual size_t size() const = 0;
188  };
189 
190 
191  //----------------------//
192  // IterableInterface //
193  //----------------------//
194 
196  class IterableInterface : public virtual Interface
197  {
198  public:
199 
200  virtual ~IterableInterface();
201 
206  virtual bool isIterable() const;
208  virtual Object objectIterator() const = 0;
210  virtual bool operator == ( const IterableInterface & other ) const;
211  virtual bool operator != ( const IterableInterface & other ) const
212  { return ! operator == ( other ); }
213  };
214 
215 
216  //----------------------//
217  // IteratorInterface //
218  //----------------------//
219 
235  class IteratorInterface : public virtual Interface
236  {
237  public:
238 
239  virtual ~IteratorInterface();
240 
245  virtual bool isIterator() const;
248  virtual bool isValid() const = 0;
250  virtual Object currentValue() const = 0;
252  virtual void next() = 0;
253  };
254 
255 
256  //-------------------------//
257  // KeyIteratorInterface //
258  //-------------------------//
259 
265  {
266  public:
267 
268  virtual ~KeyIteratorInterface();
269 
274  virtual bool isKeyIterator() const;
276  virtual Object keyObject() const = 0;
277  };
278 
279 
280  //--------------------------------//
281  // DictionaryIteratorInterface //
282  //--------------------------------//
283 
289  {
290  public:
291 
292  virtual ~DictionaryIteratorInterface();
293 
298  virtual bool isDictionaryIterator() const;
300  virtual std::string key() const = 0;
301  };
302 
303 
304  //----------------------------//
305  // IntKeyIteratorInterface //
306  //----------------------------//
307 
313  {
314  public:
315 
316  virtual ~IntKeyIteratorInterface();
317 
322  virtual bool isIntKeyIterator() const;
324  virtual long intKey() const = 0;
325  };
326 
327 
328  //------------------//
329  // ArrayInterface //
330  //------------------//
331 
334  class ArrayInterface : public virtual SizeInterface,
335  public virtual IterableInterface
336  {
337  public:
338 
339  virtual ~ArrayInterface();
340 
345  virtual bool isArray() const;
353  virtual Object getArrayItem( int index ) const = 0;
354  virtual void setArrayItem( int, Object ) = 0;
355  virtual size_t size() const = 0;
356  };
357 
358 
359  //---------------------//
360  // DynArrayInterface //
361  //---------------------//
362 
364  class DynArrayInterface : public virtual ArrayInterface
365  {
366  public:
367 
368  virtual ~DynArrayInterface();
369 
374  virtual bool isDynArray() const;
377  virtual void reserveArray( size_t ) = 0;
381  virtual void resizeArray( size_t ) = 0;
384  virtual void removeArrayItem( int ) = 0;
387  virtual void insertArrayItem( int, Object ) = 0;
388  };
389 
390 
391  //------------------------//
392  // DictionaryInterface //
393  //------------------------//
394 
403  class DictionaryInterface : public virtual SizeInterface,
404  public virtual IterableInterface
405  {
406  public:
407 
408  virtual ~DictionaryInterface();
409 
414  virtual bool isDictionary() const;
419  virtual bool getProperty( const std::string & key,
420  Object & value ) const = 0;
423  Object getProperty( const std::string & ) const;
428  Object getProperty( Object key ) const;
436  template <typename T> bool getProperty( const std::string & key,
437  T & value ) const;
438 
446  virtual void setProperty( const std::string & key, Object value ) = 0;
448  void setProperty( const std::string &, const char * );
453  template <typename T> void setProperty( const std::string &, const T & );
454 
459  virtual bool removeProperty( const std::string & ) = 0;
460 
462  virtual bool hasProperty( const std::string & key ) const = 0;
464  virtual void clearProperties();
467  virtual void copyProperties( Object source );
469  virtual bool operator == ( const DictionaryInterface & other ) const;
470  virtual bool operator != ( const DictionaryInterface & other ) const
471  { return ! operator == ( other ); }
472  };
473 
474 
475  //----------------------//
476  // SyntaxedInterface //
477  //----------------------//
478 
484  class SyntaxedInterface : public virtual Interface
485  {
486  public:
487 
488  virtual ~SyntaxedInterface();
489 
490  virtual std::string getSyntax() const = 0;
491  virtual bool hasSyntax() const = 0;
492  virtual void setSyntax( const std::string& syntactic ) = 0;
494  virtual bool operator == ( const SyntaxedInterface & other ) const;
495  virtual bool operator != ( const SyntaxedInterface & other ) const
496  { return ! operator == ( other ); }
497  };
498 
499 
500  //------------------//
501  // NoneInterface //
502  //------------------//
503 
506  class NoneInterface : public virtual Interface
507  {
508  public:
509 
510  virtual ~NoneInterface();
511 
516  virtual bool isNone() const = 0;
517  };
518 
519 
520  //-----------------//
521  // GenericObject //
522 //-----------------//
523 
525  public virtual RCObject,
526  public virtual StringInterface,
527  public virtual ScalarInterface,
528  public virtual DynArrayInterface,
529  public virtual DictionaryInterface,
530  public virtual IterableInterface,
531  public virtual IteratorInterface,
532  public virtual KeyIteratorInterface,
533  public virtual DictionaryIteratorInterface,
534  public virtual IntKeyIteratorInterface,
535  public virtual NoneInterface
536 {
537 public:
538  virtual ~GenericObject();
539 
541  virtual Object clone() const = 0;
543  virtual std::string type() const = 0;
544 
548  template<typename T> const T & value() const;
552  template<typename T> T & value();
556  template <typename T> bool value( T & ) const;
560  template<typename T> void setValue( const T &val );
561  virtual void setValue( Object val ) = 0;
562 
563  virtual size_t size() const = 0;
564 
584  template <typename T>
585  T *getInterface();
587  template <typename T>
588  const T *getInterface() const;
589  virtual bool operator == ( const GenericObject & other ) const;
590  virtual bool operator != ( const GenericObject & other ) const
591  { return ! operator == ( other ); }
592 
593 protected:
594  friend class Object;
595 
596  virtual Interface *_getGenericInterface() = 0;
597  virtual const void *_getAddressOfValue() const = 0;
598 };
599 
600 
601  //----------//
602  // Object //
603 //----------//
604 
605 class Object : public rc_ptr<GenericObject>
606 {
607 public:
608  Object();
609  Object( const Object & );
610  Object( GenericObject* data, bool externalowner = false );
611  virtual ~Object();
613  inline Object & operator = ( const Object & );
614 
616  template <typename T>
617  static inline Object value();
618 
621  template <typename T>
622  static inline Object value( const T & );
623 
628  template <typename T>
629  static inline Object reference( T & value );
630 
634  template <typename T>
635  static inline Object reference( const T & value );
636 
638  bool isSameObject( const Object & ) const;
649  bool isNone() const;
650  inline bool operator == ( const Object & other ) const;
651  inline bool operator != ( const Object & other ) const
652  { return ! operator == ( other ); }
653 };
654 
655 
656  //------------------//
657  // TypedObject<T> //
658 //------------------//
659 
660 template <typename T>
661 class TypedObject : public GenericObject
662 {
663 public:
664  TypedObject();
665  virtual ~TypedObject();
666 
667  // This function should be pure virtual but it is mandatory to
668  // do explicit instanciation of TypedObject and it is not possible
669  // with pure virtual classes.
670  virtual T &getValue();
671  inline const T &getValue() const;
672  virtual void setValue( Object val );
673 
674  // This function should be pure virtual but it is mandatory to
675  // do explicit instanciation of TypedObject and it is not possible
676  // with pure virtual classes.
677  virtual Object clone() const;
678 
679  virtual std::string type() const;
680 
681  // ScalarInterface methods
682  virtual bool isScalar() const;
683  virtual double getScalar() const;
684  virtual void setScalar( double );
685 
686  // StringInterface methods
687  virtual bool isString() const;
688  virtual std::string getString() const;
689  virtual void setString( const std::string & );
690 
691  // ArrayInterface methods
692  virtual bool isArray() const;
693  virtual Object getArrayItem( int index ) const;
694  virtual void setArrayItem( int, Object );
695  virtual size_t size() const;
696 
697  // DynArrayInterface methods
698  virtual bool isDynArray() const;
699  virtual void reserveArray( size_t );
700  virtual void resizeArray( size_t );
701  virtual void removeArrayItem( int );
702  virtual void insertArrayItem( int, Object );
703 
704  // DictionaryInterface methods
705  virtual bool getProperty( const std::string &, Object & ) const;
706  virtual bool isDictionary() const;
708 
709  virtual void setProperty( const std::string &, Object );
711 
712  virtual bool removeProperty( const std::string & );
713  virtual bool hasProperty( const std::string & ) const;
714  virtual void clearProperties();
715 
716  // IterableInterface methods
717  virtual bool isIterable() const;
718  virtual Object objectIterator() const;
719 
720  // IteratorInterface methods
721  virtual bool isIterator() const;
722  virtual bool isValid() const;
723  virtual Object currentValue() const;
724  virtual void next();
725 
726  // KeyIteratorInterface methods
727  virtual bool isKeyIterator() const;
728  virtual Object keyObject() const;
729 
730  // DictionaryIteratorInterface methods
731  virtual bool isDictionaryIterator() const;
732  virtual std::string key() const;
733 
734  // IntKeyIteratorInterface methods
735  virtual bool isIntKeyIterator() const;
736  virtual long intKey() const;
737 
738  // NoneInterface methods
739  virtual bool isNone() const;
740 
741  virtual bool operator == ( const GenericObject & other ) const;
742 
743 private:
744  Interface *_getGenericInterface();
745  const void *_getAddressOfValue() const;
746 #ifdef CARTO_DEBUG
747  static bool _debugInstantiation;
748 #endif // ifdef CARTO_DEBUG
749 };
750 
751 
752 //-----------------------------------------------------------------------------
754 // is automatically instanitiated on a non declared type.
755 template <typename T>
757 {
758  static inline void check( T *x = NULL )
759  {
760  you_must_use_DECLARE_GENERIC_OBJECT_TYPE( x );
761  }
762 };
763 
764 
765 //-----------------------------------------------------------------------------
766 template <typename T>
767 class TypedObject<const T> : public GenericObject
768 {
769 private:
770  // Forbid TypedObject on constant type
771  TypedObject() {};
772 };
773 
774 
775 
776 #ifdef CARTO_DEBUG
777 template <typename T>
778 class PrintInstantiation
779 {
780 public:
781 
782  static bool doIt( bool * );
783 };
784 
785 
786 template <typename T>
787 class PrintInstantiation<const T>
788 {
789 public:
790 
791  static bool doIt( bool * );
792 };
793 #endif // ifdef CARTO_DEBUG
794 
795 
796 
797 
798  #if 0
799  //--------------------------//
800  // ForbidInheritance<T,U> //
801 //--------------------------//
802 template <typename T, bool B>
803 class CompileErrorIfFalse
804 {
805  static inline void check() {}
806 };
807 
808 template <typename T>
809 class CompileErrorIfFalse<T,false>
810 {
811  static inline void check()
812  {
813  T::cannot_compile_this_code();
814  }
815 };
816 
817 template <typename T, typename U>
818 class ForbidInheritance : CompileErrorIfFalse< T, SUPERSUBCLASS( T, U ) >
819 {
820 };
821 #endif
822 
823 
824  //------------------//
825  // ValueObject<T> //
826 //------------------//
827 
828 template <typename T>
829 class ValueObject : public TypedObject<T>
830 {
831 public:
832  ValueObject();
833  ValueObject( const T & x );
834  virtual ~ValueObject();
835 
836  virtual T &getValue();
837 
838  virtual Object clone() const;
839 
840 private:
841 
842  // class _Test : ForbidInheritance< GenericObject, T > {};
843  mutable T _value;
844 };
845 
846 
847  //-----------------------//
848  // Useful common types //
849 //-----------------------//
850 
855 typedef std::map<std::string, Object> Dictionary;
856 typedef std::map<int, Object> IntDictionary;
857 typedef std::map<Object, Object> ObjectDictionary;
859 typedef std::vector<Object> ObjectVector;
860 
861 
862  //----------------------//
863  // ReferenceObject<T> //
864 //----------------------//
865 
866 template <typename T>
867 class ReferenceObject : public TypedObject<T>
868 {
869 public:
870  ReferenceObject( T &x );
871  virtual ~ReferenceObject();
872  virtual T &getValue();
873 
874  virtual Object clone() const;
875 
876 private:
877 
878  // class _Test : ForbidInheritance< GenericObject, T > {};
879  T &_value;
880 };
881 
882 
883  //--------------------//
884  // PointerObject<T> //
885 //--------------------//
886 
887 template <typename T>
888 class PointerObject : public TypedObject<T>
889 {
890 public:
891  PointerObject( T &x, bool owner );
892  virtual ~PointerObject();
893  virtual T &getValue();
894 
895  virtual Object clone() const;
896 
897 private:
898 
899  mutable T *_pvalue;
900  bool _owner;
901 };
902 
903 
904  //--------//
905  // none //
906 //--------//
907 
909 extern Object none();
910 
911 
912  //----------------//
913  // object_to<T> //
914 //----------------//
915 
916 //-----------------------------------------------------------------------------
917 template <typename T>
918 inline void object_to( Object o, T & r );
919 
920 //-----------------------------------------------------------------------------
921 /* This specialization is a workaaround to avoid a bug in gcc 3.4.3 (at least).
922  This function needs to be compiled and I really really don't know why.
923  Otherwise it seems not to be called at all (hopefully)
924 */
925 template <typename T>
926 inline void object_to( Object, const T & );
927 
928 
929  //------------------//
930  // instantiations //
931 //------------------//
932 
934 DECLARE_GENERIC_OBJECT_TYPE( unsigned )
936 DECLARE_GENERIC_OBJECT_TYPE( signed char )
937 DECLARE_GENERIC_OBJECT_TYPE( unsigned char )
939 DECLARE_GENERIC_OBJECT_TYPE( unsigned short )
941 DECLARE_GENERIC_OBJECT_TYPE( unsigned long )
942 DECLARE_GENERIC_OBJECT_TYPE( long long )
943 DECLARE_GENERIC_OBJECT_TYPE( unsigned long long )
947 DECLARE_GENERIC_OBJECT_TYPE( std::string )
950 
951 // DECLARE_GENERIC_OBJECT_TYPE cannot be used for Object because it refers to
952 // extern explicit template instantiations of
953 // DictionaryInterface::getProperty<Object> and
954 // DictionaryInterface::setProperty<Object>, which conflict with the (pure
955 // virtual) overload of these methods.
956 template <>
958 {
959  static inline void check() {};
960 };
961 template <>
962 void DictionaryInterface::setProperty( const std::string &key,
963  Object const &value );
964 template <>
965 bool DictionaryInterface::getProperty( const std::string &key, Object &value ) const;
966 extern template class TypedObject< Object >;
967 extern template class ValueObject< Object >;
968 extern template class ReferenceObject< Object >;
969 extern template class PointerObject< Object >;
970 extern template
971 Object const &GenericObject::value< Object >() const;
972 extern template
973 Object &GenericObject::value< Object >();
974 extern template
975 bool GenericObject::value( Object &dest ) const;
976 extern template
977 void GenericObject::setValue( Object const & x );
978 
979 
980 template <>
982 {
983  static inline void check() {};
984 };
985 extern template class TypedObject< GenericObject >;
986 extern template class ReferenceObject< GenericObject >;
987 extern template class PointerObject< GenericObject >;
988 
989 
990 DECLARE_GENERIC_OBJECT_TYPE( std::vector<int> )
991 DECLARE_GENERIC_OBJECT_TYPE( std::vector<unsigned> )
992 DECLARE_GENERIC_OBJECT_TYPE( std::vector<char> )
993 DECLARE_GENERIC_OBJECT_TYPE( std::vector<signed char> )
994 DECLARE_GENERIC_OBJECT_TYPE( std::vector<unsigned char> )
995 DECLARE_GENERIC_OBJECT_TYPE( std::vector<short> )
996 DECLARE_GENERIC_OBJECT_TYPE( std::vector<unsigned short> )
997 DECLARE_GENERIC_OBJECT_TYPE( std::vector<long> )
998 DECLARE_GENERIC_OBJECT_TYPE( std::vector<unsigned long> )
999 DECLARE_GENERIC_OBJECT_TYPE( std::vector<long long> )
1000 DECLARE_GENERIC_OBJECT_TYPE( std::vector<unsigned long long> )
1001 DECLARE_GENERIC_OBJECT_TYPE( std::vector<float> )
1002 DECLARE_GENERIC_OBJECT_TYPE( std::vector<double> )
1003 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::string> )
1004 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Object> )
1005 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Void> )
1006 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::string> * )
1007 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<int> > )
1008 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<unsigned> > )
1009 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<char> > )
1010 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<signed char> > )
1011 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<unsigned char> > )
1012 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<short> > )
1013 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<unsigned short> > )
1014 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<long> > )
1015 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<unsigned long> > )
1016 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<long long> > )
1017 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<unsigned long long> > )
1018 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<float> > )
1019 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<double> > )
1020 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<std::string> > )
1021 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::set<unsigned> > )
1022 
1023 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<std::string> > )
1024 
1025 DECLARE_GENERIC_OBJECT_TYPE( std::set<int> )
1026 DECLARE_GENERIC_OBJECT_TYPE( std::set<unsigned> )
1027 DECLARE_GENERIC_OBJECT_TYPE( std::set<char> )
1028 DECLARE_GENERIC_OBJECT_TYPE( std::set<signed char> )
1029 DECLARE_GENERIC_OBJECT_TYPE( std::set<unsigned char> )
1030 DECLARE_GENERIC_OBJECT_TYPE( std::set<short> )
1031 DECLARE_GENERIC_OBJECT_TYPE( std::set<unsigned short> )
1032 DECLARE_GENERIC_OBJECT_TYPE( std::set<long> )
1033 DECLARE_GENERIC_OBJECT_TYPE( std::set<unsigned long> )
1034 DECLARE_GENERIC_OBJECT_TYPE( std::set<long long> )
1035 DECLARE_GENERIC_OBJECT_TYPE( std::set<unsigned long long> )
1036 DECLARE_GENERIC_OBJECT_TYPE( std::set<float> )
1037 DECLARE_GENERIC_OBJECT_TYPE( std::set<double> )
1038 DECLARE_GENERIC_OBJECT_TYPE( std::set<std::string> )
1039 DECLARE_GENERIC_OBJECT_TYPE( std::set<Object> )
1040 
1041 #define _TMP_ std::map< std::string, int >
1043 #undef _TMP_
1044 #define _TMP_ std::map< std::string, unsigned >
1046 #undef _TMP_
1047 #define _TMP_ std::map< std::string, char >
1049 #undef _TMP_
1050 #define _TMP_ std::map< std::string, signed char >
1052 #undef _TMP_
1053 #define _TMP_ std::map< std::string, unsigned char >
1055 #undef _TMP_
1056 #define _TMP_ std::map< std::string, short >
1058 #undef _TMP_
1059 #define _TMP_ std::map< std::string, unsigned short >
1061 #undef _TMP_
1062 #define _TMP_ std::map< std::string, long >
1064 #undef _TMP_
1065 #define _TMP_ std::map< std::string, unsigned long >
1067 #undef _TMP_
1068 #define _TMP_ std::map< std::string, long long >
1070 #undef _TMP_
1071 #define _TMP_ std::map< std::string, unsigned long long >
1073 #undef _TMP_
1074 #define _TMP_ std::map< std::string, float >
1076 #undef _TMP_
1077 #define _TMP_ std::map< std::string, double >
1079 #undef _TMP_
1080 #define _TMP_ std::map< std::string, bool >
1082 #undef _TMP_
1083 #define _TMP_ std::map< std::string, std::string >
1085 #undef _TMP_
1086 #define _TMP_ std::map< std::string, Object >
1088 #undef _TMP_
1089 #define TMP std::map< int, Object >
1091 #undef TMP
1092 #define TMP std::map< float, Object >
1094 #undef TMP
1095 #define TMP std::map< Object, Object >
1097 #undef TMP
1098 
1099 #define _TMP_ std::map< std::string, std::vector<int> >
1101 #undef _TMP_
1102 #define _TMP_ std::map< std::string, std::vector<unsigned> >
1104 #undef _TMP_
1105 #define _TMP_ std::map< std::string, std::vector<char> >
1107 #undef _TMP_
1108 #define _TMP_ std::map< std::string, std::vector<signed char> >
1110 #undef _TMP_
1111 #define _TMP_ std::map< std::string, std::vector<unsigned char> >
1113 #undef _TMP_
1114 #define _TMP_ std::map< std::string, std::vector<short> >
1116 #undef _TMP_
1117 #define _TMP_ std::map< std::string, std::vector<unsigned short> >
1119 #undef _TMP_
1120 #define _TMP_ std::map< std::string, std::vector<long> >
1122 #undef _TMP_
1123 #define _TMP_ std::map< std::string, std::vector<unsigned long> >
1125 #undef _TMP_
1126 #define _TMP_ std::map< std::string, std::vector<long long> >
1128 #undef _TMP_
1129 #define _TMP_ std::map< std::string, std::vector<unsigned long long> >
1131 #undef _TMP_
1132 #define _TMP_ std::map< std::string, std::vector<float> >
1134 #undef _TMP_
1135 #define _TMP_ std::map< std::string, std::vector<double> >
1137 #undef _TMP_
1138 #define _TMP_ std::map< std::string, std::vector<std::string> >
1140 #undef _TMP_
1141 #define _TMP_ std::map< std::string, std::vector<Object> >
1143 #undef _TMP_
1144 
1145 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<int> > )
1146 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<unsigned> > )
1147 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<char> > )
1148 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<signed char> > )
1149 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<unsigned char> > )
1150 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<short> > )
1151 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<unsigned short> > )
1152 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<long> > )
1153 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<unsigned long> > )
1154 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<long long> > )
1155 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<unsigned long long> > )
1156 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<float> > )
1157 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<double> > )
1158 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<Object> > )
1159 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< std::vector<std::string> > )
1160 
1161 #define _TMP_ rc_ptr< std::map< std::string, int > >
1163 #undef _TMP_
1164 #define _TMP_ rc_ptr< std::map< std::string, unsigned > >
1166 #undef _TMP_
1167 #define _TMP_ rc_ptr< std::map< std::string, char > >
1169 #undef _TMP_
1170 #define _TMP_ rc_ptr< std::map< std::string, signed char > >
1172 #undef _TMP_
1173 #define _TMP_ rc_ptr< std::map< std::string, unsigned char > >
1175 #undef _TMP_
1176 #define _TMP_ rc_ptr< std::map< std::string, short > >
1178 #undef _TMP_
1179 #define _TMP_ rc_ptr< std::map< std::string, unsigned short > >
1181 #undef _TMP_
1182 #define _TMP_ rc_ptr< std::map< std::string, long > >
1184 #undef _TMP_
1185 #define _TMP_ rc_ptr< std::map< std::string, unsigned long > >
1187 #undef _TMP_
1188 #define _TMP_ rc_ptr< std::map< std::string, long long > >
1190 #undef _TMP_
1191 #define _TMP_ rc_ptr< std::map< std::string, unsigned long long > >
1193 #undef _TMP_
1194 #define _TMP_ rc_ptr< std::map< std::string, float > >
1196 #undef _TMP_
1197 #define _TMP_ rc_ptr< std::map< std::string, double > >
1199 #undef _TMP_
1200 #define _TMP_ rc_ptr< std::map< std::string, bool > >
1202 #undef _TMP_
1203 #define _TMP_ rc_ptr< std::map< std::string, std::string > >
1205 #undef _TMP_
1206 #define _TMP_ rc_ptr< std::map< std::string, Object > >
1208 #undef _TMP_
1209 #define _TMP_ rc_ptr< std::map< int, Object > >
1211 #undef _TMP_
1212 #define _TMP_ rc_ptr< std::map< float, Object > >
1214 #undef _TMP_
1215 #define TMP rc_ptr< std::map< Object, Object > >
1217 #undef TMP
1218 
1219 #define _TMP_ rc_ptr< std::map< std::string, std::vector<int> > >
1221 #undef _TMP_
1222 #define _TMP_ rc_ptr< std::map< std::string, std::vector<unsigned> > >
1224 #undef _TMP_
1225 #define _TMP_ rc_ptr< std::map< std::string, std::vector<char> > >
1227 #undef _TMP_
1228 #define _TMP_ rc_ptr< std::map< std::string, std::vector<signed char> > >
1230 #undef _TMP_
1231 #define _TMP_ rc_ptr< std::map< std::string, std::vector<unsigned char> > >
1233 #undef _TMP_
1234 #define _TMP_ rc_ptr< std::map< std::string, std::vector<short> > >
1236 #undef _TMP_
1237 #define _TMP_ rc_ptr< std::map< std::string, std::vector<unsigned short> > >
1239 #undef _TMP_
1240 #define _TMP_ rc_ptr< std::map< std::string, std::vector<long> > >
1242 #undef _TMP_
1243 #define _TMP_ rc_ptr< std::map< std::string, std::vector<unsigned long> > >
1245 #undef _TMP_
1246 #define _TMP_ rc_ptr< std::map< std::string, std::vector<long long> > >
1248 #undef _TMP_
1249 #define _TMP_ rc_ptr< std::map< std::string, std::vector<unsigned long long> > >
1251 #undef _TMP_
1252 #define _TMP_ rc_ptr< std::map< std::string, std::vector<float> > >
1254 #undef _TMP_
1255 #define _TMP_ rc_ptr< std::map< std::string, std::vector<double> > >
1257 #undef _TMP_
1258 #define _TMP_ rc_ptr< std::map< std::string, std::vector<std::string> > >
1260 #undef _TMP_
1261 #define _TMP_ rc_ptr< std::map< std::string, std::vector<Object> > >
1263 #undef _TMP_
1264 
1266 DECLARE_GENERIC_OBJECT_TYPE( Semantic )
1268 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr<SyntaxSet> )
1269 
1270 template <typename T>
1271 inline void object_to( Object o, T & r );
1272 template <>
1273 void object_to( Object o, bool &r );
1274 template <>
1275 void object_to( Object o, unsigned char &r );
1276 template <>
1277 void object_to( Object o, signed char &r );
1278 template <>
1279 inline void object_to( Object o, char &r );
1280 template <>
1281 void object_to( Object o, unsigned short &r );
1282 template <>
1283 void object_to( Object o, short &r );
1284 template <>
1285 void object_to( Object o, unsigned &r );
1286 template <>
1287 void object_to( Object o, int &r );
1288 template <>
1289 void object_to( Object o, float &r );
1290 template <>
1291 inline void object_to( Object o, double &r );
1292 template <>
1293 void object_to( Object o, std::string &r );
1294 template <>
1295 inline void object_to( Object o, Object & r );
1296 
1297 
1298 
1299  //----------------//
1300  // object_to<T> //
1301 //----------------//
1302 
1303 //-----------------------------------------------------------------------------
1304 template <typename T>
1305 inline void object_to( Object o, T & r )
1306 {
1307  r = o->GenericObject::value<T>();
1308 }
1309 
1310 //-----------------------------------------------------------------------------
1311 /* This specialization is a workaaround to avoid a bug in gcc 3.4.3 (at least).
1312  This function needs to be compiled and I really really don't know why.
1313  Otherwise it seems not to be called at all (hopefully)
1314 */
1315 template <typename T>
1316 inline void object_to( Object, const T & )
1317 {
1318  throw std::runtime_error( std::string( "cannot convert object to const " ) +
1319  DataTypeCode<T>::name() + " (this is probably a "
1320  "bug)" );
1321 }
1322 
1323 //-----------------------------------------------------------------------------
1324 template <>
1325 inline void object_to( Object o, Object & r )
1326 {
1327  r = o;
1328 }
1329 
1330 
1331 //-----------------------------------------------------------------------------
1332 template <>
1333 inline void object_to( Object o, char &r )
1334 {
1335 #ifdef __CHAR_UNSIGNED__
1336  object_to<unsigned char>( o, reinterpret_cast<unsigned char &>( r ) );
1337 #else
1338  object_to<signed char>( o, reinterpret_cast<signed char &>( r ) );
1339 #endif
1340 }
1341 
1342 
1343 
1344 
1345 //-----------------------------------------------------------------------------
1346 template <>
1347 inline void object_to( Object o, double &r )
1348 {
1349  r = o->getScalar();
1350 }
1351 
1352 
1353  //------------------------//
1354  // DictionaryInterface //
1355 //------------------------//
1356 
1357 
1358 //---------------------------------------------------------------------------
1359 template <typename T>
1360 void DictionaryInterface::setProperty( const std::string &,
1361  const T &value )
1362 {
1363  you_must_use_DECLARE_GENERIC_OBJECT_TYPE( value );
1364 }
1365 
1366 
1367 //---------------------------------------------------------------------------
1368 template <typename T>
1369 bool DictionaryInterface::getProperty( const std::string &, T &value ) const
1370 {
1371  return you_must_use_DECLARE_GENERIC_OBJECT_TYPE( value );
1372 }
1373 
1374 
1375  //-----------------//
1376  // GenericObject //
1377 //-----------------//
1378 
1379 //-----------------------------------------------------------------------------
1380 template <class T>
1382 {
1383  return dynamic_cast<T *>( _getGenericInterface() );
1384 }
1385 
1386 //-----------------------------------------------------------------------------
1387 template <class T>
1388 inline const T *GenericObject::getInterface() const
1389 {
1390  /* using const_cast is ugly, OK, right.
1391  But it avoids redefining a const version of _getGenericInterface() with
1392  exactly the same contents. I'm lazy today.
1393  */
1394  return dynamic_cast<T *>( const_cast<GenericObject *>( this )
1395  ->_getGenericInterface() );
1396 }
1397 
1398 
1399  //----------//
1400  // Object //
1401 //----------//
1402 
1403 
1404 //-----------------------------------------------------------------------------
1406 {
1407 }
1408 
1409 //-----------------------------------------------------------------------------
1410 inline Object::Object( const Object &x ) : rc_ptr<GenericObject>( x )
1411 {
1412 }
1413 
1414 //-----------------------------------------------------------------------------
1415 inline Object::Object( GenericObject *x, bool externalowner )
1416  : rc_ptr<GenericObject>( x, externalowner )
1417 {
1418 }
1419 
1420 //-----------------------------------------------------------------------------
1422 {
1423 }
1424 
1425 
1426 //-----------------------------------------------------------------------------
1427 inline Object & Object::operator = ( const Object &other )
1428 {
1429  if ( this != &other ) {
1431  ( static_cast< const rc_ptr<GenericObject> & >( other ) );
1432  }
1433  return *this;
1434 }
1435 
1436 
1437 //-----------------------------------------------------------------------------
1438 inline bool Object::operator == ( const Object &other ) const
1439 {
1440  if ( this == &other || ( !get() && !other.get() ) )
1441  return true;
1442  if( !get() || !other.get() )
1443  return false;
1444  return *get() == *other.get();
1445 }
1446 
1447 
1448 #if 0
1449 //-----------------------------------------------------------------------------
1450 template <typename T, bool>
1451 class ObjectValueTrait
1452 {
1453 public:
1454  static inline Object value()
1455  {
1456  return Object( static_cast<GenericObject *>( new ValueObject<T>() ) );
1457  }
1458  static inline Object value( const T &v )
1459  {
1460  return Object( static_cast<GenericObject *>( new ValueObject<T>( v ) ) );
1461  }
1462 };
1463 
1464 //-----------------------------------------------------------------------------
1465 template <typename T>
1466 class ObjectValueTrait<T,true>
1467 {
1468 public:
1469  static inline Object value()
1470  {
1471  return Object( (GenericObject *) new T );
1472  }
1473  static inline Object value( const T &v )
1474  {
1475  return v.clone();
1476  }
1477 };
1478 #endif
1479 
1480 //-----------------------------------------------------------------------------
1481 template <typename T>
1483 {
1484  return Object( static_cast<GenericObject *>( new ValueObject<T>() ) );
1485  // return ObjectValueTrait< T, SUPERSUBCLASS(GenericObject,T) >::value();
1486 }
1487 
1488 
1489 //-----------------------------------------------------------------------------
1490 template <typename T>
1491 inline Object Object::value( const T &v )
1492 {
1493  return Object( static_cast<GenericObject *>( new ValueObject<T>( v ) ) );
1494  // return ObjectValueTrait< T, SUPERSUBCLASS(GenericObject,T) >::value( v );
1495 }
1496 
1497 
1498 //-----------------------------------------------------------------------------
1499 template <>
1500 inline Object Object::value( const Object &value )
1501 {
1502  return value;
1503 }
1504 
1505 
1506 #if 0
1507 //-----------------------------------------------------------------------------
1508 template <typename T, bool>
1509 class ObjectReferenceTrait
1510 {
1511 public:
1512  static inline Object reference( T &v )
1513  {
1514  return Object( static_cast< GenericObject *>( new ReferenceObject<T>( v ) ) );
1515  }
1516 };
1517 
1518 //-----------------------------------------------------------------------------
1519 template <typename T>
1520 class ObjectReferenceTrait<T,true>
1521 {
1522 public:
1523  static inline Object reference( T &v )
1524  {
1525  return Object( &v );
1526  }
1527 };
1528 #endif
1529 
1530 //-----------------------------------------------------------------------------
1531 template <typename T>
1532 inline Object Object::reference( T &value )
1533 {
1534  return Object( static_cast< GenericObject *>( new ReferenceObject<T>( value ) ) );
1535  // return ObjectReferenceTrait< T, SUPERSUBCLASS( GenericObject, T ) >::reference( value );
1536 }
1537 
1538 
1539 //-----------------------------------------------------------------------------
1540 template <typename T>
1541 inline Object Object::reference( const T &value )
1542 {
1543  return Object::reference( const_cast< T & >( value ) );
1544 }
1545 
1546 
1547 //-----------------------------------------------------------------------------
1548 inline bool Object::isNone() const
1549 {
1550  // may be replaced by a comparison to the global none object
1551  return get() == 0;
1552 }
1553 
1554 
1555  //--------------------------------//
1556  // DataTypeCode<ValueObject<T>> //
1557 //--------------------------------//
1558 
1559 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
1560 
1561 template<typename T> class DataTypeCode< ValueObject<T> >
1562 {
1563  public:
1564  static inline std::string objectType()
1565  { return "ValueObject"; }
1566  static inline std::string dataType()
1567  { return DataTypeCode<T>::name(); }
1568  static inline std::string name()
1569  {
1570  return std::string("ValueObject of ") + DataTypeCode< T >::name();
1571  }
1572 };
1573 
1574 #endif
1575 
1576  //------------------------//
1577  // DataTypeCode<Object> //
1578 //------------------------//
1579 
1580 //-----------------------------------------------------------------------------
1581 template<> inline std::string DataTypeCode<Object>::dataType()
1582 {
1583  return "object";
1584 }
1585 
1586 
1587 //-----------------------------------------------------------------------------
1588 template<> inline std::string DataTypeCode<Object>::objectType()
1589 {
1590  return "object";
1591 }
1592 
1593 
1594 //-----------------------------------------------------------------------------
1595 template<> inline std::string DataTypeCode<Object>::name()
1596 {
1597  return "object";
1598 }
1599 
1600 
1601  //-------------------------------//
1602  // DataTypeCode<GenericObject> //
1603 //-------------------------------//
1604 
1605 //-----------------------------------------------------------------------------
1606 template<> inline std::string DataTypeCode<GenericObject>::dataType()
1607 {
1608  return "any";
1609 }
1610 
1611 
1612 //-----------------------------------------------------------------------------
1613 template<> inline std::string DataTypeCode<GenericObject>::objectType()
1614 {
1615  return "genericobject";
1616 }
1617 
1618 
1619 //-----------------------------------------------------------------------------
1620 template<> inline std::string DataTypeCode<GenericObject>::name()
1621 {
1622  return "genericobject";
1623 }
1624 
1625 
1626 
1627  //------------------------------------//
1628  // DataTypeCode<ReferenceObject<T>> //
1629 //------------------------------------//
1630 
1631 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
1632 
1633 template<typename T> class DataTypeCode< ReferenceObject<T> >
1634 {
1635  public:
1636  static inline std::string objectType()
1637  { return "ReferenceObject"; }
1638  static inline std::string dataType()
1639  { return DataTypeCode<T>::name(); }
1640  static inline std::string name()
1641  {
1642  return std::string("ReferenceObject of ") + DataTypeCode< T >::name();
1643  }
1644 };
1645 
1646 #endif
1647 
1648 
1649 //-----------------------------------------------------------------------------
1650 
1651 
1652 } // namespace carto
1653 
1654 
1655 #endif
#define DECLARE_GENERIC_OBJECT_TYPE(T)
Definition: object.h:56
Objects whose value may be represented as a character string.
Definition: object.h:143
std::map< Object, Object > ObjectDictionary
Definition: object.h:857
virtual ~Interface()
virtual bool getProperty(const std::string &key, Object &value) const =0
Access the element ok key key.
T * getInterface()
Obtain a specific Interface subclass.
Definition: object.h:1381
base abstract generic object class.
Definition: object.h:524
std::map< int, Object > IntDictionary
Definition: object.h:856
Specialized NoneInterface for empty objects (null, None).
Definition: object.h:506
ArrayInterface represents any container whose elements can be accessed via an integer index...
Definition: object.h:334
This class is just a hint to convert an actual data type to an identifier string used in input/output...
Definition: types.h:110
Base class for reference counted objects (intrusive)
Definition: rcptr.h:106
#define _TMP_
Definition: object.h:1261
Container objects which can be iterated.
Definition: object.h:196
storage wrapper, derived and instanciable template class
Definition: object.h:829
static void check(T *x=NULL)
Definition: object.h:758
static Object reference(T &value)
factory function: builds an Object by referencing the value from a ReferenceObject storage wrapper...
Definition: object.h:1532
Specialized IteratorInterface for key/value storage.
Definition: object.h:264
bool operator==(const carto::block< T, N > &b1, const carto::block< T, N > &b2)
Definition: block.h:252
Specialized IteratorInterface for dictionaries.
Definition: object.h:312
storage wrapper, derived but still abstract template class
Definition: object.h:88
bool isNone() const
check if we hold a "none" object.
Definition: object.h:1548
std::map< std::string, Syntax > SyntaxSet
This lookup table associates a syntactic attribute with its syntax.
Definition: syntax.h:83
bool operator==(const Object &other) const
Definition: object.h:1438
Reference-counting pointer.
Definition: rcptr.h:639
static std::string objectType()
Definition: object.h:1564
All scalar numbers implement the ScalarInterface (all ints, float, double...)
Definition: object.h:109
void object_to(Object o, T &r)
Definition: object.h:1305
Specialized IteratorInterface for dictionaries.
Definition: object.h:288
static std::string name()
Definition: types.h:243
#define CARTOBASE_API
virtual ~Object()
Definition: object.h:1421
A Syntaxed object is an object containing an additional character string giving it a kind of type (a ...
Definition: object.h:484
void setValue(const T &val)
Store value in object by copying it.
Definition: object_d.h:167
static std::string objectType()
Definition: types.h:223
std::vector< Object > ObjectVector
Common type used for ArrayInterface implementation.
Definition: object.h:859
#define TMP
Definition: object.h:1215
Class to force a compilation error when a generic object.
Definition: object.h:756
SemanticSet Syntax
Specify syntax for syntactic attributes.
Definition: syntax.h:78
virtual void setProperty(const std::string &key, Object value)=0
Set (insert or replace) the element of key key with the value object.
All container objects inherit the SizeInterface.
Definition: object.h:177
storage wrapper, derived and instanciable template class
Definition: object.h:867
static Object value()
factory function: builds an Object by using the default constructor
Definition: object.h:1482
const T & value() const
Retreive value in object, const reference.
Definition: object_d.h:126
T * get() const
Definition: rcptr.h:666
std::map< std::string, Object > Dictionary
Common type used for DictionaryInterface implementation.
Definition: object.h:855
static std::string dataType()
Definition: types.h:230
Void does not contain anything.
Definition: types.h:60
Object none()
An empty singleton object (holds a null pointer)
A dynamic array has resize and insertion capabilities (like a STL vector)
Definition: object.h:364
Object & operator=(const Object &)
uses the reference-counting so the underlying GenericObject is shared
Definition: object.h:1427
An iterator object is a reference to another object.
Definition: object.h:235
Interface for dictionary-like objects.
Definition: object.h:403