CCData.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CCDATA_H__
  23. #define __CCDATA_H__
  24. #include "platform/CCPlatformMacros.h"
  25. #include <stdint.h> // for ssize_t on android
  26. #include <string> // for ssize_t on linux
  27. #include "platform/CCStdC.h" // for ssize_t on window
  28. /**
  29. * @addtogroup base
  30. * @js NA
  31. * @lua NA
  32. */
  33. NS_CC_BEGIN
  34. class CC_DLL Data
  35. {
  36. friend class Properties;
  37. public:
  38. /**
  39. * This parameter is defined for convenient reference if a null Data object is needed.
  40. */
  41. static const Data Null;
  42. /**
  43. * Constructor of Data.
  44. */
  45. Data();
  46. /**
  47. * Copy constructor of Data.
  48. */
  49. Data(const Data& other);
  50. /**
  51. * Copy constructor of Data.
  52. */
  53. Data(Data&& other);
  54. /**
  55. * Destructor of Data.
  56. */
  57. ~Data();
  58. /**
  59. * Overloads of operator=.
  60. */
  61. Data& operator= (const Data& other);
  62. /**
  63. * Overloads of operator=.
  64. */
  65. Data& operator= (Data&& other);
  66. /**
  67. * Gets internal bytes of Data. It will return the pointer directly used in Data, so don't delete it.
  68. *
  69. * @return Pointer of bytes used internal in Data.
  70. */
  71. unsigned char* getBytes() const;
  72. /**
  73. * Gets the size of the bytes.
  74. *
  75. * @return The size of bytes of Data.
  76. */
  77. ssize_t getSize() const;
  78. /** Copies the buffer pointer and its size.
  79. * @note This method will copy the whole buffer.
  80. * Developer should free the pointer after invoking this method.
  81. * @see Data::fastSet
  82. */
  83. void copy(const unsigned char* bytes, const ssize_t size);
  84. /** Fast set the buffer pointer and its size. Please use it carefully.
  85. * @param bytes The buffer pointer, note that it have to be allocated by 'malloc' or 'calloc',
  86. * since in the destructor of Data, the buffer will be deleted by 'free'.
  87. * @note 1. This method will move the ownship of 'bytes'pointer to Data,
  88. * 2. The pointer should not be used outside after it was passed to this method.
  89. * @see Data::copy
  90. */
  91. void fastSet(unsigned char* bytes, const ssize_t size);
  92. /**
  93. * Clears data, free buffer and reset data size.
  94. */
  95. void clear();
  96. /**
  97. * Check whether the data is null.
  98. *
  99. * @return True if the Data is null, false if not.
  100. */
  101. bool isNull() const;
  102. /**
  103. * Get the internal buffer of data and set data to empty state.
  104. *
  105. * The ownership of the buffer removed from the data object.
  106. * That is the user have to free the returned buffer.
  107. * The data object is set to empty state, that is internal buffer is set to nullptr
  108. * and size is set to zero.
  109. * Usage:
  110. * @code
  111. * Data d;
  112. * // ...
  113. * ssize_t size;
  114. * unsigned char* buffer = d.takeBuffer(&size);
  115. * // use buffer and size
  116. * free(buffer);
  117. * @endcode
  118. *
  119. * @param size Will fill with the data buffer size in bytes, if you do not care buffer size, pass nullptr.
  120. * @return the internal data buffer, free it after use.
  121. */
  122. unsigned char* takeBuffer(ssize_t* size);
  123. private:
  124. void move(Data& other);
  125. private:
  126. unsigned char* _bytes;
  127. ssize_t _size;
  128. };
  129. NS_CC_END
  130. /** @} */
  131. #endif // __CCDATA_H__