Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / runtime / Test / TestComponent.cxx
1 // Copyright (C) 2006-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
20 #include "Any.hxx"
21 #include <iostream>
22 #include <cstring>
23 #include <cmath>
24 #include "TestComponent.hxx"
25
26
27 extern "C" void * __init()
28 {
29         TestComponent * obj = new TestComponent();
30         obj->info.code = 0;
31         obj->info.message = "";
32         return obj;
33 }
34  
35 extern "C" void __terminate(void **pObj)
36 {
37         TestComponent * obj = * (TestComponent **) pObj;
38         delete obj;
39         *pObj = NULL;
40 }
41
42 extern "C" void __ping()
43 {
44         std::cerr << "ping TestComponent" << std::endl;
45 }
46     
47 extern "C" void __run(void *vObj, const char *service, int nIn, int nOut, 
48                          YACS::ENGINE::Any **In, YACS::ENGINE::Any **Out, returnInfo * r)
49 {
50         TestComponent * obj = (TestComponent *) vObj;
51         
52         if (obj == NULL) {
53                 r->code = -1;
54                 r->message = "TestComponent has not been initialized";
55                 return;
56         }
57         
58         obj->info.message = "";
59         obj->info.code = 0;
60         
61         Out[0] = NULL;
62         if (std::strncmp(service, "f", 1) == 0) 
63         {
64                 double _arg0 = In[0]->getDoubleValue();
65                 double _res = obj->f(_arg0);
66                 if (obj->info.code == 0)
67                   Out[0] = YACS::ENGINE::AtomAny::New(_res);
68         }
69         else
70         {
71                 obj->info.code = 1;
72                 obj->info.message = "service ";
73                 obj->info.message += service;
74                 obj->info.message += " doesn't exist in TestComponent";
75         }
76         *r = obj->info;
77 }
78
79 double TestComponent::f(double x)
80 {
81         double y;
82         
83         if (x >= 0.0)
84         {
85           y = std::sqrt(x);
86         }
87         else {
88           y = 0.0;
89           info.message = "TestComponent::f : argument must be positive or null";
90           info.code = 2;
91         }
92         return y;
93 }
94
95 double TestComponent::g(int n, double x)
96 {
97         double y;
98         y = std::pow(x, n);
99         return y;
100 }