Version
menu_open
link
Wwise SDK 2018.1.11
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: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> 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  /// \warning The functions in this interface are not thread-safe, unless stated otherwise.
41  class IReadBytes
42  {
43  public:
44  ////////////////////////////////////////////////////////////////////////
45  /// @name Interface
46  //@{
47 
48  /// Reads some bytes into a buffer.
49  /// \return True if the operation was successful, False otherwise
50  virtual bool ReadBytes(
51  void * in_pData, ///< Pointer to a buffer
52  AkInt32 in_cBytes, ///< Size of the buffer (in bytes)
53  AkInt32 & out_cRead ///< Returned number of read bytes
54  ) = 0;
55 
56  //@}
57 
58  ////////////////////////////////////////////////////////////////////////
59  /// @name Helpers
60  //@{
61 
62  /// Reads a simple type or structure.
63  /// \warning Not for object serialization.
64  /// \return True if the operation was successful, False otherwise.
65  template<class T>
66  bool Read(
67  T & out_data ) ///< Data to be read
68  {
69  AkInt32 cRead;
70  return ReadBytes( &out_data, sizeof( T ), cRead );
71  }
72 
73  /// Reads a simple type or structure.
74  /// \warning This method does not allow for error checking. Use other methods when error cases need to be handled.
75  /// \warning Not for object serialization.
76  /// \return Read data
77  template<class T>
78  T Read()
79  {
80  T value;
81 
82  AkInt32 cRead;
83  ReadBytes( &value, sizeof( T ), cRead );
84 
85  return value;
86  }
87 
88  /// Reads a unicode string into a fixed-size buffer.
89  /// \return True if the operation was successful, False otherwise. An insufficient buffer size does not cause failure.
90  bool ReadString(
91  wchar_t * out_pszString, ///< Pointer to a fixed-size buffer
92  AkInt32 in_nMax ) ///< Maximum number of characters to be read in out_pszString, including the terminating NULL character
93  {
94  AkInt32 cChars;
95  if ( !Read<AkInt32>( cChars ) )
96  return false;
97 
98  bool bRet = true;
99 
100  if ( cChars > 0 )
101  {
102  AkInt32 cRead;
103 
104  if ( cChars < in_nMax )
105  {
106  ReadBytes( out_pszString, cChars * sizeof( wchar_t ), cRead );
107  out_pszString[ cChars ] = 0;
108 
109  bRet = cRead == (AkInt32)( cChars * sizeof( wchar_t ) );
110  }
111  else
112  {
113  ReadBytes( out_pszString, in_nMax * sizeof( wchar_t ), cRead );
114  out_pszString[ in_nMax - 1 ] = 0;
115 
116  bRet = cRead == (AkInt32)( in_nMax * sizeof(wchar_t));
117 
118  if ( bRet )
119  {
120  // Read extra characters in temp buffer.
121  AkInt32 cRemaining = cChars - in_nMax;
122 
123  wchar_t * pTemp = new wchar_t[ cRemaining ];
124 
125  ReadBytes( pTemp, cRemaining * sizeof( wchar_t ), cRead );
126 
127  bRet = cRead == (AkInt32)(cRemaining * sizeof(wchar_t));
128 
129  delete [] pTemp;
130  }
131  }
132  }
133  else
134  {
135  out_pszString[ 0 ] = 0;
136  }
137 
138  return bRet;
139  }
140  //@}
141  };
142 
143  /// Generic binary output interface.
144  /// \warning The functions in this interface are not thread-safe, unless stated otherwise.
145  class IWriteBytes
146  {
147  public:
148  ////////////////////////////////////////////////////////////////////////
149  /// @name Interface
150  //@{
151 
152  /// Writes some bytes from a buffer.
153  /// \return True if the operation was successful, False otherwise
154  virtual bool WriteBytes(
155  const void * in_pData, ///< Pointer to a buffer
156  AkInt32 in_cBytes, ///< Size of the buffer (in bytes)
157  AkInt32 & out_cWritten ///< Returned number of written bytes
158  ) = 0;
159 
160  //@}
161 
162  ////////////////////////////////////////////////////////////////////////
163  /// @name Helpers
164  //@{
165 
166  /// Writes a simple type or struct.
167  /// \warning Not for object serialization.
168  /// \return True if the operation was successful, False otherwise
169  template<class T>
170  bool Write(
171  const T & in_data ) ///< Data to be written
172  {
173  AkInt32 cWritten;
174  return WriteBytes( &in_data, sizeof( T ), cWritten );
175  }
176 
177  /// Writes a unicode string.
178  /// \return True if the operation was successful, False otherwise
179  bool WriteString(
180  const wchar_t * in_pszString ) ///< String to be written
181  {
182  if ( in_pszString != NULL )
183  {
184  size_t cChars = wcslen( in_pszString );
185  if ( !Write<AkUInt32>( (AkUInt32) cChars ) )
186  return false;
187 
188  AkInt32 cWritten = 0;
189  AkInt32 cToWrite = (AkInt32)( cChars * sizeof( wchar_t ) );
190 
191  if ( cChars > 0 )
192  {
193  WriteBytes( in_pszString, cToWrite, cWritten );
194  }
195 
196  return cWritten == cToWrite;
197  }
198  return Write<AkUInt32>( 0 );
199  }
200 
201  /// Writes an ansi string.
202  /// \return True if the operation was successful, False otherwise
203  bool WriteString(
204  const char * in_pszString ) ///< String to be written
205  {
206  if ( in_pszString != NULL )
207  {
208  size_t cChars = strlen( in_pszString );
209  if ( !Write<AkUInt32>( (AkUInt32) cChars ) )
210  return false;
211 
212  AkInt32 cWritten = 0;
213 
214  if ( cChars > 0 )
215  {
216  WriteBytes( in_pszString, (AkInt32) cChars, cWritten );
217  }
218 
219  return cWritten == (AkInt32) cChars;
220  }
221  return Write<AkUInt32>( 0 );
222  }
223  //@}
224  };
225 
226  /// Generic memory buffer interface.
227  /// \warning The functions in this interface are not thread-safe, unless stated otherwise.
228  class IWriteBuffer : public IWriteBytes
229  {
230  public:
231  ////////////////////////////////////////////////////////////////////////
232  /// @name Interface
233  //@{
234 
235  /// Get the number of bytes written to the buffer.
236  /// \return number of bytes written.
237  virtual AkInt32 Count() const = 0;
238 
239  /// Get pointer to buffer.
240  /// \return pointer to buffer.
241  virtual AkUInt8 * Bytes() const = 0;
242 
243  /// Set number of bytes written.
244  virtual void SetCount( AkInt32 in_cBytes ) = 0;
245 
246  /// Allocate memory.
247  /// \return true if allocation was successful.
248  virtual bool Reserve( AkInt32 in_cBytes ) = 0;
249 
250  /// Clear the buffer contents.
251  virtual void Clear() = 0;
252 
253  /// Return pointer to buffer and clear internal pointer.
254  virtual AkUInt8 * Detach() = 0;
255 
256  //@}
257  };
258 }
259 
260 #endif // _AK_IBYTES_H
bool Write(const T &in_data)
Definition: IBytes.h:170
uint8_t AkUInt8
Unsigned 8-bit integer.
Definition: AkTypes.h:77
virtual void SetCount(AkInt32 in_cBytes)=0
Set number of bytes written.
virtual bool Reserve(AkInt32 in_cBytes)=0
Audiokinetic namespace.
T Read()
Definition: IBytes.h:78
int32_t AkInt32
Signed 32-bit integer.
Definition: AkTypes.h:92
bool Read(T &out_data)
Definition: IBytes.h:66
virtual void Clear()=0
Clear the buffer contents.
virtual AkUInt8 * Detach()=0
Return pointer to buffer and clear internal pointer.
virtual AkUInt8 * Bytes() const =0
virtual AkInt32 Count() const =0
bool WriteString(const wchar_t *in_pszString)
Definition: IBytes.h:179
#define NULL
Definition: AkTypes.h:49
bool ReadString(wchar_t *out_pszString, AkInt32 in_nMax)
Definition: IBytes.h:90
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:79
bool WriteString(const char *in_pszString)
Definition: IBytes.h:203
virtual bool WriteBytes(const void *in_pData, AkInt32 in_cBytes, AkInt32 &out_cWritten)=0
virtual bool ReadBytes(void *in_pData, AkInt32 in_cBytes, AkInt32 &out_cRead)=0

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