Skip to content

Commit

Permalink
hotplace rev.500 grooming
Browse files Browse the repository at this point in the history
  • Loading branch information
princeb612 committed Apr 20, 2024
1 parent 9fc1410 commit e011b51
Show file tree
Hide file tree
Showing 22 changed files with 498 additions and 380 deletions.
2 changes: 1 addition & 1 deletion sdk/base/basic/cmdline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ return_t cmdline_t<T>::parse(int argc, char** argv) {
}

if (_mandatory.size()) {
ret = errorcode_t::insufficiency;
ret = errorcode_t::insufficient;
}

return ret;
Expand Down
2 changes: 2 additions & 0 deletions sdk/base/basic/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <ostream>
#include <sdk/base/basic/variant.hpp>
#include <sdk/base/stl.hpp>
#include <sdk/base/system/types.hpp>

namespace hotplace {
Expand Down Expand Up @@ -339,6 +340,7 @@ return_t variant::to_binary(binary_t& target) const {
}
return ret;
}

return_t variant::to_string(std::string& target) const {
return_t ret = errorcode_t::success;

Expand Down
2 changes: 1 addition & 1 deletion sdk/base/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const error_description error_descriptions[] = {
errordef(busy, "busy"),
errordef(query, "query"),
errordef(fetch, "fetch"),
errordef(insufficiency, "insufficiency"),
errordef(insufficient, "insufficient"),
errordef(reserved, "reserved"),
errordef(suspicious, "suspicious"),
errordef(unknown, "unknown"),
Expand Down
2 changes: 1 addition & 1 deletion sdk/base/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ enum errorcode_t {
/* 0xef010025 4009820197 */ busy,
/* 0xef010026 4009820198 */ query,
/* 0xef010027 4009820199 */ fetch,
/* 0xef010028 4009820200 */ insufficiency,
/* 0xef010028 4009820200 */ insufficient,
/* 0xef010029 4009820201 */ reserved,
/* 0xef01002a 4009820202 */ suspicious,
/* 0xef01002b 4009820203 */ unknown,
Expand Down
183 changes: 133 additions & 50 deletions sdk/io/basic/payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ payload_member::payload_member(uint8 value, const char* name, const char* group)
get_variant().set_uint8(value);
}

payload_member::payload_member(uint8 value, uint16 repeat, const char* name, const char* group)
: _change_endian(false), _member_value_of(nullptr), _reserve(repeat) {
set_name(name).set_group(group);
binary_t bin;
uint16 temp = repeat;
while (temp--) {
bin.insert(bin.end(), value);
}
get_variant().set_binary_new(bin);
}

payload_member::payload_member(uint16 value, bool change_endian, const char* name, const char* group)
: _change_endian(change_endian), _member_value_of(nullptr), _reserve(0) {
set_name(name).set_group(group);
Expand Down Expand Up @@ -82,12 +93,25 @@ variant& payload_member::get_variant() { return _vt; }
size_t payload_member::get_space() {
size_t space = 0;
payload_member* ref = get_value_of();
if (variant_flag_t::flag_int == get_variant().flag()) {
if (_reserve) {
space = _reserve;
} else if (variant_flag_t::flag_int == get_variant().flag()) {
space = get_variant().size();
} else if (ref) {
space = t_variant_to_int<size_t>(ref->get_variant().content());
} else if (_reserve) {
}
return space;
}

size_t payload_member::get_capacity() {
size_t space = 0;
payload_member* ref = get_value_of();
if (_reserve) {
space = _reserve;
} else if (ref) {
space = t_variant_to_int<size_t>(ref->get_variant().content());
} else {
space = get_variant().size();
}
return space;
}
Expand Down Expand Up @@ -168,10 +192,10 @@ payload_member& payload_member::read(byte_t* ptr, size_t size_ptr, size_t* size_
} else {
size_t size = 0;
payload_member* ref = get_value_of();
if (ref) {
size = t_variant_to_int<size_t>(ref->get_variant().content());
} else if (_reserve) {
if (_reserve) {
size = _reserve;
} else if (ref) {
size = t_variant_to_int<size_t>(ref->get_variant().content());
}

if (size_ptr >= size) {
Expand Down Expand Up @@ -246,72 +270,105 @@ payload& payload::set_reference_value(std::string const& name, std::string const
return *this;
}

payload& payload::dump(binary_t& bin) {
return_t payload::dump(binary_t& bin) {
return_t ret = errorcode_t::success;
for (auto item : _members) {
bool condition = get_group_condition(item->get_group());
if (condition) {
item->dump(bin);
}
}
return *this;
return ret;
}

payload& payload::read(binary_t const& bin) { return read((byte_t*)&bin[0], bin.size()); }
return_t payload::read(binary_t const& bin) { return read((byte_t*)&bin[0], bin.size()); }

payload& payload::read(byte_t* base, size_t size) {
size_t size_sum = 0;
std::list<payload_member*> _size_unknown;
return_t payload::read(byte_t* base, size_t size) {
return_t ret = errorcode_t::success;

{
byte_t* p = base;
size_t len = size;
size_t pos = 0;
size_t size_read = 0;
bool check = true;
for (auto item : _members) {
uint16 space = item->get_space();
size_sum += space;
if (0 == space) {
if (nullptr == item->get_value_of()) {
_size_unknown.push_back(item);
__try2 {
if (nullptr == base) {
ret = errorcode_t::invalid_parameter;
__leave2;
}

size_t size_sum = 0;
std::list<payload_member*> _size_unknown;

{
byte_t* p = base;
size_t len = size;
size_t pos = 0;
size_t size_read = 0;
bool check = true;
for (auto item : _members) {
bool condition = get_group_condition(item->get_group());
if (false == condition) {
continue;
}
check = false;
} else {
if (check) {
item->read(p + pos, len, &size_read);
p += size_read;
len -= size_read;

uint16 space = item->get_space();
size_sum += space;
if (0 == space) {
if (nullptr == item->get_value_of()) {
_size_unknown.push_back(item);
}
check = false;
} else {
if (check) {
item->read(p + pos, len, &size_read);
p += size_read;
len -= size_read;
}
}
}
}
}

if (size > size_sum) {
if (1 == _size_unknown.size()) {
size_t remain = size - size_sum;
payload_member* item = *(_size_unknown.begin());
item->reserve(remain);
_size_unknown.clear();
if (_size_unknown.size() > 1) {
ret = errorcode_t::unknown;
__leave2;
}
}

if (_size_unknown.empty()) {
byte_t* p = base;
size_t len = size;
size_t pos = 0;
size_t size_read = 0;
bool check = true;
for (auto item : _members) {
uint16 space = item->get_space();
size_sum += space;
if (size > size_sum) {
if (1 == _size_unknown.size()) {
size_t remain = size - size_sum;
payload_member* item = *(_size_unknown.begin());
item->reserve(remain);
_size_unknown.clear();
}
}

if (_size_unknown.empty()) {
byte_t* p = base;
size_t len = size;
size_t pos = 0;
size_t size_read = 0;
bool check = true;
for (auto item : _members) {
bool condition = get_group_condition(item->get_group());
if (false == condition) {
continue;
}

uint16 space = item->get_space();
size_sum += space;

if (len < space) {
ret = errorcode_t::insufficient;
break;
}

item->read(p + pos, len, &size_read);
p += size_read;
len -= size_read;
item->read(p + pos, len, &size_read);
p += size_read;
len -= size_read;
}
}
}
__finally2 {
// do nothing
}

return *this;
return ret;
}

payload& payload::for_each(std::function<void(payload_member*)> func) {
Expand All @@ -330,6 +387,32 @@ payload_member* payload::select(std::string const& name) {
return item;
}

size_t payload::size_estimated() {
size_t ret_value = 0;
for (auto item : _members) {
bool condition = get_group_condition(item->get_group());
if (false == condition) {
continue;
}

ret_value += item->get_space();
}
return ret_value;
}

size_t payload::size_occupied() {
size_t ret_value = 0;
for (auto item : _members) {
bool condition = get_group_condition(item->get_group());
if (false == condition) {
continue;
}

ret_value += item->get_capacity();
}
return ret_value;
}

payload& payload::clear() {
for (auto item : _members) {
delete item;
Expand Down
21 changes: 17 additions & 4 deletions sdk/io/basic/payload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ namespace io {
* << new payload_member((uint32)0, true, "value")
* << new payload_member(pad, "pad", "pad");
* binary_t decoded = base16_decode("036461746100001000706164");
* pl.set_reference_value("pad", "padlen").read(decoded); // sizeof "pad" refers "padlen" value
* pl.set_reference_value("pad", "padlen");
* pl.read(decoded); // sizeof "pad" refers "padlen" value
*/
class payload_member {
public:
payload_member(uint8 value, const char* name = nullptr, const char* group = nullptr);
payload_member(uint8 value, uint16 repeat, const char* name = nullptr, const char* group = nullptr);
payload_member(uint16 value, bool change_endian, const char* name = nullptr, const char* group = nullptr);
payload_member(uint32_24_t value, const char* name = nullptr, const char* group = nullptr);
payload_member(uint32 value, bool change_endian, const char* name = nullptr, const char* group = nullptr);
Expand All @@ -81,6 +83,7 @@ class payload_member {
variant& get_variant();

size_t get_space();
size_t get_capacity();
payload_member* get_value_of();
payload_member& set_value_of(payload_member* member);

Expand Down Expand Up @@ -110,13 +113,23 @@ class payload {
bool get_group_condition(std::string const& name);
payload& set_reference_value(std::string const& name, std::string const& ref);

payload& dump(binary_t& bin);
payload& read(binary_t const& bin);
payload& read(byte_t* p, size_t size);
return_t dump(binary_t& bin);
return_t read(binary_t const& bin);
return_t read(byte_t* p, size_t size);

payload& for_each(std::function<void(payload_member*)> func);
payload_member* select(std::string const& name);

/**
* @brief size
* @return size estimated
* @remarks
* if (stream_size >= pl.size_estimated()) {
* pl.read(stream, stream_size);
* }
*/
size_t size_estimated();
size_t size_occupied();
payload& clear();

private:
Expand Down
Loading

0 comments on commit e011b51

Please sign in to comment.