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());
95 struct DataTypeSerializer {
97 std::string outputTypeName;
98 DataTypeSerializer(
const std::string& otn):outputTypeName(otn) {}
99 virtual ~DataTypeSerializer() {}
101 virtual DataTypeSerializer* clone()
const = 0;
103 virtual void writeData(std::ostream& os,
const DataType *data)=0;
105 virtual DataType* readData(std::istream &is)=0;
111 struct TypedDataSerializer :
public DataTypeSerializer {
112 TypedDataSerializer(
const std::string& otn):DataTypeSerializer(otn) {}
114 virtual void write(std::ostream& os,
const T& value)=0;
116 virtual bool read(std::istream& is, T& value)=0;
118 void writeData(std::ostream& os,
const DataType* data) {
119 write(os, *((T*) data->value));
121 DataType* readData(std::istream& is) {
123 bool ok = read(is, value);
126 return new TypedData<T>(
new T(value));
134 class DataTypeSerializerContainer {
138 ~DataTypeSerializerContainer() {
139 TLP_HASH_MAP<std::string, DataTypeSerializer*>::iterator it = tnTodts.begin();
141 for (; it != tnTodts.end() ; ++it) {
146 TLP_HASH_MAP<std::string, DataTypeSerializer*> tnTodts;
147 TLP_HASH_MAP<std::string, DataTypeSerializer*> otnTodts;
161 std::list< std::pair<std::string, DataType*> > data;
167 static DataTypeSerializerContainer serializerContainer;
168 static void registerDataTypeSerializer(
const std::string& typeName,
169 DataTypeSerializer* dts);
189 template<
typename T>
bool get(
const std::string &key, T& value)
const;
199 template<
typename T>
bool getAndFree(
const std::string &key, T& value);
209 template<
typename T>
void set(
const std::string &key,
const T& value);
217 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
223 void writeData(std::ostream& os,
const std::string& prop,
const DataType* dt)
const;
228 static void write(std::ostream& os,
const DataSet& ds);
233 bool readData(std::istream& is,
const std::string& prop,
const std::string& outputTypeName);
238 static bool read(std::istream& is,
DataSet& ds);
244 bool exist(
const std::string &str)
const;
250 void remove(
const std::string &str);
256 DataType* getData(
const std::string &str)
const;
261 void setData(
const std::string &str,
const DataType* value);
266 tlp::Iterator< std::pair<std::string, DataType*> > *getValues()
const;
271 unsigned int size()
const;
281 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
286 std::string toString()
const;
291 #include "cxx/DataSet.cxx"