Tulip  5.2.0
Large graphs analysis and drawing
PropertyTypes.h
1 /*
2  *
3  * This file is part of Tulip (http://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 TYPES_H
22 #define TYPES_H
23 
24 #include <set>
25 #include <vector>
26 //#include <string>
27 //#include <list>
28 //#include <iostream>
29 #include <sstream>
30 //#include <float.h>
31 #include <tulip/StoredType.h>
32 #include <tulip/Coord.h>
33 #include <tulip/Size.h>
34 #include <tulip/Color.h>
35 //#include <tulip/StringCollection.h>
36 #include <tulip/Edge.h>
37 #include <tulip/Node.h>
38 #include <tulip/AbstractProperty.h>
39 #include <tulip/TypeInterface.h>
40 #include <tulip/SerializableType.h>
41 
42 namespace tlp {
43 
44 class Graph;
45 
46 class TLP_SCOPE GraphType : public TypeInterface<tlp::Graph *> {
47 public:
48  static RealType undefinedValue();
49  static RealType defaultValue();
50  static void write(std::ostream &oss, const RealType &v);
51  static void writeb(std::ostream &oss, const RealType &v);
52  static bool read(std::istream &iss, RealType &v);
53  static bool readb(std::istream &iss, RealType &v);
54  FORWARD_STRING_METHODS(GraphType)
55 };
56 
57 class TLP_SCOPE EdgeSetType : public TypeInterface<std::set<tlp::edge>> {
58 public:
59  static void write(std::ostream &oss, const RealType &v);
60  static void writeb(std::ostream &oss, const RealType &v);
61  static bool read(std::istream &iss, RealType &v);
62  static bool readb(std::istream &iss, RealType &v);
63  FORWARD_STRING_METHODS(EdgeSetType)
64 };
65 
66 class TLP_SCOPE DoubleType : public TypeInterface<double> {
67 public:
68  static double undefinedValue();
69  static double defaultValue();
70  static void write(std::ostream &oss, const RealType &v) {
71  oss << v;
72  }
73  static bool read(std::istream &iss, RealType &v);
74  FORWARD_STRING_METHODS(DoubleType)
75 };
76 
77 class TLP_SCOPE FloatType : public SerializableType<float> {
78 public:
79  static float undefinedValue();
80  static float defaultValue();
81  static bool read(std::istream &iss, RealType &v);
82  FORWARD_STRING_METHODS(FloatType)
83 };
84 
85 typedef SerializableVectorType<double, DoubleType, false> DoubleVectorType;
86 
87 class TLP_SCOPE IntegerType : public SerializableType<int> {
88 public:
89  static int undefinedValue();
90  static int defaultValue();
91 };
92 
93 class TLP_SCOPE LongType : public SerializableType<long> {
94 public:
95  static long undefinedValue();
96  static long defaultValue();
97 };
98 
99 class TLP_SCOPE UnsignedIntegerType : public SerializableType<unsigned int> {
100 public:
101  static unsigned int undefinedValue();
102  static unsigned int defaultValue();
103 };
104 
105 typedef SerializableVectorType<int, IntegerType, false> IntegerVectorType;
106 typedef SerializableVectorType<unsigned int, UnsignedIntegerType, false> UnsignedIntegerVectorType;
107 
108 class TLP_SCOPE BooleanType : public TypeInterface<bool> {
109 public:
110  static RealType undefinedValue();
111  static RealType defaultValue();
112  static void write(std::ostream &, const RealType &);
113  static bool read(std::istream &, RealType &, bool untilEnd = false);
114  static bool fromString(RealType &, const std::string &, bool untilEnd = false);
115  FORWARD_TOSTRING(BooleanType)
116 };
117 
118 class TLP_SCOPE BooleanVectorType : public TypeInterface<std::vector<bool>> {
119 public:
120  static void write(std::ostream &, const RealType &);
121  static void writeb(std::ostream &, const RealType &);
122  static bool read(std::istream &, RealType &, char openChar = '(', char sepChar = ',',
123  char closeChar = ')');
124  static bool read(const std::vector<std::string> &vs, RealType &v);
125  static bool tokenize(const std::string &, std::vector<std::string> &, char openChar = '(',
126  char sepChar = ',', char closeChar = ')');
127  static bool readb(std::istream &, RealType &);
128  FORWARD_STRING_METHODS(BooleanVectorType)
129 };
130 
131 class TLP_SCOPE PointType : public SerializableType<tlp::Coord> {
132 public:
133  static RealType undefinedValue();
134  static RealType defaultValue();
135  static bool read(std::istream &, RealType &);
136  static bool fromString(RealType &, const std::string &);
137  FORWARD_TOSTRING(PointType)
138 };
139 
140 class TLP_SCOPE LineType : public SerializableVectorType<tlp::Coord, PointType, false> {
141 public:
142  static bool read(std::istream &, RealType &, char openChar = '(', char sepChar = ',',
143  char closeChar = ')');
144  FORWARD_STRING_METHODS(LineType)
145 };
146 
147 class TLP_SCOPE SizeType : public SerializableType<tlp::Size> {
148 public:
149  static RealType undefinedValue();
150  static RealType defaultValue();
151  static bool read(std::istream &, RealType &);
152  static bool fromString(RealType &, const std::string &);
153  FORWARD_TOSTRING(SizeType)
154 };
155 
156 typedef SerializableVectorType<tlp::Size, SizeType, true> SizeVectorType;
157 
158 class TLP_SCOPE StringType : public TypeInterface<std::string> {
159 public:
160  static RealType undefinedValue();
161  static RealType defaultValue();
162 
163  static void write(std::ostream &, const RealType &, char openCloseChar = '"');
164  static void writeb(std::ostream &, const RealType &);
165  static bool read(std::istream &, RealType &, char openChar = '"', char closeChar = '"');
166  static bool readb(std::istream &, RealType &);
167 
168  static std::string toString(const RealType &v);
169  static bool fromString(RealType &v, const std::string &s);
170 };
171 
172 class TLP_SCOPE StringVectorType : public TypeInterface<std::vector<std::string>> {
173 public:
174  static void write(std::ostream &, const RealType &);
175  static void writeb(std::ostream &oss, const RealType &vStr);
176  static bool read(std::istream &, RealType &, char openChar = '(', char sepchar = ',',
177  char closeChar = ')');
178  static bool read(const std::vector<std::string> &vs, RealType &v) {
179  v.clear();
180  v.reserve(vs.size());
181  for (const std::string &s : vs)
182  v.push_back(s);
183 
184  return true;
185  }
186  static bool tokenize(const std::string &s, std::vector<std::string> &v, char openChar = '(',
187  char sepChar = ',', char closeChar = ')') {
188  std::istringstream is(s);
189  return read(is, v, openChar, sepChar, closeChar);
190  }
191  static bool readb(std::istream &iss, RealType &vStr);
192  FORWARD_STRING_METHODS(StringVectorType)
193 };
194 
195 class TLP_SCOPE ColorType : public TypeInterface<tlp::Color> {
196 public:
197  static RealType undefinedValue();
198 
199  static void write(std::ostream &, const RealType &);
200  static bool read(std::istream &, RealType &);
201 
202  static std::string toString(const RealType &v);
203  static bool fromString(RealType &v, const std::string &s);
204 };
205 
206 typedef SerializableVectorType<tlp::Color, ColorType, true> ColorVectorType;
207 typedef SerializableVectorType<tlp::Coord, PointType, true> CoordVectorType;
208 
209 DECL_STORED_STRUCT(tlp::EdgeSetType::RealType)
210 DECL_STORED_STRUCT(std::set<tlp::node>)
211 DECL_STORED_STRUCT(tlp::DoubleVectorType::RealType)
212 DECL_STORED_STRUCT(tlp::IntegerVectorType::RealType)
213 DECL_STORED_STRUCT(tlp::BooleanVectorType::RealType)
214 DECL_STORED_STRUCT(tlp::LineType::RealType)
215 DECL_STORED_STRUCT(tlp::PointType::RealType)
216 // DECL_STORED_STRUCT(tlp::SizeType::RealType) <=> PointType::RealType
217 // DECL_STORED_STRUCT(tlp::SizeVectorType::RealType) <=> LineType::RealType
218 DECL_STORED_STRUCT(tlp::StringType::RealType)
219 DECL_STORED_STRUCT(tlp::StringVectorType::RealType)
220 DECL_STORED_STRUCT(tlp::ColorVectorType::RealType)
221 
222 // template class to automatize definition of serializers
223 template <typename T>
224 struct KnownTypeSerializer : public TypedDataSerializer<typename T::RealType> {
225  KnownTypeSerializer(const std::string &otn) : TypedDataSerializer<typename T::RealType>(otn) {}
226  KnownTypeSerializer(const char *otn)
227  : TypedDataSerializer<typename T::RealType>(std::string(otn)) {}
228 
229  DataTypeSerializer *clone() const override {
230  return new KnownTypeSerializer<T>(this->outputTypeName);
231  }
232 
233  void write(std::ostream &os, const typename T::RealType &v) override {
234  T::write(os, v);
235  }
236  bool read(std::istream &iss, typename T::RealType &v) override {
237  return T::read(iss, v);
238  }
239  bool setData(tlp::DataSet &ds, const std::string &prop, const std::string &value) override {
240  bool result = true;
241  typename T::RealType val;
242 
243  if (value.empty())
244  val = T::defaultValue();
245  else
246  result = T::fromString(val, value);
247 
248  ds.set(prop, val);
249  return result;
250  }
251 };
252 
253 extern void initTypeSerializers();
254 #ifdef _MSC_VER
255 template class SerializableType<double>;
256 template class SerializableType<float>;
257 template class SerializableType<int>;
258 template class SerializableType<long>;
259 template class SerializableType<unsigned int>;
260 template class SerializableType<tlp::Coord>;
261 template class SerializableType<tlp::Size>;
262 template class SerializableVectorType<double, DoubleType, false>;
263 template class SerializableVectorType<int, IntegerType, false>;
264 template class SerializableVectorType<unsigned int, UnsignedIntegerType, false>;
265 template class SerializableVectorType<tlp::Coord, PointType, false>;
266 template class SerializableVectorType<tlp::Size, SizeType, true>;
267 template class SerializableVectorType<tlp::Color, ColorType, true>;
268 template class SerializableVectorType<tlp::Coord, PointType, true>;
269 template class TypeInterface<tlp::Graph *>;
270 template class TypeInterface<std::set<tlp::edge>>;
271 template class TypeInterface<bool>;
272 template class TypeInterface<std::vector<bool>>;
273 template class TypeInterface<std::vector<std::string>>;
274 template class TypeInterface<tlp::Color>;
275 template class TypeInterface<std::string>;
276 
277 template class AbstractVectorProperty<DoubleVectorType, tlp::DoubleType>;
278 template class AbstractVectorProperty<IntegerVectorType, tlp::IntegerType>;
279 template class AbstractVectorProperty<BooleanVectorType, tlp::BooleanType>;
280 template class AbstractVectorProperty<StringVectorType, tlp::StringType>;
281 template class AbstractVectorProperty<ColorVectorType, tlp::ColorType>;
282 template class AbstractVectorProperty<SizeVectorType, tlp::SizeType>;
283 template class AbstractVectorProperty<CoordVectorType, tlp::PointType>;
284 #endif
285 }
286 
287 #endif
288 ///@endcond
void set(const std::string &key, const T &value)
Stores a copy of the given param, associated with the key. The value must have a well-formed copy con...
Definition: DataSet.cxx:55
A container that can store data from any type.
Definition: DataSet.h:189