Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constexpr constants instead of defines. #4

Merged
merged 4 commits into from
Mar 6, 2024
Merged
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
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);
}
Loading