Skip to content

Commit 6f36e38

Browse files
committed
DBArrayBase -> DBArrayAlloc static class
Break header dependencies on dbarray.h
1 parent ecde82e commit 6f36e38

7 files changed

Lines changed: 81 additions & 59 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ set(LCF_SOURCES
196196
set(LCF_HEADERS
197197
src/lcf/data.h
198198
src/lcf/dbarray.h
199+
src/lcf/dbarrayalloc.h
199200
src/lcf/dbbitarray.h
200201
src/lcf/dbstring.h
201202
src/lcf/encoder.h

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ liblcf_la_SOURCES = \
203203
lcfinclude_HEADERS = \
204204
src/lcf/data.h \
205205
src/lcf/dbarray.h \
206+
src/lcf/dbarrayalloc.h \
206207
src/lcf/dbbitarray.h \
207208
src/lcf/dbstring.h \
208209
src/lcf/encoder.h \

src/dbarray.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "lcf/dbarrayalloc.h"
12
#include "lcf/dbarray.h"
23
#include "lcf/dbstring.h"
34
#include <cassert>
@@ -11,11 +12,11 @@
1112

1213
namespace lcf {
1314

14-
constexpr DBArrayBase::size_type DBArrayBase::_empty_buf;
15+
constexpr DBArrayAlloc::size_type DBArrayAlloc::_empty_buf;
1516
constexpr DBString::size_type DBString::npos;
1617

1718
static ptrdiff_t HeaderSize(size_t align) {
18-
return std::max(sizeof(DBArrayBase::size_type), align);
19+
return std::max(sizeof(DBArrayAlloc::size_type), align);
1920
}
2021

2122
static size_t AllocSize(size_t size, size_t align) {
@@ -26,7 +27,7 @@ static void* Adjust(void* p, ptrdiff_t off) {
2627
return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(p) + off);
2728
}
2829

29-
void* DBArrayBase::alloc(size_type size, size_type field_size, size_type align) {
30+
void* DBArrayAlloc::alloc(size_type size, size_type field_size, size_type align) {
3031
if (size == 0) {
3132
return empty_buf();
3233
}
@@ -46,7 +47,7 @@ void* DBArrayBase::alloc(size_type size, size_type field_size, size_type align)
4647
return p;
4748
}
4849

49-
void DBArrayBase::free(void* p, size_type align) noexcept {
50+
void DBArrayAlloc::free(void* p, size_type align) noexcept {
5051
assert(p != nullptr);
5152
if (p != empty_buf()) {
5253
auto* raw = Adjust(p, -HeaderSize(align));

src/lcf/dbarray.h

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,18 @@
1717
#include <algorithm>
1818
#include <type_traits>
1919

20+
#include "lcf/dbarrayalloc.h"
2021
#include "lcf/scope_guard.h"
2122

2223
namespace lcf {
2324

24-
class DBArrayBase {
25-
public:
26-
using size_type = uint32_t;
27-
28-
protected:
29-
constexpr DBArrayBase() = default;
30-
31-
static void* alloc(size_type size, size_type field_size, size_type align);
32-
static void free(void* p, size_type align) noexcept;
33-
34-
static constexpr void* empty_buf() {
35-
return const_cast<size_type*>(&_empty_buf + 1);
36-
}
37-
38-
static size_type* get_size_ptr(void* p) {
39-
return reinterpret_cast<size_type*>(p) - 1;
40-
}
41-
42-
static const size_type* get_size_ptr(const void* p) {
43-
return reinterpret_cast<const size_type*>(p) - 1;
44-
}
45-
46-
private:
47-
static constexpr const size_type _empty_buf = 0;
48-
};
49-
5025
// An array data structure optimized for database storage.
5126
// Low memory footprint and not dynamically resizable.
5227
template <typename T>
53-
class DBArray : private DBArrayBase {
28+
class DBArray {
5429
public:
5530
using value_type = T;
56-
using DBArrayBase::size_type;
31+
using size_type = DBArrayAlloc::size_type;
5732

5833
using iterator = T*;
5934
using const_iterator = const T*;
@@ -123,15 +98,15 @@ class DBArray : private DBArrayBase {
12398
const_reverse_iterator crend() const { return rend(); }
12499

125100
bool empty() const { return size() == 0; }
126-
size_type size() const { return *get_size_ptr(_storage); }
101+
size_type size() const { return *DBArrayAlloc::get_size_ptr(_storage); }
127102

128103
private:
129104
T* alloc(size_t count) {
130-
return reinterpret_cast<T*>(DBArrayBase::alloc(count * sizeof(T), count, alignof(T)));
105+
return reinterpret_cast<T*>(DBArrayAlloc::alloc(count * sizeof(T), count, alignof(T)));
131106
}
132107

133108
void free(void* p) {
134-
DBArrayBase::free(p, alignof(T));
109+
DBArrayAlloc::free(p, alignof(T));
135110
}
136111

137112
template <typename F>
@@ -140,7 +115,7 @@ class DBArray : private DBArrayBase {
140115
void destroy() noexcept;
141116
void destroy_impl(size_type n) noexcept;
142117
private:
143-
void* _storage = DBArrayBase::empty_buf();
118+
void* _storage = DBArrayAlloc::empty_buf();
144119
};
145120

146121
template <typename T>
@@ -197,18 +172,16 @@ void DBArray<T>::construct(size_type count, const F& make) {
197172

198173
template <typename T>
199174
void DBArray<T>::destroy() noexcept {
200-
if (empty()) {
201-
return;
175+
if (_storage != DBArrayAlloc::empty_buf()) {
176+
destroy_impl(size());
177+
_storage = DBArrayAlloc::empty_buf();
202178
}
203-
destroy_impl(size());
204-
_storage = DBArrayBase::empty_buf();
205179
}
206180

207181
template <typename T>
208182
void DBArray<T>::destroy_impl(size_type n) noexcept {
209183
while (n > 0) {
210-
--n;
211-
data()[n].~T();
184+
data()[--n].~T();
212185
}
213186
free(_storage);
214187
}

src/lcf/dbarrayalloc.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of liblcf. Copyright (c) 2020 liblcf authors.
3+
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
4+
*
5+
* liblcf is Free/Libre Open Source Software, released under the MIT License.
6+
* For the full copyright and license information, please view the COPYING
7+
* file that was distributed with this source code.
8+
*/
9+
10+
#ifndef LCF_DBARRAYALLOC_H
11+
#define LCF_DBARRAYALLOC_H
12+
#include <utility>
13+
#include <string>
14+
#include <iterator>
15+
#include <cstdint>
16+
#include <limits>
17+
#include <algorithm>
18+
#include <type_traits>
19+
20+
namespace lcf {
21+
22+
struct DBArrayAlloc {
23+
using size_type = uint32_t;
24+
25+
static void* alloc(size_type size, size_type field_size, size_type align);
26+
static void free(void* p, size_type align) noexcept;
27+
28+
static constexpr void* empty_buf() {
29+
return const_cast<size_type*>(&_empty_buf + 1);
30+
}
31+
32+
static constexpr size_type* get_size_ptr(void* p) {
33+
return static_cast<size_type*>(p) - 1;
34+
}
35+
36+
static constexpr const size_type* get_size_ptr(const void* p) {
37+
return static_cast<const size_type*>(p) - 1;
38+
}
39+
40+
private:
41+
static constexpr const size_type _empty_buf = 0;
42+
};
43+
44+
} // namespace lcf
45+
46+
#endif

src/lcf/dbbitarray.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace lcf {
2424

2525
class DBBitProxy {
2626
public:
27-
using size_type = DBArrayBase::size_type;
27+
using size_type = DBArrayAlloc::size_type;
2828

2929
constexpr DBBitProxy() = default;
3030

@@ -109,10 +109,10 @@ class DBBitProxyIterator {
109109

110110
// An array data structure optimized for database storage.
111111
// Low memory footprint and not dynamically resizable.
112-
class DBBitArray : private DBArrayBase {
112+
class DBBitArray {
113113
public:
114114
using value_type = bool;
115-
using DBArrayBase::size_type;
115+
using size_type = DBArrayAlloc::size_type;
116116

117117
using iterator = DBBitProxyIterator<DBBitProxy>;
118118
using const_iterator = DBBitProxyIterator<const DBBitProxy>;
@@ -177,7 +177,7 @@ class DBBitArray : private DBArrayBase {
177177
const_reverse_iterator crend() const { return rend(); }
178178

179179
bool empty() const { return size() == 0; }
180-
size_type size() const { return *get_size_ptr(_storage); }
180+
size_type size() const { return *DBArrayAlloc::get_size_ptr(_storage); }
181181

182182
void set_all() { std::memset(_storage, 0xff, bytes_up_from_bits(size())); }
183183
void reset_all() { std::memset(_storage, 0, bytes_up_from_bits(size())); }
@@ -199,16 +199,16 @@ class DBBitArray : private DBArrayBase {
199199

200200
void* alloc(size_type bits) {
201201
auto bytes = bytes_up_from_bits(bits);
202-
return DBArrayBase::alloc(bytes, bits, alignof(size_type));
202+
return DBArrayAlloc::alloc(bytes, bits, alignof(size_type));
203203
}
204204

205205
void free(void* p) {
206-
DBArrayBase::free(p, alignof(size_type));
206+
DBArrayAlloc::free(p, alignof(size_type));
207207
}
208208

209209
void destroy() noexcept;
210210
private:
211-
void* _storage = DBArrayBase::empty_buf();
211+
void* _storage = DBArrayAlloc::empty_buf();
212212
};
213213

214214
inline bool operator==(const DBBitArray& l, const DBBitArray& r) { return std::equal(l.begin(), l.end(), r.begin(), r.end()); }
@@ -236,9 +236,9 @@ inline DBBitArray& DBBitArray::operator=(DBBitArray&& o) noexcept {
236236
}
237237

238238
void DBBitArray::destroy() noexcept {
239-
if (_storage != this->empty_buf()) {
239+
if (_storage != DBArrayAlloc::empty_buf()) {
240240
free(_storage);
241-
_storage = this->empty_buf();
241+
_storage = DBArrayAlloc::empty_buf();
242242
}
243243
}
244244

src/lcf/dbstring.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
#include <ostream>
2020

2121
#include "lcf/string_view.h"
22-
#include "lcf/dbarray.h"
22+
#include "lcf/dbarrayalloc.h"
2323

2424
namespace lcf {
2525

2626
// A custom string class optimized for database storage.
2727
// This string type is good for storing and retrieving values.
2828
// It is not good for string manipulation like insertion or concatenation.
29-
class DBString : public DBArrayBase {
29+
class DBString {
3030
public:
3131
using value_type = char;
3232
using size_type = uint32_t;
@@ -100,20 +100,20 @@ class DBString : public DBArrayBase {
100100
const_reverse_iterator crend() const { return rend(); }
101101

102102
bool empty() const { return size() == 0; }
103-
size_type size() const { return *this->get_size_ptr(_storage); }
103+
size_type size() const { return *DBArrayAlloc::get_size_ptr(_storage); }
104104

105105
private:
106106
char* alloc(size_t count) {
107-
return reinterpret_cast<char*>(DBArrayBase::alloc(count + 1, count, 1));
107+
return reinterpret_cast<char*>(DBArrayAlloc::alloc(count + 1, count, 1));
108108
}
109109
void free(void* p) {
110-
DBArrayBase::free(p, 1);
110+
DBArrayAlloc::free(p, 1);
111111
}
112112
void destroy() noexcept;
113113
char* construct_z(const char* s, size_t len);
114114
char* construct_sv(const char* s, size_t len);
115115
private:
116-
void* _storage = this->empty_buf();
116+
void* _storage = DBArrayAlloc::empty_buf();
117117
};
118118

119119
// This should be used over the conversion operator, so we can track all dbstr -> str instances
@@ -163,9 +163,9 @@ inline DBString& DBString::operator=(DBString&& o) noexcept {
163163
}
164164

165165
inline void DBString::destroy() noexcept {
166-
if (_storage != this->empty_buf()) {
166+
if (_storage != DBArrayAlloc::empty_buf()) {
167167
free(_storage);
168-
_storage = this->empty_buf();
168+
_storage = DBArrayAlloc::empty_buf();
169169
}
170170
}
171171

0 commit comments

Comments
 (0)