OpenNI 1.5.7
XnDumpWriters.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_DUMP_WRITERS_H__
22 #define __XN_DUMP_WRITERS_H__
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include "XnDump.h"
28 
29 //---------------------------------------------------------------------------
30 // Types
31 //---------------------------------------------------------------------------
32 typedef struct XnDumpWriterFileHandle
33 {
34  void* pInternal;
36 
37 typedef struct XnDumpWriter
38 {
39  void* pCookie;
40  XnDumpWriterFileHandle (XN_CALLBACK_TYPE* OpenFile)(void* pCookie, const XnChar* strDumpName, XnBool bSessionDump, const XnChar* strFileName);
41  void (XN_CALLBACK_TYPE* Write)(void* pCookie, XnDumpWriterFileHandle hFile, const void* pBuffer, XnUInt32 nBufferSize);
42  void (XN_CALLBACK_TYPE* CloseFile)(void* pCookie, XnDumpWriterFileHandle hFile);
44 
45 //---------------------------------------------------------------------------
46 // Functions
47 //---------------------------------------------------------------------------
49 
50 XN_C_API void XN_C_DECL xnDumpUnregisterWriter(XnDumpWriter* pWriter);
51 
52 XN_C_API XnStatus XN_C_DECL xnDumpSetFilesOutput(XnBool bOn);
53 
54 //---------------------------------------------------------------------------
55 // Helpers
56 //---------------------------------------------------------------------------
57 #ifdef __cplusplus
58 
59 class XnDumpWriterBase
60 {
61 public:
62  XnDumpWriterBase() : m_bRegistered(FALSE)
63  {
64  m_cObject.pCookie = this;
65  m_cObject.OpenFile = OpenFileCallback;
66  m_cObject.Write = WriteCallback;
67  m_cObject.CloseFile = CloseFileCallback;
68  }
69 
70  virtual ~XnDumpWriterBase()
71  {
72  Unregister();
73  }
74 
75  XnStatus Register()
76  {
77  XnStatus nRetVal = XN_STATUS_OK;
78 
79  if (!m_bRegistered)
80  {
81  OnRegister();
82 
83  nRetVal = xnDumpRegisterWriter(&m_cObject);
84  if (nRetVal != XN_STATUS_OK)
85  {
86  OnUnregister();
87  return (nRetVal);
88  }
89 
90  m_bRegistered = TRUE;
91  }
92 
93  return (XN_STATUS_OK);
94  }
95 
96  void Unregister()
97  {
98  if (m_bRegistered)
99  {
100  xnDumpUnregisterWriter(&m_cObject);
101  m_bRegistered = FALSE;
102 
103  OnUnregister();
104  }
105  }
106 
107  inline XnBool IsRegistered() { return m_bRegistered; }
108 
109  virtual XnDumpWriterFileHandle OpenFile(const XnChar* strDumpName, XnBool bSessionDump, const XnChar* strFileName) = 0;
110  virtual void Write(XnDumpWriterFileHandle hFile, const void* pBuffer, XnUInt32 nBufferSize) = 0;
111  virtual void CloseFile(XnDumpWriterFileHandle hFile) = 0;
112 
113  operator const XnDumpWriter*() const
114  {
115  return &m_cObject;
116  }
117 
118 protected:
119  virtual void OnRegister() {}
120  virtual void OnUnregister() {}
121 
122 private:
123  static XnDumpWriterFileHandle XN_CALLBACK_TYPE OpenFileCallback(void* pCookie, const XnChar* strDumpName, XnBool bSessionDump, const XnChar* strFileName)
124  {
125  XnDumpWriterBase* pThis = (XnDumpWriterBase*)pCookie;
126  return pThis->OpenFile(strDumpName, bSessionDump, strFileName);
127  }
128 
129  static void XN_CALLBACK_TYPE WriteCallback(void* pCookie, XnDumpWriterFileHandle hFile, const void* pBuffer, XnUInt32 nBufferSize)
130  {
131  XnDumpWriterBase* pThis = (XnDumpWriterBase*)pCookie;
132  return pThis->Write(hFile, pBuffer, nBufferSize);
133  }
134 
135  static void XN_CALLBACK_TYPE CloseFileCallback(void* pCookie, XnDumpWriterFileHandle hFile)
136  {
137  XnDumpWriterBase* pThis = (XnDumpWriterBase*)pCookie;
138  return pThis->CloseFile(hFile);
139  }
140 
141  XnDumpWriter m_cObject;
142  XnBool m_bRegistered;
143 };
144 
145 #endif
146 
147 #endif // __XN_DUMP_WRITERS_H__
XN_C_API void XN_C_DECL xnDumpUnregisterWriter(XnDumpWriter *pWriter)
struct XnDumpWriter XnDumpWriter
XN_C_API XnStatus XN_C_DECL xnDumpSetFilesOutput(XnBool bOn)
struct XnDumpWriterFileHandle XnDumpWriterFileHandle
XN_C_API XnStatus XN_C_DECL xnDumpRegisterWriter(XnDumpWriter *pWriter)
#define TRUE
Definition: XnPlatform.h:85
#define FALSE
Definition: XnPlatform.h:89
#define XN_C_API
Definition: XnPlatform.h:121
XnUInt32 XnStatus
Definition: XnStatus.h:33
#define XN_STATUS_OK
Definition: XnStatus.h:36
Definition: XnDumpWriters.h:33
void * pInternal
Definition: XnDumpWriters.h:34
Definition: XnDumpWriters.h:38
void(* CloseFile)(void *pCookie, XnDumpWriterFileHandle hFile)
Definition: XnDumpWriters.h:42
XnDumpWriterFileHandle(* OpenFile)(void *pCookie, const XnChar *strDumpName, XnBool bSessionDump, const XnChar *strFileName)
Definition: XnDumpWriters.h:40
void * pCookie
Definition: XnDumpWriters.h:39
void(* Write)(void *pCookie, XnDumpWriterFileHandle hFile, const void *pBuffer, XnUInt32 nBufferSize)
Definition: XnDumpWriters.h:41