23 #include <tulip/tulipconf.h> 24 #include <tulip/tuliphash.h> 25 #include <tulip/StlIterator.h> 38 struct TLP_SCOPE DataMem {
44 template<
typename TYPE>
struct TypedValueContainer:
public DataMem {
46 TypedValueContainer() {}
47 TypedValueContainer(
const TYPE& val) : value(val) {}
48 ~TypedValueContainer() {
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());
95 TypedData(
void *value) :
DataType(value) {}
100 return new TypedData<T>(
new T(*(T*)value));
103 std::string getTypeName()
const {
104 return std::string(
typeid(T).name());
107 const T& getValue() {
108 return (T&) this->value;
116 struct DataTypeSerializer {
118 std::string outputTypeName;
119 DataTypeSerializer(
const std::string& otn):outputTypeName(otn) {}
120 virtual ~DataTypeSerializer() {}
122 virtual DataTypeSerializer* clone()
const = 0;
124 virtual void writeData(std::ostream& os,
const DataType *data)=0;
126 virtual std::string toString(
const DataType *data) {
127 std::stringstream ss;
132 virtual DataType* readData(std::istream &is)=0;
134 virtual bool setData(
DataSet& ds,
const std::string& prop,
const std::string& value)=0;
140 struct TypedDataSerializer :
public DataTypeSerializer {
141 TypedDataSerializer(
const std::string& otn):DataTypeSerializer(otn) {}
143 virtual void write(std::ostream& os,
const T& value)=0;
145 virtual bool read(std::istream& is, T& value)=0;
147 void writeData(std::ostream& os,
const DataType* data) {
148 write(os, *((T*) data->
value));
150 DataType* readData(std::istream& is) {
152 bool ok = read(is, value);
155 return new TypedData<T>(
new T(value));
160 virtual bool setData(
DataSet& ds,
const std::string& prop,
const std::string& value)=0;
165 class DataTypeSerializerContainer {
169 ~DataTypeSerializerContainer() {
170 TLP_HASH_MAP<std::string, DataTypeSerializer*>::iterator it = tnTodts.begin();
172 for (; it != tnTodts.end() ; ++it) {
177 TLP_HASH_MAP<std::string, DataTypeSerializer*> tnTodts;
178 TLP_HASH_MAP<std::string, DataTypeSerializer*> otnTodts;
192 std::list< std::pair<std::string, DataType*> > data;
198 static DataTypeSerializerContainer serializerContainer;
199 static void registerDataTypeSerializer(
const std::string& typeName,
200 DataTypeSerializer* dts);
220 template<
typename T>
bool get(
const std::string &key, T& value)
const;
230 template<
typename T>
bool getAndFree(
const std::string &key, T& value);
240 template<
typename T>
void set(
const std::string &key,
const T& value);
248 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
254 void writeData(std::ostream& os,
const std::string& prop,
const DataType* dt)
const;
259 static void write(std::ostream& os,
const DataSet& ds);
264 bool readData(std::istream& is,
const std::string& prop,
const std::string& outputTypeName);
269 static bool read(std::istream& is,
DataSet& ds);
275 bool exist(
const std::string &str)
const;
281 void remove(
const std::string &str);
287 DataType* getData(
const std::string &str)
const;
292 void setData(
const std::string &str,
const DataType* value);
297 tlp::Iterator< std::pair<std::string, DataType*> > *getValues()
const;
302 unsigned int size()
const;
312 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
317 std::string toString()
const;
322 #include "cxx/DataSet.cxx"
static void registerDataTypeSerializer(const DataTypeSerializer &serializer)
Registers a serializer for a known type.
Describes a value of any type.
A container that can store data from any type.
bool isTulipProperty() const
indicates if it is a Tulip property
void * value
The actual pointer to the element's data.