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