Version
menu

Wwise SDK 2025.1.0
AkBytesMem.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  Copyright (c) 2025 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 // AkBytesMem.h
28 
29 /// \file
30 /// IReadBytes / IWriteBytes implementation on a growing memory buffer. This
31 /// version uses the AK::MemoryMgr allocator.
32 
33 #pragma once
34 
35 #include <AK/IBytes.h>
38 
39 namespace AK
40 {
42  : public AK::IReadBytes
43  {
44  public:
45 
48  const void * in_pBytes,
49  AkInt32 in_cBytes
50  );
52 
53  // IReadBytes implementation
54 
56  void * in_pData,
57  AkInt32 in_cBytes,
58  AkInt32 & out_cRead
59  );
60 
61  // Public methods
62 
64  const void * in_pBytes,
65  AkInt32 in_cBytes
66  );
67 
68  private:
69  AkInt32 m_cBytes;
70  const AkUInt8 * m_pBytes;
71 
72  AkInt32 m_cPos;
73  };
74 
75  // In-memory buffer that can dynamically grow with use.
76  // If you need something more lean to wrap a fixed memory area, consider using AK::InMemoryBuffer instead.
78  : public AK::IWriteBuffer
79  {
80  public:
81 
84 
85  // IWriteBytes implementation
86 
88  const void * in_pData,
89  AkInt32 in_cBytes,
90  AkInt32& out_cWritten);
91 
92  // IWriteBuffer implementation
93 
94  AKSOUNDENGINE_API virtual bool Reserve(
95  AkInt32 in_cBytes
96  );
97 
98  AKSOUNDENGINE_API virtual AkInt32 Count() const;
99 
101  AkInt32 in_cBytes
102  );
103 
105 
106  AKSOUNDENGINE_API virtual AkUInt8 * Bytes() const;
107 
109 
110  AKSOUNDENGINE_API virtual void Clear();
111 
112  // Public methods
113 
115 
117 
118  // Reserves space for writing in_cBytes and returns the location, advancing the write pointer.
120  {
121  AkInt32 cPos = m_cPos;
122  AkInt32 cNewPos = cPos + in_cBytes;
123 
124  if ((m_cBytes >= cNewPos) || Grow(cNewPos))
125  {
126  m_cPos = cNewPos;
127  return m_pBytes + cPos;
128  }
129  else
130  return nullptr;
131  }
132 
133  template<class T>
135  {
136  static_assert(alignof(T) == 1, "T must have an alignment of 1 to avoid crashes due to unaligned writes on 32-bit ARM targets. Use Write<T> instead.");
137  return reinterpret_cast<T*>(GetWritePtr(sizeof(T)));
138  }
139 
140  template<class T>
141  bool Write(const T & in_data)
142  {
143  if (AkUInt8* pWrite = GetWritePtr(sizeof(T)))
144  {
145  AK::WriteUnaligned<T>(pWrite, in_data);
146  return true;
147  }
148  else
149  return false;
150  }
151 
152  private:
153  bool Grow(
154  AkInt32 in_cBytes
155  );
156 
157  AkInt32 m_cBytes;
158  AkUInt8 * m_pBytes;
159 
160  AkInt32 m_cPos;
161 
162  AkMemPoolId m_pool;
163  };
164 
166  : public AK::IWriteBytes
167  {
168  public:
169 
171 
173 
174  AKSOUNDENGINE_API void SetBuffer(AkUInt8* in_pBytes, AkInt32 in_cSize);
175 
176  // IWriteBytes implementation
177 
179  const void* in_pData,
180  AkInt32 in_cBytes,
181  AkInt32& out_cWritten);
182 
184 
186 
187  private:
188 
189  AkUInt8* m_pBytes;
190  AkInt32 m_cSize;
191  AkInt32 m_cPos;
192  };
193 }
virtual bool Reserve(AkInt32 in_cBytes)
Definition of data structures for AkAudioObject.
void Attach(const void *in_pBytes, AkInt32 in_cBytes)
virtual AkUInt8 * Detach()
Return pointer to buffer and clear internal pointer.
virtual ~ReadBytesMem()
uint8_t AkUInt8
Unsigned 8-bit integer.
AkInt32 AkMemPoolId
Memory pool ID.
Definition: AkTypedefs.h:49
virtual bool WriteBytes(const void *in_pData, AkInt32 in_cBytes, AkInt32 &out_cWritten)
int32_t AkInt32
Signed 32-bit integer.
#define AKSOUNDENGINE_API
bool Write(const T &in_data)
Definition: AkBytesMem.h:141
virtual ~WriteBytesBuffer()
AkInt32 GetPos() const
void SetBuffer(AkUInt8 *in_pBytes, AkInt32 in_cSize)
ReadBytesMem(const void *in_pBytes, AkInt32 in_cBytes)
virtual void SetCount(AkInt32 in_cBytes)
Set number of bytes written.
AkInt32 Size() const
virtual ~WriteBytesMem()
virtual AkUInt8 * Bytes() const
virtual AkInt32 Count() const
virtual bool ReadBytes(void *in_pData, AkInt32 in_cBytes, AkInt32 &out_cRead)
virtual bool WriteBytes(const void *in_pData, AkInt32 in_cBytes, AkInt32 &out_cWritten)
virtual void Clear()
Clear the buffer contents.
void SetMemPool(AkMemPoolId in_pool)
AkUInt8 * GetWritePtr(AkInt32 in_cBytes)
Definition: AkBytesMem.h:119

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