OpenNI 1.5.4: XnLogWriterBase.h Source File

OpenNI

OpenNI 1.5.4
XnLogWriterBase.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 *                                                                           *
00003 *  OpenNI 1.x Alpha                                                         *
00004 *  Copyright (C) 2011 PrimeSense Ltd.                                       *
00005 *                                                                           *
00006 *  This file is part of OpenNI.                                             *
00007 *                                                                           *
00008 *  OpenNI is free software: you can redistribute it and/or modify           *
00009 *  it under the terms of the GNU Lesser General Public License as published *
00010 *  by the Free Software Foundation, either version 3 of the License, or     *
00011 *  (at your option) any later version.                                      *
00012 *                                                                           *
00013 *  OpenNI is distributed in the hope that it will be useful,                *
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
00016 *  GNU Lesser General Public License for more details.                      *
00017 *                                                                           *
00018 *  You should have received a copy of the GNU Lesser General Public License *
00019 *  along with OpenNI. If not, see <http://www.gnu.org/licenses/>.           *
00020 *                                                                           *
00021 ****************************************************************************/
00022 #ifndef __XN_LOG_WRITER_BASE_H__
00023 #define __XN_LOG_WRITER_BASE_H__
00024 
00025 //---------------------------------------------------------------------------
00026 // Includes
00027 //---------------------------------------------------------------------------
00028 #include <XnLogTypes.h>
00029 #include <XnLog.h>
00030 
00031 //---------------------------------------------------------------------------
00032 // Types
00033 //---------------------------------------------------------------------------
00034 class XnLogWriterBase
00035 {
00036 public:
00037     XnLogWriterBase() : m_bRegistered(FALSE)
00038     {
00039         m_cObject.pCookie = this;
00040         m_cObject.WriteEntry = WriteEntryCallback;
00041         m_cObject.WriteUnformatted = WriteUnformattedCallback;
00042         m_cObject.OnConfigurationChanged = OnConfigurationChangedCallback;
00043         m_cObject.OnClosing = OnClosingCallback;
00044     }
00045 
00046     virtual ~XnLogWriterBase()
00047     {
00048         Unregister();
00049     }
00050 
00051     XnStatus Register()
00052     {
00053         XnStatus nRetVal = XN_STATUS_OK;
00054         
00055         if (!m_bRegistered)
00056         {
00057             OnRegister();
00058 
00059             nRetVal = xnLogRegisterLogWriter(&m_cObject);
00060             if (nRetVal != XN_STATUS_OK)
00061             {
00062                 OnUnregister();
00063                 return (nRetVal);
00064             }
00065 
00066             m_bRegistered = TRUE;
00067         }
00068         
00069         return (XN_STATUS_OK);
00070     }
00071 
00072     void Unregister()
00073     {
00074         if (m_bRegistered)
00075         {
00076             xnLogUnregisterLogWriter(&m_cObject);
00077             m_bRegistered = FALSE;
00078 
00079             OnUnregister();
00080         }
00081     }
00082 
00083     inline XnBool IsRegistered() { return m_bRegistered; }
00084 
00085     virtual void WriteEntry(const XnLogEntry* pEntry) = 0;
00086     virtual void WriteUnformatted(const XnChar* strMessage) = 0;
00087     virtual void OnConfigurationChanged() {};
00088     virtual void OnClosing() 
00089     {
00090         Unregister();
00091     };
00092 
00093     operator const XnLogWriter*() const
00094     {
00095         return &m_cObject;
00096     }
00097 
00098 protected:
00099     virtual void OnRegister() {}
00100     virtual void OnUnregister() {}
00101 
00102 private:
00103     static void XN_CALLBACK_TYPE WriteEntryCallback(const XnLogEntry* pEntry, void* pCookie)
00104     {
00105         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00106         pThis->WriteEntry(pEntry);
00107     }
00108     static void XN_CALLBACK_TYPE WriteUnformattedCallback(const XnChar* strMessage, void* pCookie)
00109     {
00110         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00111         pThis->WriteUnformatted(strMessage);
00112     }
00113     static void XN_CALLBACK_TYPE OnConfigurationChangedCallback(void* pCookie)
00114     {
00115         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00116         pThis->OnConfigurationChanged();
00117     }
00118     static void XN_CALLBACK_TYPE OnClosingCallback(void* pCookie)
00119     {
00120         XnLogWriterBase* pThis = (XnLogWriterBase*)pCookie;
00121         pThis->OnClosing();
00122     }
00123 
00124     XnLogWriter m_cObject;
00125     XnBool m_bRegistered;
00126 };
00127 
00128 #endif // __XN_LOG_WRITER_BASE_H__
Generated on Wed May 16 2012 10:16:05 for OpenNI 1.5.4 by   doxygen 1.7.5.1