23 #include <tulip/tulipconf.h>
24 #include <tulip/tuliphash.h>
25 #include <tulip/StlIterator.h>
37 struct TLP_SCOPE DataMem {
43 template<
typename TYPE>
struct TypedValueContainer:
public DataMem {
45 TypedValueContainer() {}
46 TypedValueContainer(
const TYPE& val) : value(val) {}
47 ~TypedValueContainer() {
80 TypedData(
void *value) :
DataType(value) {}
84 DataType* clone()
const {
85 return new TypedData<T>(
new T(*(T*)value));
88 std::string getTypeName()
const {
89 return std::string(
typeid(T).name());
97 struct DataTypeSerializer {
99 std::string outputTypeName;
100 DataTypeSerializer(
const std::string& otn):outputTypeName(otn) {}
101 virtual ~DataTypeSerializer() {}
103 virtual DataTypeSerializer* clone()
const = 0;
105 virtual void writeData(std::ostream& os,
const DataType *data)=0;
107 virtual DataType* readData(std::istream &is)=0;
109 virtual bool setData(DataSet& ds,
const std::string& prop,
const std::string& value)=0;
115 struct TypedDataSerializer :
public DataTypeSerializer {
116 TypedDataSerializer(
const std::string& otn):DataTypeSerializer(otn) {}
118 virtual void write(std::ostream& os,
const T& value)=0;
120 virtual bool read(std::istream& is, T& value)=0;
122 void writeData(std::ostream& os,
const DataType* data) {
123 write(os, *((T*) data->value));
125 DataType* readData(std::istream& is) {
127 bool ok = read(is, value);
130 return new TypedData<T>(
new T(value));
135 virtual bool setData(DataSet& ds,
const std::string& prop,
const std::string& value)=0;
140 class DataTypeSerializerContainer {
144 ~DataTypeSerializerContainer() {
145 TLP_HASH_MAP<std::string, DataTypeSerializer*>::iterator it = tnTodts.begin();
147 for (; it != tnTodts.end() ; ++it) {
152 TLP_HASH_MAP<std::string, DataTypeSerializer*> tnTodts;
153 TLP_HASH_MAP<std::string, DataTypeSerializer*> otnTodts;
167 std::list< std::pair<std::string, DataType*> > data;
173 static DataTypeSerializerContainer serializerContainer;
174 static void registerDataTypeSerializer(
const std::string& typeName,
175 DataTypeSerializer* dts);
195 template<
typename T>
bool get(
const std::string &key, T& value)
const;
205 template<
typename T>
bool getAndFree(
const std::string &key, T& value);
215 template<
typename T>
void set(
const std::string &key,
const T& value);
223 registerDataTypeSerializer(std::string(
typeid(T).name()), serializer.clone());
229 void writeData(std::ostream& os,
const std::string& prop,
const DataType* dt)
const;
234 static void write(std::ostream& os,
const DataSet& ds);
239 bool readData(std::istream& is,
const std::string& prop,
const std::string& outputTypeName);
244 static bool read(std::istream& is,
DataSet& ds);
250 bool exist(
const std::string &str)
const;
256 void remove(
const std::string &str);
262 DataType* getData(
const std::string &str)
const;
267 void setData(
const std::string &str,
const DataType* value);
272 tlp::Iterator< std::pair<std::string, DataType*> > *getValues()
const;
277 unsigned int size()
const;
287 static DataTypeSerializer *typenameToSerializer(
const std::string &name);
292 std::string toString()
const;
297 #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.
virtual std::string getTypeName() const =0
void * value
The actual pointer to the element's data.
virtual DataType * clone() const =0