Salome HOME
PR: what YACS means
[modules/yacs.git] / src / basicData / Data.cxx
1 #include "Data.hxx"
2
3 using namespace YACS::ENGINE;
4
5 const char Data::UNRECOGNIZEDTYPENAME[]="Unrecognized type";
6
7 Data::Data(DynType typeOfContent):_edType(typeOfContent),_exContent(ContentOfDataFlow::neuter())
8 {
9 }
10
11 Data::Data(const std::string& content, DynType typeOfContent) throw(Exception):_edType(typeOfContent)
12 {
13   _exContent=ContentOfDataFlow::createNewContent(content,typeOfContent);
14 }
15
16 Data::Data(const Data& other):_edType(other._edType)
17 {
18   ContentOfDataFlow::duplicate(other._exContent);
19   _exContent=other._exContent;
20 }
21
22 Data::~Data()
23 {
24   ContentOfDataFlow::release(_exContent);
25 }
26
27 Data &Data::operator =(const Data& other) throw(ConversionException)
28 {
29   if(&other!=this)
30     {
31       if(other._exContent==ContentOfDataFlow::neuter())
32         {
33           ContentOfDataFlow::release(_exContent);
34           _exContent=ContentOfDataFlow::neuter();
35           return *this;
36         }
37       if(other._edType==_edType)
38         {
39           ContentOfDataFlow::duplicate(other._exContent);
40           ContentOfDataFlow::release(_exContent);
41           _exContent=other._exContent;
42         }
43       else
44         {
45           ContentOfDataFlow::release(_exContent);
46           _exContent=ContentOfDataFlow::convertContent(other._exContent,other._edType,_edType);
47         }
48     }
49   return *this;
50 }
51
52 std::string Data::edGetRepresentation() const
53 {
54   std::ostringstream stream;
55   stream << _edType << "_" << ContentOfDataFlow::getRepresentation(_exContent);
56   return stream.str();
57 }
58
59 void Data::edSetNewType(DynType newTypeOfContent) throw(ConversionException)
60 {
61   if(_edType==newTypeOfContent)
62     return;
63   ContentOfDataFlow::Type newContent=ContentOfDataFlow::convertContent(_exContent,_edType,newTypeOfContent);
64   ContentOfDataFlow::release(_exContent);
65   _exContent=newContent;
66   _edType=newTypeOfContent;
67 }
68
69 void Data::exSetContent(const std::string& content) throw(ConversionException)
70 {
71   ContentOfDataFlow::release(_exContent);
72   _exContent=ContentOfDataFlow::createNewContent(content,_edType);
73 }
74
75 void Data::edInitToType(DynType typeOfContent)
76 {
77   ContentOfDataFlow::release(_exContent);
78   _exContent=ContentOfDataFlow::neuter();
79   _edType=typeOfContent;
80 }
81
82 void Data::exInit()
83 {
84   ContentOfDataFlow::release(_exContent);
85   _exContent=ContentOfDataFlow::neuter();
86 }
87
88 // Part of Data::edGetType implementation
89 template< YACS::DynType type >
90 class TreatmentForNameOfType
91 {
92 public:
93   static void perform(int input, std::string& ret)
94   {
95     ret=TypeDescriptorTraits<type>::_name;
96   }
97 };
98
99 std::string Data::edGetTypeInPrintableForm(DynType type)
100 {
101   try
102     {
103       int fake1;
104       std::string ret;
105       DynToStaticTypeDispatcher1<TreatmentForNameOfType>::perform(type, fake1, ret);
106       return ret;
107     }
108   catch(Exception& e)
109     {
110       return UNRECOGNIZEDTYPENAME;
111     }
112 }
113
114 bool Data::isStaticallyCompatibleWith(const Data& other) const throw(Exception)
115 {
116   return areStaticallyCompatible(_edType, other._edType);
117 }
118
119 bool Data::areStaticallyCompatible(DynType type1, DynType type2) throw(Exception)
120 {
121   return ContentOfDataFlow::isStaticallyCompatibleWith(type1, type2);
122 }
123
124 bool Data::empty() const
125 {
126   return _exContent==ContentOfDataFlow::neuter();
127 }