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);
97 DataType *clone()
const override {
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;
119 DataTypeSerializer(
const std::string &otn) : outputTypeName(otn), graph(nullptr) {}
120 virtual ~DataTypeSerializer() {}
122 void setGraph(Graph *g) {
126 virtual DataTypeSerializer *clone()
const = 0;
128 virtual void writeData(std::ostream &os,
const DataType *data) = 0;
130 virtual std::string toString(
const DataType *data) {
131 std::stringstream ss;
136 virtual DataType *readData(std::istream &is) = 0;
138 virtual bool setData(DataSet &ds,
const std::string &prop,
const std::string &value) = 0;
143 template <
typename T>
144 struct TypedDataSerializer :
public DataTypeSerializer {
145 TypedDataSerializer(
const std::string &otn) : DataTypeSerializer(otn) {}
147 virtual void write(std::ostream &os,
const T &value) = 0;
149 virtual bool read(std::istream &is, T &value) = 0;
151 void writeData(std::ostream &os,
const DataType *data)
override {
152 write(os, *(
static_cast<T *
>(data->value)));
154 DataType *readData(std::istream &is)
override {
156 bool ok = read(is, value);
159 return new TypedData<T>(
new T(value));
164 bool setData(DataSet &ds,
const std::string &prop,
const std::string &value)
override = 0;
169 class DataTypeSerializerContainer {
172 ~DataTypeSerializerContainer() {
173 std::unordered_map<std::string, DataTypeSerializer *>::iterator it = tnTodts.begin();
175 for (; it != tnTodts.end(); ++it) {
180 std::unordered_map<std::string, DataTypeSerializer *> tnTodts;
181 std::unordered_map<std::string, DataTypeSerializer *> otnTodts;
197 std::list<std::pair<std::string, DataType *>> data;
200 std::list<std::pair<std::string, std::string>> *deprecated;
202 void addDeprecated(
const std::string &oldName,
const std::string &usedName);
204 const std::string &getUsedName(
const std::string &oldName)
const;
211 static DataTypeSerializerContainer serializerContainer;
212 static void registerDataTypeSerializer(
const std::string &typeName, DataTypeSerializer *dts);
215 DataSet() : deprecated(
nullptr) {}
233 template <
typename T>
234 bool get(
const std::string &key, T &value)
const;
246 template <
typename T>
247 bool getDeprecated(
const std::string &key,
const std::string &oldKey, T &value)
const;
257 template <
typename T>
258 bool getAndFree(
const std::string &key, T &value);
268 template <
typename T>
269 void set(
const std::string &key,
const T &value);
280 template <
typename T>
281 void setDeprecated(
const std::string &key,
const std::string &deprecatedKey,
const T &value);
288 template <
typename T>
290 return std::string(
typeid(T).name());
298 template <
typename T>
300 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
306 void writeData(std::ostream &os,
const std::string &prop,
const DataType *dt)
const;
311 static void write(std::ostream &os,
const DataSet &ds);
316 bool readData(std::istream &is,
const std::string &prop,
const std::string &outputTypeName);
321 static bool read(std::istream &is,
DataSet &ds);
327 bool exists(
const std::string &str)
const;
333 std::string getTypeName(
const std::string &str)
const;
339 void remove(
const std::string &str);
345 DataType *getData(
const std::string &str)
const;
350 void setData(
const std::string &str,
const DataType *value);
360 unsigned int size()
const;
371 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
376 std::string toString()
const;
380 #include "cxx/DataSet.cxx"