From d01414ef348b1c65a5f455d762d99ae332986357 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 12:22:43 -0500 Subject: [PATCH 01/12] GLZ_META macros more easily register structs --- include/glaze/core/macros.hpp | 29 +++++++++++++++++++++++++++ tests/json_test/json_test.cpp | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/glaze/core/macros.hpp b/include/glaze/core/macros.hpp index bb2b7f51da..d4ec98f95e 100644 --- a/include/glaze/core/macros.hpp +++ b/include/glaze/core/macros.hpp @@ -3,4 +3,33 @@ #pragma once +// utility macros +// https://www.scs.stanford.edu/~dm/blog/va-opt.html + +#define PARENS () + +#define EXPAND(...) EXPAND4(EXPAND4(EXPAND4(EXPAND4(__VA_ARGS__)))) +#define EXPAND4(...) EXPAND3(EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__)))) +#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__)))) +#define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__)))) +#define EXPAND1(...) __VA_ARGS__ + +#define FOR_EACH(macro, ...) \ + __VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__))) +#define FOR_EACH_HELPER(macro, a1, ...) \ + macro(a1)__VA_OPT__(,) \ + __VA_OPT__(FOR_EACH_AGAIN PARENS (macro, __VA_ARGS__)) +#define FOR_EACH_AGAIN() FOR_EACH_HELPER + +// Glaze specific macros + +#define GLZ_X(a) #a, &T::a + +#define GLZ_META(C, ...) template <> struct glz::meta { \ +using T = C; \ +static constexpr auto value = object(FOR_EACH(GLZ_X, __VA_ARGS__)); } + +#define GLZ_LOCAL_META(C, ...) struct glaze { \ +using T = C; \ +static constexpr auto value = glz::object(FOR_EACH(GLZ_X, __VA_ARGS__)); } diff --git a/tests/json_test/json_test.cpp b/tests/json_test/json_test.cpp index ab252a17cf..af92c3bbe9 100644 --- a/tests/json_test/json_test.cpp +++ b/tests/json_test/json_test.cpp @@ -11,6 +11,7 @@ #include #include +#include "glaze/core/macros.hpp" #include "boost/ut.hpp" #include "glaze/json/json_ptr.hpp" #include "glaze/json/from_ptr.hpp" @@ -1862,6 +1863,42 @@ suite nan_tests = [] { }; }; +struct macro_t +{ + double x = 5.0; + std::string y = "yay!"; + int z = 55; +}; + +GLZ_META(macro_t, x, y, z); + +struct local_macro_t +{ + double x = 5.0; + std::string y = "yay!"; + int z = 55; + + GLZ_LOCAL_META(local_macro_t, x, y, z); +}; + +suite macro_tests = [] { + "macro test"_test = [] { + macro_t obj{}; + std::string b{}; + glz::write_json(obj, b); + + expect(b == R"({"x":5,"y":"yay!","z":55})"); + }; + + "local macro test"_test = [] { + local_macro_t obj{}; + std::string b{}; + glz::write_json(obj, b); + + expect(b == R"({"x":5,"y":"yay!","z":55})"); + }; +}; + int main() { using namespace boost::ut; From 542b357fba2d9733aacfe0964deda448b049dde6 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 13:05:43 -0500 Subject: [PATCH 02/12] MSVC preprocessor flag --- CMakeLists.txt | 5 +++++ include/glaze/core/macros.hpp | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 551d5904b3..da215d8945 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,11 @@ project( LANGUAGES CXX ) +if (MSVC) + # for a C++ standards compliant preprocessor: + target_compile_options(${PROJECT_NAME} INTERFACE "/Zc:preprocessor") +endif() + include(cmake/project-is-top-level.cmake) include(cmake/variables.cmake) diff --git a/include/glaze/core/macros.hpp b/include/glaze/core/macros.hpp index d4ec98f95e..905757e29b 100644 --- a/include/glaze/core/macros.hpp +++ b/include/glaze/core/macros.hpp @@ -15,10 +15,10 @@ #define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__)))) #define EXPAND1(...) __VA_ARGS__ -#define FOR_EACH(macro, ...) \ +#define FOR_EACH(macro, ...) \ __VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__))) -#define FOR_EACH_HELPER(macro, a1, ...) \ - macro(a1)__VA_OPT__(,) \ +#define FOR_EACH_HELPER(macro, a, ...) \ + macro(a)__VA_OPT__(,) \ __VA_OPT__(FOR_EACH_AGAIN PARENS (macro, __VA_ARGS__)) #define FOR_EACH_AGAIN() FOR_EACH_HELPER From c4ee1f29c8a76d0928721ac42f36fd335554c7ce Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 13:12:04 -0500 Subject: [PATCH 03/12] Update README.md --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index a261bb26d9..0bf844ca4e 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,33 @@ struct my_struct > Template specialization of `glz::meta` is preferred when separating class definition from the serialization mapping. Local glaze metadata is helpful for working within the local namespace or when the class itself is templated. +## Struct Registration Macros + +Glaze provides macros to more efficiently register your C++ structs. + +> In order to use these macros you must include the header: `glaze/core/macros.hpp` + +- GLZ_META is for external registration +- GLZ_LOCAL_META is for internal registration + +```c++ +struct macro_t { + double x = 5.0; + std::string y = "yay!"; + int z = 55; +}; + +GLZ_META(macro_t, x, y, z); + +struct local_macro_t { + double x = 5.0; + std::string y = "yay!"; + int z = 55; + + GLZ_LOCAL_META(local_macro_t, x, y, z); +}; +``` + ## JSON Pointer Syntax [Here is a simple JSON pointer syntax explanation](https://github.com/stephenberry/JSON-Pointer) From 0c8c9d83b255a423e1926318762efea3ec0b43f7 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 13:14:17 -0500 Subject: [PATCH 04/12] getting msvc to build --- CMakeLists.txt | 5 ----- tests/json_test/CMakeLists.txt | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index da215d8945..551d5904b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,11 +8,6 @@ project( LANGUAGES CXX ) -if (MSVC) - # for a C++ standards compliant preprocessor: - target_compile_options(${PROJECT_NAME} INTERFACE "/Zc:preprocessor") -endif() - include(cmake/project-is-top-level.cmake) include(cmake/variables.cmake) diff --git a/tests/json_test/CMakeLists.txt b/tests/json_test/CMakeLists.txt index f283687f2f..6e33df195a 100644 --- a/tests/json_test/CMakeLists.txt +++ b/tests/json_test/CMakeLists.txt @@ -1,5 +1,10 @@ project(json_test) +if (MSVC) + # for a C++ standards compliant preprocessor: + target_compile_options(${PROJECT_NAME} INTERFACE "/Zc:preprocessor") +endif() + add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp) target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) From f7af550300934e1e4eccf9eee76af605c59af6db Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 13:20:28 -0500 Subject: [PATCH 05/12] Update CMakeLists.txt --- tests/json_test/CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/json_test/CMakeLists.txt b/tests/json_test/CMakeLists.txt index 6e33df195a..0a0d24f2de 100644 --- a/tests/json_test/CMakeLists.txt +++ b/tests/json_test/CMakeLists.txt @@ -1,10 +1,5 @@ project(json_test) -if (MSVC) - # for a C++ standards compliant preprocessor: - target_compile_options(${PROJECT_NAME} INTERFACE "/Zc:preprocessor") -endif() - add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp) target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) @@ -12,3 +7,8 @@ target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) target_link_libraries(${PROJECT_NAME} PRIVATE glaze::glaze Boost::ut) add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) + +if (MSVC) + # for a C++ standards compliant preprocessor: + target_compile_options(${PROJECT_NAME} "/Zc:preprocessor") +endif() From 109c29c50d5f1cf767f51090c42bac9e99c8d52b Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 13:24:09 -0500 Subject: [PATCH 06/12] Update CMakeLists.txt --- tests/json_test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/json_test/CMakeLists.txt b/tests/json_test/CMakeLists.txt index 0a0d24f2de..78e4486871 100644 --- a/tests/json_test/CMakeLists.txt +++ b/tests/json_test/CMakeLists.txt @@ -10,5 +10,5 @@ add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) if (MSVC) # for a C++ standards compliant preprocessor: - target_compile_options(${PROJECT_NAME} "/Zc:preprocessor") + target_compile_options(${PROJECT_NAME} PUBLIC "/Zc:preprocessor") endif() From 3d6979261642d2ecf86082a101fa8125208c46e1 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 14:08:26 -0500 Subject: [PATCH 07/12] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0bf844ca4e..be8d2ef1da 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,8 @@ struct local_macro_t { }; ``` +**Note: MSVC requires the compiler flag `/Zc:preprocessor` for a standards compliant preprocessor.** + ## JSON Pointer Syntax [Here is a simple JSON pointer syntax explanation](https://github.com/stephenberry/JSON-Pointer) From c220bd4f43f3ec27ee2fcaff5a603ec7997be4d1 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 14:32:40 -0500 Subject: [PATCH 08/12] using standard compliant preproccessor for MSVC --- CMakeLists.txt | 5 +++++ tests/CMakeLists.txt | 2 +- tests/json_test/CMakeLists.txt | 5 ----- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 551d5904b3..9b21afeb2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,11 @@ include(cmake/variables.cmake) add_library(glaze_glaze INTERFACE) add_library(glaze::glaze ALIAS glaze_glaze) +if (MSVC) + # for a C++ standards compliant preprocessor: + target_compile_options(glaze_glaze INTERFACE "/Zc:preprocessor") +endif() + set_property(TARGET glaze_glaze PROPERTY EXPORT_NAME glaze) target_compile_features(glaze_glaze INTERFACE cxx_std_20) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8bad10ca9c..a911e9da3f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ CPMFindPackage( NAME ut - OPTIONS "BOOST_UT_ENABLE_RUN_AFTER_BUILD OFF" + OPTIONS "BOOST_UT_ENABLE_RUN_AFTER_BUILD OFF" "BOOST_UT_DISABLE_MODULE ON" GIT_REPOSITORY https://github.com/boost-ext/ut.git GIT_TAG v1.1.9 ) diff --git a/tests/json_test/CMakeLists.txt b/tests/json_test/CMakeLists.txt index 78e4486871..f283687f2f 100644 --- a/tests/json_test/CMakeLists.txt +++ b/tests/json_test/CMakeLists.txt @@ -7,8 +7,3 @@ target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) target_link_libraries(${PROJECT_NAME} PRIVATE glaze::glaze Boost::ut) add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) - -if (MSVC) - # for a C++ standards compliant preprocessor: - target_compile_options(${PROJECT_NAME} PUBLIC "/Zc:preprocessor") -endif() From 1dc2deb03fd69898173705c1e2c43d707ddd8d9e Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 15:13:47 -0500 Subject: [PATCH 09/12] Create msvc_cpm.yml --- .github/workflows/msvc_cpm.yml | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/msvc_cpm.yml diff --git a/.github/workflows/msvc_cpm.yml b/.github/workflows/msvc_cpm.yml new file mode 100644 index 0000000000..86f8227ef2 --- /dev/null +++ b/.github/workflows/msvc_cpm.yml @@ -0,0 +1,46 @@ +name: msvc + +on: + push: + branches: + - main + - feature/* + paths-ignore: + - '**.md' + pull_request: + branches: [main] + paths-ignore: + - '**.md' + +env: + BUILD_TYPE: Release + +jobs: + build: + runs-on: windows-2019 + + steps: + - uses: actions/checkout@v3 + + - name: Install Conan + uses: turtlebrowser/get-conan@main + + - name: Configure Conan + run: | + conan profile new default + cp .github/vs-16.profile "$env:userprofile/.conan/profiles/default" + conan profile update settings.build_type=${{env.BUILD_TYPE}} default + conan profile update "settings.compiler.runtime=MD$(($env:BUILD_TYPE -eq 'Release') ? '' : 'd')" default + + - name: Install dependencies + run: conan install . -b missing + + - name: Configure CMake + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE="./conan/conan_toolchain.cmake" + + - name: Build + run: cmake --build build --config ${{env.BUILD_TYPE}} -j 2 + + - name: Test + working-directory: build + run: ctest -C ${{env.BUILD_TYPE}} -j 2 --output-on-failure From f662ad718b2a7a15fefebf6c5f01993abf7198e2 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 15:17:01 -0500 Subject: [PATCH 10/12] Update msvc_cpm.yml --- .github/workflows/msvc_cpm.yml | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/.github/workflows/msvc_cpm.yml b/.github/workflows/msvc_cpm.yml index 86f8227ef2..4702029129 100644 --- a/.github/workflows/msvc_cpm.yml +++ b/.github/workflows/msvc_cpm.yml @@ -1,4 +1,4 @@ -name: msvc +name: msvc_cpm on: push: @@ -22,21 +22,8 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Install Conan - uses: turtlebrowser/get-conan@main - - - name: Configure Conan - run: | - conan profile new default - cp .github/vs-16.profile "$env:userprofile/.conan/profiles/default" - conan profile update settings.build_type=${{env.BUILD_TYPE}} default - conan profile update "settings.compiler.runtime=MD$(($env:BUILD_TYPE -eq 'Release') ? '' : 'd')" default - - - name: Install dependencies - run: conan install . -b missing - - name: Configure CMake - run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE="./conan/conan_toolchain.cmake" + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - name: Build run: cmake --build build --config ${{env.BUILD_TYPE}} -j 2 From 153e5620febc7288d962c3a6a9c862cd20c0a88b Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 15:26:02 -0500 Subject: [PATCH 11/12] reduced cmake required version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b21afeb2f..76d36750b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.18) include(cmake/prelude.cmake) From 399ef690a304a63d043a0e7c5f09dd41394465df Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 25 Oct 2022 15:30:31 -0500 Subject: [PATCH 12/12] using newer windows sdk --- .github/workflows/msvc.yml | 3 ++- .github/workflows/msvc_cpm.yml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/msvc.yml b/.github/workflows/msvc.yml index 86f8227ef2..9dbea3df26 100644 --- a/.github/workflows/msvc.yml +++ b/.github/workflows/msvc.yml @@ -36,7 +36,8 @@ jobs: run: conan install . -b missing - name: Configure CMake - run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE="./conan/conan_toolchain.cmake" + # using specific windows SDK to address this issue: https://stackoverflow.com/questions/65402366/c5105-and-other-compiler-warnings-when-building-with-github-actions-winsdk-10 + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE="./conan/conan_toolchain.cmake" -DCMAKE_SYSTEM_VERSION="10.0.22621.0" - name: Build run: cmake --build build --config ${{env.BUILD_TYPE}} -j 2 diff --git a/.github/workflows/msvc_cpm.yml b/.github/workflows/msvc_cpm.yml index 4702029129..1d7cc36a19 100644 --- a/.github/workflows/msvc_cpm.yml +++ b/.github/workflows/msvc_cpm.yml @@ -22,8 +22,9 @@ jobs: steps: - uses: actions/checkout@v3 + # using specific windows SDK to address this issue: https://stackoverflow.com/questions/65402366/c5105-and-other-compiler-warnings-when-building-with-github-actions-winsdk-10 - name: Configure CMake - run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_SYSTEM_VERSION="10.0.22621.0" - name: Build run: cmake --build build --config ${{env.BUILD_TYPE}} -j 2