Skip to content

Commit

Permalink
Use constexpr constants instead of defines. (#4)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Klaim authored Mar 6, 2024
1 parent 3b8c296 commit 799b3ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions include/sparrow/sparrow_version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

13 changes: 12 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"

#include <format>
#include <string>

#include <sparrow/sparrow_version.hpp>

TEST_CASE("version is readable")
{
// TODO: once available on OSX, use `<format>` 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);
}

0 comments on commit 799b3ce

Please sign in to comment.