libASPL
Loading...
Searching...
No Matches
Storage.hpp
Go to the documentation of this file.
1// Copyright (c) libASPL authors
2// Licensed under MIT
3
4//! @file aspl/Storage.hpp
5//! @brief Plugin persistent storage.
6
7#pragma once
8
9#include <aspl/Context.hpp>
10
11#include <CoreAudio/AudioServerPlugIn.h>
12
13#include <memory>
14#include <string>
15#include <utility>
16#include <vector>
17
18namespace aspl {
19
20//! Plugin persistent storage.
21//!
22//! Storage data persists across audio server restart and rebooting.
23//! Storage keys for one plug-in do not collide with the keys for other plug-ins.
24//!
25//! This class is a wrapper for "Storage Operations" API in
26//! AudioServerPlugInHostInterface, which is stored in Context.Host. It becomes
27//! available during driver initialization; until that, attempt to use Storage
28//! methods will lead to errors.
30{
31public:
32 //! Construct storage.
33 explicit Storage(std::shared_ptr<Context> context);
34
35 Storage(const Storage&) = delete;
36 Storage& operator=(const Storage&) = delete;
37
38 //! Get context.
39 std::shared_ptr<const Context> GetContext() const;
40
41 //! Read CFData value from storage and decode it into byte array.
42 //! @remarks
43 //! Returns error if value does not exist or has wrong type.
44 //! @note
45 //! Uses Context.Host.CopyFromStorage.
46 std::pair<std::vector<UInt8>, bool> ReadBytes(std::string key) const;
47
48 //! Read CFString value from storage and decode it into UTF-8 string.
49 //! @remarks
50 //! Returns error if value does not exist or has wrong type.
51 //! @note
52 //! Uses Context.Host.CopyFromStorage.
53 std::pair<std::string, bool> ReadString(std::string key) const;
54
55 //! Read CFBoolean value from storage and decode it into bool.
56 //! @remarks
57 //! Returns error if value does not exist or has wrong type.
58 //! @note
59 //! Uses Context.Host.CopyFromStorage.
60 std::pair<bool, bool> ReadBoolean(std::string key) const;
61
62 //! Read CFNumber value from storage and decode it into SInt64.
63 //! @remarks
64 //! Returns error if value does not exist, has wrong type,
65 //! or can not be represented as SInt64 without loss.
66 //! @note
67 //! Uses Context.Host.CopyFromStorage.
68 std::pair<SInt64, bool> ReadInt(std::string key) const;
69
70 //! Read CFNumber value from storage and decode it into Float64.
71 //! @remarks
72 //! Returns error if value does not exist, has wrong type,
73 //! or can not be represented as Float64 without loss.
74 //! @note
75 //! Uses Context.Host.CopyFromStorage.
76 std::pair<Float64, bool> ReadFloat(std::string key) const;
77
78 //! Read CFPropertyList value from storage and return it.
79 //! @remarks
80 //! Returns error if value does not exist.
81 //! Caller is responsible to release returned value.
82 //! @note
83 //! Uses Context.Host.CopyFromStorage.
84 std::pair<CFPropertyListRef, bool> ReadCustom(std::string key) const;
85
86 //! Encode byte array into CFData value and write it to storage.
87 //! @remarks
88 //! Returns error if value can not be encoded or written.
89 //! @note
90 //! Uses Context.Host.WriteToStorage.
91 bool WriteBytes(std::string key, std::vector<UInt8> value);
92
93 //! Encode C++ string into CFString value and write it to storage.
94 //! @remarks
95 //! Returns error if value can not be encoded or written.
96 //! @note
97 //! Uses Context.Host.WriteToStorage.
98 bool WriteString(std::string key, std::string value);
99
100 //! Encode bool into CFBoolean value and write it to storage.
101 //! @remarks
102 //! Returns error if value can not be encoded or written.
103 //! @note
104 //! Uses Context.Host.WriteToStorage.
105 bool WriteBoolean(std::string key, bool value);
106
107 //! Encode SInt64 into CFNumber value and write it to storage.
108 //! @remarks
109 //! Returns error if value can not be encoded or written.
110 //! @note
111 //! Uses Context.Host.WriteToStorage.
112 bool WriteInt(std::string key, SInt64 value);
113
114 //! Encode Float64 into CFNumber value and write it to storage.
115 //! @remarks
116 //! Returns error if value can not be encoded or written.
117 //! @note
118 //! Uses Context.Host.WriteToStorage.
119 bool WriteFloat(std::string key, Float64 value);
120
121 //! Write CFPropertyList value to storage.
122 //! @remarks
123 //! Returns error if value can not be encoded or written.
124 //! Does not take ownership of the value.
125 //! @note
126 //! Uses Context.Host.WriteToStorage.
127 bool WriteCustom(std::string key, CFPropertyListRef value);
128
129 //! Delete value from storage.
130 //! @remarks
131 //! Returns error if value does not exist or can not be deleted.
132 //! @note
133 //! Uses Context.Host.DeleteFromStorage.
134 bool Delete(std::string key);
135
136private:
137 template <class T>
138 std::pair<T, bool> CopyFromStorage_(const char* type, std::string key) const;
139
140 template <class T>
141 bool WriteToStorage_(const char* type, std::string key, T value);
142
143 bool DeleteFromStorage_(std::string key);
144
145 const std::shared_ptr<Context> context_;
146};
147
148} // namespace aspl
Context.
Doubly-buffered value with non-blocking read and blocking write.
Plugin persistent storage.
Definition Storage.hpp:30
std::pair< std::vector< UInt8 >, bool > ReadBytes(std::string key) const
Read CFData value from storage and decode it into byte array.
bool WriteString(std::string key, std::string value)
Encode C++ string into CFString value and write it to storage.
std::pair< CFPropertyListRef, bool > ReadCustom(std::string key) const
Read CFPropertyList value from storage and return it.
bool WriteBoolean(std::string key, bool value)
Encode bool into CFBoolean value and write it to storage.
bool WriteBytes(std::string key, std::vector< UInt8 > value)
Encode byte array into CFData value and write it to storage.
std::pair< SInt64, bool > ReadInt(std::string key) const
Read CFNumber value from storage and decode it into SInt64.
std::shared_ptr< const Context > GetContext() const
Get context.
std::pair< std::string, bool > ReadString(std::string key) const
Read CFString value from storage and decode it into UTF-8 string.
bool WriteFloat(std::string key, Float64 value)
Encode Float64 into CFNumber value and write it to storage.
std::pair< bool, bool > ReadBoolean(std::string key) const
Read CFBoolean value from storage and decode it into bool.
bool WriteInt(std::string key, SInt64 value)
Encode SInt64 into CFNumber value and write it to storage.
std::pair< Float64, bool > ReadFloat(std::string key) const
Read CFNumber value from storage and decode it into Float64.
bool WriteCustom(std::string key, CFPropertyListRef value)
Write CFPropertyList value to storage.
Storage(std::shared_ptr< Context > context)
Construct storage.
bool Delete(std::string key)
Delete value from storage.