Salome HOME
PR: what YACS means
[modules/yacs.git] / src / basicData / Data.hxx
1 #ifndef __DATA_HXX__
2 #define __DATA_HXX__
3
4 #include "define.hxx"
5 #include "ContentOfDataFlow.hxx"
6 #include "ConversionException.hxx"
7 #include <string>
8
9 namespace YACS
10 {
11   namespace ENGINE
12   {
13     /**
14      *
15      * Manage Data exchanged through dataflow pipe between 2 nodes. This class minimizes duplications of data.
16      * WARNING : Data has a type defined on constructor or after having called edSetNewType method.
17      *          All the methods that change the content of data check the validity regarding _edType attribute.
18      *
19      */
20     class Data
21     {
22     private:
23       DynType _edType;
24       ContentOfDataFlow::Type _exContent;
25       static const char Data::UNRECOGNIZEDTYPENAME[];
26     public:
27       Data(DynType typeOfContent);
28       Data(const std::string& content, DynType typeOfContent) throw(Exception);
29       template<class T>
30       Data(T val);
31       Data(const Data& other);
32       ~Data();
33       Data &operator =(const Data& other) throw(ConversionException);
34       template<class T>
35       Data &operator =(T val) throw(ConversionException);
36       std::string edGetRepresentation() const;
37       void edSetNewType(DynType newTypeOfContent) throw(ConversionException);
38       void exSetContent(const std::string& content) throw(ConversionException);
39       template<class T>
40       T exGet() const throw(ConversionException);
41       void edInitToType(DynType typeOfContent);
42       void exInit();
43       DynType edGetType() const { return _edType; }
44       static std::string edGetTypeInPrintableForm(DynType type);
45       bool isStaticallyCompatibleWith(const Data& other) const throw(Exception);
46       static bool areStaticallyCompatible(DynType type1, DynType type2) throw(Exception);
47       bool empty() const;
48     };
49
50     template<class T>
51     Data::Data(T val)
52     {
53       _edType=TypeDescriptorTraitsInv<T>::_typeEnum;
54       _exContent=ContentOfDataFlow::createNewContent(val);
55     }
56
57     template<class T>
58     Data &Data::operator =(T val) throw(ConversionException)
59     {
60       ContentOfDataFlow::release(_exContent);
61       typename ContentOfDataFlow::Type content=ContentOfDataFlow::createNewContent(val);
62       if(_edType==TypeDescriptorTraitsInv<T>::_typeEnum)
63         {
64           _exContent=content;
65         }
66       else
67         {
68           _exContent=ContentOfDataFlow::convertContent(content,TypeDescriptorTraitsInv<T>::_typeEnum,_edType);
69           ContentOfDataFlow::release(content);
70         }
71       return *this;
72     }
73     
74     template<class T>
75     T Data::exGet() const throw(ConversionException)
76     {
77       T ret;
78       ContentOfDataFlow::readContent(_exContent,ret);
79       return ret;
80     }
81   }
82 }
83
84 #endif