23 #include <tulip/tulipconf.h>
24 #include <tulip/tuliphash.h>
30 #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() {
81 TypedData(
void *value) :
DataType(value) {}
85 DataType* clone()
const {
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 DataType* readData(std::istream &is)=0;
110 virtual bool setData(DataSet& ds,
const std::string& prop,
const std::string& value)=0;
116 struct TypedDataSerializer :
public DataTypeSerializer {
117 TypedDataSerializer(
const std::string& otn):DataTypeSerializer(otn) {}
119 virtual void write(std::ostream& os,
const T& value)=0;
121 virtual bool read(std::istream& is, T& value)=0;
123 void writeData(std::ostream& os,
const DataType* data) {
124 write(os, *((T*) data->value));
126 DataType* readData(std::istream& is) {
128 bool ok = read(is, value);
131 return new TypedData<T>(
new T(value));
136 virtual bool setData(DataSet& ds,
const std::string& prop,
const std::string& value)=0;
141 class DataTypeSerializerContainer {
145 ~DataTypeSerializerContainer() {
146 TLP_HASH_MAP<std::string, DataTypeSerializer*>::iterator it = tnTodts.begin();
148 for (; it != tnTodts.end() ; ++it) {
153 TLP_HASH_MAP<std::string, DataTypeSerializer*> tnTodts;
154 TLP_HASH_MAP<std::string, DataTypeSerializer*> otnTodts;
168 std::list< std::pair<std::string, DataType*> > data;
174 static DataTypeSerializerContainer serializerContainer;
175 static void registerDataTypeSerializer(
const std::string& typeName,
176 DataTypeSerializer* dts);
196 template<
typename T>
bool get(
const std::string &key, T& value)
const;
206 template<
typename T>
bool getAndFree(
const std::string &key, T& value);
216 template<
typename T>
void set(
const std::string &key,
const T& value);
224 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
230 void writeData(std::ostream& os,
const std::string& prop,
const DataType* dt)
const;
235 static void write(std::ostream& os,
const DataSet& ds);
240 bool readData(std::istream& is,
const std::string& prop,
const std::string& outputTypeName);
245 static bool read(std::istream& is,
DataSet& ds);
251 bool exist(
const std::string &str)
const;
257 void remove(
const std::string &str);
263 DataType* getData(
const std::string &str)
const;
268 void setData(
const std::string &str,
const DataType* value);
273 tlp::Iterator< std::pair<std::string, DataType*> > *getValues()
const;
278 unsigned int size()
const;
288 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
293 std::string toString()
const;
298 #include "cxx/DataSet.cxx"