16 #ifndef INCLUDE_NN_NLIB_STRINGHOLDER_H_ 17 #define INCLUDE_NN_NLIB_STRINGHOLDER_H_ 20 #include "nn/nlib/Swap.h" 24 template<
size_t N,
class Ch =
char>
33 str_.sso[N - 1] =
static_cast<Ch
>(-1);
36 if (str_.sso[N - 1] && str_.ptr)
nlib_free(str_.ptr);
38 #ifdef __cpp_rvalue_references 39 StringHolder(StringHolder&& rhs)
NLIB_NOEXCEPT : str_(rhs.str_) {
40 rhs.str_.ptr =
nullptr;
41 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
45 rhs.str_.ptr =
nullptr;
46 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
50 StringHolder(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT : str_(rhs.str_) {
51 rhs.str_.ptr =
nullptr;
52 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
54 StringHolder& assign(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT {
56 static_cast<const void*>(&rhs.str_.sso[0]), N);
57 rhs.str_.ptr =
nullptr;
58 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
64 nlib_memcpy(static_cast<void*>(&sso[0]),
sizeof(sso),
65 static_cast<const void*>(&str_),
sizeof(str_));
66 nlib_memcpy(static_cast<void*>(&str_),
sizeof(str_),
67 static_cast<const void*>(&rhs.str_),
sizeof(rhs.str_));
68 nlib_memcpy(static_cast<void*>(&rhs.str_),
sizeof(rhs.str_),
69 static_cast<const void*>(&sso[0]),
sizeof(sso));
73 errno_t Init(
const Ch* str) NLIB_NOEXCEPT {
77 size_t length() const NLIB_NOEXCEPT {
78 if (str_.sso[N - 1]) {
79 return str_.ptr ?
static_cast<size_t>(str_.len.val) : 0U;
84 const Ch* c_str() const NLIB_NOEXCEPT {
85 return str_.sso[N - 1] ? str_.ptr : &str_.sso[0];
87 const Ch*
get()
const NLIB_NOEXCEPT {
91 operator const Ch*()
const NLIB_NOEXCEPT {
return c_str(); }
92 void reset() NLIB_NOEXCEPT {
94 if (str_.sso[N - 1]) {
97 str_.sso[N - 1] =
static_cast<Ch
>(-1);
114 template<
size_t N,
class Ch>
115 errno_t StringHolder<N, Ch>::Init(
const Ch* first,
const Ch* last) NLIB_NOEXCEPT {
117 size_t n = std::distance(first, last);
121 if (!p)
return ENOMEM;
122 if (str_.sso[N - 1]) {
125 str_.sso[N - 1] =
static_cast<Ch
>(-1);
131 static_cast<const void*>(first), n);
132 p[n] =
static_cast<Ch
>(
'\0');
135 if (str_.sso[N - 1]) {
137 str_.sso[N - 1] =
static_cast<Ch
>(
'\0');
141 static_cast<const void*>(first), n);
143 p[n] =
static_cast<Ch
>(
'\0');
156 str_.sso[15] =
static_cast<Ch
>(-1);
158 ~StringHolder() NLIB_NOEXCEPT {
159 if (str_.sso[15] && str_.ptr)
nlib_free(str_.ptr);
161 #ifdef __cpp_rvalue_references 162 StringHolder(StringHolder&& rhs)
NLIB_NOEXCEPT : str_(rhs.str_) {
163 rhs.str_.ptr =
nullptr;
164 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
166 StringHolder& operator=(StringHolder&& rhs) NLIB_NOEXCEPT {
167 nlib_memcpy(&str_.sso[0], 16, &rhs.str_.sso[0], 16);
168 rhs.str_.ptr =
nullptr;
169 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
173 StringHolder(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT : str_(rhs.str_) {
174 rhs.str_.ptr =
nullptr;
175 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
177 StringHolder& assign(StringHolder& rhs, move_tag) NLIB_NOEXCEPT {
179 static_cast<const void*>(&rhs.str_.sso[0]), 16);
180 rhs.str_.ptr =
nullptr;
181 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
187 nlib_memcpy(static_cast<void*>(&sso[0]),
sizeof(sso),
188 static_cast<const void*>(&str_),
sizeof(str_));
189 nlib_memcpy(static_cast<void*>(&str_),
sizeof(str_),
190 static_cast<const void*>(&rhs.str_),
sizeof(rhs.str_));
191 nlib_memcpy(static_cast<void*>(&rhs.str_),
sizeof(rhs.str_),
192 static_cast<const void*>(&sso[0]),
sizeof(sso));
196 errno_t Init(
const Ch* str) NLIB_NOEXCEPT {
200 size_t length() const NLIB_NOEXCEPT {
202 if (!str_.ptr)
return 0U;
203 int64_t len = str_.len.minus;
204 #ifndef NLIB_LITTLE_ENDIAN 207 return static_cast<size_t>(-len);
212 const Ch* c_str() const NLIB_NOEXCEPT {
213 return str_.sso[15] ? str_.ptr : &str_.sso[0];
215 const Ch*
get()
const NLIB_NOEXCEPT {
219 operator const Ch*()
const NLIB_NOEXCEPT {
return c_str(); }
220 void reset() NLIB_NOEXCEPT {
225 str_.sso[15] =
static_cast<Ch
>(-1);
243 errno_t StringHolder<16, Ch>::Init(
const Ch* first,
const Ch* last) NLIB_NOEXCEPT {
245 ptrdiff_t n = std::distance(first, last);
249 if (!p)
return ENOMEM;
255 #ifndef NLIB_LITTLE_ENDIAN 260 static_cast<const void*>(first), n);
261 p[n] =
static_cast<Ch
>(
'\0');
266 str_.sso[15] =
static_cast<Ch
>(
'\0');
270 static_cast<const void*>(first), n);
272 p[n] =
static_cast<Ch
>(
'\0');
278 template<
class BYTE = nlib_byte_t,
size_t N = 232>
281 BytePool() NLIB_NOEXCEPT {
283 cur_ = &init_buf_[0];
284 end_ = &init_buf_[0] + N;
285 chunk_stack_ =
nullptr;
287 ~BytePool() NLIB_NOEXCEPT { Reset(); }
288 void Reset() NLIB_NOEXCEPT;
289 BYTE* Alloc(
size_t n) NLIB_NOEXCEPT;
303 template<
class BYTE,
size_t N>
304 void BytePool<BYTE, N>::Reset() NLIB_NOEXCEPT {
305 Chunk* p = chunk_stack_;
307 Chunk* tmp = p->next;
311 cur_ = &init_buf_[0];
312 end_ = &init_buf_[0] + N;
313 chunk_stack_ =
nullptr;
316 template<
class BYTE,
size_t N>
317 BYTE* BytePool<BYTE, N>::Alloc(
size_t n) NLIB_NOEXCEPT {
319 size_t real_n = (n +
sizeof(Chunk*) + 4095) & ~4095;
320 Chunk* chk =
static_cast<Chunk*
>(
nlib_malloc(real_n));
322 chk->next = chunk_stack_;
325 end_ =
reinterpret_cast<BYTE*
>(chk) + real_n;
335 NLIB_DEFINE_STD_SWAP_T_BEGIN2(
nn, nlib)
336 #ifndef __cpp_rvalue_references 338 void swap(nlib_ns::StringHolder<N, Ch>& lhs, nlib_ns::StringHolder<N, Ch>& rhs) {
344 NLIB_DEFINE_STD_SWAP_T_END2(
nn, nlib)
346 #endif // INCLUDE_NN_NLIB_STRINGHOLDER_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#define NLIB_CEXPR14
constexpr is defined if C++14 constexpr is available for use. If not, holds an empty string...
An empty structure indicating that an argument to a function needs to be moved.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.