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;
81 TypedData(
void *value) :
DataType(value) {}
86 return new TypedData<T>(
new T(*(T*)value));
89 std::string getTypeName()
const {
90 return std::string(
typeid(T).name());
98 struct DataTypeSerializer {
100 std::string outputTypeName;
101 DataTypeSerializer(
const std::string& otn):outputTypeName(otn) {}
102 virtual ~DataTypeSerializer() {}
104 virtual DataTypeSerializer* clone()
const = 0;
106 virtual void writeData(std::ostream& os,
const DataType *data)=0;
108 virtual std::string toString(
const DataType *data) {
109 std::stringstream ss;
114 virtual DataType* readData(std::istream &is)=0;
116 virtual bool setData(
DataSet& ds,
const std::string& prop,
const std::string& value)=0;
122 struct TypedDataSerializer :
public DataTypeSerializer {
123 TypedDataSerializer(
const std::string& otn):DataTypeSerializer(otn) {}
125 virtual void write(std::ostream& os,
const T& value)=0;
127 virtual bool read(std::istream& is, T& value)=0;
129 void writeData(std::ostream& os,
const DataType* data) {
130 write(os, *((T*) data->
value));
132 DataType* readData(std::istream& is) {
134 bool ok = read(is, value);
137 return new TypedData<T>(
new T(value));
142 virtual bool setData(
DataSet& ds,
const std::string& prop,
const std::string& value)=0;
147 class DataTypeSerializerContainer {
151 ~DataTypeSerializerContainer() {
152 TLP_HASH_MAP<std::string, DataTypeSerializer*>::iterator it = tnTodts.begin();
154 for (; it != tnTodts.end() ; ++it) {
159 TLP_HASH_MAP<std::string, DataTypeSerializer*> tnTodts;
160 TLP_HASH_MAP<std::string, DataTypeSerializer*> otnTodts;
174 std::list< std::pair<std::string, DataType*> > data;
180 static DataTypeSerializerContainer serializerContainer;
181 static void registerDataTypeSerializer(
const std::string& typeName,
182 DataTypeSerializer* dts);
202 template<
typename T>
bool get(
const std::string &key, T& value)
const;
212 template<
typename T>
bool getAndFree(
const std::string &key, T& value);
222 template<
typename T>
void set(
const std::string &key,
const T& value);
230 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
236 void writeData(std::ostream& os,
const std::string& prop,
const DataType* dt)
const;
241 static void write(std::ostream& os,
const DataSet& ds);
246 bool readData(std::istream& is,
const std::string& prop,
const std::string& outputTypeName);
251 static bool read(std::istream& is,
DataSet& ds);
257 bool exist(
const std::string &str)
const;
263 void remove(
const std::string &str);
269 DataType* getData(
const std::string &str)
const;
274 void setData(
const std::string &str,
const DataType* value);
279 tlp::Iterator< std::pair<std::string, DataType*> > *getValues()
const;
284 unsigned int size()
const;
294 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
299 std::string toString()
const;
304 #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.
void * value
The actual pointer to the element's data.