Skip to content

Commit

Permalink
Merge pull request #29 from stephenberry/meta_macros
Browse files Browse the repository at this point in the history
GLZ_META macros, addressing issue #27
  • Loading branch information
stephenberry authored Oct 25, 2022
2 parents 654b3b8 + 399ef69 commit 1370a1e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/msvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/msvc_cpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: msvc_cpm

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

# 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}} -DCMAKE_SYSTEM_VERSION="10.0.22621.0"

- 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
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.18)

include(cmake/prelude.cmake)

Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,35 @@ 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);
};
```
**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)
Expand Down
29 changes: 29 additions & 0 deletions include/glaze/core/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, a, ...) \
macro(a)__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<C> { \
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__)); }
37 changes: 37 additions & 0 deletions tests/json_test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <list>
#include <deque>

#include "glaze/core/macros.hpp"
#include "boost/ut.hpp"
#include "glaze/json/json_ptr.hpp"
#include "glaze/json/from_ptr.hpp"
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1370a1e

Please sign in to comment.