From 7d146cbab9362309ebb6a9b31bebc0d154bb56b6 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Tue, 16 Apr 2024 10:25:59 -0600 Subject: [PATCH] Add JSON submodule and state class. This is failing to compile properly on macOS, trying on Linux --- .gitignore | 1 + .gitmodules | 3 +++ CMakeLists.txt | 9 ++++++++ src/json | 1 + src/main.cpp | 8 +++++-- src/state.cpp | 7 ++++++ src/state.h | 56 +++++++++++++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 2 ++ test/state_test.cpp | 21 +++++++++++++++++ 9 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 src/json create mode 100644 src/state.cpp create mode 100644 src/state.h create mode 100644 test/CMakeLists.txt create mode 100644 test/state_test.cpp diff --git a/.gitignore b/.gitignore index 378eac2..0339490 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build +*.DS_Store \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d2b2562 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src/json"] + path = src/json + url = https://github.com/nlohmann/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt index e84b92f..a376af1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules) + find_package(OpenGL REQUIRED) find_package(GLUT REQUIRED) find_package(OpenCV HINTS /usr/local/opt/opencv /usr/local/Cellar/opencv REQUIRED) @@ -14,6 +15,14 @@ set( NAME_SRC src/main.cpp ) +ENABLE_TESTING() +ADD_SUBDIRECTORY( test ) +set(UNIT_TEST state_test) +add_test(NAME ${UNIT_TEST} COMMAND ${UNIT_TEST}) +add_custom_target(run_unit_test ALL + COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure + DEPENDS ${UNIT_TEST}) + set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) add_executable( fd ${NAME_SRC} ${NAME_HEADERS} ) diff --git a/src/json b/src/json new file mode 160000 index 0000000..8c391e0 --- /dev/null +++ b/src/json @@ -0,0 +1 @@ +Subproject commit 8c391e04fe4195d8be862c97f38cfe10e2a3472e diff --git a/src/main.cpp b/src/main.cpp index ce2460c..dce72ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,10 @@ #include "image.h" +#include "state.h" -int main(int argc, char** argv) -{ +using namespace std; +int main(int argc, char** argv) { + string action_load = "{\"action\" : 0 }"; + json msg_data = json::parse(action_load); + MessageIn msgIn(msg_data); } \ No newline at end of file diff --git a/src/state.cpp b/src/state.cpp new file mode 100644 index 0000000..b76ce74 --- /dev/null +++ b/src/state.cpp @@ -0,0 +1,7 @@ +#include "state.h" + +MessageIn::MessageIn (json msgData) { + if (msgData.contains("action")) { + action = static_cast(msgData["action"]); + } +} \ No newline at end of file diff --git a/src/state.h b/src/state.h new file mode 100644 index 0000000..1e84476 --- /dev/null +++ b/src/state.h @@ -0,0 +1,56 @@ +#ifndef STATE_h +#define STATE_h + +#include "json/single_include/nlohmann/json.hpp" +#include + +using json = nlohmann::json; +using namespace std; + +enum Action { + LOAD, + DISPLAY, + STOP +}; + +enum Mode { + RGB, + BW, + + INVERT, + BW_INVERT, + + RGB_CHANNELS, + INVERT_CHANNELS +}; + +class MessageIn { + private: + Action action; + string file; + + Mode mode; + vector exposure; + bool start; + public: + MessageIn(json msgJson); + Action getAction () { return action;} +}; +/* +class MessageOut { + public: + MessageOut(Action a, bool s); + Action action; + bool success; + string toString(); +}; + +class State { + public : + MessageIn active; + State(); + void processMessage(string msgString); + void createMessage(Action action, bool success); +}; +*/ +#endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..4f11378 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_EXECUTABLE( state_test state_test.cpp ) +ADD_TEST( fd state_test ) \ No newline at end of file diff --git a/test/state_test.cpp b/test/state_test.cpp new file mode 100644 index 0000000..34dd162 --- /dev/null +++ b/test/state_test.cpp @@ -0,0 +1,21 @@ +#include +#include "../src/state.h" + +void IS_TRUE(string name, bool x) { + if (!(x)) { + cout << "Failed test " << name << ":" << endl; + cout << __FUNCTION__ << " failed on line " << __LINE__ << endl; + exit(EXIT_FAILURE); + } +} + +void test_message_in() { + //string action_load = "{\"action\" : 0 }"; + //json msg_data = json::parse(action_load); + //MessageIn msgIn(msg_data); + //IS_TRUE("parses_action_load", msgIn.getAction() == LOAD); +} + +int main(void) { + test_message_in(); +} \ No newline at end of file