Clay Library  1.0.1
 All Classes Functions Variables Groups
clay.h
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of "clay" library.
4 It is licensed under the terms of the MIT license.
5 For the latest info, see http://libclay.sourceforge.net
6 
7 Copyright (c) 2010-2013 Lin Jia Jun (Joe Lam)
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __CLAY_H_INCLUDED__
30 #define __CLAY_H_INCLUDED__
31 
32 #define CLAY_SVC_FUNC(func) (clay::core::CLAY_SVC_CLASS_FUNC)(&func)
33 
34 #include <map>
35 #include <string>
36 
37 namespace clay
38 {
39 
40 namespace core
41 {
42 
44 class ClayServiceProvider;
45 
46 typedef int (*CLAY_SVC_STATIC_FUNC)(void*);
47 
48 typedef int (ClayServiceProvider::*CLAY_SVC_CLASS_FUNC)(void*);
49 
50 typedef std::map<int, CLAY_SVC_CLASS_FUNC> ClayServiceIntMap;
51 typedef std::map<std::string, CLAY_SVC_CLASS_FUNC> ClayServiceStrMap;
52 
53 typedef std::map<int, ClayServiceProvider*> ClayServiceProviderIntMap;
54 typedef std::map<std::string, ClayServiceProvider*> ClayServiceProviderStrMap;
55 
58 {
59 protected:
60 
62  int m_code;
63 
65  std::string m_name;
66 
68  ClayServiceIntMap m_svcintmap;
69 
71  ClayServiceStrMap m_svcstrmap;
72 
74  void ClearServices();
75 
81  int RegisterService(int iSvcCode, CLAY_SVC_CLASS_FUNC pSvcFunc);
82 
88  int RegisterService(const std::string& sSvcName, CLAY_SVC_CLASS_FUNC pSvcFunc);
89 
94  int UnregisterService(int iSvcCode);
95 
100  int UnregisterService(const std::string& sSvcName);
101 
105  int GetServiceCount();
106 
108  virtual void SetupServices();
109 
114  int GetVersion(void* pParam);
115 
116 public:
117 
121  int GetCode();
122 
126  std::string GetName();
127 
132  bool ServiceExists(int iSvcCode);
133 
138  bool ServiceExists(const std::string& sSvcName);
139 
144  CLAY_SVC_CLASS_FUNC GetService(int iSvcCode);
145 
150  CLAY_SVC_CLASS_FUNC GetService(const std::string& sSvcName);
151 
157  virtual int DoService(int iSvcCode, void* pParam);
158 
164  virtual int DoService(const std::string& sSvcName, void* pParam);
165 
169  explicit ClayServiceProvider(int iCode);
170 
174  explicit ClayServiceProvider(const std::string& sName);
175 
180  ClayServiceProvider(int iCode, const std::string& sName);
181 
183  virtual ~ClayServiceProvider();
184 
185 };
186 
189 {
190 
191 private:
192 
193  ClayServiceProviderIntMap m_spintmap;
194  ClayServiceProviderStrMap m_spstrmap;
195 
196  ClayServiceAgent* m_external;
197 
198 public:
199 
201  ~ClayServiceAgent();
202 
207  int RegSp(ClayServiceProvider* pServiceProvider);
208 
213  int UnregSp(int iSpCode);
214 
219  int UnregSp(const std::string& sSpName);
220 
225  ClayServiceProvider* GetSp(int iSpCode);
226 
231  ClayServiceProvider* GetSp(const std::string& sSpName);
232 
236  int GetSpCount();
237 
244  int CallService(int iSpCode, int iSvcCode, void* pParam);
245 
252  int CallService(int iSpCode, const std::string& sSvcName, void* pParam);
253 
260  int CallService(const std::string& sSpName, int iSvcCode, void* pParam);
261 
268  int CallService(const std::string& sSpName, const std::string& sSvcName, void* pParam);
269 
274  bool SpExists(int iSpCode);
275 
280  bool SpExists(const std::string& sSpName);
281 
287  bool SvcExists(int iSpCode, int iSvcCode);
288 
294  bool SvcExists(int iSpCode, const std::string& sSvcName);
295 
301  bool SvcExists(const std::string& sSpName, int iSvcCode);
302 
308  bool SvcExists(const std::string& sSpName, const std::string& sSvcName);
309 
311  void Clear();
312 
316  void SetExternalAgent(ClayServiceAgent* pAgent);
317 
322  {
323  // Meyer's Singleton ...
324  static ClayServiceAgent instance;
325 
326  ClayServiceAgent* pAgent = instance.m_external;
327 
328  if(pAgent != NULL) return pAgent;
329  else return &instance;
330  }
331 
332 
333 };
334 
335 
336 } // end of clay::core
337 
338 
339 namespace lib
340 {
341 
346 std::string GetTimeStr();
350 
354 void Log(const char* sMsg);
355 
360 
364 void SetAgent(void* pAgent);
365 
370 int RegisterSp(clay::core::ClayServiceProvider* pServiceProvider);
371 
375 int GetSpCount();
376 
382 
387 clay::core::ClayServiceProvider* GetProvider(const std::string& sSpName);
388 
393 bool ProviderExists(int iSpCode);
394 
399 bool ProviderExists(const std::string& sSpName);
400 
406 bool ServiceExists(int iSpCode, int iSvcCode);
407 
413 bool ServiceExists(int iSpCode, const std::string& sSvcName);
414 
420 bool ServiceExists(const std::string& sSpName, int iSvcCode);
421 
427 bool ServiceExists(const std::string& sSpName, const std::string& sSvcName);
428 
435 int CallService(int iSpCode, int iSvcCode, void* pParam = NULL);
436 
443 int CallService(int iSpCode, const std::string& sSvcName, void* pParam = NULL);
444 
451 int CallService(const std::string& sSpName, int iSvcCode, void* pParam = NULL);
452 
459 int CallService(const std::string& sSpName, const std::string& sSvcName, void* pParam = NULL);
460 
462 void Clear();
463 
466 } // end of clay::lib
467 
468 } // end of clay
469 
470 
471 #endif // __CLAY_H_INCLUDED__