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