Jelajahi Sumber

Make windows happy.

jcsyshc 2 tahun lalu
induk
melakukan
57c1524f0c

+ 1 - 1
CMakeLists.txt

@@ -12,4 +12,4 @@ target_include_directories(${PROJECT_NAME} PRIVATE ./src)
 target_link_libraries(${PROJECT_NAME} Sophiar2Lib)
 
 add_subdirectory(app)
-add_subdirectory(tests)
+#add_subdirectory(tests)

+ 2 - 13
app/main.cpp

@@ -1,10 +1,4 @@
-#include "core/global_defs.h"
-
-#include <boost/asio/io_context.hpp>
-
-#include <fstream>
-
-#include <nlohmann/json.hpp>
+#include "core/local_connection.h"
 
 #include <spdlog/spdlog.h>
 
@@ -13,11 +7,6 @@ using namespace sophiar;
 int main(int argc, char *argv[]) {
     spdlog::set_level(spdlog::level::trace);
     assert(argc == 2);
-    std::ifstream config_file(argv[1]);
-    assert(config_file.is_open());
-    auto config = nlohmann::json::parse(config_file);
-    auto ok = initialize(config);
-    assert(ok);
-    global_context->run();
+    run_sophiar(argv[1]);
     return 0;
 }

+ 8 - 4
src/CMakeLists.txt

@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 3.13)
 
 project(Sophiar2Lib)
-set(CMAKE_CXX_STANDARD 20)
+set(CMAKE_CXX_STANDARD 23)
 
 add_compile_options(-march=native)
 add_compile_options(-mno-avx) # enable avx will cause some stack pointer alignment error with Eigen
@@ -13,8 +13,10 @@ find_package(Boost REQUIRED COMPONENTS iostreams)
 list(APPEND BASIC_LIBS ${Boost_LIBRARIES})
 include_directories(${Boost_INCLUDE_DIRS})
 
-find_package(fmt REQUIRED)
-list(APPEND BASIC_LIBS fmt::fmt)
+if (NOT WIN32)
+    find_package(fmt REQUIRED)
+    list(APPEND BASIC_LIBS fmt::fmt)
+endif ()
 
 find_package(spdlog REQUIRED)
 list(APPEND BASIC_LIBS spdlog::spdlog)
@@ -40,4 +42,6 @@ include(robot/CMakeLists.txt)
 add_library(${PROJECT_NAME} main_empty.cpp)
 
 target_link_libraries(${PROJECT_NAME} ${BASIC_LIBS})
-target_link_libraries(${PROJECT_NAME} ${SOPHIAR_LIBS})
+target_link_libraries(${PROJECT_NAME} ${SOPHIAR_LIBS})
+
+add_subdirectory(../tests tests)

+ 2 - 0
src/algorithm/transform_stabilizer.cpp

@@ -9,6 +9,8 @@
 
 #include <Eigen/Geometry>
 
+#include <numbers>
+
 DEFAULT_TRISTATE_OBJ_DEF(transform_stabilizer)
 
 namespace sophiar {

+ 2 - 1
src/core/basic_obj_types.hpp

@@ -2,6 +2,7 @@
 #define SOPHIAR2_BASIC_OBJ_TYPES_HPP
 
 #include "core/small_obj.hpp"
+#include "utility/debug_utility.hpp"
 
 #include <Eigen/Geometry>
 
@@ -248,7 +249,7 @@ namespace sophiar {
             } \
             default: { \
                 assert(false); \
-                __builtin_unreachable(); \
+                unreachable(); \
             } \
         }
 

+ 5 - 0
src/core/local_connection.cpp

@@ -9,7 +9,12 @@
 #include <boost/asio/detached.hpp>
 #include <boost/asio/post.hpp>
 
+#ifndef _MSC_VER
+
 #include <fmt/ostream.h>
+
+#endif
+
 #include <spdlog/spdlog.h>
 
 #include <atomic>

+ 0 - 1
src/core/sophiar_manager.cpp

@@ -18,7 +18,6 @@
 #include <boost/asio/use_awaitable.hpp>
 #include <boost/system/error_code.hpp>
 
-#include <fmt/format.h>
 #include <spdlog/spdlog.h>
 
 #include <algorithm>

