OpenNI 1.5.7
XnModuleCppRegistratration.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef __XN_MODULE_CPP_REGISTRATION_H__
00022 #define __XN_MODULE_CPP_REGISTRATION_H__
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include "XnModuleCppInterface.h"
00028 #include <XnUtils.h>
00029 
00030 using namespace xn;
00031 
00032 //---------------------------------------------------------------------------
00033 // Internal Macros
00034 //---------------------------------------------------------------------------
00036 #define _XN_MODULE_INST g_pTheModule
00037 
00038 #define _CONCAT(a,b) a##b
00039 
00040 inline XnModuleNodeHandle __ModuleNodeToHandle(xn::ModuleProductionNode* pNode)
00041 {
00042     return ((XnModuleNodeHandle)pNode);
00043 }
00044 
00045 #define __XN_EXPORT_NODE_COMMON(ExportedClass, ExportedName, Type)                                          \
00046                                                                     \
00047     static ExportedClass* ExportedName = new ExportedClass();                                               \
00048                                                                                                             \
00049     void XN_CALLBACK_TYPE _CONCAT(ExportedClass,GetDescription)(XnProductionNodeDescription* pDescription)  \
00050     {                                                                                                       \
00051         ExportedName->GetDescription(pDescription);                                                         \
00052     }                                                                                                       \
00053                                                                                                             \
00054     XnStatus XN_CALLBACK_TYPE _CONCAT(ExportedClass,EnumerateProductionTrees)                               \
00055         (XnContext* pContext, XnNodeInfoList* pTreesList, XnEnumerationErrors* pErrors)                     \
00056     {                                                                                                       \
00057         Context context(pContext);                                                                          \
00058         NodeInfoList list(pTreesList);                                                                      \
00059         EnumerationErrors errors(pErrors);                                                                  \
00060         return ExportedName->EnumerateProductionTrees(context, list, pErrors == NULL ? NULL : &errors);     \
00061     }                                                                                                       \
00062                                                                                                             \
00063     XnStatus XN_CALLBACK_TYPE _CONCAT(ExportedClass,Create)(XnContext* pContext,                            \
00064         const XnChar* strInstanceName,                                                                      \
00065         const XnChar* strCreationInfo,                                                                      \
00066         XnNodeInfoList* pNeededTrees,                                                                       \
00067         const XnChar* strConfigurationDir,                                                                  \
00068         XnModuleNodeHandle* phInstance)                                                                     \
00069     {                                                                                                       \
00070         xn::NodeInfoList* pNeeded = NULL;                                                                   \
00071         if (pNeededTrees != NULL)                                                                           \
00072         {                                                                                                   \
00073             pNeeded = XN_NEW(xn::NodeInfoList, pNeededTrees);                                               \
00074         }                                                                                                   \
00075         ModuleProductionNode* pNode;                                                                        \
00076         Context context(pContext);                                                                          \
00077         XnStatus nRetVal = ExportedName->Create(context, strInstanceName, strCreationInfo,                  \
00078             pNeeded, strConfigurationDir, &pNode);                                                          \
00079         if (nRetVal != XN_STATUS_OK)                                                                        \
00080         {                                                                                                   \
00081             XN_DELETE(pNeeded);                                                                             \
00082             return (nRetVal);                                                                               \
00083         }                                                                                                   \
00084         *phInstance = __ModuleNodeToHandle(pNode);                                                          \
00085         XN_DELETE(pNeeded);                                                                                 \
00086         return (XN_STATUS_OK);                                                                              \
00087     }                                                                                                       \
00088                                                                                                             \
00089     void XN_CALLBACK_TYPE _CONCAT(ExportedClass,Destroy)(XnModuleNodeHandle hInstance)                      \
00090     {                                                                                                       \
00091         ModuleProductionNode* pNode = (ModuleProductionNode*)hInstance;                                     \
00092         ExportedName->Destroy(pNode);                                                                       \
00093     }                                                                                                       \
00094                                                                                                             \
00095     void XN_CALLBACK_TYPE _CONCAT(ExportedClass,GetExportedInterface)(                                      \
00096         XnModuleExportedProductionNodeInterface* pInterface)                                                \
00097     {                                                                                                       \
00098         pInterface->GetDescription = _CONCAT(ExportedClass,GetDescription);                                 \
00099         pInterface->EnumerateProductionTrees = _CONCAT(ExportedClass,EnumerateProductionTrees);             \
00100         pInterface->Create = _CONCAT(ExportedClass,Create);                                                 \
00101         pInterface->Destroy = _CONCAT(ExportedClass,Destroy);                                               \
00102         pInterface->GetInterface.General = __ModuleGetGetInterfaceFunc(Type);                               \
00103     }                                                                                                       \
00104                                                                                                             \
00105     static XnStatus _CONCAT(ExportedClass,RegisterResult) =                                                 \
00106         _XN_MODULE_INST->AddExportedNode(_CONCAT(ExportedClass,GetExportedInterface));
00107 
00108 #define _XN_EXPORT_NODE_COMMON(ExportedClass, Type)                     \
00109     __XN_EXPORT_NODE_COMMON(ExportedClass, _g_##ExportedClass, Type)
00110 
00111 //---------------------------------------------------------------------------
00112 // Forward Declarations
00113 //---------------------------------------------------------------------------
00114 
00115 void XN_CALLBACK_TYPE __ModuleGetProductionNodeInterface(XnModuleProductionNodeInterface* pInterface);
00116 void XN_CALLBACK_TYPE __ModuleGetDeviceInterface(XnModuleDeviceInterface* pInterface);
00117 void XN_CALLBACK_TYPE __ModuleGetGeneratorInterface(XnModuleGeneratorInterface* pInterface);
00118 void XN_CALLBACK_TYPE __ModuleGetMapGeneratorInterface(XnModuleMapGeneratorInterface* pInterface);
00119 void XN_CALLBACK_TYPE __ModuleGetDepthGeneratorInterface(XnModuleDepthGeneratorInterface* pInterface);
00120 void XN_CALLBACK_TYPE __ModuleGetImageGeneratorInterface(XnModuleImageGeneratorInterface* pInterface);
00121 void XN_CALLBACK_TYPE __ModuleGetIRGeneratorInterface(XnModuleIRGeneratorInterface* pInterface);
00122 void XN_CALLBACK_TYPE __ModuleGetUserGeneratorInterface(XnModuleUserGeneratorInterface* pInterface);
00123 void XN_CALLBACK_TYPE __ModuleGetHandsGeneratorInterface(XnModuleHandsGeneratorInterface* pInterface);
00124 void XN_CALLBACK_TYPE __ModuleGetGestureGeneratorInterface(XnModuleGestureGeneratorInterface* pInterface);
00125 void XN_CALLBACK_TYPE __ModuleGetSceneAnalyzerInterface(XnModuleSceneAnalyzerInterface* pInterface);
00126 void XN_CALLBACK_TYPE __ModuleGetAudioGeneratorInterface(XnModuleAudioGeneratorInterface* pInterface);
00127 void XN_CALLBACK_TYPE __ModuleGetRecorderInterface(XnModuleRecorderInterface* pInterface);
00128 void XN_CALLBACK_TYPE __ModuleGetPlayerInterface(XnModulePlayerInterface* pInterface);
00129 void XN_CALLBACK_TYPE __ModuleGetCodecInterface(XnModuleCodecInterface* pInterface);
00130 void XN_CALLBACK_TYPE __ModuleGetScriptNodeInterface(XnModuleScriptNodeInterface* pInterface);
00131 
00132 //---------------------------------------------------------------------------
00133 // Utility Macros
00134 //---------------------------------------------------------------------------
00135 
00136 typedef void (XN_CALLBACK_TYPE *GetInterfaceFuncPtr)(void* pInterface);
00137 
00138 static GetInterfaceFuncPtr __ModuleGetGetInterfaceFunc(XnProductionNodeType type)
00139 {
00140     // start with concrete type
00141     if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_DEVICE))
00142         return (GetInterfaceFuncPtr)__ModuleGetDeviceInterface;
00143     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_DEPTH))
00144         return (GetInterfaceFuncPtr)__ModuleGetDepthGeneratorInterface;
00145     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_IMAGE))
00146         return (GetInterfaceFuncPtr)__ModuleGetImageGeneratorInterface;
00147     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_IR))
00148         return (GetInterfaceFuncPtr)__ModuleGetIRGeneratorInterface;
00149     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_USER))
00150         return (GetInterfaceFuncPtr)__ModuleGetUserGeneratorInterface;
00151     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_GESTURE))
00152         return (GetInterfaceFuncPtr)__ModuleGetGestureGeneratorInterface;
00153     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_SCENE))
00154         return (GetInterfaceFuncPtr)__ModuleGetSceneAnalyzerInterface;
00155     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_AUDIO))
00156         return (GetInterfaceFuncPtr)__ModuleGetAudioGeneratorInterface;
00157     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_RECORDER))
00158         return (GetInterfaceFuncPtr)__ModuleGetRecorderInterface;
00159     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_PLAYER))
00160         return (GetInterfaceFuncPtr)__ModuleGetPlayerInterface;
00161     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_HANDS))
00162         return (GetInterfaceFuncPtr)__ModuleGetHandsGeneratorInterface;
00163     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_CODEC))
00164         return (GetInterfaceFuncPtr)__ModuleGetCodecInterface;
00165     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_SCRIPT))
00166         return (GetInterfaceFuncPtr)__ModuleGetScriptNodeInterface;
00167     // and continue with abstract ones
00168     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_MAP_GENERATOR))
00169         return (GetInterfaceFuncPtr)__ModuleGetMapGeneratorInterface;
00170     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_GENERATOR))
00171         return (GetInterfaceFuncPtr)__ModuleGetGeneratorInterface;
00172     else if (xnIsTypeDerivedFrom(type, XN_NODE_TYPE_PRODUCTION_NODE))
00173         return (GetInterfaceFuncPtr)__ModuleGetProductionNodeInterface;
00174 
00175     // unknown
00176     XN_ASSERT(FALSE);
00177     return NULL;
00178 }
00179 
00181 #if XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
00182     #define XN_EXPORT_MODULE(ModuleClass)                                                \
00183                                                  \
00184         ModuleClass __moduleInstance;                                                    \
00185         Module* _XN_MODULE_INST = &__moduleInstance;
00186 #else
00187     #define XN_EXPORT_MODULE(ModuleClass)                                                \
00188                                                  \
00189         static ModuleClass __moduleInstance;                                             \
00190         static Module* _XN_MODULE_INST = &__moduleInstance;
00191 #endif
00192 
00194 #define XN_EXPORT_NODE(ExportedClass, nodeType)                             \
00195     _XN_EXPORT_NODE_COMMON(ExportedClass, nodeType)
00196 
00198 #define XN_EXPORT_DEVICE(ExportedClass)                                     \
00199     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_DEVICE)
00200 
00202 #define XN_EXPORT_DEPTH(ExportedClass)                                      \
00203     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_DEPTH)
00204 
00206 #define XN_EXPORT_IMAGE(ExportedClass)                                      \
00207     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_IMAGE)
00208 
00210 #define XN_EXPORT_IR(ExportedClass)                                         \
00211     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_IR)
00212 
00213 #define XN_EXPORT_USER(ExportedClass)                                       \
00214     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_USER)
00215 
00216 #define XN_EXPORT_HANDS(ExportedClass)                                      \
00217     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_HANDS)
00218 
00219 #define XN_EXPORT_GESTURE(ExportedClass)                                    \
00220     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_GESTURE)
00221 
00222 #define XN_EXPORT_SCENE(ExportedClass)                                      \
00223     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_SCENE)
00224 
00226 #define XN_EXPORT_AUDIO(ExportedClass)                                      \
00227     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_AUDIO)
00228 
00230 #define XN_EXPORT_RECORDER(ExportedClass)                                   \
00231     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_RECORDER)
00232 
00234 #define XN_EXPORT_PLAYER(ExportedClass)                                     \
00235     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_PLAYER)
00236 
00237 #define XN_EXPORT_CODEC(ExportedClass)                                      \
00238     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_CODEC)
00239 
00240 #define XN_EXPORT_SCRIPT(ExportedClass)                                     \
00241     _XN_EXPORT_NODE_COMMON(ExportedClass, XN_NODE_TYPE_SCRIPT)
00242 
00243 //---------------------------------------------------------------------------
00244 // Exported C functions
00245 //---------------------------------------------------------------------------
00246 #if XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
00247     #include <XnModuleCFunctions.h>
00248     #define XN_MODULE_FUNC_TYPE XN_C_API_EXPORT
00249     extern Module* _XN_MODULE_INST;
00250 #else
00251     #define XN_MODULE_FUNC_TYPE static
00252     static Module* _XN_MODULE_INST;
00253 #endif
00254 
00255 XN_MODULE_FUNC_TYPE XnStatus XN_C_DECL XN_MODULE_LOAD()
00256 {
00257     XnStatus nRetVal = XN_STATUS_OK;
00258     
00259     nRetVal = _XN_MODULE_INST->Load();
00260     XN_IS_STATUS_OK(nRetVal);
00261     
00262     return (XN_STATUS_OK);
00263 }
00264 
00265 XN_MODULE_FUNC_TYPE void XN_C_DECL XN_MODULE_UNLOAD()
00266 {
00267     _XN_MODULE_INST->Unload();
00268 }
00269 
00270 XN_MODULE_FUNC_TYPE XnUInt32 XN_C_DECL XN_MODULE_GET_EXPORTED_NODES_COUNT()
00271 {
00272     return _XN_MODULE_INST->GetExportedNodesCount();
00273 }
00274 
00275 XN_MODULE_FUNC_TYPE XnStatus XN_C_DECL XN_MODULE_GET_EXPORTED_NODES_ENTRY_POINTS(XnModuleGetExportedInterfacePtr* aEntryPoints, XnUInt32 nCount)
00276 {
00277     return _XN_MODULE_INST->GetExportedNodes(aEntryPoints, nCount);
00278 }
00279 
00280 XN_MODULE_FUNC_TYPE void XN_C_DECL XN_MODULE_GET_OPEN_NI_VERSION(XnVersion* pVersion)
00281 {
00282     pVersion->nMajor = XN_MAJOR_VERSION;
00283     pVersion->nMinor = XN_MINOR_VERSION;
00284     pVersion->nMaintenance = XN_MAINTENANCE_VERSION;
00285     pVersion->nBuild = XN_BUILD_VERSION;
00286 }
00287 
00288 #if !XN_PLATFORM_SUPPORTS_DYNAMIC_LIBS
00289 #include <XnUtils.h>
00290 
00291 static XnOpenNIModuleInterface moduleInterface = 
00292 {
00293     XN_MODULE_LOAD,
00294     XN_MODULE_UNLOAD,
00295     XN_MODULE_GET_EXPORTED_NODES_COUNT,
00296     XN_MODULE_GET_EXPORTED_NODES_ENTRY_POINTS,
00297     XN_MODULE_GET_OPEN_NI_VERSION
00298 };
00299 static XnStatus registerResult = xnRegisterModuleWithOpenNI(&moduleInterface, NULL, __FILE__);
00300 #endif
00301 
00302 #endif // __XN_MODULE_CPP_REGISTRATION_H__