OpenNI 1.5.7
XnGeneralBuffer.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_GENERAL_BUFFER_H__
00022 #define __XN_GENERAL_BUFFER_H__
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include "XnPlatform.h"
00028 #include "XnOS.h"
00029 #include "XnStatusCodes.h"
00030 
00031 //---------------------------------------------------------------------------
00032 // Types
00033 //---------------------------------------------------------------------------
00034 
00035 /* Describes a general buffer. */
00036 typedef struct XnGeneralBuffer
00037 {
00038     /* A pointer to the actual data. */
00039     void* pData;
00040     /* The size of the data in bytes. */
00041     XnUInt32 nDataSize;
00042 } XnGeneralBuffer;
00043 
00044 //---------------------------------------------------------------------------
00045 // Exported Functions
00046 //---------------------------------------------------------------------------
00047 
00051 inline XnGeneralBuffer XnGeneralBufferPack(void* pData, XnUInt32 nDataSize)
00052 {
00053     XnGeneralBuffer result;
00054     result.pData = pData;
00055     result.nDataSize = nDataSize;
00056     return result;
00057 }
00058 
00062 inline XnStatus XnGeneralBufferCopy(XnGeneralBuffer* pDest, const XnGeneralBuffer* pSrc)
00063 {
00064     XN_VALIDATE_INPUT_PTR(pDest);
00065     XN_VALIDATE_INPUT_PTR(pSrc);
00066 
00067     if (pSrc->nDataSize > pDest->nDataSize)
00068         return XN_STATUS_OUTPUT_BUFFER_OVERFLOW;
00069 
00070     xnOSMemCopy(pDest->pData, pSrc->pData, pSrc->nDataSize);
00071     pDest->nDataSize = pSrc->nDataSize;
00072     return XN_STATUS_OK;
00073 }
00074 
00075 inline XnStatus XnGeneralBufferAlloc(XnGeneralBuffer* pDest, XnUInt32 nSize)
00076 {
00077     XN_VALIDATE_INPUT_PTR(pDest);
00078 
00079     void* pData;
00080     pData = xnOSMalloc(nSize);
00081     XN_VALIDATE_ALLOC_PTR(pData);
00082 
00083     pDest->pData = pData;
00084     pDest->nDataSize = nSize;
00085     return XN_STATUS_OK;
00086 }
00087 
00088 inline XnStatus XnGeneralBufferRealloc(XnGeneralBuffer* pDest, XnUInt32 nSize)
00089 {
00090     XN_VALIDATE_INPUT_PTR(pDest);
00091 
00092     void* pData;
00093     pData = xnOSRealloc(pDest, nSize);
00094     XN_VALIDATE_ALLOC_PTR(pData);
00095 
00096     pDest->pData = pData;
00097     pDest->nDataSize = nSize;
00098     return XN_STATUS_OK;
00099 }
00100 
00101 inline void XnGeneralBufferFree(XnGeneralBuffer* pDest)
00102 {
00103     XN_FREE_AND_NULL(pDest->pData);
00104     pDest->nDataSize = 0;
00105 }
00106 
00107 //---------------------------------------------------------------------------
00108 // Helper Macros
00109 //---------------------------------------------------------------------------
00110 #define XN_PACK_GENERAL_BUFFER(x)       XnGeneralBufferPack(&x, sizeof(x))
00111 
00112 #define XN_VALIDATE_GENERAL_BUFFER_TYPE(gb, t)  \
00113     if ((gb).nDataSize != sizeof(t))            \
00114     {                                           \
00115         return XN_STATUS_INVALID_BUFFER_SIZE;   \
00116     }
00117 
00118 #endif //__XN_GENERAL_BUFFER_H__