]> SALOME platform Git repositories - modules/yacs.git/blob - src/basicData/ContentOfDataFlow.hxx
Salome HOME
PR: first version from Antony GEAY, with directory restructuration
[modules/yacs.git] / src / basicData / ContentOfDataFlow.hxx
1 #ifndef __CONTENTOFDATAFLOW_HXX__
2 #define __CONTENTOFDATAFLOW_HXX__
3
4 #include "define.hxx"
5 #include "TypeCheckerDataFlow.hxx"
6 #include "ConversionException.hxx"
7
8 #include <string>
9 #include <cstring>
10 #include <sstream>
11 #include <stdlib.h>
12
13 namespace YACS
14 {
15   namespace ENGINE
16   {
17     class ContentOfDataFlow
18     {
19     public:
20       typedef void * Type;
21
22       static void duplicate(Type content);
23       static void release(Type& content);
24       static Type createNewContent(const std::string& content, DynType typeOfContent) throw(Exception);
25       static Type convertContent(Type content, DynType fromType, DynType toType) throw(ConversionException);
26       static bool isStaticallyCompatibleWith(DynType fromType, DynType toType) throw(Exception);
27       template<class T>
28       static Type createNewContent(T value);
29       template<YACS::DynType typ>
30       static Type createNewContent(const std::string& content) throw(Exception);
31       template<class T>
32       static void readContent(Type content, T& value) throw(Exception);
33       static Type neuter() { return 0; }
34       static std::string getRepresentation(Type content);
35     private:
36       static Type allocate(int lgth);
37     private:
38       static const char STRFORNULLREPR[];
39     };
40
41     template<class T>
42     ContentOfDataFlow::Type ContentOfDataFlow::createNewContent(T value)
43     {
44       std::ostringstream stream;
45       stream << value;
46       int length=stream.str().length();
47       ContentOfDataFlow::Type ret=allocate(length);
48       int *retC=(int *)ret;
49       memcpy(retC+1,stream.str().c_str(),length+1);
50       return ret;
51     }
52
53     template<class T>
54     void ContentOfDataFlow::readContent(ContentOfDataFlow::Type content, T& value) throw(Exception)
55     {
56       if(content==ContentOfDataFlow::neuter())
57         throw Exception("no content to read");
58       char *contentC=(char *)content;
59       contentC+=sizeof(int);
60       const DynType typ=TypeDescriptorTraitsInv<T>::_typeEnum;
61       std::istringstream stream(contentC);
62       stream >> value;
63       if(stream.fail() || !stream.eof())
64         {
65           std::string what="Content not recognized as "; what+=TypeDescriptorTraits<TypeDescriptorTraitsInv<T>::_typeEnum>::_name;
66           throw Exception(what);
67         }
68     }
69
70     template<YACS::DynType typ>
71     ContentOfDataFlow::Type ContentOfDataFlow::createNewContent(const std::string& content) throw(Exception)
72     {
73       std::istringstream stream(content);
74       typename TypeDescriptorTraits<typ>::ConcreteType value;
75       stream >> value;
76       if(!stream.fail() && stream.eof())
77         return createNewContent(value);
78       else
79         {
80           std::string what(content);
81           what+=" is not recognized as "; what+=TypeDescriptorTraits<typ>::_name;
82           throw Exception(what);
83         }
84     }
85   }
86 }
87
88 #endif