diff --git a/CMakeLists.txt b/CMakeLists.txt index 5849ac5..d6adeaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,11 @@ add_definitions("-std=c++2a -g") include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() -add_library(commons_lib include/commons.cpp) +add_library(commons_lib + include/commons.cpp + src/protocol.cpp + ) + target_include_directories (commons_lib PUBLIC ${CONAN_INCLUDE_DIRS} @@ -23,6 +27,22 @@ target_link_libraries(commons_lib ${CONAN_LIBS} ) +add_executable(commons + src/main.cpp + ) + +target_include_directories(commons + PUBLIC + ${CONAN_INCLUDE_DIRS} + include + commons_lib + ) + +target_link_directories(commons + PUBLIC + commons_lib + ) + #enable_testing() #add_subdirectory(tests) set_target_properties(commons_lib PROPERTIES LINKER_LANGUAGE CXX) diff --git a/conanfile.txt b/conanfile.txt index 4d6782c..cc1992a 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,4 +1,5 @@ [requires] +glm/0.9.9.8 boost/1.78.0 spdlog/1.10.0 gtest/cci.20210126 diff --git a/include/protocol.hpp b/include/protocol.hpp new file mode 100644 index 0000000..ffac37a --- /dev/null +++ b/include/protocol.hpp @@ -0,0 +1,180 @@ +#pragma once + +#include + +#include +#include +#include + +namespace commons::protocol +{ + +enum class Type +{ + DRAW_RECTANGLE = 0, + DRAW_PIXEL, + COMPOSED_MESSAGE, +}; + + +struct header +{ + Type type; + uint32_t message_length; +}; + +struct generic_message_base +{ + virtual constexpr Type get_type() const = 0; + virtual uint32_t get_size() const = 0; + + //virtual void operator()() + //{ + // if(is_handler) + // { + // handle_message(); + // return; + // } + + // create_message(); + //} + + //virtual void handle_message() = 0; + //virtual void create_message() = 0; + + bool is_handler = false; +}; + +template +struct generic_message : public generic_message_base +{ + virtual ~generic_message() = default; + + virtual constexpr Type get_type() const override + { + return type; + } + + virtual header get_header() const + { + return header{get_type(), get_size()}; + } + + static constexpr Type type = TypeValue; +}; + +struct message +{ + //header head; + //generic_message_base* msg; + + //bool init_header() + //{ + // if(!msg) + // { + // return false; + // } + + // head = msg->get_header(); + // return true; + //} + + //uint32_t get_size() const + //{ + //} +}; + +struct composed_message : public generic_message +{ + message** messages; +}; + +struct draw_rectangle : public generic_message +{ + virtual ~draw_rectangle() = default; + + virtual uint32_t get_size() const override + { + return sizeof(*this); + } + + glm::vec2 position; + glm::vec2 size; + glm::vec3 color; +}; + +struct draw_pixel : public generic_message +{ + virtual ~draw_pixel() = default; + + virtual uint32_t get_size() const override + { + return sizeof(*this); + } + + glm::vec2 position; + glm::vec3 color; +}; + +uint32_t get_size(Type type) +{ + static std::map type_map{}; + + //initalizes map on first run + static bool run_once = [&](){ + type_map.emplace(Type::DRAW_PIXEL, sizeof(draw_pixel)); + type_map.emplace(Type::DRAW_RECTANGLE, sizeof(draw_rectangle)); + type_map.emplace(Type::COMPOSED_MESSAGE, sizeof(composed_message)); + return true; + }(); + + return type_map[type]; +} + + +generic_message_base* get_object(Type type, generic_message_base* msg) +{ + switch(type) + { + case Type::COMPOSED_MESSAGE: + { + return reinterpret_cast(msg); + } + case Type::DRAW_RECTANGLE: + { + return reinterpret_cast(msg); + } + case Type::DRAW_PIXEL: + { + return reinterpret_cast(msg); + } + default: + return nullptr; + } +} + +//template +//auto* get_object(generic_message_base* msg) +//{ +// return nullptr; +//} +// +//template<> +//auto* get_object(generic_message_base* msg) +//{ +// return reinterpret_cast(msg); +//} +// +//template<> +//auto* get_object(generic_message_base* msg) +//{ +// return reinterpret_cast(msg); +//} +// +//template<> +//auto* get_object(generic_message_base* msg) +//{ +// return reinterpret_cast(msg); +//} + +} diff --git a/src/main.cpp b/src/main.cpp index 743782f..a7f6d8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,27 @@ #include #include -#include "crow.h" + +#include "protocol.hpp" int main() { - crow::SimpleApp app; + using namespace commons::protocol; + auto draw_msg = draw_rectangle{}; - CROW_ROUTE(app, "/")([]() { - return "Hello World"; - }); + for(int i = 0; i < 3; ++i) + { + std::cout << "Size of " << i << " is " << get_size(static_cast(i)) << "\n"; + } + + auto rectangle = std::make_unique(); + void* void_ptr = reinterpret_cast(rectangle.get()); + + generic_message_base* rectangle_ptr = reinterpret_cast(void_ptr); + + std::cout << "Type: " << static_cast(rectangle_ptr->get_type()) << "\n"; + std::cout << "Size: " << static_cast(rectangle_ptr->get_size()) << "\n"; + + auto* final_msg = get_object(rectangle_ptr->get_type(), rectangle_ptr); - app.port(18080).run(); return 0; } diff --git a/src/protocol.cpp b/src/protocol.cpp new file mode 100644 index 0000000..c2a0fc0 --- /dev/null +++ b/src/protocol.cpp @@ -0,0 +1 @@ +#include "protocol.hpp"