23 #include <unordered_map> 24 #include <tulip/tulipconf.h> 25 #include <tulip/StlIterator.h> 38 struct TLP_SCOPE DataMem {
44 template <
typename TYPE>
45 struct TypedValueContainer :
public DataMem {
47 TypedValueContainer() {}
48 TypedValueContainer(
const TYPE &val) : value(val) {}
49 ~TypedValueContainer()
override {}
59 DataType(
void *value) : value(value) {}
69 virtual std::string getTypeName()
const = 0;
74 static bool isTulipProperty(
const std::string &typeName);
80 return isTulipProperty(getTypeName());
93 TypedData(
void *value) :
DataType(value) {}
94 ~TypedData()
override {
95 delete static_cast<T *
>(value);
98 return new TypedData<T>(
new T(getValue()));
101 std::string getTypeName()
const override {
102 return std::string(
typeid(T).name());
105 const T &getValue()
const {
106 return *(
static_cast<const T *
>(this->value));
114 struct DataTypeSerializer {
116 std::string outputTypeName;
117 DataTypeSerializer(
const std::string &otn) : outputTypeName(otn) {}
118 virtual ~DataTypeSerializer() {}
120 virtual DataTypeSerializer *clone()
const = 0;
122 virtual void writeData(std::ostream &os,
const DataType *data) = 0;
124 virtual std::string toString(
const DataType *data) {
125 std::stringstream ss;
130 virtual DataType *readData(std::istream &is) = 0;
132 virtual bool setData(
DataSet &ds,
const std::string &prop,
const std::string &value) = 0;
137 template <
typename T>
138 struct TypedDataSerializer :
public DataTypeSerializer {
139 TypedDataSerializer(
const std::string &otn) : DataTypeSerializer(otn) {}
141 virtual void write(std::ostream &os,
const T &value) = 0;
143 virtual bool read(std::istream &is, T &value) = 0;
145 void writeData(std::ostream &os,
const DataType *data)
override {
146 write(os, *(static_cast<T *>(data->
value)));
148 DataType *readData(std::istream &is)
override {
150 bool ok = read(is, value);
153 return new TypedData<T>(
new T(value));
158 bool setData(
DataSet &ds,
const std::string &prop,
const std::string &value)
override = 0;
163 class DataTypeSerializerContainer {
166 ~DataTypeSerializerContainer() {
167 std::unordered_map<std::string, DataTypeSerializer *>::iterator it = tnTodts.begin();
169 for (; it != tnTodts.end(); ++it) {
174 std::unordered_map<std::string, DataTypeSerializer *> tnTodts;
175 std::unordered_map<std::string, DataTypeSerializer *> otnTodts;
191 std::list<std::pair<std::string, DataType *>> data;
197 static DataTypeSerializerContainer serializerContainer;
198 static void registerDataTypeSerializer(
const std::string &typeName, DataTypeSerializer *dts);
219 template <
typename T>
220 bool get(
const std::string &key, T &value)
const;
230 template <
typename T>
231 bool getAndFree(
const std::string &key, T &value);
241 template <
typename T>
242 void set(
const std::string &key,
const T &value);
249 template <
typename T>
251 return std::string(
typeid(T).name());
259 template <
typename T>
261 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
267 void writeData(std::ostream &os,
const std::string &prop,
const DataType *dt)
const;
272 static void write(std::ostream &os,
const DataSet &ds);
277 bool readData(std::istream &is,
const std::string &prop,
const std::string &outputTypeName);
282 static bool read(std::istream &is,
DataSet &ds);
288 bool exists(
const std::string &str)
const;
294 std::string getTypeName(
const std::string &str)
const;
299 _DEPRECATED
bool exist(
const std::string &str)
const {
307 void remove(
const std::string &str);
313 DataType *getData(
const std::string &str)
const;
318 void setData(
const std::string &str,
const DataType *value);
328 unsigned int size()
const;
339 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
344 std::string toString()
const;
348 #include "cxx/DataSet.cxx"
static void registerDataTypeSerializer(const DataTypeSerializer &serializer)
Registers a serializer for a known type.
Interface for Tulip iterators. Allows basic iteration operations only.
bool exist(const std::string &str) const
for compatibility only
Describes a value of any type.
A container that can store data from any type.
std::string getTypeName() const
Returns the mangled name of a type.
void * value
The actual pointer to the element's data.
bool isTulipProperty() const
indicates if it is a Tulip property