Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
# Changelog

#[Unreleased](https://github.com/bwhitchurch/CubeTimer/compare/0.1.0...HEAD)
#[Unreleased](https://github.com/bwhitchurch/CubeTimer/compare/0.1.1...HEAD)

## New Features

- feat: reflective enums through preprocessor shenanigans. [`7d34190`](https://github.com/bwhitchurch/CubeTimer/commit/7d34190747b18cabc922cf4bad7c1ce38c7e5b6a)

## Fixes

- Fix: dummy test in scramblerTest to ensure valid program [`b4b1ec5`](https://github.com/bwhitchurch/CubeTimer/commit/b4b1ec58fc2620da520f6bc95eb0f5b67333afbd)
- Fix: check for constexpr string support [`c1c9cda`](https://github.com/bwhitchurch/CubeTimer/commit/c1c9cdaa66722f1f56c57ed2d08b5dad9bc5418f)
- Fix: checking constexpr string support (again) [`2c1a04f`](https://github.com/bwhitchurch/CubeTimer/commit/2c1a04f1fab6c3f10914c418750da9827369b543)
- Fix: remove findpackage from subdirectory cmakelists [`e388169`](https://github.com/bwhitchurch/CubeTimer/commit/e388169d2ee39cc8a97ad0b35b9fd68975d36475)
- fix: constexpr string check changed to ifdef [`78bdc18`](https://github.com/bwhitchurch/CubeTimer/commit/78bdc184ab927b76b3fa0bf987f7267765917c52)

## Miscellaneous

- Change: cube header makes use of enum utils. [`9b0f1a6`](https://github.com/bwhitchurch/CubeTimer/commit/9b0f1a644d1720c5d9cd796d8867fd476183e103)
- Enum things [`8320a5e`](https://github.com/bwhitchurch/CubeTimer/commit/8320a5ee7d8ffa4e60959c8de8ed9dcadecc7703)
- Change: Finished reworking scrambler to use new enum and cubeMove classes [`e0edfb1`](https://github.com/bwhitchurch/CubeTimer/commit/e0edfb1b2f518fd5ef3a43b6c988eeb1a40dc062)
- enum.hpp is dead [`f268ef2`](https://github.com/bwhitchurch/CubeTimer/commit/f268ef27d5541603acb786a228f410548e4e52f0)
- Fix naming in ppUtils [`ccfba25`](https://github.com/bwhitchurch/CubeTimer/commit/ccfba2500685859fcfb39d08341ff165bf93febc)
#[0.1.1](https://github.com/bwhitchurch/CubeTimer/compare/0.1.0...0.1.1)

## New Features

- Add: build and test github action [`9abdf56`](https://github.com/bwhitchurch/CubeTimer/commit/9abdf5625baa201edb13cab1a4e2500f5ac396fe)

## Minor Changes

- dev: some basic ci [`8573379`](https://github.com/bwhitchurch/CubeTimer/commit/8573379353ac600f34d8fe38a4d46ab47479b7aa)
- dev: some basic ci [`adde650`](https://github.com/bwhitchurch/CubeTimer/commit/adde6502800a7ca5425df555e5ffed572a7dab2f)

## Miscellaneous

Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ dynamic_project_options(
--suppress=unmatchedSuppression
--suppress=passedByValue
--suppress=syntaxError
--suppress=duplicateExpression
--suppress=knownConditionTrueFalse
--suppress=preprocessorErrorDirective
--inconclusive)

if(ENABLE_INCLUDE_WHAT_YOU_USE)
Expand Down
10 changes: 6 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
add_library(scrambler scrambler.cpp)
add_subdirectory(ppUtils)
add_subdirectory(EnumUtils)
add_library(scrambler scrambler.cpp cube.cpp)
target_include_directories(scrambler PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(scrambler PROPERTIES PUBLIC_HEADER
"scrambler.hpp;enum.hpp")
"scrambler.hpp;cube.cpp")
target_link_libraries(
scrambler
PUBLIC project_options project_warnings
PRIVATE fmt::fmt spdlog::spdlog)
PRIVATE project_options project_warnings
PUBLIC fmt::fmt spdlog::spdlog EnumUtils)

target_include_directories(
scrambler PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include")
Expand Down
10 changes: 10 additions & 0 deletions src/EnumUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_library(EnumUtils enumUtils.cpp)
target_include_directories(EnumUtils PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(
EnumUtils
PUBLIC ppUtils
PRIVATE project_options project_warnings)

if(ENABLE_TESTING)
add_subdirectory(test)
endif()
118 changes: 118 additions & 0 deletions src/EnumUtils/enumUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#ifndef ENUM_UTILS_HPP
#define ENUM_UTILS_HPP
#include "ppUtils.hpp"

#include <algorithm>
#include <array>
#include <functional>
#include <string_view>
using namespace std::string_view_literals;
#if __cpp_lib_constexpr_string >= 201907L
#define CUBE_TIMER_CONSTEXPR constexpr
#else
#define CUBE_TIMER_CONSTEXPR
#endif

template < typename Type > struct IgnoreEquals {
Type value;

template < typename Any >
constexpr IgnoreEquals& operator=([[maybe_unused]] Any t_val) {
return *this;
}

constexpr explicit IgnoreEquals() : value() {}

constexpr explicit IgnoreEquals(Type t_value) : value(t_value) {}

// NOLINTNEXTLINE(hicpp-explicit-conversions)
constexpr operator Type() const { return value; }

constexpr IgnoreEquals operator*(const Type& t_value) {
return IgnoreEquals(t_value);
}
};

// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#define MAKE_ENUM(enum_name, enum_type, ...) \
enum enum_name : enum_type { __VA_ARGS__ };

#define NAME_ENTRY(arg) #arg##sv.substr(0, #arg##sv.find_first_of('=') - 1)

#define MAKE_NAMES(array_prefix, enum_name, ...) \
constexpr static std:: \
array< std::string_view, MM_COUNT_ARGS(__VA_ARGS__) > \
array_prefix##_names{{MM_TRANSFORM(NAME_ENTRY, __VA_ARGS__)}};

// NOLINTNEXTLINE(bugprone-macro-parentheses)
#define VALUE_ENTRY(enum_name, arg) IgnoreEquals< enum_name >{} * arg
#define MAKE_VALUES(array_prefix, enum_name, ...) \
constexpr static std::array< enum_name, MM_COUNT_ARGS(__VA_ARGS__) > \
array_prefix##_values{ \
{MM_TRANSFORM_1(VALUE_ENTRY, enum_name, __VA_ARGS__)}};

#define BETTER_ENUM(enum_name, type, ...) \
class enum_name { \
public: \
MAKE_ENUM(m_enumeration, type, __VA_ARGS__) \
private: \
MAKE_VALUES(m, m_enumeration, __VA_ARGS__) \
MAKE_NAMES(m, m_enumeration, __VA_ARGS__) \
constexpr static std::string_view m_name{#enum_name##sv}; \
m_enumeration m_value; \
\
public: \
using value_container = decltype(m_values); \
using value_iterator = typename value_container::iterator; \
using value_type = typename value_container::value_type; \
using name_container = decltype(m_names); \
using name_iterator = typename name_container::iterator; \
using name_type = typename name_container::value_type; \
using underlying = type; \
\
/* NOLINTNEXTLINE(hicpp-explicit-conversions)*/ \
constexpr enum_name(const m_enumeration& t_val) : m_value(t_val) {} \
constexpr operator underlying() const { return m_value; } \
\
friend constexpr enum_name operator+(const m_enumeration& t_val); \
\
constexpr static value_container& values() { return m_values; } \
constexpr static name_container& names() { return m_names; } \
constexpr static size_t size() { return MM_COUNT_ARGS(__VA_ARGS__); } \
constexpr static std::string_view name() { return m_name; } \
constexpr static const value_type& from_string(std::string_view t_name \
) { \
const auto* found_ptr = std::find_if( \
m_names.begin(), \
m_names.end(), \
[t_name](auto t_iter_name) { return t_iter_name == t_name; } \
); \
auto start_val = m_values.begin(); \
std::advance( \
start_val, std::distance(m_names.begin(), found_ptr) \
); \
return *start_val; \
} \
constexpr name_type to_string() { \
const auto* found_ptr = std::find_if( \
m_values.begin(), \
m_values.end(), \
[this](auto t_iter_val) { return t_iter_val == m_value; } \
); \
auto start_name = m_names.begin(); \
std::advance( \
start_name, std::distance(m_values.begin(), found_ptr) \
); \
return *start_name; \
} \
}; \
constexpr enum_name operator+(const enum_name::m_enumeration& t_val) { \
return enum_name(t_val); \
} \
template <> struct std::hash< enum_name > { \
size_t operator()(const enum_name& t_enum) const noexcept { \
return std::hash< type >{}(t_enum); \
} \
};
// NOLINTEND(cppcoreguidelines-macro-usage)
#endif
20 changes: 20 additions & 0 deletions src/EnumUtils/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_executable(enumUtilsTest enumUtilsTest.cpp)
target_link_libraries(enumUtilsTest PUBLIC Catch2::Catch2WithMain EnumUtils
fmt::fmt)
target_link_libraries(enumUtilsTest PRIVATE project_options project_warnings)
set_target_properties(enumUtilsTest PROPERTIES RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_TEST_OUTPUT_DIRECTORY}")

include(Catch)
catch_discover_tests(
enumUtilsTest
TEST_PREFIX
"libEnumUtils."
REPORTER
XML
OUTPUT_DIR
.
OUTPUT_PREFIX
"libEnumUtils."
OUTPUT_SUFFIX
.xml)
28 changes: 28 additions & 0 deletions src/EnumUtils/test/enumUtilsTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "enumUtils.hpp"

#include <fmt/format.h>
#include <fmt/ranges.h>

#include <catch2/catch_test_macros.hpp>

TEST_CASE("ENUM MACROS") {
MAKE_ENUM(Color, int, RED, GREEN, BLUE = 3, ORANGE)

MAKE_NAMES(Color, Color, RED, GREEN, BLUE = 3, ORANGE)
fmt::print("{}\n", Color_names);
CHECK(Color_names[0] == "RED");

MAKE_VALUES(Color, Color, RED, GREEN, BLUE = 3, ORANGE)
CHECK(Color_values[0] == RED);
}

BETTER_ENUM(Color, int, RED, GREEN, ORANGE = 7, YELLOW)

TEST_CASE("BETTER_ENUM") {
CHECK(Color::values()[0] == Color::RED);
fmt::print("{}\n", Color::name());
fmt::print("{}\n", Color::names());
fmt::print("{}\n", Color::values());
fmt::print("{}\n", Color::from_string("ORANGE"));
fmt::print("{}\n", (+Color::RED).to_string());
}
16 changes: 16 additions & 0 deletions src/cube.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "cube.hpp"

#include <unordered_map>

const std::unordered_map< CubeFace, CubeFace >& getAxisPairs() {

const static std::unordered_map< CubeFace, CubeFace > face_opposites{
{ CubeFace::UP, CubeFace::DOWN},
{ CubeFace::DOWN, CubeFace::UP},
{CubeFace::RIGHT, CubeFace::LEFT},
{ CubeFace::LEFT, CubeFace::RIGHT},
{CubeFace::FRONT, CubeFace::BACK},
{ CubeFace::BACK, CubeFace::FRONT},
};
return face_opposites;
}
Loading