Skip to content

Commit

Permalink
Merge pull request #1 from RedSkittleFox/v3
Browse files Browse the repository at this point in the history
v3-update
  • Loading branch information
RedSkittleFox authored Jan 17, 2024
2 parents b52d590 + 88096a4 commit f7ce538
Show file tree
Hide file tree
Showing 12 changed files with 860 additions and 533 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/cmake-msvc-build.yml

This file was deleted.

75 changes: 75 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CMake on multiple platforms

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, cl] # clang,
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
# - os: ubuntu-latest
# c_compiler: clang
# cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: gcc
# - os: windows-latest
# c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v3

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ bin/*
bin-etc/*
lib/*
output/*
out/*
.vs/*

# Autogenerated
test/reflexpr_test_types.inl

# Prerequisites
*.d
Expand Down
27 changes: 19 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ if (PROJECT_IS_TOP_LEVEL)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin-etc")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED)
endif()

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
enable_testing()

include(CheckIPOSupported)
check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_NOT_SUPPORTED_ERROR LANGUAGES CXX)
Expand All @@ -33,13 +32,25 @@ add_subdirectory("include")

add_library(fox::reflexpr ALIAS reflexpr)

option(sample ON)
# option(test ON)
option(
FOX_REFLEXPR_BUILD_SAMPLES
"If samples are built."
ON
)

if (sample)
option(
FOX_REFLEXPR_BUILD_TESTS
"If unit tests are built"
ON
)

option(FOX_REFLEXPR_BUILD_SAMPLES ON)
option(FOX_REFLEXPR_BUILD_TESTS ON)

if (FOX_REFLEXPR_BUILD_SAMPLES)
add_subdirectory("sample")
endif()

# if (test)
# add_subdirectory("test")
# endif()
if (FOX_REFLEXPR_BUILD_TESTS)
add_subdirectory("test")
endif()
62 changes: 62 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x64-Clang-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "clang_cl_x64_x64" ],
"variables": []
},
{
"name": "Mingw64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": "",
"inheritEnvironments": [ "mingw_64" ],
"environments": [
{
"MINGW64_ROOT": "C:/msys64/mingw64",
"BIN_ROOT": "${env.MINGW64_ROOT}/bin",
"FLAVOR": "x86_64-w64-mingw32",
"TOOLSET_VERSION": "9.1.0",
"PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
"INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}",
"environment": "mingw_64"
}
],
"variables": [
{
"name": "CMAKE_C_COMPILER",
"value": "${env.BIN_ROOT}/gcc.exe",
"type": "STRING"
},
{
"name": "CMAKE_CXX_COMPILER",
"value": "${env.BIN_ROOT}/g++.exe",
"type": "STRING"
}
],
"intelliSenseMode": "linux-gcc-x64"
}
]
}
104 changes: 28 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,106 +1,58 @@
[![CMake MSVC Build and Test](https://github.com/RedSkittleFox/reflexpr/actions/workflows/cmake-msvc-build.yml/badge.svg)](https://github.com/RedSkittleFox/reflexpr/actions/workflows/cmake-msvc-build.yml)
[![CMake on multiple platforms](https://github.com/RedSkittleFox/reflexpr/actions/workflows/cmake-multi-platform.yml/badge.svg)](https://github.com/RedSkittleFox/reflexpr/actions/workflows/cmake-multi-platform.yml)

# reflexpr
is a c++20 compile and runtime aggregate reflections header only library. It allows you to iterate over aggregate type's member variables.

# Example Usage
```cpp
// demo.cpp
#include <iostream>
#include <string_view>
#include <algorithm>
#include <string>

#include <fox/reflexpr.hpp>

struct aggregate_type
{
int a;
float b;
std::string str;
};

REFLECT(
struct aggregate_type_reflected
struct my_aggregate
{
int a;
float b;
std::string str;
}
);

struct functor
{
template<class T>
void operator()() const
{
std::cout << "Type: " << typeid(T).name() << '\n';
}
};

struct functor_reflected
{
template<class T>
void operator()(const std::string& name) const
{
std::cout << "Name: " << name << " Type: " << typeid(T).name() << '\n';
}
std::string c;
int& d;
};

int main()
{
// DEMO: fox::reflexpr::for_each_member_variable
int d = 5;
my_aggregate obj
{
std::cout << "For each member variable:\n";
auto func = []<class T>(T & v)
{
std::cout << "Type: " << typeid(T).name() << " Value: " << v << '\n';
};
.a = 1 ,
.b = 3.5f,
.c = "Foxes are great!",
.d = d
};

aggregate_type at{ 1 , 3.5f, "Foxes are great!" };

static_assert(fox::reflexpr::aggregate<aggregate_type>, "sus");
fox::reflexpr::for_each_member_variable(at, func);
std::cout << '\n';
}

// DEMO: fox::reflexpr::for_each_member_type
{
std::cout << "For each member type:\n";
auto&& [v0, v1, v2, v3] = obj;

fox::reflexpr::for_each_member_type<aggregate_type, functor>(functor{});
std::cout << '\n';
}
// Get Nth member - fox::reflexpr::get<N>(aggregate)
std::cout << fox::reflexpr::get<0>(obj) << '\n'; // prints obj.a

// Iterate over members - fox::reflexpr::for_each(aggregate, func)
fox::reflexpr::for_each(obj, [](auto&& v) {std::cout << v << ' '; }), std::cout << '\n';

// DEMO: fox::reflexpr::for_each_reflected_member_variable
{
std::cout << "For each member variable reflected:\n";
auto func = []<class T>(T & v, const std::string& name)
{
std::cout << "Name: " << name << " Type: " << typeid(T).name() << " Value: " << v << '\n';
};
// Create a tuple-tie from members - fox::reflexpr::tie(aggregate)
auto tie = fox::reflexpr::tie(obj);
std::cout << (std::get<2>(tie) = 2) << '\n';

aggregate_type_reflected at{ 1 , 3.5f, "Foxes are great!" };
// Create a tuple from members - fox::reflexpr::make_tuple(aggregate)
auto tuple = fox::reflexpr::make_tuple(obj);
std::cout << (std::get<2>(tuple)) << '\n';

fox::reflexpr::for_each_reflected_member_variable(at, func);
std::cout << '\n';
}
// Tuple size - fox::reflexpr::tuple_size_v<aggregate_type>
static_assert(fox::reflexpr::tuple_size_v<my_aggregate> == static_cast<std::size_t>(4));

// DEMO: fox::reflexpr::for_each_reflected_member_type
{
std::cout << "For each member type reflected:\n";
// Tuple Nth type
static_assert(std::is_same_v<fox::reflexpr::tuple_element_t<3, my_aggregate>, int&>);

fox::reflexpr::for_each_reflected_member_type<aggregate_type_reflected, functor_reflected>(functor_reflected{});
std::cout << '\n';
}

return 1;
return 0;
}
```

# Planned Improvements
* Improving member variable names parsing.
* Write unit tests.

# Limitation
Right now it supports only up to 40 member variables and introduces small runtime overhead when registering member variable names.
Loading

0 comments on commit f7ce538

Please sign in to comment.