1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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 #ifndef NVS_HANDLE_LOCKED_HPP_
15 #define NVS_HANDLE_LOCKED_HPP_
16 
17 #include "nvs_handle_simple.hpp"
18 
19 namespace nvs {
20 
21 /**
22  * @brief A class which behaves the same as NVSHandleSimple, except that all public member functions are locked.
23  *
24  * This class follows the decorator design pattern. The reason why we don't want locks in NVSHandleSimple is that
25  * NVSHandleSimple can also be used by the C-API which locks its public functions already.
26  * Thus, we avoid double-locking.
27  *
28  * @note this class becomes responsible for its internal NVSHandleSimple object, i.e. it deletes the handle object on
29  * destruction
30  */
31 class NVSHandleLocked : public NVSHandle {
32 public:
33     /**
34      * @param handle The NVSHandleSimple instance which this class "decorates" (see Decorator design pattern).
35      *
36      * @note Lock::init() MUST be called BEFORE an instance of this class is used.
37      */
38     NVSHandleLocked(NVSHandleSimple *handle);
39 
40     virtual ~NVSHandleLocked();
41 
42     esp_err_t set_string(const char *key, const char* str) override;
43 
44     esp_err_t set_blob(const char *key, const void* blob, size_t len) override;
45 
46     esp_err_t get_string(const char *key, char* out_str, size_t len) override;
47 
48     esp_err_t get_blob(const char *key, void* out_blob, size_t len) override;
49 
50     esp_err_t get_item_size(ItemType datatype, const char *key, size_t &size) override;
51 
52     esp_err_t erase_item(const char* key) override;
53 
54     esp_err_t erase_all() override;
55 
56     esp_err_t commit() override;
57 
58     esp_err_t get_used_entry_count(size_t& usedEntries) override;
59 
60 protected:
61     esp_err_t set_typed_item(ItemType datatype, const char *key, const void* data, size_t dataSize) override;
62 
63     esp_err_t get_typed_item(ItemType datatype, const char *key, void* data, size_t dataSize) override;
64 
65 private:
66     NVSHandleSimple *handle;
67 };
68 
69 } // namespace nvs
70 
71 #endif // NVS_HANDLE_LOCKED_HPP_
72