Salome HOME
0cef073102c6d9884caa1b1c1665558c38779535
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingRefCountObject.i
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20
21 namespace INTERP_KERNEL
22 {
23   class Exception
24   {
25   public:
26     Exception(const char* what);
27     ~Exception() throw ();
28     const char *what() const throw ();
29     %extend
30     {
31       std::string __str__() const
32       {
33         return std::string(self->what());
34       }
35     }
36   };
37 }
38
39 /*
40  * ABN: Install default exception handler: this catches all INTERP_KERNEL::Exception (even when no
41  * except declaration was added to the function declaration) and propagates it to the Python level.
42  */
43 %exception {
44   try {
45     $action
46   }
47   catch (INTERP_KERNEL::Exception& _e) {
48     // Reraise with SWIG_Python_Raise
49     SWIG_Python_Raise(SWIG_NewPointerObj((new INTERP_KERNEL::Exception(static_cast< const INTERP_KERNEL::Exception& >(_e))),SWIGTYPE_p_INTERP_KERNEL__Exception,SWIG_POINTER_OWN), "INTERP_KERNEL::Exception", SWIGTYPE_p_INTERP_KERNEL__Exception);
50     SWIG_fail;
51   }
52 }
53
54 namespace MEDCoupling
55 {
56   class TimeLabel
57   {
58   public:
59     void declareAsNew() const;
60     virtual void updateTime() const;
61     unsigned int getTimeOfThis() const;
62   protected:
63     ~TimeLabel();
64   };
65 }
66
67 namespace MEDCoupling
68 {
69   enum class DeallocType
70     {
71       C_DEALLOC = 2,
72       CPP_DEALLOC = 3
73     };
74
75   const char *MEDCouplingVersionStr();
76   int MEDCouplingVersion();
77   int MEDCouplingSizeOfVoidStar();
78   int MEDCouplingSizeOfIDs();
79   bool MEDCouplingByteOrder();
80   const char *MEDCouplingByteOrderStr();
81   bool IsCXX11Compiled();
82   
83   class BigMemoryObject
84   {
85   public:
86     std::size_t getHeapMemorySize() const;
87     std::string getHeapMemorySizeStr() const;
88     bool isObjectInTheProgeny(const BigMemoryObject *obj) const;
89     std::size_t getHeapMemorySizeWithoutChildren() const;
90     std::string debugHeapMemorySize() const;
91     std::string getClassName() const;
92     virtual ~BigMemoryObject();
93     %extend
94     {
95       virtual PyObject *getDirectChildren() const
96       {
97         std::vector<const BigMemoryObject *> c(self->getDirectChildren());
98         PyObject *ret(PyList_New(c.size()));
99         for(std::size_t i=0;i<c.size();i++)
100           PyList_SetItem(ret,i,SWIG_NewPointerObj(SWIG_as_voidptr(c[i]),SWIGTYPE_p_MEDCoupling__BigMemoryObject, 0 | 0 ));
101         return ret;
102       }
103
104       PyObject *getAllTheProgeny() const
105       {
106         std::vector<const BigMemoryObject *> c(self->getAllTheProgeny());
107         PyObject *ret(PyList_New(c.size()));
108         for(std::size_t i=0;i<c.size();i++)
109           PyList_SetItem(ret,i,SWIG_NewPointerObj(SWIG_as_voidptr(c[i]),SWIGTYPE_p_MEDCoupling__BigMemoryObject, 0 | 0 ));
110         return ret;
111       }
112
113       static std::size_t GetHeapMemorySizeOfObjs(PyObject *objs)
114       {
115         std::vector<const BigMemoryObject *> cppObjs;
116         convertFromPyObjVectorOfObj<const MEDCoupling::BigMemoryObject *>(objs,SWIGTYPE_p_MEDCoupling__BigMemoryObject,"BigMemoryObject",cppObjs);
117         return BigMemoryObject::GetHeapMemorySizeOfObjs(cppObjs);
118       }
119     }
120   };
121   
122   class RefCountObjectOnly
123   {
124   public:
125     bool decrRef() const;
126     void incrRef() const;
127     int getRCValue() const;
128   protected:
129     ~RefCountObjectOnly();
130   };
131
132   class RefCountObject : public RefCountObjectOnly, public BigMemoryObject
133   {
134   protected:
135     ~RefCountObject();
136   };
137
138   class GlobalDict
139   {
140   public:
141     static GlobalDict *GetInstance();
142     bool hasKey(const std::string& key) const;
143     std::string value(const std::string& key) const;
144     std::vector<std::string> keys() const;
145     void erase(const std::string& key);
146     void clear();
147     void setKeyValue(const std::string& key, const std::string& value);
148     void setKeyValueForce(const std::string& key, const std::string& value);
149   private:
150     GlobalDict();
151   public:
152     %extend
153     {
154       std::string __str__() const
155       {
156         return self->printSelf();
157       }
158     }
159   };
160 }
161
162 %inline
163 {
164   PyObject *MEDCouplingVersionMajMinRel()
165   {
166     int tmp0=0,tmp1=0,tmp2=0;
167     MEDCouplingVersionMajMinRel(tmp0,tmp1,tmp2);
168     PyObject *res = PyList_New(3);
169     PyList_SetItem(res,0,SWIG_From_int(tmp0));
170     PyList_SetItem(res,1,SWIG_From_int(tmp1));
171     PyList_SetItem(res,2,SWIG_From_int(tmp2));
172     return res;
173   }
174
175   bool MEDCouplingHasNumPyBindings()
176   {
177 #ifdef WITH_NUMPY
178     return true;
179 #else
180     return false;
181 #endif
182   }
183
184   bool MEDCouplingHasSciPyBindings()
185   {
186 #ifdef WITH_SCIPY
187     return true;
188 #else
189     return false;
190 #endif
191   }
192   
193   bool MEDCouplingUse64BitIDs()
194   {
195 #ifndef MEDCOUPLING_USE_64BIT_IDS
196     return false;
197 #else
198     return true;
199 #endif
200   }
201
202   std::string MEDCouplingCompletionScript()
203   {
204     static const char script[]="import rlcompleter,readline\nreadline.parse_and_bind('tab:complete')";
205     std::ostringstream oss; oss << "MEDCouplingCompletionScript : error when trying to activate completion ! readline not present ?\nScript is :\n" << script;
206     if(PyRun_SimpleString(script)!=0)
207       throw INTERP_KERNEL::Exception(oss.str().c_str());
208     return std::string(script);
209   }
210 }
211
212 %pythoncode %{
213 def INTERPKERNELExceptionReduceFunct(a,b):
214     ret=InterpKernelException.__new__(a)
215     ret.__init__(*b)
216     return ret
217 def INTERPKERNELExceptionReduce(self):
218     return INTERPKERNELExceptionReduceFunct,(InterpKernelException,(self.what(),))
219 %}