Uniot Core
0.8.1
Loading...
Searching...
No Matches
CBORStorage.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2020 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21// doc: https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html
22#include <Storage.h>
23#include <CBORObject.h>
24
25namespace uniot
26{
38class CBORStorage : public Storage
39{
40public:
46 CBORStorage(const String &path) : Storage(path)
47 {
48 }
49
53 virtual ~CBORStorage() {}
54
61 {
62 return mCbor;
63 }
64
74 virtual bool store() override
75 {
76 if (mCbor.dirty())
77 {
78 mData = mCbor.build();
79 return Storage::store();
80 }
81 return true;
82 }
83
92 virtual bool restore() override
93 {
94 auto success = Storage::restore();
95 if (success)
96 {
97 mCbor.read(mData);
98 }
99 return success;
100 }
101
110 virtual bool clean() override
111 {
112 mCbor.clean();
113 return Storage::clean();
114 }
115
116protected:
118};
119
120} // namespace uniot
Definition CBORObject.h:40
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:92
virtual ~CBORStorage()
Virtual destructor.
Definition CBORStorage.h:53
virtual bool clean() override
Clean the CBOR object and remove the storage file.
Definition CBORStorage.h:110
CBORObject mCbor
The CBOR object used for data serialization/deserialization.
Definition CBORStorage.h:117
virtual bool store() override
Store the CBOR object to the filesystem.
Definition CBORStorage.h:74
CBORStorage(const String &path)
Constructs a new CBORStorage object.
Definition CBORStorage.h:46
CBORObject & object()
Get access to the underlying CBORObject.
Definition CBORStorage.h:60
Storage(const String &path)
Constructs a Storage object for a specific file.
Definition Storage.h:74
virtual bool clean()
Clears data and removes the file from the file system.
Definition Storage.h:189
Bytes mData
The byte array containing the data to be stored or the loaded data.
Definition Storage.h:205
virtual bool store()
Writes the current data to the file system.
Definition Storage.h:135
virtual bool restore()
Loads data from the file system into memory.
Definition Storage.h:165
Contains all classes and functions related to the Uniot Core.