Salome HOME
Bug correction in the medcoupling <-> numpy layer
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingRefCountObject.i
1 // Copyright (C) 2007-2016  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 namespace MEDCoupling
40 {
41   class TimeLabel
42   {
43   public:
44     void declareAsNew() const;
45     virtual void updateTime() const;
46     unsigned int getTimeOfThis() const;
47   protected:
48     ~TimeLabel();
49   };
50 }
51
52 namespace MEDCoupling
53 {
54   enum class DeallocType
55     {
56       C_DEALLOC = 2,
57       CPP_DEALLOC = 3
58     };
59
60   const char *MEDCouplingVersionStr();
61   int MEDCouplingVersion();
62   int MEDCouplingSizeOfVoidStar();
63   bool MEDCouplingByteOrder();
64   const char *MEDCouplingByteOrderStr();
65   bool IsCXX11Compiled();
66   
67   class BigMemoryObject
68   {
69   public:
70     std::size_t getHeapMemorySize() const throw(INTERP_KERNEL::Exception);
71     std::string getHeapMemorySizeStr() const throw(INTERP_KERNEL::Exception);
72     bool isObjectInTheProgeny(const BigMemoryObject *obj) const throw(INTERP_KERNEL::Exception);
73     virtual std::size_t getHeapMemorySizeWithoutChildren() const throw(INTERP_KERNEL::Exception);
74     virtual ~BigMemoryObject();
75     %extend
76     {
77       virtual PyObject *getDirectChildren() const throw(INTERP_KERNEL::Exception)
78       {
79         std::vector<const BigMemoryObject *> c(self->getDirectChildren());
80         PyObject *ret(PyList_New(c.size()));
81         for(std::size_t i=0;i<c.size();i++)
82           PyList_SetItem(ret,i,SWIG_NewPointerObj(SWIG_as_voidptr(c[i]),SWIGTYPE_p_MEDCoupling__BigMemoryObject, 0 | 0 ));
83         return ret;
84       }
85
86       PyObject *getAllTheProgeny() const throw(INTERP_KERNEL::Exception)
87       {
88         std::vector<const BigMemoryObject *> c(self->getAllTheProgeny());
89         PyObject *ret(PyList_New(c.size()));
90         for(std::size_t i=0;i<c.size();i++)
91           PyList_SetItem(ret,i,SWIG_NewPointerObj(SWIG_as_voidptr(c[i]),SWIGTYPE_p_MEDCoupling__BigMemoryObject, 0 | 0 ));
92         return ret;
93       }
94
95       static std::size_t GetHeapMemorySizeOfObjs(PyObject *objs) throw(INTERP_KERNEL::Exception)
96       {
97         std::vector<const BigMemoryObject *> cppObjs;
98         convertFromPyObjVectorOfObj<const MEDCoupling::BigMemoryObject *>(objs,SWIGTYPE_p_MEDCoupling__BigMemoryObject,"BigMemoryObject",cppObjs);
99         return BigMemoryObject::GetHeapMemorySizeOfObjs(cppObjs);
100       }
101     }
102   };
103   
104   class RefCountObjectOnly
105   {
106   public:
107     bool decrRef() const;
108     void incrRef() const;
109     int getRCValue() const;
110   protected:
111     ~RefCountObjectOnly();
112   };
113
114   class RefCountObject : public RefCountObjectOnly, public BigMemoryObject
115   {
116   protected:
117     ~RefCountObject();
118   };
119
120   class GlobalDict
121   {
122   public:
123     static GlobalDict *GetInstance() throw(INTERP_KERNEL::Exception);
124     bool hasKey(const std::string& key) const throw(INTERP_KERNEL::Exception);
125     std::string value(const std::string& key) const throw(INTERP_KERNEL::Exception);
126     std::vector<std::string> keys() const throw(INTERP_KERNEL::Exception);
127     void erase(const std::string& key) throw(INTERP_KERNEL::Exception);
128     void clear() throw(INTERP_KERNEL::Exception);
129     void setKeyValue(const std::string& key, const std::string& value) throw(INTERP_KERNEL::Exception);
130     void setKeyValueForce(const std::string& key, const std::string& value) throw(INTERP_KERNEL::Exception);
131   private:
132     GlobalDict();
133   public:
134     %extend
135     {
136       std::string __str__() const
137       {
138         return self->printSelf();
139       }
140     }
141   };
142 }
143
144 %inline
145 {
146   PyObject *MEDCouplingVersionMajMinRel()
147   {
148     int tmp0=0,tmp1=0,tmp2=0;
149     MEDCouplingVersionMajMinRel(tmp0,tmp1,tmp2);
150     PyObject *res = PyList_New(3);
151     PyList_SetItem(res,0,SWIG_From_int(tmp0));
152     PyList_SetItem(res,1,SWIG_From_int(tmp1));
153     PyList_SetItem(res,2,SWIG_From_int(tmp2));
154     return res;
155   }
156
157   bool MEDCouplingHasNumPyBindings()
158   {
159 #ifdef WITH_NUMPY
160     return true;
161 #else
162     return false;
163 #endif
164   }
165
166   bool MEDCouplingHasSciPyBindings()
167   {
168 #ifdef WITH_SCIPY
169     return true;
170 #else
171     return false;
172 #endif
173   }
174
175   std::string MEDCouplingCompletionScript() throw(INTERP_KERNEL::Exception)
176   {
177     static const char script[]="import rlcompleter,readline\nreadline.parse_and_bind('tab:complete')";
178     std::ostringstream oss; oss << "MEDCouplingCompletionScript : error when trying to activate completion ! readline not present ?\nScript is :\n" << script;
179     if(PyRun_SimpleString(script)!=0)
180       throw INTERP_KERNEL::Exception(oss.str().c_str());
181     return std::string(script);
182   }
183 }
184
185 %pythoncode %{
186 def INTERPKERNELExceptionReduceFunct(a,b):
187     ret=InterpKernelException.__new__(a)
188     ret.__init__(*b)
189     return ret
190 def INTERPKERNELExceptionReduce(self):
191     return INTERPKERNELExceptionReduceFunct,(InterpKernelException,(self.what(),))
192 %}