Tulip  4.10.0
Better Visualization Through Research
StoredType.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef _TLPRETURNTYPE_
22 #define _TLPRETURNTYPE_
23 
24 namespace tlp {
25 
26 // the template below defines how are returned and stored
27 // the types of value a MutableContainer can managed
28 // basically a type is returned and stored by value
29 template <typename TYPE>
30 struct StoredType {
31  // the type of the stored value
32  typedef TYPE Value;
33  // the type of the returned value
34  typedef TYPE ReturnedValue;
35  // the type of a returned const value
36  typedef TYPE ReturnedConstValue;
37  // indicates if a pointer to the value is stored
38  enum {isPointer=0};
39  // simply get
40  inline static TYPE& get(const TYPE& val) {
41  return (TYPE&) val;
42  }
43  // equallity test
44  inline static bool equal(const TYPE& val1, const TYPE& val2) {
45  return val2 == val1;
46  }
47  // cloning before storage
48  inline static Value clone(const TYPE& val) {
49  return val;
50  }
51  // destruction of stored value
52  inline static void destroy(Value) {}
53  // the default value of that type
54  inline static Value defaultValue() {
55  return (Value) 0;
56  }
57 };
58 
59 // non basic types are returned by reference
60 // and stored by pointer
61 // This last point enables a better management of the default value
62 // which can simply be flagged in storing a null pointer
63 // the macro below must be used to enable thies type of management
64 
65 #ifdef TLP_NO_CONST_STORED_TYPE
66 #define DECL_STORED_STRUCT(T) \
67  template <> \
68  struct StoredType<T > { \
69  typedef T *Value; \
70  typedef T& ReturnedValue; \
71  typedef T ReturnedConstValue; \
72  \
73  enum {isPointer=1}; \
74  \
75  inline static T& get(const Value& val) { \
76  return *val; \
77  } \
78  \
79  inline static bool equal(Value val1, const T& val2) { \
80  return val2 == *val1; \
81  } \
82  \
83  inline static bool equal(const T& val2, Value val1) { \
84  return val2 == *val1; \
85  } \
86  \
87  inline static Value clone(const T& val) { \
88  return new T(val); \
89  } \
90  \
91  inline static void destroy(Value val) { \
92  delete val; \
93  } \
94  inline static Value defaultValue() { \
95  return new T(); \
96  } \
97  };
98 }
99 #else
100 #define DECL_STORED_STRUCT(T) \
101  template <> \
102  struct StoredType<T > { \
103  typedef T *Value; \
104  typedef T& ReturnedValue; \
105  typedef const T& ReturnedConstValue; \
106  \
107  enum {isPointer=1}; \
108  \
109  inline static T& get(const Value& val) { \
110  return *val; \
111  } \
112  \
113  inline static bool equal(Value val1, const T& val2) { \
114  return val2 == *val1; \
115  } \
116  \
117  inline static bool equal(const T& val2, Value val1) { \
118  return val2 == *val1; \
119  } \
120  \
121  inline static Value clone(const T& val) { \
122  return new T(val); \
123  } \
124  \
125  inline static void destroy(Value val) { \
126  delete val; \
127  } \
128  inline static Value defaultValue() { \
129  return new T(); \
130  } \
131  };
132 }
133 #endif
134 #endif
135 ///@endcond