Salome HOME
8c93d825672c6cdde6418a4529166675404784ba
[modules/yacs.git] / src / runtime / CppContainer.hxx
1 // Copyright (C) 2006-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
20 #ifndef __Cpp_CONTAINER_HXX__
21 #define __Cpp_CONTAINER_HXX__
22
23 #include <map>
24 #include <string>
25 #include "Mutex.hxx"
26 #include "Container.hxx"
27 #include "CppComponent.hxx"
28
29 #ifdef WIN32
30 #include <windows.h>
31 #endif
32
33 namespace YACS
34 {
35   namespace ENGINE
36   {
37   
38     struct LocalLibrary {
39       
40 #if defined( WIN32 )
41       HMODULE handle;
42 #else
43       void * handle;
44 #endif
45       
46       InitFunction initHandle;
47       RunFunction runHandle;
48       PingFunction pingHandle;
49       TerminateFunction terminateHandle;
50       
51 #if defined( WIN32 )
52       LocalLibrary(HMODULE h, InitFunction i, RunFunction r, 
53 #else
54       LocalLibrary(void *h, InitFunction i, RunFunction r, 
55 #endif
56                             PingFunction p, TerminateFunction t) 
57         : handle(h), initHandle(i), runHandle(r), 
58                            pingHandle(p), terminateHandle(t) {}
59       LocalLibrary() 
60         : handle(NULL), initHandle(NULL), runHandle(NULL), 
61                               pingHandle(NULL), terminateHandle(NULL) {}
62                               
63       bool good() {
64         return (handle != NULL)    && (initHandle != NULL) && 
65                (runHandle != NULL) && (terminateHandle != NULL);
66       }
67         
68     };
69
70     // Local container singleton
71     class LocalContainer {
72
73       friend class CppComponent;
74
75     public:
76
77       static YACS::ENGINE::LocalContainer * get();
78       void destroy();
79       LocalLibrary loadComponentLibrary(const std::string &, const char * prefix = NULL, 
80                                         bool forcedLoad = false);
81       CppComponent * createComponentInstance(const char * componentName);
82       void createInternalInstance(const char * componentName, 
83                                   void *& obj, RunFunction &r, TerminateFunction &t);
84      void unLoadComponentLibrary(const std::string & aCompName);
85       void unregisterComponentInstance(CppComponent * C);
86                   
87     protected:
88
89       LocalContainer();
90       virtual ~LocalContainer();
91
92       YACS::BASES::Mutex _instance_mapMutex, _library_mapMutex;
93       static std::map<std::string, LocalLibrary > _library_map; // libraries, loaded
94       static std::multimap<std::string, CppComponent *>  _instance_map;
95       
96       
97     private:
98       static LocalContainer *_singleton;
99       
100
101     };
102
103     class CppContainer : public Container
104     {
105       friend class CppComponent;
106       friend class LocalContainer;
107     public:
108       CppContainer();
109       virtual ~CppContainer();
110       std::string getKind() const;
111       bool isAlreadyStarted(const Task *askingNode) const;
112       void start(const Task *askingNode);
113       void shutdown(int level);
114       std::string getPlacementId(const Task *askingNode) const;
115       std::string getFullPlacementId(const Task *askingNode) const;
116       YACS::ENGINE::Container *clone() const;
117       Container *cloneAlways() const;
118       void lock();
119       void unLock();
120
121       void checkCapabilityToDealWith(const ComponentInstance *inst) const;
122       bool loadComponentLibrary(const std::string & componentName);
123       CppComponent * createComponentInstance(const std::string & componentName);
124       void createInternalInstance(const std::string & componentName, 
125                                   void *& obj, RunFunction &r, TerminateFunction &t);
126       void unregisterComponentInstance(CppComponent * C);
127       //
128       void setProperty(const std::string& name,const std::string& value) { }
129       std::string getProperty(const std::string& name) const { return std::string(); }
130       void clearProperties() { }
131       void addComponentName(const std::string& name) { }
132       std::map<std::string,std::string> getProperties() const { return std::map<std::string,std::string>(); }
133       std::map<std::string,std::string> getResourceProperties(const std::string& name) const { return std::map<std::string,std::string>(); }
134       //
135     public:
136       static char KIND[];
137     protected:
138       YACS::BASES::Mutex _mutex;
139       LocalContainer * _trueCont;
140
141     };
142   };
143 };
144
145 #endif
146