OpenNI 1.5.4
XnModuleCppRegistratration.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2011 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * OpenNI is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published *
10 * by the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * OpenNI is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 ****************************************************************************/
22 #ifndef __XN_MODULE_CPP_REGISTRATION_H__
23 #define __XN_MODULE_CPP_REGISTRATION_H__
24 
25 //---------------------------------------------------------------------------
26 // Includes
27 //---------------------------------------------------------------------------
28 #include "XnModuleCppInterface.h"
29 #include <XnUtils.h>
30 
31 using namespace xn;
32 
33 //---------------------------------------------------------------------------
34 // Internal Macros
35 //---------------------------------------------------------------------------
37 #define _XN_MODULE_INST g_pTheModule
38 
39 #define _CONCAT(a,b) a##b
40 
41 inline XnModuleNodeHandle __ModuleNodeToHandle(xn::ModuleProductionNode* pNode)
42 {
43  return ((XnModuleNodeHandle)pNode);
44 }
45 
46 #define __XN_EXPORT_NODE_COMMON(ExportedClass, ExportedName, Type) \
47  \
48  static ExportedClass* ExportedName = new ExportedClass(); \
49  \
50  void XN_CALLBACK_TYPE _CONCAT(ExportedClass,GetDescription)(XnProductionNodeDescription* pDescription) \
51  { \
52  ExportedName->GetDescription(pDescription); \
53  } \
54  \
55  XnStatus XN_CALLBACK_TYPE _CONCAT(ExportedClass,EnumerateProductionTrees) \
56  (XnContext* pContext, XnNodeInfoList* pTreesList, XnEnumerationErrors* pErrors) \
57  { \
58  Context context(pContext); \
59  NodeInfoList list(pTreesList); \
60  EnumerationErrors errors(pErrors); \
61  return ExportedName->EnumerateProductionTrees(context, list, pErrors == NULL ? NULL : &errors); \
62  } \
63  \
64  XnStatus XN_CALLBACK_TYPE _CONCAT(ExportedClass,Create)(XnContext* pContext, \
65  const XnChar* strInstanceName, \
66  const XnChar* strCreationInfo, \
67  XnNodeInfoList* pNeededTrees, \
68  const XnChar* strConfigurationDir, \
69  XnModuleNodeHandle* phInstance) \
70  { \
71  xn::NodeInfoList* pNeeded = NULL; \
72  if (pNeededTrees != NULL) \
73  { \
74  pNeeded = XN_NEW(xn::NodeInfoList, pNeededTrees); \
75  } \
76  ModuleProductionNode* pNode; \
77  Context context(pContext); \
78  XnStatus nRetVal = ExportedName->Create(context, strInstanceName, strCreationInfo, \
79  pNeeded, strConfigurationDir, &pNode); \
80  if (nRetVal != XN_STATUS_OK) \
81  { \
82  XN_DELETE(pNeeded); \
83  return (nRetVal); \
84  } \
85  *phInstance = __ModuleNodeToHandle(pNode); \
86  XN_DELETE(pNeeded); \
87  return (XN_STATUS_OK); \
88  } \
89  \
90  void XN_CALLBACK_TYPE _CONCAT(ExportedClass,Destroy)(XnModuleNodeHandle hInstance) \
91  { \
92  ModuleProductionNode* pNode = (ModuleProductionNode*)hInstance; \
93  ExportedName->Destroy(pNode); \
94  } \
95  \
96  void XN_CALLBACK_TYPE _CONCAT(ExportedClass,GetExportedInterface)( \
97  XnModuleExportedProductionNodeInterface* pInterface) \
98  { \
99  pInterface->GetDescription = _CONCAT(ExportedClass,GetDescription); \
100  pInterface->EnumerateProductionTrees = _CONCAT(ExportedClass,EnumerateProductionTrees); \
101  pInterface->Create = _CONCAT(ExportedClass,Create); \
102  pInterface->Destroy = _CONCAT(ExportedClass,Destroy); \
103  pInterface->GetInterface.General = __ModuleGetGetInterfaceFunc(Type); \
104  } \
105  \
106  static XnStatus _CONCAT(ExportedClass,RegisterResult) = \
107  _XN_MODULE_INST->AddExportedNode(_CONCAT(ExportedClass,GetExportedInterface));
108 
109 #define _XN_EXPORT_NODE_COMMON(ExportedClass, Type) \
110  __XN_EXPORT_NODE_COMMON(ExportedClass, _g_##ExportedClass, Type)
111 
112 //---------------------------------------------------------------------------
113 // Forward Declarations
114 //---------------------------------------------------------------------------
115 
116 void XN_CALLBACK_TYPE __ModuleGetProductionNodeInterface(XnModuleProductionNodeInterface* pInterface);
117 void XN_CALLBACK_TYPE __ModuleGetDeviceInterface(XnModuleDeviceInterface* pInterface);
118 void XN_CALLBACK_TYPE __ModuleGetGeneratorInterface(XnModuleGeneratorInterface* pInterface);
119 void XN_CALLBACK_TYPE __ModuleGetMapGeneratorInterface(XnModuleMapGeneratorInterface* pInterface);
120 void XN_CALLBACK_TYPE __ModuleGetDepthGeneratorInterface(XnModuleDepthGeneratorInterface* pInterface);
121 void XN_CALLBACK_TYPE __ModuleGetImageGeneratorInterface(XnModuleImageGeneratorInterface* pInterface);
122 void XN_CALLBACK_TYPE __ModuleGetIRGeneratorInterface(XnModuleIRGeneratorInterface* pInterface);
123 void XN_CALLBACK_TYPE __ModuleGetUserGeneratorInterface(XnModuleUserGeneratorInterface* pInterface);
124 void XN_CALLBACK_TYPE __ModuleGetHandsGeneratorInterface(XnModuleHandsGeneratorInterface* pInterface);
125 void XN_CALLBACK_TYPE __ModuleGetGestureGeneratorInterface(XnModuleGestureGeneratorInterface* pInterface);
126 void XN_CALLBACK_TYPE __ModuleGetSceneAnalyzerInterface(XnModuleSceneAnalyzerInterface* pInterface);
127 void XN_CALLBACK_TYPE __ModuleGetAudioGeneratorInterface(XnModuleAudioGeneratorInterface* pInterface);
128 void XN_CALLBACK_TYPE __ModuleGetRecorderInterface(XnModuleRecorderInterface* pInterface);
129 void XN_CALLBACK_TYPE __ModuleGetPlayerInterface(XnModulePlayerInterface* pInterface);
130 void XN_CALLBACK_TYPE __ModuleGetCodecInterface(XnModuleCodecInterface* pInterface);
131 void XN_CALLBACK_TYPE __ModuleGetScriptNodeInterface(XnModuleScriptNodeInterface* pInterface);
132 
133 //---------------------------------------------------------------------------
134 // Utility Macros
135 //---------------------------------------------------------------------------
136 
137 typedef void (XN_CALLBACK_TYPE *GetInterfaceFuncPtr)(void* pInterface);
138 
139 static GetInterfaceFuncPtr __ModuleGetGetInterfaceFunc(XnProductionNodeType type)
140 {
141  // start with concrete type
143  return (GetInterfaceFuncPtr)__ModuleGetDeviceInterface;
144  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_DEPTH))
145  return (GetInterfaceFuncPtr)__ModuleGetDepthGeneratorInterface;
146  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_IMAGE))
147  return (GetInterfaceFuncPtr)__ModuleGetImageGeneratorInterface;
148  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_IR))
149  return (GetInterfaceFuncPtr)__ModuleGetIRGeneratorInterface;
150  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_USER))
151  return (GetInterfaceFuncPtr)__ModuleGetUserGeneratorInterface;
153  return (GetInterfaceFuncPtr)__ModuleGetGestureGeneratorInterface;
154  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_SCENE))
155  return (GetInterfaceFuncPtr)__ModuleGetSceneAnalyzerInterface;
156  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_AUDIO))
157  return (GetInterfaceFuncPtr)__ModuleGetAudioGeneratorInterface;
159  return (GetInterfaceFuncPtr)__ModuleGetRecorderInterface;
161  return (GetInterfaceFuncPtr)__ModuleGetPlayerInterface;
162  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_HANDS))
163  return (GetInterfaceFuncPtr)__ModuleGetHandsGeneratorInterface;
164  else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_CODEC))
165  return (GetInterfaceFuncPtr)__ModuleGetCodecInterface;
167  return (GetInterfaceFuncPtr)__ModuleGetScriptNodeInterface;
168  // and continue with abstract ones
170  return (GetInterfaceFuncPtr)__ModuleGetMapGeneratorInterface;
172  return (GetInterfaceFuncPtr)__ModuleGetGeneratorInterface;
174  return (GetInterfaceFuncPtr)__ModuleGetProductionNodeInterface;
175 
176  // unknown
177  XN_ASSERT(FALSE);
178  return NULL;
179 }
180 
182 #if XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
183  #define XN_EXPORT_MODULE(ModuleClass) \
184  \
185  ModuleClass __moduleInstance; \
186  Module* _XN_MODULE_INST = &__moduleInstance;
187 #else
188  #define XN_EXPORT_MODULE(ModuleClass) \
189  \
190  static ModuleClass __moduleInstance; \
191  static Module* _XN_MODULE_INST = &__moduleInstance;
192 #endif
193 
195 #define XN_EXPORT_NODE(ExportedClass, nodeType) \
196  _XN_EXPORT_NODE_COMMON(ExportedClass, nodeType)
197 
199 #define XN_EXPORT_DEVICE(ExportedClass) \
200  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_DEVICE)
201 
203 #define XN_EXPORT_DEPTH(ExportedClass) \
204  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_DEPTH)
205 
207 #define XN_EXPORT_IMAGE(ExportedClass) \
208  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_IMAGE)
209 
211 #define XN_EXPORT_IR(ExportedClass) \
212  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_IR)
213 
214 #define XN_EXPORT_USER(ExportedClass) \
215  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_USER)
216 
217 #define XN_EXPORT_HANDS(ExportedClass) \
218  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_HANDS)
219 
220 #define XN_EXPORT_GESTURE(ExportedClass) \
221  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_GESTURE)
222 
223 #define XN_EXPORT_SCENE(ExportedClass) \
224  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_SCENE)
225 
227 #define XN_EXPORT_AUDIO(ExportedClass) \
228  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_AUDIO)
229 
231 #define XN_EXPORT_RECORDER(ExportedClass) \
232  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_RECORDER)
233 
235 #define XN_EXPORT_PLAYER(ExportedClass) \
236  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_PLAYER)
237 
238 #define XN_EXPORT_CODEC(ExportedClass) \
239  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_CODEC)
240 
241 #define XN_EXPORT_SCRIPT(ExportedClass) \
242  _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_SCRIPT)
243 
244 //---------------------------------------------------------------------------
245 // Exported C functions
246 //---------------------------------------------------------------------------
247 #if XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
248  #include <XnModuleCFunctions.h>
249  #define XN_MODULE_FUNC_TYPE XN_C_API_EXPORT
250  extern Module* _XN_MODULE_INST;
251 #else
252  #define XN_MODULE_FUNC_TYPE static
253  static Module* _XN_MODULE_INST;
254 #endif
255 
257 {
258  XnStatus nRetVal = XN_STATUS_OK;
259 
260  nRetVal = _XN_MODULE_INST->Load();
261  XN_IS_STATUS_OK(nRetVal);
262 
263  return (XN_STATUS_OK);
264 }
265 
267 {
268  _XN_MODULE_INST->Unload();
269 }
270 
272 {
273  return _XN_MODULE_INST->GetExportedNodesCount();
274 }
275 
277 {
278  return _XN_MODULE_INST->GetExportedNodes(aEntryPoints, nCount);
279 }
280 
282 {
283  pVersion->nMajor = XN_MAJOR_VERSION;
284  pVersion->nMinor = XN_MINOR_VERSION;
286  pVersion->nBuild = XN_BUILD_VERSION;
287 }
288 
289 #if !XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
290 #include <XnUtils.h>
291 
292 static XnOpenNIModuleInterface moduleInterface =
293 {
299 };
300 static XnStatus registerResult = xnRegisterModuleWithOpenNI(&moduleInterface, NULL, __FILE__);
301 #endif
302 
303 #endif // __XN_MODULE_CPP_REGISTRATION_H__
XN_MODULE_FUNC_TYPE XnStatus XN_C_DECL XN_MODULE_LOAD()
Definition: XnModuleCppRegistratration.h:256
Definition: XnTypes.h:112
Definition: XnTypes.h:136
#define XN_IS_STATUS_OK(x)
Definition: XnMacros.h:60
Definition: XnTypes.h:127
#define FALSE
Definition: XnPlatform.h:94
virtual XnStatus Load()
Definition: XnModuleCppInterface.h:53
void * XnModuleNodeHandle
Definition: XnTypes.h:219
#define XN_MAINTENANCE_VERSION
Definition: XnVersion.h:38
Definition: XnModuleInterface.h:325
Definition: XnTypes.h:115
XN_MODULE_FUNC_TYPE void XN_C_DECL XN_MODULE_GET_OPEN_NI_VERSION(XnVersion *pVersion)
Definition: XnModuleCppRegistratration.h:281
Definition: XnTypes.h:133
Definition: XnModuleCppInterface.h:151
#define XN_STATUS_OK
Definition: XnStatus.h:37
Definition: XnTypes.h:151
Definition: XnTypes.h:121
Definition: XnModuleCppInterface.h:47
Definition: XnTypes.h:141
Definition: XnModuleInterface.h:70
Definition: XnModuleInterface.h:699
Definition: XnModuleInterface.h:1329
Definition: XnTypes.h:103
XnUInt32 XnStatus
Definition: XnStatus.h:34
#define XN_MODULE_FUNC_TYPE
Definition: XnModuleCppRegistratration.h:252
XnUInt32 GetExportedNodesCount()
Definition: XnModuleCppInterface.h:58
Definition: XnTypes.h:142
XnStatus GetExportedNodes(XnModuleGetExportedInterfacePtr *aEntryPoints, XnUInt32 nCount)
Definition: XnModuleCppInterface.h:60
Definition: XnModuleInterface.h:1314
XnInt32 XnProductionNodeType
Definition: XnTypes.h:92
Definition: XnModuleInterface.h:1087
XnUInt8 nMinor
Definition: XnTypes.h:154
Definition: XnTypes.h:118
XN_MODULE_FUNC_TYPE XnStatus XN_C_DECL XN_MODULE_GET_EXPORTED_NODES_ENTRY_POINTS(XnModuleGetExportedInterfacePtr *aEntryPoints, XnUInt32 nCount)
Definition: XnModuleCppRegistratration.h:276
Definition: XnModuleInterface.h:1202
Definition: XnTypes.h:109
Definition: XnModuleInterface.h:715
Definition: XnModuleInterface.h:885
Definition: XnTypes.h:139
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:1144
Definition: XnModuleInterface.h:1286
Definition: XnTypes.h:124
XN_MODULE_FUNC_TYPE XnUInt32 XN_C_DECL XN_MODULE_GET_EXPORTED_NODES_COUNT()
Definition: XnModuleCppRegistratration.h:271
XN_MODULE_FUNC_TYPE void XN_C_DECL XN_MODULE_UNLOAD()
Definition: XnModuleCppRegistratration.h:266
Definition: XnModuleInterface.h:1027
Definition: XnTypes.h:140
Definition: XnModuleInterface.h:1185
XnUInt32 nBuild
Definition: XnTypes.h:156
XnUInt8 nMajor
Definition: XnTypes.h:153
XnUInt16 nMaintenance
Definition: XnTypes.h:155
void(* XnModuleGetExportedInterfacePtr)(XnModuleExportedProductionNodeInterface *pInterface)
Definition: XnModuleInterface.h:63
Definition: XnModuleInterface.h:1159
Definition: XnModuleInterface.h:573
Definition: XnTypes.h:130
Definition: XnModuleInterface.h:1340
#define XN_BUILD_VERSION
Definition: XnVersion.h:40
#define XN_MINOR_VERSION
Definition: XnVersion.h:36
XN_C_API XnStatus XN_C_DECL xnRegisterModuleWithOpenNI(XnOpenNIModuleInterface *pInterface, const XnChar *strConfigDir, const XnChar *strName)
#define XN_MAJOR_VERSION
Definition: XnVersion.h:34
Definition: XnTypes.h:106
virtual void Unload()
Definition: XnModuleCppInterface.h:54
Definition: XnCppWrapper.h:34
void(* GetInterfaceFuncPtr)(void *pInterface)
Definition: XnModuleCppRegistratration.h:137
Definition: XnModuleInterface.h:412