File include/jlib/rarray.h changed (mode: 100644) (index 5001c81..f0bd40c) |
10 |
10 |
#include <cinttypes> |
#include <cinttypes> |
11 |
11 |
#include "assert.h" |
#include "assert.h" |
12 |
12 |
|
|
|
13 |
|
#include <type_traits> |
|
14 |
|
|
13 |
15 |
namespace jl |
namespace jl |
14 |
16 |
{ |
{ |
15 |
17 |
/** |
/** |
|
... |
... |
namespace jl |
37 |
39 |
*/ |
*/ |
38 |
40 |
void init() { *this = {}; } |
void init() { *this = {}; } |
39 |
41 |
|
|
|
42 |
|
rarray() = default; |
|
43 |
|
|
40 |
44 |
/** |
/** |
41 |
|
* @brief Initialize array with specified items count. |
|
42 |
|
* |
|
43 |
|
* Function must only be called if darray was not initialized by any of rarray::init functions. |
|
|
45 |
|
* @brief Initialize read-only rarray as array with single value. |
44 |
46 |
* |
* |
45 |
|
* @param[in] count Space specified by count of items to allocate in the array. |
|
46 |
|
* |
|
47 |
|
* @return True if success, otherwise false and allocation is failed, |
|
48 |
|
* array cannot be used until this function is successfull. |
|
|
47 |
|
* @param value Single item to be pointed to. |
49 |
48 |
*/ |
*/ |
50 |
|
[[nodiscard]] bool init(size_t count) |
|
51 |
|
{ |
|
52 |
|
jassert(count, "must not be 0, use init() instead"); |
|
53 |
|
if (not jl::allocate(&p_begin, count)) |
|
54 |
|
return false; |
|
55 |
|
p_end_allocated = p_begin + count; |
|
56 |
|
return true; |
|
57 |
|
} |
|
|
49 |
|
rarray(T &value) : p_begin(&value), p_end_allocated(p_begin+1) |
|
50 |
|
{ static_assert (std::is_const<T>::value, "only for read-only rarray"); } |
58 |
51 |
|
|
59 |
52 |
/** |
/** |
60 |
53 |
* @brief Initialize with already allocated memory. |
* @brief Initialize with already allocated memory. |
|
... |
... |
namespace jl |
66 |
59 |
* Allocated memory must only be deallocated outside of rarray, so |
* Allocated memory must only be deallocated outside of rarray, so |
67 |
60 |
* memory will deallocate in rarray::destroy or rarray::resize, for example: |
* memory will deallocate in rarray::destroy or rarray::resize, for example: |
68 |
61 |
* @code |
* @code |
69 |
|
* darray<char> data; |
|
|
62 |
|
* darray<int> data; |
70 |
63 |
* data.init(); |
* data.init(); |
71 |
|
* data.insert(8, 0); |
|
|
64 |
|
* data.insert(4); |
|
65 |
|
* data.insert(8); |
72 |
66 |
* rarray arr; |
* rarray arr; |
73 |
|
* arr.init_move(data.begin(), data.end()); // darray p_allocation_end can be bigger |
|
74 |
|
* // than pointer returned by end(), |
|
75 |
|
* // but it does not affect deallocating. |
|
|
67 |
|
* arr = {data.begin(), data.end()}; // darray p_allocation_end can be bigger |
|
68 |
|
* // than pointer returned by end(), |
|
69 |
|
* // but it does not affect deallocating. |
76 |
70 |
* data.init(); // reset dynamic array clean to prevent double memory deallocation |
* data.init(); // reset dynamic array clean to prevent double memory deallocation |
77 |
71 |
* arr.destroy(); // deallocate memory |
* arr.destroy(); // deallocate memory |
78 |
72 |
* @endcode |
* @endcode |
|
... |
... |
namespace jl |
80 |
74 |
* @param[in] p_allocated_mem Pointer to begin of already allocated memory. |
* @param[in] p_allocated_mem Pointer to begin of already allocated memory. |
81 |
75 |
* @param[in] p_end_alocated_mem Pointer to end of already allocated memory, first byte after an allocation. |
* @param[in] p_end_alocated_mem Pointer to end of already allocated memory, first byte after an allocation. |
82 |
76 |
*/ |
*/ |
83 |
|
void init_move(T *p_allocated_mem, T *p_end_alocated_mem) |
|
84 |
|
{ |
|
85 |
|
jassert(p_end_alocated_mem >= p_allocated_mem, "end must be after beginning"); |
|
86 |
|
p_begin = p_allocated_mem; |
|
87 |
|
p_end_allocated = p_end_alocated_mem; |
|
88 |
|
} |
|
|
77 |
|
rarray(T* p_allocated_mem, T* p_end_alocated_mem) |
|
78 |
|
: p_begin(p_allocated_mem), p_end_allocated(p_end_alocated_mem) |
|
79 |
|
{ jassert(p_end_allocated >= p_begin, "wrong pointers order"); } |
89 |
80 |
|
|
90 |
81 |
/** |
/** |
91 |
82 |
* @brief Initialize with already allocated memory. |
* @brief Initialize with already allocated memory. |
92 |
83 |
* |
* |
93 |
|
* Same as rarray::init_move, but with different second argument |
|
|
84 |
|
* Same as rarray::rarray(T* p_allocated_mem, T* p_end_alocated_mem), but with alternative second argument |
94 |
85 |
* |
* |
95 |
86 |
* @param[in] p_allocated_mem Pointer to begin of already allocated memory. |
* @param[in] p_allocated_mem Pointer to begin of already allocated memory. |
96 |
87 |
* @param[in] allocated_count Size of allocated memory in items count. |
* @param[in] allocated_count Size of allocated memory in items count. |
97 |
88 |
* |
* |
98 |
|
* @see rarray::init_move |
|
99 |
|
*/ |
|
100 |
|
void init_move(T *p_allocated_mem, size_t allocated_count) |
|
101 |
|
{ init_move(p_allocated_mem, p_allocated_mem + allocated_count); } |
|
102 |
|
|
|
103 |
|
/** |
|
104 |
|
* @brief Create rarray with already allocated memory. |
|
105 |
|
* @return rarray |
|
106 |
|
* @see rarray::init_move |
|
|
89 |
|
* @see rarray::rarray(T* p_allocated_mem, T* p_end_alocated_mem) |
107 |
90 |
*/ |
*/ |
108 |
|
template<typename ...Args> |
|
109 |
|
[[nodiscard]] static rarray built_move(Args ... args) |
|
110 |
|
{ |
|
111 |
|
rarray r; |
|
112 |
|
r.init_move(args...); |
|
113 |
|
return r; |
|
114 |
|
} |
|
|
91 |
|
rarray(T *p_allocated_mem, size_t allocated_count) |
|
92 |
|
: p_begin(p_allocated_mem), p_end_allocated(p_allocated_mem + allocated_count){} |
115 |
93 |
|
|
116 |
94 |
/** |
/** |
117 |
|
* @brief Create read-only rarray by pointer to array. |
|
|
95 |
|
* @brief Initialize array with specified items count. |
118 |
96 |
* |
* |
119 |
|
* @param[in] p_begin Pointer to begin of array. |
|
120 |
|
* @param[in] p_end Pointer to end of array, first byte after last byte of an array. |
|
|
97 |
|
* Function must only be called if darray was not initialized by any of rarray::init functions. |
|
98 |
|
* |
|
99 |
|
* @param[in] count Space specified by count of items to allocate in the array. |
121 |
100 |
* |
* |
122 |
|
* @return read-only rarray |
|
|
101 |
|
* @return True if success, otherwise false and allocation is failed, |
|
102 |
|
* array cannot be used until this function is successfull. |
123 |
103 |
*/ |
*/ |
124 |
|
[[nodiscard]] static rarray<const T> built_const(const T *p_begin, const T *p_end) |
|
|
104 |
|
[[nodiscard]] bool init(size_t count) |
125 |
105 |
{ |
{ |
126 |
|
jassert(p_end >= p_begin, "end must be after beginning"); |
|
127 |
|
rarray r; |
|
128 |
|
r.p_begin = p_begin; |
|
129 |
|
r.p_end_allocated = p_end; |
|
130 |
|
return r; |
|
|
106 |
|
jassert(count, "must not be 0, use init() instead"); |
|
107 |
|
if (not jl::allocate(&p_begin, count)) |
|
108 |
|
return false; |
|
109 |
|
p_end_allocated = p_begin + count; |
|
110 |
|
return true; |
131 |
111 |
} |
} |
132 |
112 |
|
|
133 |
|
/** |
|
134 |
|
* @brief Create read-only rarray by pointer to array. |
|
135 |
|
* |
|
136 |
|
* @param[in] p_begin Pointer to begin of array. |
|
137 |
|
* @param[in] count Size of array in items count. |
|
138 |
|
* |
|
139 |
|
* @return read-only rarray |
|
140 |
|
*/ |
|
141 |
|
[[nodiscard]] static rarray<const T> built_const(const T *p_begin, size_t count) |
|
142 |
|
{ return built_const(p_begin, p_begin + count); } |
|
143 |
113 |
/** |
/** |
144 |
114 |
* @brief Deallocate space, set item count to 0. |
* @brief Deallocate space, set item count to 0. |
145 |
115 |
* |
* |
File include/jlib/string.h changed (mode: 100644) (index 81a5681..72b8259) |
... |
... |
namespace jl |
25 |
25 |
struct carray : rarray<T> |
struct carray : rarray<T> |
26 |
26 |
{ |
{ |
27 |
27 |
using rarray<T>::init; |
using rarray<T>::init; |
28 |
|
|
|
29 |
|
template<typename ...Args> |
|
30 |
|
[[nodiscard]] static carray<const T> built_const(Args ...args) |
|
31 |
|
{ return carray<const T>(rarray<T>::built_const(args...)); } |
|
|
28 |
|
using rarray<T>::rarray; |
32 |
29 |
|
|
33 |
30 |
/** |
/** |
34 |
31 |
* @brief Initialize array with c-style string. |
* @brief Initialize array with c-style string. |
|
... |
... |
namespace jl |
88 |
85 |
return true; |
return true; |
89 |
86 |
} |
} |
90 |
87 |
|
|
|
88 |
|
/// @see rarray(T &value) |
|
89 |
|
carray(T &value) : rarray<T>(value) {} |
|
90 |
|
|
91 |
91 |
/** |
/** |
92 |
92 |
* @brief Built read-only access carray by given c-string pointer. |
* @brief Built read-only access carray by given c-string pointer. |
93 |
93 |
* |
* |
|
... |
... |
namespace jl |
95 |
95 |
* |
* |
96 |
96 |
* @param[in] null_terminated_str Pointer to C-style null-terminated string, |
* @param[in] null_terminated_str Pointer to C-style null-terminated string, |
97 |
97 |
* must be valid pointer while carray is in use. |
* must be valid pointer while carray is in use. |
98 |
|
* @return Read-only carray. |
|
99 |
98 |
*/ |
*/ |
100 |
|
[[nodiscard]] static carray<const T> built_const(const char *null_terminated_str) |
|
101 |
|
{ |
|
102 |
|
jassert(null_terminated_str, "invalid pointer"); |
|
103 |
|
carray<const T> ca; |
|
104 |
|
ca.init_move(null_terminated_str, strlen(null_terminated_str)+1); |
|
105 |
|
return ca; |
|
106 |
|
} |
|
|
99 |
|
carray(const char *null_terminated_str) |
|
100 |
|
: rarray<const char>(null_terminated_str, strlen(null_terminated_str)+1) |
|
101 |
|
{ jassert(null_terminated_str, "invalid pointer"); } |
107 |
102 |
|
|
108 |
103 |
|
|
109 |
104 |
/// @brief Length of character array, specified by count of chars, excluding null-char '\0'. |
/// @brief Length of character array, specified by count of chars, excluding null-char '\0'. |