Numworks Epsilon  1.4.1
Graphing Calculator Operating System
storage.h
Go to the documentation of this file.
1 #ifndef ION_STORAGE_H
2 #define ION_STORAGE_H
3 
4 #include <stddef.h>
5 
6 namespace Ion {
7 
8 /* Storage : | Magic | Record1 | Record2 | ... | MagicĀ |
9  * | Magic | Size1(uint16_t) | Name1 | Body1 | Size2(uint16_t) | Name2 | Body2 | ... | Magic */
10 
11 class Storage {
12 public:
13  static Storage * sharedStorage();
14  class Record {
15  /* A Record is identified by the CRC32 on his name because:
16  * - a record is identified by its name which is unique
17  * - we cannot keep the address pointing to the name because if another
18  * record is modified, it might alter our record name address and keeping
19  * a buffer with the name will waste memory as we cannot forsee the size
20  * of the name. */
21  friend class Storage;
22  public:
23  enum class ErrorStatus {
24  None = 0,
25  NameTaken = 1,
26  NonCompliantName = 2,
29  };
30  struct Data {
31  const void * buffer;
32  size_t size;
33  };
34  Record(const char * name = nullptr);
35  bool operator==(const Record & other) const {
36  return m_nameCRC32 == other.m_nameCRC32;
37  }
38  bool isNull() const {
39  return m_nameCRC32 == 0;
40  }
41  const char * name() const {
42  return Storage::sharedStorage()->nameOfRecord(*this);
43  }
44  ErrorStatus setName(const char * name) {
45  return Storage::sharedStorage()->setNameOfRecord(*this, name);
46  }
47  Data value() const {
48  return Storage::sharedStorage()->valueOfRecord(*this);
49  }
51  return Storage::sharedStorage()->setValueOfRecord(*this, data);
52  }
53  void destroy() {
54  return Storage::sharedStorage()->destroyRecord(*this);
55  }
56  private:
57  uint32_t m_nameCRC32;
58  };
59  Storage();
60  size_t availableSize();
61  Record::ErrorStatus createRecord(const char * name, const void * data, size_t size);
62  int numberOfRecordsWithExtension(const char * extension);
63  Record recordWithExtensionAtIndex(const char * extension, int index);
64  Record recordNamed(const char * name);
66 private:
67  constexpr static uint32_t Magic = 0xEE0BDDBA;
68  constexpr static size_t k_storageSize = 4096;
69  constexpr static size_t k_maxRecordSize = (1 << sizeof(record_size_t)*8);
70 
71  /* Getters/Setters on recordID */
72  const char * nameOfRecord(const Record record);
73  Record::ErrorStatus setNameOfRecord(const Record record, const char * name);
74  Record::Data valueOfRecord(const Record record);
75  Record::ErrorStatus setValueOfRecord(const Record record, Record::Data data);
76  void destroyRecord(const Record record);
77 
78  /* Getters on address in buffer */
79  record_size_t sizeOfRecordStarting(char * start) const;
80  const char * nameOfRecordStarting(char * start) const;
81  const void * valueOfRecordStarting(char * start) const;
82 
83  /* Overriders */
84  size_t overrideSizeAtPosition(char * position, record_size_t size);
85  size_t overrideNameAtPosition(char * position, const char * name);
86  size_t overrideValueAtPosition(char * position, const void * data, record_size_t size);
87 
88  bool isNameTaken(const char * name, Record * recordToExclude = nullptr);
89  bool nameCompliant(const char * name) const;
90  char * endBuffer();
91  size_t sizeOfRecord(const char * name, size_t size) const;
92  bool slideBuffer(char * position, int delta);
93  class RecordIterator {
94  public:
95  RecordIterator(char * start) : m_recordStart(start) {}
96  char * operator*() { return m_recordStart; }
97  RecordIterator& operator++();
98  bool operator!=(const RecordIterator& it) const { return m_recordStart != it.m_recordStart; }
99  private:
100  char * m_recordStart;
101  };
102  RecordIterator begin() const {
103  if (sizeOfRecordStarting((char *)m_buffer) == 0) {
104  return nullptr;
105  }
106  return RecordIterator((char *)m_buffer);
107  };
108  RecordIterator end() const { return RecordIterator(nullptr); };
109 
110  uint32_t m_magicHeader;
111  char m_buffer[k_storageSize];
112  uint32_t m_magicFooter;
113 };
114 
115 }
116 
117 #endif
Record recordWithExtensionAtIndex(const char *extension, int index)
Definition: storage.cpp:96
def data
Definition: i18n.py:176
bool isNull() const
Definition: storage.h:38
Record::ErrorStatus createRecord(const char *name, const void *data, size_t size)
Definition: storage.cpp:60
unsigned short uint16_t
Definition: stdint.h:5
const void * buffer
Definition: storage.h:31
int numberOfRecordsWithExtension(const char *extension)
Definition: storage.cpp:84
Record recordNamed(const char *name)
Definition: storage.cpp:116
static Storage * sharedStorage()
Definition: storage.cpp:22
unsigned int uint32_t
Definition: stdint.h:6
Record(const char *name=nullptr)
Definition: storage.cpp:30
const char * name() const
Definition: storage.h:41
ErrorStatus setName(const char *name)
Definition: storage.h:44
Data value() const
Definition: storage.h:47
void start()
Definition: rt0.cpp:31
ErrorStatus setValue(Data data)
Definition: storage.h:50
size_t availableSize()
Definition: storage.cpp:56
bool operator==(const Record &other) const
Definition: storage.h:35
Definition: backlight.h:6
uint16_t record_size_t
Definition: storage.h:65