xref: /aosp_15_r20/external/executorch/backends/apple/coreml/runtime/kvstore/types.hpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 //
2 // types.hpp
3 //
4 // Copyright © 2024 Apple Inc. All rights reserved.
5 //
6 // Please refer to the license found in the LICENSE file in the root directory of the source tree.
7 
8 #pragma once
9 
10 #include <string>
11 #include <system_error>
12 #include <variant>
13 #include <vector>
14 
15 namespace executorchcoreml {
16 namespace sqlite {
17 
18 struct Null {};
19 
20 struct UnOwnedBlob {
UnOwnedBlobexecutorchcoreml::sqlite::UnOwnedBlob21     inline explicit UnOwnedBlob(const void *data, size_t size)
22     :data(data), size(size)
23     {}
24 
25     const void *data;
26     size_t size;
27 };
28 
29 struct UnOwnedString {
UnOwnedStringexecutorchcoreml::sqlite::UnOwnedString30     inline UnOwnedString(const char *data, size_t size)
31     :data(data), size(size)
32     {}
33 
UnOwnedStringexecutorchcoreml::sqlite::UnOwnedString34     inline UnOwnedString(const std::string& string)
35     :data(string.c_str()), size(string.size())
36     {}
37 
emptyexecutorchcoreml::sqlite::UnOwnedString38     inline bool empty() const noexcept {
39         return size == 0;
40     }
41 
toStringexecutorchcoreml::sqlite::UnOwnedString42     inline std::string toString() const noexcept {
43         return std::string(data);
44     }
45 
46     const char *data;
47     size_t size;
48 };
49 
50 struct Blob {
copy_dataexecutorchcoreml::sqlite::Blob51     static inline void *copy_data(const void *data, size_t size) {
52         void *result = ::operator new(size);
53         std::memcpy(result, data, size);
54         return result;
55     }
56 
Blobexecutorchcoreml::sqlite::Blob57     inline Blob(const void *data, size_t size)
58     :data(copy_data(data, size)), size(size)
59     {}
60 
61     Blob(Blob const&) noexcept = delete;
62     Blob& operator=(Blob const&) noexcept = delete;
63 
Blobexecutorchcoreml::sqlite::Blob64     inline Blob(Blob&& other) noexcept
65     :data(std::exchange(other.data, nullptr)),
66     size(std::exchange(other.size, 0))
67     {}
68 
operator =executorchcoreml::sqlite::Blob69     inline Blob& operator=(Blob&& other) noexcept {
70         std::swap(data, other.data);
71         std::swap(size, other.size);
72         return *this;
73     }
74 
~Blobexecutorchcoreml::sqlite::Blob75     inline ~Blob() {
76         if (data) {
77             ::operator delete(data);
78         }
79     }
80 
toUnOwnedexecutorchcoreml::sqlite::Blob81     inline UnOwnedBlob toUnOwned() const noexcept {
82         return UnOwnedBlob(data, size);
83     }
84 
85     void *data = nullptr;
86     size_t size = 0;
87 };
88 
89 enum class StorageType: uint8_t {
90     Null,
91     Blob,
92     Text,
93     Double,
94     Integer
95 };
96 
97 using Value = std::variant<int64_t, double, std::string, Blob, Null>;
98 using UnOwnedValue = std::variant<int64_t, double, UnOwnedString, UnOwnedBlob, Null>;
99 
100 } // namespace sqlite
101 } // namespace executorchcoreml
102