Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / TypeCode.hxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef __TYPECODE_HXX__
20 #define __TYPECODE_HXX__
21
22 #include "RefCounter.hxx"
23 #include "Exception.hxx"
24 #include "Any.hxx"
25
26 #include <string>
27 #include <list>
28
29 namespace YACS
30 {
31   namespace ENGINE
32   {
33     class Visitor;
34
35     typedef enum
36       {
37         NONE     = 0,
38         Double   = 1,
39         Int      = 2,
40         String   = 3,
41         Bool     = 4,
42         Objref   = 5,
43         Sequence = 6,
44         Array    = 7,
45         Struct   = 8
46       } DynType;
47
48 //     typedef enum DynType StreamType;
49
50     class TypeCodeObjref;
51
52 /*! \brief Base class for all type objects.
53  *
54  * \ingroup TypeCodes
55  *
56  * All type objects should be a subclass of TypeCode.  Some type objects,
57  * TypeCodeObjref for example, represent one individual type.  Other type
58  * objects, such as TypeCodeSeq, are composite types (sequence, here)
59  *
60  * \see TypeCodeObjref
61  * \see TypeCodeSeq
62  * \see TypeCodeStruct
63  * \see TypeCodeArray
64  */
65     class TypeCode : public RefCounter
66     {
67     public:
68       TypeCode(DynType kind);
69
70       DynType kind() const;
71       const char * getKindRepr() const;
72       
73       virtual TypeCode *clone() const;
74       virtual void putReprAtPlace(char *pt, const char *val, bool deepCpy) const;
75       virtual void destroyZippedAny(char *data) const;
76       virtual AnyPtr getOrBuildAnyFromZippedData(char *data) const;
77       virtual const char * name()       const throw(Exception);
78       virtual const char * shortName()  const;
79       virtual const char * id()         const throw(Exception);
80       virtual const TypeCode * contentType() const throw(Exception);
81       virtual int isA(const char* repositoryId) const throw(Exception);
82       virtual int isA(const TypeCode* tc) const ;
83       virtual int isAdaptable(const TypeCode* tc) const;
84       virtual int isEquivalent(const TypeCode* tc) const;
85       virtual unsigned getSizeInByteOfAnyReprInSeq() const;
86
87       static const char *getKindRepr(DynType kind);
88       static TypeCode * interfaceTc(const char* id, const char* name);
89       static TypeCode * interfaceTc(const char* id, const char* name, const std::list<TypeCodeObjref *>& ltc);
90       static TypeCode * sequenceTc (const char* id, const char* name, TypeCode *content);
91       static TypeCode * structTc (const char* id, const char* name);
92
93     protected:
94       // --- These operators are placed here to avoid them being used externally
95       TypeCode(const TypeCode& tc);
96       TypeCode& operator=(const TypeCode& tc);
97 #ifndef SWIG
98       virtual ~TypeCode();
99 #endif
100     protected:
101       const DynType _kind;
102       static const char *KIND_STR_REPR [];
103     };
104
105     class TypeCodeComposed : public TypeCode
106     {
107     protected:
108       TypeCodeComposed(const TypeCodeComposed& other);
109       TypeCodeComposed(DynType kind, const char* repositoryId, const char* name);
110     protected:
111       const std::string _name;
112       const std::string _repoId;
113       std::string _shortName;
114     };
115
116
117 /*! \brief Class for reference objects.
118  *
119  * \ingroup TypeCodes
120  *
121  */
122     class TypeCodeObjref : public TypeCodeComposed
123     {
124       friend class Visitor;
125     public:
126       TypeCodeObjref(const char* repositoryId, const char* name);
127
128       TypeCodeObjref(const char* repositoryId, const char* name, const std::list<TypeCodeObjref *>& ltc);
129
130       TypeCode *clone() const;
131       void putReprAtPlace(char *pt, const char *val, bool deepCpy) const;
132       void destroyZippedAny(char *data) const;
133       AnyPtr getOrBuildAnyFromZippedData(char *data) const;
134       const char * id() const   throw(Exception);
135       const char * name() const throw(Exception);
136       const char * shortName() const;
137       int isA(const char* repositoryId) const throw(Exception);
138       virtual int isA(const TypeCode* tc) const ;
139       virtual int isAdaptable(const TypeCode* tc) const;
140       virtual int isEquivalent(const TypeCode* tc) const;
141     protected:
142 #ifndef SWIG
143       virtual ~TypeCodeObjref();
144 #endif
145       TypeCodeObjref(const TypeCodeObjref& other);
146     private:
147       std::list<TypeCodeObjref *> _listOfBases;
148     };
149
150
151 /*! \brief Class for sequence objects.
152  *
153  * \ingroup TypeCodes
154  *
155  */
156     class TypeCodeSeq: public TypeCodeComposed
157     {
158     public:
159       TypeCodeSeq(const char* repositoryId, const char* name, const TypeCode *content);
160
161       TypeCode *clone() const;
162       void putReprAtPlace(char *pt, const char *val, bool deepCpy) const;
163       void destroyZippedAny(char *data) const;
164       virtual unsigned getSizeInByteOfAnyReprInSeq() const;
165       AnyPtr getOrBuildAnyFromZippedData(char *data) const;
166       const char * id()   const throw(Exception);
167       const char * name() const throw(Exception);
168       const char * shortName() const;
169
170       virtual const TypeCode * contentType() const throw(Exception);
171       virtual int isA(const TypeCode* tc) const ;
172       virtual int isAdaptable(const TypeCode* tc) const;
173       virtual int isEquivalent(const TypeCode* tc) const;
174     protected:
175 #ifndef SWIG
176       virtual ~TypeCodeSeq();
177 #endif
178       TypeCodeSeq(const TypeCodeSeq& tc);
179     private:
180       const TypeCode  *_content;
181     };
182
183 /*! \brief Class for array objects.
184  *
185  * \ingroup TypeCodes
186  *
187  */
188     class TypeCodeArray : public TypeCodeComposed
189     {
190     public:
191       TypeCodeArray(const char* repositoryId, const char* name, const TypeCode *content, unsigned staticLgth);
192       TypeCode *clone() const;
193       void putReprAtPlace(char *pt, const char *val, bool deepCpy) const;
194       void destroyZippedAny(char *data) const;
195       AnyPtr getOrBuildAnyFromZippedData(char *data) const;
196       const char * id()   const throw(Exception);
197       const char * name() const throw(Exception);
198       const char * shortName() const;
199       unsigned getStaticLgth() const;
200
201       virtual const TypeCode * contentType() const throw(Exception);
202       virtual int isA(const TypeCode* tc) const ;
203       virtual int isAdaptable(const TypeCode* tc) const;
204       virtual int isEquivalent(const TypeCode* tc) const;
205       unsigned getSizeInByteOfAnyReprInSeq() const;
206     protected:
207 #ifndef SWIG
208       virtual ~TypeCodeArray();
209 #endif
210       TypeCodeArray(const TypeCodeArray& tc);
211     private:
212       const TypeCode  *_content;
213       const unsigned _staticLgth;
214     };
215
216     class StructAny;
217
218 /*! \brief Class for struct type.
219  *
220  * \ingroup TypeCodes
221  *
222  */
223     class TypeCodeStruct : public TypeCodeComposed
224     {
225       friend class StructAny;//Access to _members attribute.
226     public:
227       TypeCodeStruct(const char* repositoryId, const char* name);
228       TypeCode *clone() const;
229       void putReprAtPlace(char *pt, const char *val, bool deepCpy) const;
230       void destroyZippedAny(char *data) const;
231       AnyPtr getOrBuildAnyFromZippedData(char *data) const;
232       const char * id() const   throw(Exception);
233       const char * name() const throw(Exception);
234       const char * shortName() const;
235       virtual unsigned getSizeInByteOfAnyReprInSeq() const;
236       const TypeCode * contentType() const throw(Exception);
237       virtual int isA(const char* repositoryId) const throw(Exception);
238       virtual int isA(const TypeCode* tc) const ;
239       virtual int isAdaptable(const TypeCode* tc) const;
240       virtual int isEquivalent(const TypeCode* tc) const;
241       //! The only non const method.
242       virtual void addMember(const std::string& name,TypeCode* tc);
243       const TypeCode *getMember(const std::string& name, unsigned& offset) const;
244       int memberCount() const;
245       const char*  memberName(int index) const;
246       TypeCode*  memberType(int index) const;
247     protected:
248 #ifndef SWIG
249       virtual ~TypeCodeStruct();
250 #endif
251       TypeCodeStruct(const TypeCodeStruct& tc);
252     private:
253       std::vector< std::pair<std::string,TypeCode*> > _members;
254     };
255
256   }
257 }
258 #endif