2011-01-15 18:05:35 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCX_COMMON_H__
|
|
|
|
#define __CCX_COMMON_H__
|
|
|
|
|
|
|
|
#include "ccxMacros.h"
|
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
NS_CC_BEGIN
|
2011-01-15 18:05:35 +08:00
|
|
|
|
|
|
|
// [u]int[8|16|32|64]
|
|
|
|
// char
|
|
|
|
typedef unsigned char ccxByte;
|
|
|
|
typedef signed short ccxInt16;
|
|
|
|
typedef unsigned short ccxUInt16;
|
|
|
|
// int
|
|
|
|
// unsigned
|
|
|
|
typedef long long ccxInt64;
|
|
|
|
typedef unsigned long long ccxUInt64;
|
|
|
|
|
|
|
|
/// The max length of CCXLog message.
|
|
|
|
static const int kMaxLogLen = 255;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Output Debug message.
|
|
|
|
*/
|
|
|
|
void CCX_DLL_PS CCXLog(const char * pszFormat, ...);
|
|
|
|
|
2011-01-27 17:07:36 +08:00
|
|
|
struct ccxNullDeleter { template< class TPTR > void operator()(TPTR ) {} };
|
2011-02-17 14:31:52 +08:00
|
|
|
struct ccxNewDeleter { template< class TPTR > void operator()(TPTR p) { if (p) delete p; } };
|
|
|
|
struct ccxNewArrayDeleter { template< class TPTR > void operator()(TPTR p) { if (p) delete[] p; } };
|
2011-01-19 14:23:26 +08:00
|
|
|
|
2011-01-15 18:05:35 +08:00
|
|
|
/**
|
|
|
|
@brief A simple scoped pointer.
|
|
|
|
*/
|
2011-01-27 17:07:36 +08:00
|
|
|
template < class T, class D = ccxNewDeleter >
|
|
|
|
class ccxScopedPtr // noncopyable
|
2011-01-19 14:23:26 +08:00
|
|
|
: private D
|
2011-01-15 18:05:35 +08:00
|
|
|
{
|
|
|
|
public:
|
2011-01-27 17:07:36 +08:00
|
|
|
explicit ccxScopedPtr(T * p = 0): m_ptr(p) {}
|
|
|
|
~ccxScopedPtr() { (*static_cast<D*>(this))(m_ptr); }
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-01-27 17:07:36 +08:00
|
|
|
void reset(T * p = 0) { ccxScopedPtr< T >(p).swap(*this); }
|
2011-01-19 14:23:26 +08:00
|
|
|
T * get() const { return m_ptr; }
|
2011-01-27 17:07:36 +08:00
|
|
|
void swap(ccxScopedPtr & b) { T * tmp = b.m_ptr; b.m_ptr = m_ptr; m_ptr = tmp; }
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-01-19 14:23:26 +08:00
|
|
|
T & operator*() const { return * m_ptr; }
|
|
|
|
T * operator->() const { return m_ptr; }
|
|
|
|
operator bool () const { return m_ptr != 0; }
|
2011-01-15 18:05:35 +08:00
|
|
|
|
|
|
|
private:
|
2011-01-27 17:07:36 +08:00
|
|
|
ccxScopedPtr(const ccxScopedPtr&);
|
|
|
|
ccxScopedPtr & operator=(const ccxScopedPtr&);
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2011-01-27 17:07:36 +08:00
|
|
|
void operator==(const ccxScopedPtr& ) const;
|
|
|
|
void operator!=(const ccxScopedPtr& ) const;
|
2011-01-15 18:05:35 +08:00
|
|
|
|
|
|
|
T * m_ptr;
|
|
|
|
};
|
2011-01-19 14:23:26 +08:00
|
|
|
/**
|
|
|
|
@brief A simple scoped point for array.
|
|
|
|
*/
|
2011-01-27 17:07:36 +08:00
|
|
|
template< class T, class D = ccxNewArrayDeleter >
|
|
|
|
class CCX_DLL_PS ccxScopedArray // noncopyable
|
2011-01-19 14:23:26 +08:00
|
|
|
: private D
|
|
|
|
{
|
|
|
|
public:
|
2011-01-27 17:07:36 +08:00
|
|
|
explicit ccxScopedArray( T * p = 0 ) : m_ptr( p ) {}
|
|
|
|
~ccxScopedArray() { (*static_cast<D*>(this))(m_ptr); }
|
2011-01-19 14:23:26 +08:00
|
|
|
|
2011-01-27 17:07:36 +08:00
|
|
|
void reset(T * p = 0) { ccxScopedArray<T>(p).swap(*this); }
|
2011-01-19 14:23:26 +08:00
|
|
|
T * get() const { return m_ptr; }
|
2011-01-27 17:07:36 +08:00
|
|
|
void swap(ccxScopedArray & b) { T * tmp = b.m_ptr; b.m_ptr = m_ptr; m_ptr = tmp; }
|
2011-01-19 14:23:26 +08:00
|
|
|
|
|
|
|
T & operator[](int i) const { CCX_ASSERT(m_ptr && i >= 0); return m_ptr[i]; }
|
|
|
|
operator bool () const { return m_ptr != 0; }
|
|
|
|
|
|
|
|
private:
|
2011-01-27 17:07:36 +08:00
|
|
|
ccxScopedArray(ccxScopedArray const &);
|
|
|
|
ccxScopedArray & operator=(ccxScopedArray const &);
|
2011-01-19 14:23:26 +08:00
|
|
|
|
2011-01-27 17:07:36 +08:00
|
|
|
void operator==( ccxScopedArray const& ) const;
|
|
|
|
void operator!=( ccxScopedArray const& ) const;
|
2011-01-19 14:23:26 +08:00
|
|
|
|
|
|
|
T * m_ptr;
|
|
|
|
};
|
2011-01-15 18:05:35 +08:00
|
|
|
|
|
|
|
NS_CC_END;
|
|
|
|
|
|
|
|
#endif // __CCX_COMMON_H__
|