+ 0 - 2
src/core/sophiar_manager.h

@@ -7,8 +7,6 @@
 #include <boost/asio/awaitable.hpp>
 #include <boost/asio/io_context.hpp>
 
-#include <fmt/format.h>
-
 #include <nlohmann/json.hpp>
 
 #include <coroutine>

+ 3 - 4
src/sensor/ndi/ndi_interface.cpp

@@ -34,7 +34,6 @@
 
 #endif // BOOST_OS_WINDOWS_AVAILABLE
 
-#include <fmt/format.h>
 #include <spdlog/spdlog.h>
 
 #include <chrono>
@@ -189,7 +188,7 @@ namespace sophiar {
                 return async_read_value<ndi_endian, T>(*ndi_tcp_socket);
             }
             assert(false);
-            __builtin_unreachable();
+            unreachable();
         }
 
         template<WriteableMemory MemoryType>
@@ -200,7 +199,7 @@ namespace sophiar {
                 return async_fill_memory_from(*ndi_tcp_socket, mem);
             }
             assert(false);
-            __builtin_unreachable();
+            unreachable();
         }
 
         awaitable<dynamic_memory::pointer> read_binary_reply(uint16_t header_magic) {
@@ -398,7 +397,7 @@ namespace sophiar {
                 return async_write(*ndi_tcp_socket, buffer(cmd.data(), cmd.size()), use_awaitable);
             }
             assert(false);
-            __builtin_unreachable();
+            unreachable();
         }
 
         template<typename T, typename OutputIterator>

+ 1 - 1
src/third_party/static_block.hpp

@@ -74,7 +74,7 @@
 
 #define STATIC_BLOCK_IMPL2(function_name,var_name) \
 static void function_name(); \
-static int var_name __attribute((unused)) = (function_name(), 0) ; \
+static [[maybe_unused]] int var_name = (function_name(), 0) ; \
 static void function_name()
 
 #endif // STATIC_BLOCK_HPP_

+ 113 - 84
src/utility/config_utility.hpp

@@ -1,6 +1,11 @@
 #ifndef SOPHIAR2_CONFIG_UTILITY_HPP
 #define SOPHIAR2_CONFIG_UTILITY_HPP
 
+#include "core/global_defs.h"
+#include "core/sophiar_pool.h"
+
+#include <nlohmann/json.hpp>
+
 #define ENSURE_ARRAY(item_name) \
     assert(config.contains(item_name)); \
     assert(config[item_name].is_array());
@@ -13,90 +18,114 @@
     assert(config.contains(item_name)); \
     assert(config[item_name].is_string());
 
