IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
heap_varstring.hxx
Go to the documentation of this file.
1
3
4#pragma once
6#include <ice/varstring.hxx>
8
9namespace ice
10{
11
12 template<typename CharT = char>
14 {
15 static_assert(sizeof(CharT) == 1, "Wider characters are not supported yet!");
16
18 using CharType = CharT;
21 using ReverseIterator = std::reverse_iterator<CharType*>;
22 using ConstIterator = CharType const*;
23 using ConstReverseIterator = std::reverse_iterator<CharType const*>;
26
29
30 constexpr explicit HeapVarString(ice::Allocator& alloc) noexcept;
31 constexpr HeapVarString(ice::Allocator& allocator, ice::BasicString<CharType> string) noexcept;
32 constexpr ~HeapVarString() noexcept;
33
34 constexpr HeapVarString(HeapVarString&& other) noexcept;
35 constexpr HeapVarString(HeapVarString const& other) noexcept;
36
37 constexpr auto data() const noexcept -> ValueType*;
38 constexpr auto size() const noexcept -> SizeType;
39
40 constexpr void clear() noexcept;
41 constexpr auto deserialize(ice::Data data) noexcept -> ice::Data;
42
43 constexpr auto data_view() const noexcept -> ice::Data;
44
45 constexpr operator ice::BasicString<CharType>() const noexcept;
46 constexpr operator ice::VarStringBase<CharType>() const noexcept;
47 };
48
49 namespace varstring
50 {
51
52 inline auto allocate_exact(ice::Allocator& alloc, ice::ncount size, ice::usize& out_bytes) noexcept -> char*
53 {
54 if (size == 0_B)
55 {
56 return nullptr;
57 }
58
59 // Allocate enough for: bytes + size + '\0'
60 ice::usize const final_size = calc_required_size(size) + 1_B;
61 ice::Memory const result = alloc.allocate(final_size);
62 out_bytes = write_size(result.location, size);
63 return reinterpret_cast<char*>(result.location);
64 }
65
66 inline auto create(ice::Allocator& alloc, ice::String str) noexcept -> char*
67 {
68 ice::ncount const str_size = str.size();
69
70 ice::usize bytes = 0_B;
71 char* const data = allocate_exact(alloc, str_size, bytes);
72 if (data != nullptr)
73 {
74 ice::memcpy(data + bytes.value, str.begin(), str_size.bytes());
75 data[bytes.value + str_size.native()] = '\0';
76 }
77 return data;
78 }
79
80 } // namespace string::detail
81
82 template<typename CharT>
84 : _allocator{ ice::addressof(alloc) }
85 , _data{ nullptr }
86 {
87 }
88
89 template<typename CharT>
91 : _allocator{ ice::addressof(alloc) }
93 {
94 }
95
96 template<typename CharT>
98 : _allocator{ other._allocator }
99 , _data{ ice::exchange(other._data, nullptr) }
100 {
101 }
102
103 template<typename CharT>
105 : HeapVarString{ other._allocator, ice::String{ other } }
106 {
107 }
108
109 template<typename CharT>
110 inline constexpr HeapVarString<CharT>::~HeapVarString() noexcept
111 {
112 if (_data != nullptr)
113 {
114 _allocator->deallocate(_data);
115 }
116 }
117
118 template<typename CharT>
119 inline constexpr auto HeapVarString<CharT>::data() const noexcept -> ValueType*
120 {
122 }
123
124 template<typename CharT>
125 inline constexpr auto HeapVarString<CharT>::size() const noexcept -> SizeType
126 {
128 }
129
130 template<typename CharT>
131 inline constexpr void HeapVarString<CharT>::clear() noexcept
132 {
133 _allocator->deallocate(std::exchange(_data, nullptr));
134 }
135
136 template<typename CharT>
138 {
139 ICE_ASSERT_CORE(data.size >= 2_B); // 1 byte for size + 1 for a single character
140 this->clear(); // Clear the current contents
141
142 ice::usize bytes;
143 ice::ncount const size = ice::varstring::read_size(reinterpret_cast<char const*>(data.location), bytes);
144 if (size > 0)
145 {
146 char* const new_str = ice::varstring::allocate_exact(*_allocator, size, bytes);
147 if (new_str != nullptr)
148 {
149 ice::memcpy(new_str + bytes.value, ice::ptr_add(data.location, bytes), size.bytes());
150 new_str[bytes.value + size.native()] = '\0';
151 }
152 _data = new_str; // Assign the new allocated data
153 }
154
155 return ice::ptr_add(data, bytes + size.bytes());
156 }
157
158 template<typename CharT>
159 inline constexpr auto HeapVarString<CharT>::data_view() const noexcept -> ice::Data
160 {
161 ice::usize bytes = 0_B;
163
164 return {
165 .location = _data,
166 .size = { size.bytes() + bytes},
167 .alignment = ice::ualign::b_1
168 };
169 }
170
171 template<typename CharT>
173 {
174 ice::usize bytes = 0_B;
176 if (size > 0)
177 {
178 return { _data + bytes.value, size };
179 }
180 else
181 {
182 return {};
183 }
184 }
185
186 template<typename CharT>
187 inline constexpr HeapVarString<CharT>::operator ice::VarStringBase<typename HeapVarString<CharT>::CharType>() const noexcept
188 {
189 return _data;
190 }
191
192} // namespace ice
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition span.hxx:129
Definition heap_varstring.hxx:50
auto create(ice::Allocator &alloc, ice::String str) noexcept -> char *
Definition heap_varstring.hxx:66
auto read_data(CharType *data) noexcept -> CharType *
Definition varstring.hxx:86
auto allocate_exact(ice::Allocator &alloc, ice::ncount size, ice::usize &out_bytes) noexcept -> char *
Definition heap_varstring.hxx:52
auto calc_required_size(ice::ncount size) noexcept -> ice::usize
Definition varstring.hxx:48
auto write_size(void *data, ice::ncount size) noexcept -> ice::ncount
Definition varstring.hxx:96
auto read_size(char const *data, ice::usize &out_bytes) noexcept -> ice::ncount
Definition varstring.hxx:60
SPDX-License-Identifier: MIT.
Definition array.hxx:12
ice::BasicString< char > String
Definition string.hxx:82
struct _tagVarString VarStringTag
Definition varstring.hxx:12
auto alloc(ice::usize size) noexcept -> ice::AllocResult
auto ptr_add(void *pointer, ice::usize offset) noexcept -> void *
Definition mem_arithmetic.hxx:34
ice::AllocatorBase< ice::build::is_debug||ice::build::is_develop > Allocator
Definition mem_types.hxx:25
@ b_1
Definition mem_size_types.hxx:42
auto memcpy(void *dest, void const *source, ice::usize size) noexcept -> void *
Definition string.hxx:15
Definition mem_data.hxx:17
Definition heap_varstring.hxx:14
ice::ncount SizeType
Definition heap_varstring.hxx:24
constexpr auto deserialize(ice::Data data) noexcept -> ice::Data
Definition heap_varstring.hxx:137
constexpr HeapVarString(ice::Allocator &alloc) noexcept
Definition heap_varstring.hxx:83
CharType ValueType
Definition heap_varstring.hxx:19
ice::BasicString< CharType > StringType
Definition heap_varstring.hxx:25
constexpr auto size() const noexcept -> SizeType
Definition heap_varstring.hxx:125
std::reverse_iterator< CharType const * > ConstReverseIterator
Definition heap_varstring.hxx:23
CharType * Iterator
Definition heap_varstring.hxx:20
VarStringTag TypeTag
Definition heap_varstring.hxx:17
ValueType * _data
Definition heap_varstring.hxx:28
constexpr auto data() const noexcept -> ValueType *
Definition heap_varstring.hxx:119
std::reverse_iterator< CharType * > ReverseIterator
Definition heap_varstring.hxx:21
ice::Allocator * _allocator
Definition heap_varstring.hxx:27
constexpr ~HeapVarString() noexcept
Definition heap_varstring.hxx:110
constexpr void clear() noexcept
Definition heap_varstring.hxx:131
constexpr auto data_view() const noexcept -> ice::Data
Definition heap_varstring.hxx:159
CharT CharType
Definition heap_varstring.hxx:18
CharType const * ConstIterator
Definition heap_varstring.hxx:22
Definition mem_memory.hxx:13
void * location
Definition mem_memory.hxx:14
Definition varstring.hxx:16
Definition ncount.hxx:14
constexpr auto bytes(this ncount self) noexcept -> ice::usize
Definition ncount.hxx:36
constexpr auto native() const noexcept
Definition nvalue.hxx:109
Definition resizable_operations.hxx:13
Represents a unsigned size value on the given platform.
Definition mem_size_types.hxx:26
base_type value
Definition mem_size_types.hxx:35