16 #ifndef INCLUDE_NN_NLIB_STRINGHOLDER_H_ 17 #define INCLUDE_NN_NLIB_STRINGHOLDER_H_ 21 #include "nn/nlib/Swap.h" 25 template<
size_t N,
class Ch =
char>
34 str_.sso[N - 1] =
static_cast<Ch
>(-1);
37 if (str_.sso[N - 1] && str_.ptr)
nlib_free(str_.ptr);
39 #ifdef __cpp_rvalue_references 40 StringHolder(StringHolder&& rhs)
NLIB_NOEXCEPT : str_(rhs.str_) {
41 rhs.str_.ptr =
nullptr;
42 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
45 nlib_memcpy(&str_.sso[0], N, &rhs.str_.sso[0], N);
46 rhs.str_.ptr =
nullptr;
47 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
51 StringHolder(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT : str_(rhs.str_) {
52 rhs.str_.ptr =
nullptr;
53 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
55 StringHolder& assign(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT {
56 nlib_memcpy(static_cast<void*>(&str_.sso[0]), N, static_cast<const void*>(&rhs.str_.sso[0]),
58 rhs.str_.ptr =
nullptr;
59 rhs.str_.sso[N - 1] =
static_cast<Ch
>(-1);
69 if (str_.sso[N - 1]) {
70 return str_.ptr ?
static_cast<size_t>(str_.len.val) : 0U;
75 const Ch* c_str() const
NLIB_NOEXCEPT {
return str_.sso[N - 1] ? str_.ptr : &str_.sso[0]; }
83 if (str_.sso[N - 1]) {
86 str_.sso[N - 1] =
static_cast<Ch
>(-1);
103 template<
size_t N,
class Ch>
106 size_t n = std::distance(first, last);
110 if (!p)
return ENOMEM;
111 if (str_.sso[N - 1]) {
114 str_.sso[N - 1] =
static_cast<Ch
>(-1);
119 nlib_memcpy(static_cast<void*>(p), n + 1, static_cast<const void*>(first), n);
120 p[n] =
static_cast<Ch
>(
'\0');
123 if (str_.sso[N - 1]) {
125 str_.sso[N - 1] =
static_cast<Ch
>(
'\0');
128 nlib_memcpy(static_cast<void*>(p), N - 1, static_cast<const void*>(first), n);
130 p[n] =
static_cast<Ch
>(
'\0');
143 str_.sso[15] =
static_cast<Ch
>(-1);
146 if (str_.sso[15] && str_.ptr)
nlib_free(str_.ptr);
148 #ifdef __cpp_rvalue_references 149 StringHolder(StringHolder&& rhs)
NLIB_NOEXCEPT : str_(rhs.str_) {
150 rhs.str_.ptr =
nullptr;
151 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
154 nlib_memcpy(&str_.sso[0], 16, &rhs.str_.sso[0], 16);
155 rhs.str_.ptr =
nullptr;
156 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
160 StringHolder(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT : str_(rhs.str_) {
161 rhs.str_.ptr =
nullptr;
162 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
164 StringHolder& assign(StringHolder& rhs, move_tag)
NLIB_NOEXCEPT {
165 nlib_memcpy(static_cast<void*>(&str_.sso[0]), 16,
166 static_cast<const void*>(&rhs.str_.sso[0]), 16);
167 rhs.str_.ptr =
nullptr;
168 rhs.str_.sso[15] =
static_cast<Ch
>(-1);
179 if (!str_.ptr)
return 0U;
180 int64_t len = str_.len.minus;
181 #ifndef NLIB_LITTLE_ENDIAN 184 return static_cast<size_t>(-len);
189 const Ch* c_str() const
NLIB_NOEXCEPT {
return str_.sso[15] ? str_.ptr : &str_.sso[0]; }
200 str_.sso[15] =
static_cast<Ch
>(-1);
220 ptrdiff_t n = std::distance(first, last);
224 if (!p)
return ENOMEM;
230 #ifndef NLIB_LITTLE_ENDIAN 234 nlib_memcpy(static_cast<void*>(p), n + 1, static_cast<const void*>(first), n);
235 p[n] =
static_cast<Ch
>(
'\0');
240 str_.sso[15] =
static_cast<Ch
>(
'\0');
243 nlib_memcpy(static_cast<void*>(p), 15, static_cast<const void*>(first), n);
245 p[n] =
static_cast<Ch
>(
'\0');
251 template<
class BYTE = nlib_byte_t,
size_t N = 232>
256 cur_ = &init_buf_[0];
257 end_ = &init_buf_[0] + N;
258 chunk_stack_ =
nullptr;
276 template<
class BYTE,
size_t N>
278 Chunk* p = chunk_stack_;
280 Chunk* tmp = p->next;
284 cur_ = &init_buf_[0];
285 end_ = &init_buf_[0] + N;
286 chunk_stack_ =
nullptr;
289 template<
class BYTE,
size_t N>
290 inline BYTE* BytePool<BYTE, N>::Alloc(
size_t n)
NLIB_NOEXCEPT {
292 size_t real_n = (n +
sizeof(Chunk*) + 4095) & ~4095;
293 Chunk* chk =
static_cast<Chunk*
>(
nlib_malloc(real_n));
295 chk->next = chunk_stack_;
298 end_ =
reinterpret_cast<BYTE*
>(chk) + real_n;
308 NLIB_DEFINE_STD_SWAP_T_BEGIN2(
nn, nlib)
309 #ifndef __cpp_rvalue_references 310 template<
size_t N,
class Ch>
311 NLIB_ALWAYS_INLINE void swap(nlib_ns::StringHolder<N, Ch>& lhs, nlib_ns::StringHolder<N, Ch>& rhs) {
317 NLIB_DEFINE_STD_SWAP_T_END2(
nn, nlib)
319 #endif // INCLUDE_NN_NLIB_STRINGHOLDER_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
#define NLIB_CEXPR14
C++14のconstexprが利用可能であればconstexprが定義されます。そうでない場合は空文字列です。 ...
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_STATIC_ASSERT(exp)
静的アサートが定義されます。利用可能であればstatic_assertを利用します。