scsl  0.2.0
Shimmering Clarity Standard Library
Dictionary.h
Go to the documentation of this file.
1 
24 
25 #ifndef SCSL_DICTIONARY_H
26 #define SCSL_DICTIONARY_H
27 
28 
29 #include <cstdint>
30 
31 #include "Arena.h"
32 #include "TLV.h"
33 
34 
35 static constexpr uint8_t DICTIONARY_TAG_KEY = 1;
36 static constexpr uint8_t DICTIONARY_TAG_VAL = 2;
37 
38 
39 namespace scsl {
40 
41 
42 /*
43  * A Dictionary is a collection of key-value pairs, similar to how
44  * a dictionary is a mapping of names to definitions.
45  */
58 class Dictionary {
59 public:
63  Dictionary(Arena &arena) :
64  arena(arena),
65  kTag(DICTIONARY_TAG_KEY),
66  vTag(DICTIONARY_TAG_VAL)
67  {};
68 
74  Dictionary(Arena &arena, uint8_t kt, uint8_t vt) :
75  arena(arena),
76  kTag(kt),
77  vTag(vt)
78  {};
79 
86  bool Lookup(const char *key, uint8_t klen, TLV::Record &res);
87 
103  int Set(const char *key, uint8_t klen, const char *val,
104  uint8_t vlen);
105 
111  bool Contains(const char *key, uint8_t klen);
112 
118  bool Delete(const char *key, uint8_t klen);
119 
120 
126  int DumpToFile(const char *path);
127 
133  friend std::ostream &operator<<(std::ostream &os,
134  const Dictionary &dictionary);
135 private:
136  uint8_t *seek(const char *key, uint8_t klen);
137 
138  bool spaceAvailable(uint8_t klen, uint8_t vlen);
139 
140  Arena &arena;
141  uint8_t kTag;
142  uint8_t vTag;
143 };
144 
145 
146 } // namespace scsl
147 
148 #endif
Memory management using an arena.
TLV.h implements basic tag-length-value records.
Definition: Arena.h:80
Definition: Dictionary.h:58
Dictionary(Arena &arena, uint8_t kt, uint8_t vt)
Definition: Dictionary.h:74
Dictionary(Arena &arena)
Definition: Dictionary.h:63
bool Lookup(const char *key, uint8_t klen, TLV::Record &res)
Definition: Dictionary.cc:37
bool Contains(const char *key, uint8_t klen)
Definition: Dictionary.cc:114
int Set(const char *key, uint8_t klen, const char *val, uint8_t vlen)
Definition: Dictionary.cc:60
bool Delete(const char *key, uint8_t klen)
Definition: Dictionary.cc:121
friend std::ostream & operator<<(std::ostream &os, const Dictionary &dictionary)
Definition: Dictionary.cc:157
int DumpToFile(const char *path)
Definition: Dictionary.cc:184
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
Definition: TLV.h:40