IBytes.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. /// \file
  21. /// AK::IReadBytes, AK::IWriteBytes simple serialization interfaces.
  22. #ifndef _AK_IBYTES_H
  23. #define _AK_IBYTES_H
  24. #include <wchar.h>
  25. #include <AK/Tools/Common/AkPlatformFuncs.h>
  26. namespace AK
  27. {
  28. /// Generic binary input interface.
  29. /// \akwarning
  30. /// The functions in this interface are not thread-safe, unless stated otherwise.
  31. /// \endakwarning
  32. class IReadBytes
  33. {
  34. public:
  35. ////////////////////////////////////////////////////////////////////////
  36. /// @name Interface
  37. //@{
  38. /// Reads some bytes into a buffer.
  39. /// \return True if the operation was successful, False otherwise
  40. virtual bool ReadBytes(
  41. void * in_pData, ///< Pointer to a buffer
  42. AkInt32 in_cBytes, ///< Size of the buffer (in bytes)
  43. AkInt32 & out_cRead ///< Returned number of read bytes
  44. ) = 0;
  45. //@}
  46. ////////////////////////////////////////////////////////////////////////
  47. /// @name Helpers
  48. //@{
  49. /// Reads a simple type or structure.
  50. /// \akwarning
  51. /// Not for object serialization.
  52. /// \endakwarning
  53. /// \return True if the operation was successful, False otherwise.
  54. template<class T>
  55. bool Read(
  56. T & out_data) ///< Data to be read
  57. {
  58. AkInt32 cRead;
  59. return ReadBytes(&out_data, sizeof(T), cRead);
  60. }
  61. /// Reads a simple type or structure.
  62. /// \warning This method does not allow for error checking. Use other methods when error cases need to be handled.
  63. /// \warning Not for object serialization.
  64. /// \return Read data
  65. template<class T>
  66. T Read()
  67. {
  68. T value;
  69. AkInt32 cRead;
  70. ReadBytes(&value, sizeof(T), cRead);
  71. return value;
  72. }
  73. /// Reads a string into a fixed-size buffer.
  74. /// \return True if the operation was successful, False otherwise. An insufficient buffer size does not cause failure.
  75. template<class CHAR_T>
  76. bool ReadString(
  77. CHAR_T * out_pszString, ///< Pointer to a fixed-size buffer
  78. AkInt32 in_nMax) ///< Maximum number of characters to be read in out_pszString, including the terminating NULL character
  79. {
  80. AkInt32 cChars;
  81. if (!Read<AkInt32>(cChars))
  82. return false;
  83. bool bRet = true;
  84. if (cChars > 0)
  85. {
  86. AkInt32 cRead;
  87. if (cChars < in_nMax)
  88. {
  89. ReadBytes(out_pszString, cChars * sizeof(CHAR_T), cRead);
  90. out_pszString[cChars] = 0;
  91. bRet = cRead == (AkInt32)(cChars * sizeof(CHAR_T));
  92. }
  93. else
  94. {
  95. ReadBytes(out_pszString, in_nMax * sizeof(CHAR_T), cRead);
  96. out_pszString[in_nMax - 1] = 0;
  97. bRet = cRead == (AkInt32)(in_nMax * sizeof(CHAR_T));
  98. if (bRet)
  99. {
  100. // Read extra characters in temp buffer.
  101. AkInt32 cRemaining = cChars - in_nMax;
  102. CHAR_T * pTemp = new CHAR_T[cRemaining];
  103. ReadBytes(pTemp, cRemaining * sizeof(CHAR_T), cRead);
  104. bRet = cRead == (AkInt32)(cRemaining * sizeof(CHAR_T));
  105. delete[] pTemp;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. out_pszString[0] = 0;
  112. }
  113. return bRet;
  114. }
  115. //@}
  116. };
  117. /// Generic binary output interface.
  118. /// \akwarning
  119. /// The functions in this interface are not thread-safe, unless stated otherwise.
  120. /// \endakwarning
  121. class IWriteBytes
  122. {
  123. public:
  124. ////////////////////////////////////////////////////////////////////////
  125. /// @name Interface
  126. //@{
  127. /// Writes some bytes from a buffer.
  128. /// \return True if the operation was successful, False otherwise
  129. virtual bool WriteBytes(
  130. const void * in_pData, ///< Pointer to a buffer
  131. AkInt32 in_cBytes, ///< Size of the buffer (in bytes)
  132. AkInt32 & out_cWritten ///< Returned number of written bytes
  133. ) = 0;
  134. //@}
  135. ////////////////////////////////////////////////////////////////////////
  136. /// @name Helpers
  137. //@{
  138. /// Writes a simple type or struct.
  139. /// \warning Not for object serialization.
  140. /// \return True if the operation was successful, False otherwise
  141. template<class T>
  142. bool Write(
  143. const T & in_data) ///< Data to be written
  144. {
  145. AkInt32 cWritten;
  146. return WriteBytes(&in_data, sizeof(T), cWritten);
  147. }
  148. /// Writes a unicode string.
  149. /// \return True if the operation was successful, False otherwise
  150. bool WriteString(
  151. const wchar_t * in_pszString) ///< String to be written
  152. {
  153. if (in_pszString != NULL)
  154. {
  155. size_t cChars = wcslen(in_pszString);
  156. if (!Write<AkUInt32>((AkUInt32)cChars))
  157. return false;
  158. AkInt32 cWritten = 0;
  159. AkInt32 cToWrite = (AkInt32)(cChars * sizeof(wchar_t));
  160. if (cChars > 0)
  161. {
  162. WriteBytes(in_pszString, cToWrite, cWritten);
  163. }
  164. return cWritten == cToWrite;
  165. }
  166. return Write<AkUInt32>(0);
  167. }
  168. /// Writes an ansi string.
  169. /// \return True if the operation was successful, False otherwise
  170. bool WriteString(
  171. const char * in_pszString) ///< String to be written
  172. {
  173. if (in_pszString != NULL)
  174. {
  175. size_t cChars = strlen(in_pszString);
  176. if (!Write<AkUInt32>((AkUInt32)cChars))
  177. return false;
  178. AkInt32 cWritten = 0;
  179. if (cChars > 0)
  180. {
  181. WriteBytes(in_pszString, (AkInt32)cChars, cWritten);
  182. }
  183. return cWritten == (AkInt32)cChars;
  184. }
  185. return Write<AkUInt32>(0);
  186. }
  187. //@}
  188. };
  189. /// Generic memory buffer interface.
  190. /// \akwarning
  191. /// The functions in this interface are not thread-safe, unless stated otherwise.
  192. /// \endakwarning
  193. class IWriteBuffer : public IWriteBytes
  194. {
  195. public:
  196. ////////////////////////////////////////////////////////////////////////
  197. /// @name Interface
  198. //@{
  199. /// Get the number of bytes written to the buffer.
  200. /// \return number of bytes written.
  201. virtual AkInt32 Count() const = 0;
  202. /// Get pointer to buffer.
  203. /// \return pointer to buffer.
  204. virtual AkUInt8 * Bytes() const = 0;
  205. /// Set number of bytes written.
  206. virtual void SetCount(AkInt32 in_cBytes) = 0;
  207. /// Allocate memory.
  208. /// \return true if allocation was successful.
  209. virtual bool Reserve(AkInt32 in_cBytes) = 0;
  210. /// Clear the buffer contents.
  211. virtual void Clear() = 0;
  212. /// Return pointer to buffer and clear internal pointer.
  213. virtual AkUInt8 * Detach() = 0;
  214. //@}
  215. };
  216. }
  217. #endif // _AK_IBYTES_H