File include/jrf/indices.h changed (mode: 100644) (index 7bae4b6..3513cff) |
6 |
6 |
|
|
7 |
7 |
namespace jrf |
namespace jrf |
8 |
8 |
{ |
{ |
9 |
|
enum IndexFormat : uint32_t { U16 = 2, U32 = 4 }; |
|
10 |
|
|
|
11 |
|
struct Indices |
|
12 |
|
{ |
|
13 |
|
uint8_t *p_data; |
|
14 |
|
uint64_t size; |
|
15 |
|
IndexFormat format; |
|
16 |
|
|
|
17 |
|
inline void destroy() { |
|
18 |
|
jl::deallocate(&p_data); |
|
19 |
|
} |
|
20 |
|
|
|
21 |
|
template<typename IO = jl::io_agent> |
|
22 |
|
[[nodiscard]] Result read(IO *p_mediator) |
|
23 |
|
{ |
|
24 |
|
auto *p_io = jl::io_agent_p_alt_cast(p_mediator); |
|
25 |
|
if (not p_io->read_items(&format)) |
|
26 |
|
return Result::MEDIATOR_ERROR; |
|
27 |
|
if (not p_io->read_items(&size)) |
|
28 |
|
return Result::MEDIATOR_ERROR; |
|
29 |
|
|
|
30 |
|
if (size == 0) |
|
31 |
|
p_data = nullptr; |
|
32 |
|
else |
|
33 |
|
{ |
|
34 |
|
if (not jl::allocate_bytes(&p_data, size)) |
|
35 |
|
return Result::MEDIATOR_ERROR; |
|
36 |
|
if (not p_io->read_bytes(p_data, size)) |
|
37 |
|
return this->destroy(), Result::MEDIATOR_ERROR; |
|
38 |
|
} |
|
39 |
|
return Result::SUCCESS; |
|
40 |
|
} |
|
41 |
|
|
|
42 |
|
template<typename IO = jl::io_agent> |
|
43 |
|
[[nodiscard]] Result write(IO *p_mediator) const |
|
44 |
|
{ |
|
45 |
|
auto *p_io = jl::io_agent_p_alt_cast(p_mediator); |
|
46 |
|
if (not p_io->write_items(&format)) |
|
47 |
|
return Result::MEDIATOR_ERROR; |
|
48 |
|
if (not p_io->write_items(&size)) |
|
49 |
|
return Result::MEDIATOR_ERROR; |
|
50 |
|
if (not p_io->write_bytes(p_data, size)) |
|
51 |
|
return Result::MEDIATOR_ERROR; |
|
52 |
|
|
|
53 |
|
return Result::SUCCESS; |
|
54 |
|
} |
|
55 |
|
}; |
|
56 |
|
|
|
57 |
|
/* |
|
58 |
|
struct IndicesArrays |
|
59 |
|
{ |
|
60 |
|
uint32_t array_count; |
|
61 |
|
IndexFormat format; |
|
62 |
|
union |
|
63 |
|
{ |
|
64 |
|
Indices *p_indices; |
|
65 |
|
Indices indices; |
|
66 |
|
} u; |
|
67 |
|
bool is_allocated; |
|
68 |
|
|
|
69 |
|
void destroy() |
|
|
9 |
|
enum IndexFormat : uint32_t { U16 = 2, U32 = 4 }; |
|
10 |
|
|
|
11 |
|
struct Indices |
|
12 |
|
{ |
|
13 |
|
uint8_t *p_data; |
|
14 |
|
uint64_t size; |
|
15 |
|
IndexFormat format; |
|
16 |
|
|
|
17 |
|
void destroy() { jl::deallocate(&p_data); } |
|
18 |
|
|
|
19 |
|
template<typename IO = jl::io_agent> |
|
20 |
|
[[nodiscard]] Result read(IO *p_mediator) |
|
21 |
|
{ |
|
22 |
|
auto *p_io = jl::io_agent_p_alt_cast(p_mediator); |
|
23 |
|
if (not p_io->read_items(&format)) |
|
24 |
|
return Result::MEDIATOR_ERROR; |
|
25 |
|
if (not p_io->read_items(&size)) |
|
26 |
|
return Result::MEDIATOR_ERROR; |
|
27 |
|
|
|
28 |
|
if (size == 0) |
|
29 |
|
p_data = nullptr; |
|
30 |
|
else |
70 |
31 |
{ |
{ |
71 |
|
if (is_allocated) |
|
72 |
|
{ |
|
73 |
|
if (u.p_indices != nullptr) |
|
74 |
|
{ |
|
75 |
|
for (uint32_t i = 0; i < array_count; ++i) |
|
76 |
|
jl::deallocate(&u.p_indices[i].p_data); |
|
77 |
|
jl::deallocate(&u.p_indices); |
|
78 |
|
} |
|
79 |
|
} |
|
80 |
|
else u.indices.destroy(); |
|
81 |
|
} |
|
82 |
|
|
|
83 |
|
template<typename IO = jl::io_agent> |
|
84 |
|
[[nodiscard]] Result read(IO *p_mediator) |
|
85 |
|
{ |
|
86 |
|
if (p_mediator->read(&format, sizeof(format)) != sizeof(format)) |
|
87 |
|
return Result::MEDIATOR_ERROR; |
|
88 |
|
if (p_mediator->read(&array_count, sizeof (array_count)) != sizeof(array_count)) |
|
89 |
|
return Result::MEDIATOR_ERROR; |
|
90 |
|
|
|
91 |
|
if (array_count == 0) |
|
92 |
|
u.p_indices = nullptr; |
|
93 |
|
else |
|
94 |
|
{ |
|
95 |
|
Indices *p_ind; |
|
96 |
|
is_allocated = array_count > 1; |
|
97 |
|
if (is_allocated) |
|
98 |
|
{ |
|
99 |
|
if (not jl::allocate(&u.p_indices, array_count)) |
|
100 |
|
return Result::ALLOCATION_FAIL; |
|
101 |
|
p_ind = u.p_indices; |
|
102 |
|
} |
|
103 |
|
else p_ind = &u.indices; |
|
104 |
|
|
|
105 |
|
for (uint32_t i = 0; i < array_count; ++i) |
|
106 |
|
{ |
|
107 |
|
p_ind[i].p_data = nullptr; |
|
108 |
|
if (p_mediator->read(&p_ind[i].size, sizeof(Indices::size)) != sizeof(Indices::size)) |
|
109 |
|
{ |
|
110 |
|
jl::deallocate(&u.p_indices); |
|
111 |
|
return Result::MEDIATOR_ERROR; |
|
112 |
|
} |
|
113 |
|
} |
|
114 |
|
|
|
115 |
|
for (uint32_t i = 0; i < array_count; ++i) |
|
116 |
|
{ |
|
117 |
|
Result res; |
|
118 |
|
auto &array = p_ind[i]; |
|
119 |
|
if (not jl::allocate_bytes(&array.p_data, array.size)) |
|
120 |
|
{ |
|
121 |
|
res = Result::ALLOCATION_FAIL; |
|
122 |
|
goto CANCEL; |
|
123 |
|
} |
|
124 |
|
if (p_mediator->read(array.p_data, array.size) != array.size) |
|
125 |
|
{ |
|
126 |
|
res = Result::MEDIATOR_ERROR; |
|
127 |
|
CANCEL: destroy(); |
|
128 |
|
return res; |
|
129 |
|
} |
|
130 |
|
} |
|
131 |
|
} |
|
132 |
|
return Result::SUCCESS; |
|
133 |
|
} |
|
134 |
|
|
|
135 |
|
template<typename IO = jl::io_agent> |
|
136 |
|
[[nodiscard]] Result write(IO *p_mediator) const |
|
137 |
|
{ |
|
138 |
|
if (p_mediator->write(&format, sizeof(format)) != sizeof(format)) |
|
139 |
|
return Result::MEDIATOR_ERROR; |
|
140 |
|
if (p_mediator->write(&array_count, sizeof (array_count)) != sizeof (array_count)) |
|
141 |
|
return Result::MEDIATOR_ERROR; |
|
142 |
|
|
|
143 |
|
const Indices *p_ind; |
|
144 |
|
if (is_allocated) |
|
145 |
|
p_ind = u.p_indices; |
|
146 |
|
else p_ind = &u.indices; |
|
147 |
|
|
|
148 |
|
for (uint32_t i = 0; i < array_count; ++i) |
|
149 |
|
if (p_mediator->write(&p_ind[i].size, sizeof(Indices::size)) != sizeof(Indices::size)) |
|
150 |
|
return Result::MEDIATOR_ERROR; |
|
151 |
|
|
|
152 |
|
for (uint32_t i = 0; i < array_count; ++i) |
|
153 |
|
if (p_mediator->write(p_ind[i].p_data, p_ind[i].size) != int64_t(p_ind[i].size)) |
|
154 |
|
return Result::MEDIATOR_ERROR; |
|
155 |
|
|
|
156 |
|
return Result::SUCCESS; |
|
|
32 |
|
if (not jl::allocate_bytes(&p_data, size)) |
|
33 |
|
return Result::MEDIATOR_ERROR; |
|
34 |
|
if (not p_io->read_bytes(p_data, size)) |
|
35 |
|
return this->destroy(), Result::MEDIATOR_ERROR; |
157 |
36 |
} |
} |
158 |
|
}; |
|
159 |
|
*/ |
|
|
37 |
|
return Result::SUCCESS; |
|
38 |
|
} |
|
39 |
|
|
|
40 |
|
template<typename IO = jl::io_agent> |
|
41 |
|
[[nodiscard]] Result write(IO *p_mediator) const |
|
42 |
|
{ |
|
43 |
|
auto *p_io = jl::io_agent_p_alt_cast(p_mediator); |
|
44 |
|
if (not p_io->write_items(&format)) |
|
45 |
|
return Result::MEDIATOR_ERROR; |
|
46 |
|
if (not p_io->write_items(&size)) |
|
47 |
|
return Result::MEDIATOR_ERROR; |
|
48 |
|
if (not p_io->write_bytes(p_data, size)) |
|
49 |
|
return Result::MEDIATOR_ERROR; |
|
50 |
|
|
|
51 |
|
return Result::SUCCESS; |
|
52 |
|
} |
|
53 |
|
}; |
160 |
54 |
} |
} |