Otclient  14/8/2020
packed_storage.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef STDEXT_PACKEDSTORAGE_H
24 #define STDEXT_PACKEDSTORAGE_H
25 
26 #include "types.h"
27 #include "packed_any.h"
28 
29 namespace stdext {
30 
31 // disable memory alignment
32 #pragma pack(push,1)
33 
34 // this class was designed to use less memory as possible
35 template<typename Key, typename SizeType = uint8>
37  struct value_pair {
38  Key id;
39  packed_any value;
40  };
41 
42 public:
43  packed_storage() : m_values(nullptr), m_size(0) { }
44  ~packed_storage() { delete[] m_values; }
45 
46  template<typename T>
47  void set(Key id, const T& value) {
48  for(SizeType i=0;i<m_size;++i) {
49  if(m_values[i].id == id) {
50  m_values[i].value = value;
51  return;
52  }
53  }
54  auto tmp = new value_pair[m_size+1];
55  if(m_size > 0) {
56  std::copy(m_values, m_values + m_size, tmp);
57  delete[] m_values;
58  }
59  m_values = tmp;
60  m_values[m_size++] = { id, packed_any(value) };
61  }
62 
63  bool remove(Key id) {
64  auto begin = m_values;
65  auto end = m_values + m_size;
66  auto it = std::find_if(begin, end, [=](const value_pair& pair) -> bool { return pair.id == id; } );
67  if(it == end)
68  return false;
69  int pos = it - begin;
70  auto tmp = new value_pair[m_size-1];
71  std::copy(begin, begin + pos, tmp);
72  std::copy(begin + pos + 1, end, tmp + pos);
73  delete[] m_values;
74  m_values = tmp;
75  m_size--;
76  return true;
77  }
78 
79  template<typename T>
80  T get(Key id) const {
81  for(SizeType i=0;i<m_size;++i)
82  if(m_values[i].id == id)
83  return packed_any_cast<T>(m_values[i].value);
84  return T();
85  }
86 
87  bool has(Key id) const {
88  for(SizeType i=0;i<m_size;++i)
89  if(m_values[i].id == id)
90  return true;
91  return false;
92  }
93 
94  void clear() {
95  if(m_values)
96  delete [] m_values;
97  m_values = nullptr;
98  m_size = 0;
99  }
100 
101  std::size_t size() { return m_size; }
102 
103 private:
104  value_pair *m_values;
105  SizeType m_size;
106 };
107 
108 // restore memory alignment
109 #pragma pack(pop)
110 
111 }
112 
113 #endif
stdext::packed_storage::size
std::size_t size()
Definition: packed_storage.h:101
stdext::packed_storage::get
T get(Key id) const
Definition: packed_storage.h:80
stdext::packed_storage::packed_storage
packed_storage()
Definition: packed_storage.h:43
stdext::packed_any
Definition: packed_any.h:41
types.h
stdext::packed_storage::clear
void clear()
Definition: packed_storage.h:94
Fw::Key
Key
Definition: const.h:57
stdext::packed_storage
Definition: packed_storage.h:36
packed_any.h
stdext::packed_storage::has
bool has(Key id) const
Definition: packed_storage.h:87
stdext::packed_storage::set
void set(Key id, const T &value)
Definition: packed_storage.h:47
stdext::packed_storage::remove
bool remove(Key id)
Definition: packed_storage.h:63
stdext
Definition: any.h:30
stdext::packed_storage::~packed_storage
~packed_storage()
Definition: packed_storage.h:44