1
0

CCData.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCData.h"
  22. #include "base/CCConsole.h"
  23. NS_CC_BEGIN
  24. const Data Data::Null;
  25. Data::Data() :
  26. _bytes(nullptr),
  27. _size(0)
  28. {
  29. CCLOGINFO("In the empty constructor of Data.");
  30. }
  31. Data::Data(Data&& other) :
  32. _bytes(nullptr),
  33. _size(0)
  34. {
  35. CCLOGINFO("In the move constructor of Data.");
  36. move(other);
  37. }
  38. Data::Data(const Data& other) :
  39. _bytes(nullptr),
  40. _size(0)
  41. {
  42. CCLOGINFO("In the copy constructor of Data.");
  43. copy(other._bytes, other._size);
  44. }
  45. Data::~Data()
  46. {
  47. CCLOGINFO("deallocing Data: %p", this);
  48. clear();
  49. }
  50. Data& Data::operator= (const Data& other)
  51. {
  52. CCLOGINFO("In the copy assignment of Data.");
  53. copy(other._bytes, other._size);
  54. return *this;
  55. }
  56. Data& Data::operator= (Data&& other)
  57. {
  58. CCLOGINFO("In the move assignment of Data.");
  59. move(other);
  60. return *this;
  61. }
  62. void Data::move(Data& other)
  63. {
  64. clear();
  65. _bytes = other._bytes;
  66. _size = other._size;
  67. other._bytes = nullptr;
  68. other._size = 0;
  69. }
  70. bool Data::isNull() const
  71. {
  72. return (_bytes == nullptr || _size == 0);
  73. }
  74. unsigned char* Data::getBytes() const
  75. {
  76. return _bytes;
  77. }
  78. ssize_t Data::getSize() const
  79. {
  80. return _size;
  81. }
  82. void Data::copy(const unsigned char* bytes, const ssize_t size)
  83. {
  84. clear();
  85. if (size > 0)
  86. {
  87. _size = size;
  88. _bytes = (unsigned char*)malloc(sizeof(unsigned char) * _size);
  89. memcpy(_bytes, bytes, _size);
  90. }
  91. }
  92. void Data::fastSet(unsigned char* bytes, const ssize_t size)
  93. {
  94. _bytes = bytes;
  95. _size = size;
  96. }
  97. void Data::clear()
  98. {
  99. free(_bytes);
  100. _bytes = nullptr;
  101. _size = 0;
  102. }
  103. unsigned char* Data::takeBuffer(ssize_t* size)
  104. {
  105. auto buffer = getBytes();
  106. if (size)
  107. *size = getSize();
  108. fastSet(nullptr, 0);
  109. return buffer;
  110. }
  111. NS_CC_END