2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
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 __CCDATA_H__
|
|
|
|
#define __CCDATA_H__
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2013-12-23 13:45:31 +08:00
|
|
|
#include <stdint.h> // for ssize_t on android
|
|
|
|
#include <string> // for ssize_t on linux
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCStdC.h" // for ssize_t on window
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2015-03-19 10:33:11 +08:00
|
|
|
/**
|
2015-03-24 11:15:40 +08:00
|
|
|
* @addtogroup base
|
2015-03-18 20:51:18 +08:00
|
|
|
* @js NA
|
2015-03-17 20:49:00 +08:00
|
|
|
* @lua NA
|
2015-03-18 20:51:18 +08:00
|
|
|
*/
|
2015-03-27 12:01:20 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
class CC_DLL Data
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-05-15 08:24:27 +08:00
|
|
|
friend class Properties;
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
public:
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
|
|
|
* This parameter is defined for convenient reference if a null Data object is needed.
|
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
static const Data Null;
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
|
|
|
* Constructor of Data.
|
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
Data();
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
|
|
|
* Copy constructor of Data.
|
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
Data(const Data& other);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
|
|
|
* Copy constructor of Data.
|
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
Data(Data&& other);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
|
|
|
* Destructor of Data.
|
|
|
|
*/
|
2013-10-12 15:25:45 +08:00
|
|
|
~Data();
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
2015-09-22 16:08:23 +08:00
|
|
|
* Overloads of operator=.
|
2015-03-17 20:49:00 +08:00
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
Data& operator= (const Data& other);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2015-03-17 20:49:00 +08:00
|
|
|
/**
|
2015-09-22 16:08:23 +08:00
|
|
|
* Overloads of operator=.
|
2015-03-17 20:49:00 +08:00
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
Data& operator= (Data&& other);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
/**
|
2015-09-22 16:08:23 +08:00
|
|
|
* Gets internal bytes of Data. It will return the pointer directly used in Data, so don't delete it.
|
2015-03-17 20:49:00 +08:00
|
|
|
*
|
|
|
|
* @return Pointer of bytes used internal in Data.
|
2013-10-12 15:25:45 +08:00
|
|
|
*/
|
|
|
|
unsigned char* getBytes() const;
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
/**
|
2015-03-17 20:49:00 +08:00
|
|
|
* Gets the size of the bytes.
|
|
|
|
*
|
|
|
|
* @return The size of bytes of Data.
|
2013-10-12 15:25:45 +08:00
|
|
|
*/
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t getSize() const;
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
/** Copies the buffer pointer and its size.
|
|
|
|
* @note This method will copy the whole buffer.
|
|
|
|
* Developer should free the pointer after invoking this method.
|
|
|
|
* @see Data::fastSet
|
2013-10-12 15:25:45 +08:00
|
|
|
*/
|
2014-07-18 07:44:39 +08:00
|
|
|
void copy(const unsigned char* bytes, const ssize_t size);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-12-20 21:41:20 +08:00
|
|
|
/** Fast set the buffer pointer and its size. Please use it carefully.
|
2013-12-18 14:58:17 +08:00
|
|
|
* @param bytes The buffer pointer, note that it have to be allocated by 'malloc' or 'calloc',
|
|
|
|
* since in the destructor of Data, the buffer will be deleted by 'free'.
|
2013-12-20 21:41:20 +08:00
|
|
|
* @note 1. This method will move the ownship of 'bytes'pointer to Data,
|
|
|
|
* 2. The pointer should not be used outside after it was passed to this method.
|
2013-12-18 14:58:17 +08:00
|
|
|
* @see Data::copy
|
|
|
|
*/
|
|
|
|
void fastSet(unsigned char* bytes, const ssize_t size);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-17 20:49:00 +08:00
|
|
|
* Clears data, free buffer and reset data size.
|
|
|
|
*/
|
2013-12-20 21:41:20 +08:00
|
|
|
void clear();
|
2016-04-29 09:29:54 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-17 20:49:00 +08:00
|
|
|
* Check whether the data is null.
|
|
|
|
*
|
2015-11-04 16:09:58 +08:00
|
|
|
* @return True if the Data is null, false if not.
|
2015-03-17 20:49:00 +08:00
|
|
|
*/
|
2013-12-18 14:58:17 +08:00
|
|
|
bool isNull() const;
|
2016-04-29 09:29:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the internal buffer of data and set data to empty state.
|
|
|
|
*
|
|
|
|
* The ownership of the buffer removed from the data object.
|
|
|
|
* That is the user have to free the returned buffer.
|
|
|
|
* The data object is set to empty state, that is internal buffer is set to nullptr
|
|
|
|
* and size is set to zero.
|
|
|
|
* Usage:
|
|
|
|
* <pre>
|
|
|
|
* {@code
|
|
|
|
* Data d;
|
|
|
|
* // ...
|
|
|
|
* ssize_t size;
|
|
|
|
* unsigned char* buffer = d.takeBuffer(&size);
|
|
|
|
* // use buffer and size
|
|
|
|
* free(buffer);
|
|
|
|
* }
|
|
|
|
* </pre
|
|
|
|
*
|
|
|
|
* @param size Will fill with the data buffer size in bytes, if you do not care buffer size, pass nullptr.
|
|
|
|
* @return the internal data buffer, free it after use.
|
|
|
|
*/
|
|
|
|
unsigned char* takeBuffer(ssize_t* size);
|
2013-12-18 14:58:17 +08:00
|
|
|
private:
|
|
|
|
void move(Data& other);
|
2016-04-29 09:29:54 +08:00
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
private:
|
|
|
|
unsigned char* _bytes;
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t _size;
|
2013-10-12 15:25:45 +08:00
|
|
|
};
|
|
|
|
|
2015-03-24 11:15:40 +08:00
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-27 12:01:20 +08:00
|
|
|
/** @} */
|
2013-10-12 15:25:45 +08:00
|
|
|
#endif // __CCDATA_H__
|