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) {}
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());
316 bool readData(std::istream &is,
const std::string &prop,
const std::string &outputTypeName);
327 bool exists(
const std::string &str)
const;
380 #include "cxx/DataSet.cxx"
A container that can store data from any type.
void writeData(std::ostream &os, const std::string &prop, const DataType *dt) const
write an embedded value
bool empty() const
Indicates whether the set is empty of not.
tlp::Iterator< std::pair< std::string, DataType * > > * getValues() const
std::string getTypeName(const std::string &str) const
DataSet & operator=(const DataSet &set)
Performs a deep copy of a DataSet into another.
void remove(const std::string &str)
Removes an element from the DataSet.
unsigned int size() const
Returns the number of registered values.
static void registerDataTypeSerializer(const DataTypeSerializer &serializer)
Registers a serializer for a known type.
bool exists(const std::string &str) const
bool readData(std::istream &is, const std::string &prop, const std::string &outputTypeName)
Read a value and stores it into the specified type.
static DataTypeSerializer * typenameToSerializer(const std::string &name)
std::string getTypeName() const
Returns the mangled name of a type.
static void write(std::ostream &os, const DataSet &ds)
Serializes a DataSet into a stream.
void setData(const std::string &str, const DataType *value)
Set from an untyped value.
DataType * getData(const std::string &str) const
std::string toString() const
static bool read(std::istream &is, DataSet &ds)
Reads a stream and stores its contents into a DataSet.
Describes a value of any type.
bool isTulipProperty() const
indicates if it is a Tulip property
static bool isTulipProperty(const std::string &typeName)
indicates if it is a Tulip property
virtual std::string getTypeName() const =0
void * value
The actual pointer to the element's data.
virtual DataType * clone() const =0
Interface for Tulip iterators. Allows basic iteration operations only.