-#define LOAD_BOOL_ITEM(item_name) ({ \
-    assert(config.contains(item_name)); \
-    assert(config[item_name].is_boolean()); \
-    config[item_name].get<double>(); })
-
-#define LOAD_FLOAT_ITEM(item_name) ({ \
-    assert(config.contains(item_name)); \
-    assert(config[item_name].is_number()); \
-    config[item_name].get<double>(); })
-
-#define LOAD_UINT_ITEM(item_name) ({ \
-    assert(config.contains(item_name)); \
-    assert(config[item_name].is_number_unsigned()); \
-    config[item_name].get<std::uint64_t>(); })
-
-#define LOAD_STRING_ITEM(item_name) ({ \
-    ENSURE_STRING(item_name) \
-    config[item_name].get<std::string>(); })
-
-#define LOAD_STRING_ITEM2(config, item_name) ({ \
+inline auto LOAD_BOOL_ITEM2(const nlohmann::json &config,
+                            const std::string &item_name) {
+    assert(config.contains(item_name));
+    assert(config[item_name].is_boolean());
+    return config[item_name].get<bool>();
+}
+
+#define LOAD_BOOL_ITEM(item_name) \
+    LOAD_BOOL_ITEM2(config, item_name)
+
+inline auto LOAD_FLOAT_ITEM2(const nlohmann::json &config,
+                             const std::string &item_name) {
+    assert(config.contains(item_name));
+    assert(config[item_name].is_boolean());
+    return config[item_name].get<double>();
+}
+
+#define LOAD_FLOAT_ITEM(item_name) \
+    LOAD_FLOAT_ITEM2(config, item_name)
+
+inline auto LOAD_UINT_ITEM2(const nlohmann::json &config,
+                            const std::string &item_name) {
+    assert(config.contains(item_name));
+    assert(config[item_name].is_boolean());
+    return config[item_name].get<uint64_t>();
+}
+
+#define LOAD_UINT_ITEM(item_name) \
+    LOAD_UINT_ITEM2(config, item_name)
+
+inline auto LOAD_STRING_ITEM2(const nlohmann::json &config,
+                              const std::string &item_name) {
     ENSURE_STRING2(config, item_name) \
-    config[item_name].get<std::string>(); })
-
-#define LOAD_VARIABLE_INDEX(var_type, var_name) ({ \
-    auto _name = LOAD_STRING_ITEM(var_name); \
-    REQUIRE_VARIABLE(var_type, _name); })
-
-#define LOAD_VARIABLE_INDEX2(config, var_type, var_name) ({ \
-    auto _name = LOAD_STRING_ITEM2(config, var_name); \
-    REQUIRE_VARIABLE(var_type, _name); })
-
-#define TRY_LOAD_BOOL_ITEM(var_name, default_val) ({ \
-    bool _val = default_val; \
-    if (config.contains(var_name)) { \
-        assert(config[var_name].is_boolean()); \
-        _val = config[var_name].get<bool>(); \
-    } \
-        _val; \
-    })
-
-#define TRY_LOAD_FLOAT_ITEM(var_name, default_val) ({ \
-    double _val = default_val; \
-    if (config.contains(var_name)) { \
-        assert(config[var_name].is_number()); \
-        _val = config[var_name].get<double>(); \
-    } \
-        _val; \
-    })
-
-#define TRY_LOAD_FLOAT_ITEM2(config, var_name, default_val) ({ \
-    double _val = default_val; \
-    if (config.contains(var_name)) { \
-        assert(config[var_name].is_number()); \
-        _val = config[var_name].get<double>(); \
-    } \
-        _val; \
-    })
-
-#define TRY_LOAD_VARIABLE_INDEX(var_type, var_name) ({ \
-    variable_index_type _index = -1; \
-    if (config.contains(var_name)) { \
-        assert(config[var_name].is_string()); \
-        auto _name = config[var_name].get<std::string>(); \
-        _index = REQUIRE_VARIABLE(var_type, _name); \
-    } else { \
-        _index = invalid_variable_index; \
-    } \
-        _index; \
-    })
-
-#define TRY_LOAD_VARIABLE_INDEX2(config, var_type, var_name) ({ \
-    variable_index_type _index = -1; \
-    if (config.contains(var_name)) { \
-        assert(config[var_name].is_string()); \
-        auto _name = config[var_name].get<std::string>(); \
-        _index = REQUIRE_VARIABLE(var_type, _name); \
-    } else { \
-        _index = invalid_variable_index; \
-    } \
-        _index; \
-    })
-
-#define LOAD_VARIABLE_INDEX_WITH_NAME(var_type, var_name, var_name_out) ({ \
-    var_name_out = LOAD_STRING_ITEM(var_name); \
-    REQUIRE_VARIABLE(var_type, var_name_out); })
+    return config[item_name].get<std::string>();
+}
+
+#define LOAD_STRING_ITEM(item_name) \
+    LOAD_STRING_ITEM2(config, item_name)
+
+template<typename SmallObjType>
+inline auto LOAD_VARIABLE_INDEX2_IMPL(const nlohmann::json &config,
+                                      const std::string &var_name) {
+    auto name = LOAD_STRING_ITEM2(config, var_name);
+    using namespace sophiar;
+    return REQUIRE_VARIABLE(SmallObjType, name);
+}
+
+#define LOAD_VARIABLE_INDEX2(config, var_type, var_name) \
+    LOAD_VARIABLE_INDEX2_IMPL<var_type>(config, var_name)
+
+#define LOAD_VARIABLE_INDEX(var_type, var_name) \
+    LOAD_VARIABLE_INDEX2(config ,var_type, var_name)
+
+inline auto TRY_LOAD_BOOL_ITEM2(const nlohmann::json &config,
+                                const std::string &var_name, bool default_val) {
+    if (config.contains(var_name)) {
+        assert(config[var_name].is_number());
+        return config[var_name].get<bool>();
+    } else {
+        return default_val;
+    }
+}
+
+#define TRY_LOAD_BOOL_ITEM(var_name, default_val) \
+    TRY_LOAD_BOOL_ITEM2(config, var_name, default_val)
+
+inline auto TRY_LOAD_FLOAT_ITEM2(const nlohmann::json &config,
+                                 const std::string &var_name, double default_val) {
+    if (config.contains(var_name)) {
+        assert(config[var_name].is_number());
+        return config[var_name].get<double>();
+    } else {
+        return default_val;
+    }
+}
+
+#define TRY_LOAD_FLOAT_ITEM(var_name, default_val) \
+    TRY_LOAD_FLOAT_ITEM2(config, var_name, default_val)
+
+template<typename SmallObjType>
+inline sophiar::variable_index_type
+TRY_LOAD_VARIABLE_INDEX2_IMPL(const nlohmann::json &config,
+                              const std::string &var_name) {
+    using namespace sophiar;
+    if (config.contains(var_name)) {
+        assert(config[var_name].is_string());
+        auto _name = config[var_name].get<std::string>();
+        return REQUIRE_VARIABLE(SmallObjType, _name);
+    } else {
+        return invalid_variable_index;
+    }
+}
+
+#define TRY_LOAD_VARIABLE_INDEX2(config, var_type, var_name) \
+    TRY_LOAD_VARIABLE_INDEX2_IMPL<var_type>(config, var_name)
+
+#define TRY_LOAD_VARIABLE_INDEX(var_type, var_name) \
+    TRY_LOAD_VARIABLE_INDEX2(config, var_type, var_name)
+
+template<typename SmallObjType>
+inline auto LOAD_VARIABLE_INDEX_WITH_NAME_IMPL(const nlohmann::json &config,
+                                               const std::string &var_name, std::string &var_name_out) {
+    var_name_out = LOAD_STRING_ITEM(var_name);
+    using namespace sophiar;
+    return REQUIRE_VARIABLE(SmallObjType, var_name_out);
+}
+
+#define LOAD_VARIABLE_INDEX_WITH_NAME(var_type, var_name, var_name_out) \
+    LOAD_VARIABLE_INDEX_WITH_NAME_IMPL<var_type>(config, var_name, var_name_out)
 
 #endif //SOPHIAR2_CONFIG_UTILITY_HPP

