OpenNI 1.5.7
XnModuleCppRegistratration.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2012 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *****************************************************************************/
21 #ifndef __XN_MODULE_CPP_REGISTRATION_H__
22 #define __XN_MODULE_CPP_REGISTRATION_H__
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include "XnModuleCppInterface.h"
28 #include <XnUtils.h>
29 
30 using namespace xn;
31 
32 //---------------------------------------------------------------------------
33 // Internal Macros
34 //---------------------------------------------------------------------------
36 #define _XN_MODULE_INST g_pTheModule
37 
38 #define _CONCAT(a,b) a##b
39 
40 inline XnModuleNodeHandle __ModuleNodeToHandle(xn::ModuleProductionNode* pNode)
41 {
42  return ((XnModuleNodeHandle)pNode);
43 }
44 
45 #define __XN_EXPORT_NODE_COMMON(ExportedClass, ExportedName, Type) \
46  \
47  static ExportedClass* ExportedName = new ExportedClass(); \
48  \
49  void XN_CALLBACK_TYPE _CONCAT(ExportedClass,GetDescription)(XnProductionNodeDescription* pDescription) \
50  { \
51  ExportedName->GetDescription(pDescription); \
52  } \
53  \
54  XnStatus XN_CALLBACK_TYPE _CONCAT(ExportedClass,EnumerateProductionTrees) \
55  (XnContext* pContext, XnNodeInfoList* pTreesList, XnEnumerationErrors* pErrors) \
56  { \
57  Context context(pContext); \
58  NodeInfoList list(pTreesList); \
59  EnumerationErrors errors(pErrors); \
60  return ExportedName->EnumerateProductionTrees(context, list, pErrors == NULL ? NULL : &errors); \
61  } \
62  \
63  XnStatus XN_CALLBACK_TYPE _CONCAT(ExportedClass,Create)(XnContext* pContext, \
64  const XnChar* strInstanceName, \
65  const XnChar* strCreationInfo, \
66  XnNodeInfoList* pNeededTrees, \
67  const XnChar* strConfigurationDir, \
68  XnModuleNodeHandle* phInstance) \
69  { \
70  xn::NodeInfoList* pNeeded = NULL; \
71  if (pNeededTrees != NULL) \
72  { \
73  pNeeded = XN_NEW(xn::NodeInfoList, pNeededTrees); \
74  } \
75  ModuleProductionNode* pNode; \
76  Context context(pContext); \
77  XnStatus nRetVal = ExportedName->Create(context, strInstanceName, strCreationInfo, \
78  pNeeded, strConfigurationDir, &pNode); \
79  if (nRetVal != XN_STATUS_OK) \
80  { \
81  XN_DELETE(pNeeded); \
82  return (nRetVal); \
83  } \
84  *phInstance = __ModuleNodeToHandle(pNode); \
85  XN_DELETE(pNeeded); \
86  return (XN_STATUS_OK); \
87  } \
88  \
89  void XN_CALLBACK_TYPE _CONCAT(ExportedClass,Destroy)(XnModuleNodeHandle hInstance) \
90  { \
91  ModuleProductionNode* pNode = (ModuleProductionNode*)hInstance; \
92  ExportedName->Destroy(pNode); \
93  } \
94  \
95  void XN_CALLBACK_TYPE _CONCAT(ExportedClass,GetExportedInterface)( \
96  XnModuleExportedProductionNodeInterface* pInterface) \
97  { \
98  pInterface->GetDescription = _CONCAT(ExportedClass,GetDescription); \
99  pInterface->EnumerateProductionTrees = _CONCAT(ExportedClass,EnumerateProductionTrees); \
100  pInterface->Create = _CONCAT(ExportedClass,Create); \
101  pInterface->Destroy = _CONCAT(ExportedClass,Destroy); \
102  pInterface->GetInterface.General = __ModuleGetGetInterfaceFunc(Type); \
103  } \
104  \
105  static XnStatus _CONCAT(ExportedClass,RegisterResult) = \
106  _XN_MODULE_INST->AddExportedNode(_CONCAT(ExportedClass,GetExportedInterface));
107 
108 #define _XN_EXPORT_NODE_COMMON(ExportedClass, Type) \
109  __XN_EXPORT_NODE_COMMON(ExportedClass, _g_##ExportedClass, Type)
110 
111 //---------------------------------------------------------------------------
112 // Forward Declarations
113 //---------------------------------------------------------------------------
114 
115 void XN_CALLBACK_TYPE __ModuleGetProductionNodeInterface(XnModuleProductionNodeInterface* pInterface);
116 void XN_CALLBACK_TYPE __ModuleGetDeviceInterface(XnModuleDeviceInterface* pInterface);
117 void XN_CALLBACK_TYPE __ModuleGetGeneratorInterface(XnModuleGeneratorInterface* pInterface);
118 void XN_CALLBACK_TYPE __ModuleGetMapGeneratorInterface(XnModuleMapGeneratorInterface* pInterface);
119 void XN_CALLBACK_TYPE __ModuleGetDepthGeneratorInterface(XnModuleDepthGeneratorInterface* pInterface);
120 void XN_CALLBACK_TYPE __ModuleGetImageGeneratorInterface(XnModuleImageGeneratorInterface* pInterface);
121 void XN_CALLBACK_TYPE __ModuleGetIRGeneratorInterface(XnModuleIRGeneratorInterface* pInterface);
122 void XN_CALLBACK_TYPE __ModuleGetUserGeneratorInterface(XnModuleUserGeneratorInterface* pInterface);
123 void XN_CALLBACK_TYPE __ModuleGetHandsGeneratorInterface(XnModuleHandsGeneratorInterface* pInterface);
124 void XN_CALLBACK_TYPE __ModuleGetGestureGeneratorInterface(XnModuleGestureGeneratorInterface* pInterface);
125 void XN_CALLBACK_TYPE __ModuleGetSceneAnalyzerInterface(XnModuleSceneAnalyzerInterface* pInterface);
126 void XN_CALLBACK_TYPE __ModuleGetAudioGeneratorInterface(XnModuleAudioGeneratorInterface* pInterface);
127 void XN_CALLBACK_TYPE __ModuleGetRecorderInterface(XnModuleRecorderInterface* pInterface);
128 void XN_CALLBACK_TYPE __ModuleGetPlayerInterface(XnModulePlayerInterface* pInterface);
129 void XN_CALLBACK_TYPE __ModuleGetCodecInterface(XnModuleCodecInterface* pInterface);
130 void XN_CALLBACK_TYPE __ModuleGetScriptNodeInterface(XnModuleScriptNodeInterface* pInterface);
131 
132 //---------------------------------------------------------------------------
133 // Utility Macros
134 //---------------------------------------------------------------------------
135 
136 typedef void (XN_CALLBACK_TYPE *GetInterfaceFuncPtr)(void* pInterface);
137 
138 static GetInterfaceFuncPtr __ModuleGetGetInterfaceFunc(XnProductionNodeType type)
139 {
140  // start with concrete type
142  return (GetInterfaceFuncPtr)__ModuleGetDeviceInterface;
143  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_DEPTH))
144  return (GetInterfaceFuncPtr)__ModuleGetDepthGeneratorInterface;
145  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_IMAGE))
146  return (GetInterfaceFuncPtr)__ModuleGetImageGeneratorInterface;
147  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_IR))
148  return (GetInterfaceFuncPtr)__ModuleGetIRGeneratorInterface;
149  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_USER))
150  return (GetInterfaceFuncPtr)__ModuleGetUserGeneratorInterface;
152  return (GetInterfaceFuncPtr)__ModuleGetGestureGeneratorInterface;
153  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_SCENE))
154  return (GetInterfaceFuncPtr)__ModuleGetSceneAnalyzerInterface;
155  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_AUDIO))
156  return (GetInterfaceFuncPtr)__ModuleGetAudioGeneratorInterface;
158  return (GetInterfaceFuncPtr)__ModuleGetRecorderInterface;
160  return (GetInterfaceFuncPtr)__ModuleGetPlayerInterface;
161  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_HANDS))
162  return (GetInterfaceFuncPtr)__ModuleGetHandsGeneratorInterface;
163  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_CODEC))
164  return (GetInterfaceFuncPtr)__ModuleGetCodecInterface;
166  return (GetInterfaceFuncPtr)__ModuleGetScriptNodeInterface;
167  // and continue with abstract ones
169  return (GetInterfaceFuncPtr)__ModuleGetMapGeneratorInterface;
171  return (GetInterfaceFuncPtr)__ModuleGetGeneratorInterface;
173  return (GetInterfaceFuncPtr)__ModuleGetProductionNodeInterface;
174 
175  // unknown
176  XN_ASSERT(FALSE);
177  return NULL;
178 }
179 
181 #if XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
182  #define XN_EXPORT_MODULE(ModuleClass) \
183  \
184  ModuleClass __moduleInstance; \
185  Module* _XN_MODULE_INST = &__moduleInstance;
186 #else
187  #define XN_EXPORT_MODULE(ModuleClass) \
188  \
189  static ModuleClass __moduleInstance; \
190  static Module* _XN_MODULE_INST = &__moduleInstance;
191 #endif
192 
194 #define XN_EXPORT_NODE(ExportedClass, nodeType) \
195  _XN_EXPORT_NODE_COMMON(ExportedClass, nodeType)
196 
198 #define XN_EXPORT_DEVICE(ExportedClass) \
199  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_DEVICE)
200 
202 #define XN_EXPORT_DEPTH(ExportedClass) \
203  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_DEPTH)
204 
206 #define XN_EXPORT_IMAGE(ExportedClass) \
207  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_IMAGE)
208 
210 #define XN_EXPORT_IR(ExportedClass) \
211  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_IR)
212 
213 #define XN_EXPORT_USER(ExportedClass) \
214  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_USER)
215 
216 #define XN_EXPORT_HANDS(ExportedClass) \
217  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_HANDS)
218 
219 #define XN_EXPORT_GESTURE(ExportedClass) \
220  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_GESTURE)
221 
222 #define XN_EXPORT_SCENE(ExportedClass) \
223  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_SCENE)
224 
226 #define XN_EXPORT_AUDIO(ExportedClass) \
227  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_AUDIO)
228 
230 #define XN_EXPORT_RECORDER(ExportedClass) \
231  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_RECORDER)
232 
234 #define XN_EXPORT_PLAYER(ExportedClass) \
235  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_PLAYER)
236 
237 #define XN_EXPORT_CODEC(ExportedClass) \
238  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_CODEC)
239 
240 #define XN_EXPORT_SCRIPT(ExportedClass) \
241  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_SCRIPT)
242 
243 //---------------------------------------------------------------------------
244 // Exported C functions
245 //---------------------------------------------------------------------------
246 #if XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
247  #include <XnModuleCFunctions.h>
248  #define XN_MODULE_FUNC_TYPE XN_C_API_EXPORT
249  extern Module* _XN_MODULE_INST;
250 #else
251  #define XN_MODULE_FUNC_TYPE static
252  static Module* _XN_MODULE_INST;
253 #endif
254 
256 {
257  XnStatus nRetVal = XN_STATUS_OK;
258 
259  nRetVal = _XN_MODULE_INST->Load();
260  XN_IS_STATUS_OK(nRetVal);
261 
262  return (XN_STATUS_OK);
263 }
264 
266 {
267  _XN_MODULE_INST->Unload();
268 }
269 
271 {
272  return _XN_MODULE_INST->GetExportedNodesCount();
273 }
274 
276 {
277  return _XN_MODULE_INST->GetExportedNodes(aEntryPoints, nCount);
278 }
279 
281 {
282  pVersion->nMajor = XN_MAJOR_VERSION;
283  pVersion->nMinor = XN_MINOR_VERSION;
285  pVersion->nBuild = XN_BUILD_VERSION;
286 }
287 
288 #if !XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
289 #include <XnUtils.h>
290 
291 static XnOpenNIModuleInterface moduleInterface =
292 {
298 };
299 static XnStatus registerResult = xnRegisterModuleWithOpenNI(&moduleInterface, NULL, __FILE__);
300 #endif
301 
302 #endif // __XN_MODULE_CPP_REGISTRATION_H__
XN_MODULE_FUNC_TYPE XnStatus XN_C_DECL XN_MODULE_LOAD()
Definition: XnModuleCppRegistratration.h:255
void(* XnModuleGetExportedInterfacePtr)(XnModuleExportedProductionNodeInterface *pInterface)
Definition: XnModuleInterface.h:62
Definition: XnTypes.h:114
Definition: XnTypes.h:138
#define XN_IS_STATUS_OK(x)
Definition: XnMacros.h:59
Definition: XnTypes.h:129
#define FALSE
Definition: XnPlatform.h:89
virtual XnStatus Load()
Definition: XnModuleCppInterface.h:52
void * XnModuleNodeHandle
Definition: XnTypes.h:221
#define XN_MAINTENANCE_VERSION
Definition: XnVersion.h:37
Definition: XnModuleInterface.h:324
Definition: XnTypes.h:117
XN_MODULE_FUNC_TYPE void XN_C_DECL XN_MODULE_GET_OPEN_NI_VERSION(XnVersion *pVersion)
Definition: XnModuleCppRegistratration.h:280
Definition: XnTypes.h:135
Definition: XnModuleCppInterface.h:150
#define XN_STATUS_OK
Definition: XnStatus.h:36
Definition: XnTypes.h:153
Definition: XnTypes.h:123
Definition: XnModuleCppInterface.h:46
Definition: XnTypes.h:143
Definition: XnModuleInterface.h:69
Definition: XnModuleInterface.h:710
Definition: XnModuleInterface.h:1340
Definition: XnTypes.h:105
XnUInt32 XnStatus
Definition: XnStatus.h:33
#define XN_MODULE_FUNC_TYPE
Definition: XnModuleCppRegistratration.h:251
XnUInt32 GetExportedNodesCount()
Definition: XnModuleCppInterface.h:57
Definition: XnTypes.h:144
XnStatus GetExportedNodes(XnModuleGetExportedInterfacePtr *aEntryPoints, XnUInt32 nCount)
Definition: XnModuleCppInterface.h:59
Definition: XnModuleInterface.h:1325
XnInt32 XnProductionNodeType
Definition: XnTypes.h:94
Definition: XnModuleInterface.h:1098
XnUInt8 nMinor
Definition: XnTypes.h:156
Definition: XnTypes.h:120
XN_MODULE_FUNC_TYPE XnStatus XN_C_DECL XN_MODULE_GET_EXPORTED_NODES_ENTRY_POINTS(XnModuleGetExportedInterfacePtr *aEntryPoints, XnUInt32 nCount)
Definition: XnModuleCppRegistratration.h:275
Definition: XnModuleInterface.h:1213
Definition: XnTypes.h:111
Definition: XnModuleInterface.h:726
Definition: XnModuleInterface.h:896
Definition: XnTypes.h:141
XN_C_API XnBool XN_C_DECL xnIsTypeDerivedFrom(XnProductionNodeType type, XnProductionNodeType base)
Checks if a certain production node type is a derived of another type.
Definition: XnModuleInterface.h:1155
Definition: XnModuleInterface.h:1297
Definition: XnTypes.h:126
XN_MODULE_FUNC_TYPE XnUInt32 XN_C_DECL XN_MODULE_GET_EXPORTED_NODES_COUNT()
Definition: XnModuleCppRegistratration.h:270
XN_MODULE_FUNC_TYPE void XN_C_DECL XN_MODULE_UNLOAD()
Definition: XnModuleCppRegistratration.h:265
Definition: XnModuleInterface.h:1038
Definition: XnTypes.h:142
Definition: XnModuleInterface.h:1196
XnUInt32 nBuild
Definition: XnTypes.h:158
XnUInt8 nMajor
Definition: XnTypes.h:155
XnUInt16 nMaintenance
Definition: XnTypes.h:157
Definition: XnModuleInterface.h:1170
void(* GetInterfaceFuncPtr)(void *pInterface)
Definition: XnModuleCppRegistratration.h:136
Definition: XnModuleInterface.h:584
Definition: XnTypes.h:132
Definition: XnModuleInterface.h:1351
#define XN_BUILD_VERSION
Definition: XnVersion.h:39
#define XN_MINOR_VERSION
Definition: XnVersion.h:35
XN_C_API XnStatus XN_C_DECL xnRegisterModuleWithOpenNI(XnOpenNIModuleInterface *pInterface, const XnChar *strConfigDir, const XnChar *strName)
#define XN_MAJOR_VERSION
Definition: XnVersion.h:33
Definition: XnTypes.h:108
virtual void Unload()
Definition: XnModuleCppInterface.h:53
Definition: XnCppWrapper.h:33
Definition: XnModuleInterface.h:411