Salome HOME
Copyright update 2022
[modules/yacs.git] / src / evalyfx / YACSEvalSeqAny.cxx
1 // Copyright (C) 2012-2022  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 (EDF R&D)
20
21 #include "YACSEvalSeqAny.hxx"
22 #include "YACSEvalPort.hxx"
23 #include "Exception.hxx"
24
25 #include "PythonPorts.hxx"
26
27 YACSEvalSeqAny *YACSEvalSeqAny::BuildEmptyFromType(const std::string& dataType)
28 {
29   if(dataType==YACSEvalAnyDouble::TYPE_REPR)
30     return new YACSEvalSeqAnyDouble;
31   else if(dataType==YACSEvalAnyInt::TYPE_REPR)
32     return new YACSEvalSeqAnyInt;
33   else
34     throw YACS::Exception("YACSEvalSeqAny::BuildEmptyFromType : Only int and double are actualy managed !");
35 }
36
37 YACSEvalSeqAnyDouble::YACSEvalSeqAnyDouble(const std::vector<double>& arr):_arr(0)
38 {
39   std::vector<double> *zeArr(new std::vector<double>(arr));
40   _arr=YACSEvalSeqAnyInternal<double>::New(zeArr);
41 }
42
43 YACSEvalSeqAnyDouble::YACSEvalSeqAnyDouble(const YACSEvalSeqAnyDouble& other):_arr(other._arr)
44 {
45   if(_arr)
46     _arr->incrRef();
47 }
48
49 YACSEvalSeqAnyDouble::YACSEvalSeqAnyDouble():_arr(YACSEvalSeqAnyInternal<double>::New(new std::vector<double>(0)))
50 {
51 }
52
53 std::string YACSEvalSeqAnyDouble::getTypeOfConstitutingElements() const
54 {
55   return std::string(YACSEvalAnyDouble::TYPE_REPR);
56 }
57
58 YACSEvalSeqAnyDouble *YACSEvalSeqAnyDouble::copy() const
59 {
60   return new YACSEvalSeqAnyDouble(*this);
61 }
62
63 void YACSEvalSeqAnyDouble::initialize(YACS::ENGINE::InputPyPort *p) const
64 {
65   std::size_t sz(size());
66   PyObject *ob(PyList_New(sz));
67   for(std::size_t i=0;i<sz;i++)
68     {
69       PyList_SetItem(ob,i,PyFloat_FromDouble((*_arr)[i]));
70     }
71   p->put(ob);
72   p->exSaveInit();
73   Py_XDECREF(ob);
74 }
75
76 std::vector<double> *YACSEvalSeqAnyDouble::getInternal() const
77 {
78   if(!_arr)
79     throw YACS::Exception("YACSEvalSeqAnyDouble::getInternal : null internal ref !");
80   return _arr->getInternal();
81 }
82
83 YACSEvalSeqAnyInt::YACSEvalSeqAnyInt(const std::vector<int>& arr):_arr(0)
84 {
85   std::vector<int> *zeArr(new std::vector<int>(arr));
86   _arr=YACSEvalSeqAnyInternal<int>::New(zeArr);
87 }
88
89 YACSEvalSeqAnyInt::YACSEvalSeqAnyInt(const YACSEvalSeqAnyInt& other):_arr(other._arr)
90 {
91   if(_arr)
92     _arr->incrRef();
93 }
94
95 YACSEvalSeqAnyInt::YACSEvalSeqAnyInt():_arr(YACSEvalSeqAnyInternal<int>::New(new std::vector<int>(0)))
96 {
97 }
98
99 std::string YACSEvalSeqAnyInt::getTypeOfConstitutingElements() const
100 {
101   return std::string(YACSEvalAnyInt::TYPE_REPR);
102 }
103
104 YACSEvalSeqAnyInt *YACSEvalSeqAnyInt::copy() const
105 {
106   return new YACSEvalSeqAnyInt(*this);
107 }
108
109 void YACSEvalSeqAnyInt::initialize(YACS::ENGINE::InputPyPort *p) const
110 {
111   std::size_t sz(size());
112   PyObject *ob(PyList_New(sz));
113   for(std::size_t i=0;i<sz;i++)
114     {
115       PyList_SetItem(ob,i,PyLong_FromLong((*_arr)[i]));
116     }
117   p->put(ob);
118   p->exSaveInit();
119   Py_XDECREF(ob);
120 }
121
122 std::vector<int> *YACSEvalSeqAnyInt::getInternal() const
123 {
124   if(!_arr)
125     throw YACS::Exception("YACSEvalSeqAnyInt::getInternal : null internal ref !");
126   return _arr->getInternal();
127 }