+ 23 - 0
src/utility/debug_utility.hpp

@@ -13,6 +13,18 @@
 
 #include <spdlog/spdlog.h>
 
+#ifdef _MSC_VER
+
+#include <format>
+
+#define fmt std
+
+#else
+
+#include <fmt/format>
+
+#endif
+
 #include <iostream>
 
 #define DEBUG_PRINT(msg) \
@@ -87,6 +99,17 @@ namespace sophiar {
 
 #define PRINT_VARIABLE(var) print_variable(#var, var)
 
+    [[noreturn]] inline void unreachable() {
+        // Uses compiler specific extensions if possible.
+        // Even if no extension is used, undefined behavior is still raised by
+        // an empty function body and the noreturn attribute.
+#ifdef __GNUC__ // GCC, Clang, ICC
+        __builtin_unreachable();
+#else // MSVC
+        __assume(false);
+#endif
+    }
+
 }
 
 #endif //SOPHIAR2_DEBUG_MACRO_H

+ 2 - 2
src/utility/string_map.hpp

@@ -34,12 +34,12 @@ namespace sophiar {
             m.clear();
         }
 
-        static auto calc_hash(std::string_view str) {
+        auto calc_hash(std::string_view str) {
             return hash_sv(str);
         }
 
     private:
-        static constexpr auto hash_sv = std::hash<std::string_view>{};
+        std::hash<std::string_view> hash_sv;
         std::unordered_map<std::size_t, T> m;
 
         template<typename ...Args>

+ 1 - 1
tests/data/transform_tree_config.json

@@ -59,7 +59,7 @@
       }
     },
     {
-      "type": "object_watcher",
+      "type": "variable_watcher",
       "name": "transform_watcher",
       "start_config": {
         "variable_name_list": [