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

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise