Tulip 5.7.1
Large graphs analysis and drawing
Loading...
Searching...
No Matches
StoredType.h
1/*
2 *
3 * This file is part of Tulip (https://tulip.labri.fr)
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
24namespace 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
29template <typename TYPE>
30struct 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 const_cast<TYPE &>(val);
42 }
43 // equality 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 static_cast<Value>(0);
56 }
57};
58
59#define DECL_STORED_PTR(T) \
60 template <> \
61 struct StoredType<T> { \
62 typedef T Value; \
63 typedef T ReturnedValue; \
64 typedef const T ReturnedConstValue; \
65 \
66 enum { isPointer = 1 }; \
67 \
68 inline static T get(T val) { \
69 return val; \
70 } \
71 \
72 inline static bool equal(const T val1, const T val2) { \
73 return val2 == val1; \
74 } \
75 \
76 inline static T clone(T val) { \
77 return val; \
78 } \
79 \
80 inline static void destroy(T val) { \
81 delete val; \
82 } \
83 inline static T defaultValue() { \
84 return nullptr; \
85 } \
86 }
87
88// non basic types are returned by reference
89// and stored by pointer
90// This last point enables a better management of the default value
91// which can simply be flagged in storing a null pointer
92// the macro below must be used to enable thies type of management
93#ifdef TLP_NO_CONST_STORED_TYPE
94#define DECL_STORED_STRUCT(T) \
95 template <> \
96 struct StoredType<T> { \
97 typedef T *Value; \
98 typedef T &ReturnedValue; \
99 typedef T ReturnedConstValue; \
100 \
101 enum { isPointer = 1 }; \
102 \
103 inline static T &get(const Value &val) { \
104 return *val; \
105 } \
106 \
107 inline static bool equal(Value val1, const T &val2) { \
108 return val2 == *val1; \
109 } \
110 \
111 inline static bool equal(const T &val2, Value val1) { \
112 return val2 == *val1; \
113 } \
114 \
115 inline static Value clone(const T &val) { \
116 return new T(val); \
117 } \
118 \
119 inline static void destroy(Value val) { \
120 delete val; \
121 } \
122 inline static Value defaultValue() { \
123 return new T(); \
124 } \
125 };
126#else
127#define DECL_STORED_STRUCT(T) \
128 template <> \
129 struct StoredType<T> { \
130 typedef T *Value; \
131 typedef T &ReturnedValue; \
132 typedef const T &ReturnedConstValue; \
133 \
134 enum { isPointer = 1 }; \
135 \
136 inline static T &get(const Value &val) { \
137 return *val; \
138 } \
139 \
140 inline static bool equal(Value val1, const T &val2) { \
141 return val2 == *val1; \
142 } \
143 \
144 inline static bool equal(const T &val2, Value val1) { \
145 return val2 == *val1; \
146 } \
147 \
148 inline static Value clone(const T &val) { \
149 return new T(val); \
150 } \
151 \
152 inline static void destroy(Value val) { \
153 delete val; \
154 } \
155 inline static Value defaultValue() { \
156 return new T(); \
157 } \
158 };
159#endif
160} // namespace tlp
161#endif
162///@endcond