DataTable.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 
34 
35 #ifndef H_DC_DATA_TABLE_INCLUDED
36 #define H_DC_DATA_TABLE_INCLUDED
37 
38 #include <Dynacoe/Component.h>
39 #include <unordered_map>
40 #include <vector>
41 #include <string>
42 
43 namespace Dynacoe {
44 
61 class DataTable : public Component {
62  public:
63  DataTable();
64 
65 
72  DataTable & Write(const std::string & name, int i);
73  DataTable & Write(const std::string & name, uint32_t i);
74  DataTable & Write(const std::string & name, uint64_t i);
75  DataTable & Write(const std::string & name, float i);
76  DataTable & Write(const std::string & name, double i);
77  DataTable & Write(const std::string & name, const std::string & i);
78  DataTable & Write(const std::string & name, const std::vector<uint8_t> & i);
79  DataTable & Write(const std::string & name, DataTable & i);
81 
89  DataTable & Read(const std::string & name, int & i);
90  DataTable & Read(const std::string & name, uint32_t & i);
91  DataTable & Read(const std::string & name, uint64_t & i);
92  DataTable & Read(const std::string & name, float & i);
93  DataTable & Read(const std::string & name, double & i);
94  DataTable & Read(const std::string & name, std::string & i);
95  DataTable & Read(const std::string & name, std::vector<uint8_t> & i);
96  DataTable & Read(const std::string & name, DataTable & i);
98 
101  void Remove(const std::string &);
102 
106  bool Query(const std::string &) const;
107 
108 
117  std::vector<uint8_t> WriteState();
118 
119 
127  bool ReadState(const std::vector<uint8_t> &);
128 
129 
133  void Clear();
134 
135 
136  private:
137  struct DataEntry {
138  DataEntry() :
139  begin(0),
140  count(0)
141  {}
142 
143  DataEntry(uint64_t b, uint64_t c) :
144  begin(b),
145  count(c){}
146  uint64_t begin;
147  uint64_t count;
148  };
149 
150  std::unordered_map<std::string, uint64_t> nameTable;
151  std::vector<DataEntry> entryTable;
152  std::vector<uint8_t> data;
153 
154  DataEntry & Find(const std::string &, uint64_t);
155  uint64_t GetNewBlock(uint64_t);
156  void Consolidate();
157 };
158 
159 
160 
161 }
162 
163 
164 
165 #endif
std::vector< uint8_t > WriteState()
Writes a pure data-form of the table's contents. This can then be passed into ReadState() turn the ca...
Definition: AssetID.h:37
void Clear()
Removes all stored associations and data entries, restoring the container back to a default state...
void Remove(const std::string &)
Removes any datum-name association.
bool ReadState(const std::vector< uint8_t > &)
Sets the DataTable state to reflect the state defined by the input buffer.
A container meant to share values across program invocations.
Definition: DataTable.h:61
bool Query(const std::string &) const
Returns whether there is a datum-name association with the given name.
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66