From 799b3ce5173c7731d30faf167f3dbf6c6fcab6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klaim=20=28Jo=C3=ABl=20Lamotte=29?= <142265+Klaim@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:11:45 +0100 Subject: [PATCH] Use constexpr constants instead of defines. (#4) Use constexpr constants instead of defines. This is both to avoid using the preprocessor in C++ in general, and to prepare for future usage of the library as a C++ module. --- CMakeLists.txt | 4 ++-- include/sparrow/sparrow_version.hpp | 9 ++++++--- test/main.cpp | 13 ++++++++++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 051d77a8..a502ff5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,9 +28,9 @@ set(SPARROW_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) # =========== file(STRINGS "${SPARROW_INCLUDE_DIR}/sparrow/sparrow_version.hpp" sparrow_version_defines - REGEX "#define SPARROW_VERSION_(MAJOR|MINOR|PATCH)") + REGEX "constexpr int SPARROW_VERSION_(MAJOR|MINOR|PATCH)") foreach(ver ${sparrow_version_defines}) - if(ver MATCHES "#define SPARROW_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") + if(ver MATCHES "constexpr int SPARROW_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") set(SPARROW_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "") endif() endforeach() diff --git a/include/sparrow/sparrow_version.hpp b/include/sparrow/sparrow_version.hpp index 77960e62..b69da666 100644 --- a/include/sparrow/sparrow_version.hpp +++ b/include/sparrow/sparrow_version.hpp @@ -14,7 +14,10 @@ #pragma once -#define SPARROW_VERSION_MAJOR 0 -#define SPARROW_VERSION_MINOR 0 -#define SPARROW_VERSION_PATCH 1 +namespace sparrow +{ + constexpr int SPARROW_VERSION_MAJOR = 0; + constexpr int SPARROW_VERSION_MINOR = 0; + constexpr int SPARROW_VERSION_PATCH = 1; +} diff --git a/test/main.cpp b/test/main.cpp index a89b6f8a..2d6e2e75 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -15,4 +15,15 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest/doctest.h" -#include +#include + +#include + +TEST_CASE("version is readable") +{ + // TODO: once available on OSX, use `` facility instead. + // We only try to make sure the version valeus are printable, whatever their type. + // AKA this is not written to be fancy but to force conversion to string. + using namespace sparrow; + [[maybe_unused]] const std::string printable_version = std::string("sparrow version : ") + std::to_string(SPARROW_VERSION_MAJOR) + "." + std::to_string(SPARROW_VERSION_MINOR) + "." + std::to_string(SPARROW_VERSION_PATCH); +}