1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef ARRAY_BUFFER_UTIL_H
16 #define ARRAY_BUFFER_UTIL_H
17 
18 #include <memory>
19 
20 namespace OHOS {
21 namespace IntellVoiceUtils {
22 template<class T>
23 class ArrayBufferUtil {
24 public:
25     ~ArrayBufferUtil() = default;
26 
GetData()27     T *GetData()
28     {
29         if (data_ == nullptr) {
30             return nullptr;
31         }
32 
33         return data_.get();
34     }
GetSize()35     uint32_t GetSize() const
36     {
37         return size_;
38     }
39 
40     template<typename C>
41     friend std::unique_ptr<ArrayBufferUtil<C>> CreateArrayBuffer(uint32_t size, const C *data);
42 
43 private:
44     ArrayBufferUtil() = default;
45     bool Init(uint32_t size, const T *data = nullptr);
46     std::unique_ptr<T[]> data_ = nullptr;
47     uint32_t size_ = 0;
48 };
49 
50 using Uint8ArrayBuffer = ArrayBufferUtil<uint8_t>;
51 template<class T> std::unique_ptr<ArrayBufferUtil<T>> CreateArrayBuffer(uint32_t size, const T *data = nullptr);
52 
53 }
54 }
55 #endif
56