A lot of query reworking

This commit is contained in:
WolverinDEV 2021-01-28 20:59:14 +01:00
parent eb77a7fefb
commit 0cd49a6a2b
4 changed files with 73 additions and 79 deletions

View File

@ -7,8 +7,7 @@
#include <thread> #include <thread>
#include <condition_variable> #include <condition_variable>
namespace ts { namespace ts::event {
namespace event {
class EventExecutor; class EventExecutor;
class EventEntry { class EventEntry {
@ -33,8 +32,9 @@ namespace ts {
void event_execute(const std::chrono::system_clock::time_point &point) override { void event_execute(const std::chrono::system_clock::time_point &point) override {
auto _instance = this->instance.lock(); auto _instance = this->instance.lock();
if(!_instance) if(!_instance) {
return; return;
}
auto callback_ptr = (void**) &this->callback; auto callback_ptr = (void**) &this->callback;
(*(static_callback_t*) callback_ptr)(&*_instance, point); (*(static_callback_t*) callback_ptr)(&*_instance, point);
@ -87,4 +87,3 @@ namespace ts {
std::string _thread_prefix; std::string _thread_prefix;
}; };
} }
}

View File

@ -111,9 +111,6 @@ namespace ts {
this->memory_state.id_branded = true; this->memory_state.id_branded = true;
this->setPacketId(packetId, generationId); this->setPacketId(packetId, generationId);
} }
Command BasicPacket::asCommand() {
return Command::parse(this->data());
}
/** /**
* @param buffer -> [mac][Header [uint16 BE packetId | [uint8](4bit flags | 4bit type)]][Data] * @param buffer -> [mac][Header [uint16 BE packetId | [uint8](4bit flags | 4bit type)]][Data]

View File

@ -78,39 +78,37 @@ namespace ts {
} }
*/ */
Command Command::parse(const pipes::buffer_view &buffer, bool expect_type, bool drop_non_utf8) { Command Command::parse(const std::string_view& command_data, bool expect_type, bool drop_non_utf8) {
string_view data{buffer.data_ptr<const char>(), buffer.length()};
Command result; Command result;
size_t current_index = std::string::npos, end_index; size_t current_index = std::string::npos, end_index;
if(expect_type) { if(expect_type) {
current_index = data.find(' ', 0); current_index = command_data.find(' ', 0);
if(current_index == std::string::npos){ if(current_index == std::string::npos){
result._command = std::string(data); result._command = std::string{command_data};
return result; return result;
} else { } else {
result._command = std::string(data.substr(0, current_index)); result._command = std::string{command_data.substr(0, current_index)};
} }
} }
size_t bulk_index = 0; size_t bulk_index = 0;
while(++current_index > 0 || (current_index == 0 && !expect_type && (expect_type = true))) { while(++current_index > 0 || (current_index == 0 && !expect_type && (expect_type = true))) {
end_index = data.find_first_of(" |", current_index); end_index = command_data.find_first_of(" |", current_index);
if(end_index != current_index && current_index < data.length()) { /* else we've found another space or a pipe */ if(end_index != current_index && current_index < command_data.length()) { /* else we've found another space or a pipe */
if(data[current_index] == '-') { if(command_data[current_index] == '-') {
string trigger(data.substr(current_index + 1, end_index - current_index - 1)); string trigger(command_data.substr(current_index + 1, end_index - current_index - 1));
result.paramethers.push_back(trigger); result.paramethers.push_back(trigger);
} else { } else {
auto index_assign = data.find_first_of('=', current_index); auto index_assign = command_data.find_first_of('=', current_index);
string key, value; string key, value;
if(index_assign == string::npos || index_assign > end_index) { if(index_assign == string::npos || index_assign > end_index) {
key = data.substr(current_index, end_index - current_index); key = command_data.substr(current_index, end_index - current_index);
} else { } else {
key = data.substr(current_index, index_assign - current_index); key = command_data.substr(current_index, index_assign - current_index);
try { try {
value = query::unescape(string(data.substr(index_assign + 1, end_index - index_assign - 1)), true); value = query::unescape(string(command_data.substr(index_assign + 1, end_index - index_assign - 1)), true);
} catch(const std::invalid_argument& ex) { } catch(const std::invalid_argument& ex) {
(void) ex; (void) ex;
@ -134,7 +132,7 @@ namespace ts {
} }
} }
if(end_index < data.length() && data[end_index] == '|') if(end_index < command_data.length() && command_data[end_index] == '|')
bulk_index++; bulk_index++;
current_index = end_index; current_index = end_index;
} }

View File

@ -161,7 +161,7 @@ operator type(){ \
class Command { class Command {
public: public:
static Command parse(const pipes::buffer_view& buffer, bool expect_command = true, bool drop_non_utf8 = false); static Command parse(const std::string_view& command_data, bool expect_command = true, bool drop_non_utf8 = false);
explicit Command(const std::string& command); explicit Command(const std::string& command);
explicit Command(const std::string& command, std::initializer_list<variable>); explicit Command(const std::string& command, std::initializer_list<variable>);