From fb0ae4a2b8cd9502fa58359ddd3bc0d42465c64f Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Sun, 28 Jul 2024 14:47:37 +0200 Subject: [PATCH] Another approach for modules --- CMakeLists.txt | 2 +- include/semver/semver.hpp | 20 +++++++++++----- module/semver.cppm | 48 +++++++++++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51d7914..6ed93e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ endif() option(SEMVER_BUILD_TESTS "Enable builds of tests" ${TOP_PROJECT}) option(SEMVER_INSTALL "Enable install target" ${TOP_PROJECT}) -option(SEMVER_BUILD_MODULE "Build as C++20 module" OFF) +option(SEMVER_BUILD_MODULE "Build as C++20 module" ON) if (SEMVER_BUILD_MODULE) cmake_minimum_required(VERSION 3.28) diff --git a/include/semver/semver.hpp b/include/semver/semver.hpp index 961d1f9..196e9a1 100644 --- a/include/semver/semver.hpp +++ b/include/semver/semver.hpp @@ -25,6 +25,7 @@ SOFTWARE. #ifndef Z4KN4FEIN_SEMVER_H #define Z4KN4FEIN_SEMVER_H +#ifndef SEMVER_MODULE #include #include #include @@ -38,6 +39,13 @@ SOFTWARE. #include #endif #endif +#endif + +#ifdef SEMVER_MODULE +#define SEMVER_EXPORT export +#else +#define SEMVER_EXPORT +#endif namespace semver { @@ -51,7 +59,7 @@ namespace semver "?(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))" "?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"; - struct semver_exception : public std::runtime_error { + SEMVER_EXPORT struct semver_exception : public std::runtime_error { explicit semver_exception(const std::string& message) : std::runtime_error(message) { } }; @@ -211,9 +219,9 @@ namespace semver } }; - enum inc { major, minor, patch, prerelease }; + SEMVER_EXPORT enum inc { major, minor, patch, prerelease }; - class version { + SEMVER_EXPORT class version { private: uint64_t m_major; uint64_t m_minor; @@ -374,18 +382,18 @@ namespace semver } }; - inline std::ostream & operator<<(std::ostream& str, const version& version) { + SEMVER_EXPORT inline std::ostream & operator<<(std::ostream& str, const version& version) { for (const auto s : version.str()) str.put(s); return str; } namespace literals { - inline version operator""_v(const char* text, std::size_t length) { + SEMVER_EXPORT inline version operator""_v(const char* text, std::size_t length) { return version::parse(std::string(text, length)); } - inline version operator""_lv(const char* text, std::size_t length) { + SEMVER_EXPORT inline version operator""_lv(const char* text, std::size_t length) { return version::parse(std::string(text, length), false); } } diff --git a/module/semver.cppm b/module/semver.cppm index 11c728a..8c21caa 100644 --- a/module/semver.cppm +++ b/module/semver.cppm @@ -24,18 +24,42 @@ SOFTWARE. module; -#include +#include +#include +#include +#include +#include + +// conditionally include and its dependency for C++20 +#ifdef __cpp_lib_format +#if __cpp_lib_format >= 201907L +#include +#include +#endif +#endif export module semver; -export namespace semver { - using semver::version; - using semver::inc; - using semver::semver_exception; - using semver::operator<<; -} - -export namespace semver::literals { - using semver::literals::operator""_v; - using semver::literals::operator""_lv; -} \ No newline at end of file +#define SEMVER_MODULE + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable: 5244) +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#if __has_warning("-Winclude-angled-in-module-purview") +#pragma clang diagnostic ignored "-Winclude-angled-in-module-purview" +#endif +#endif + +#include + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#ifdef _MSC_VER +#pragma warning(pop) +#endif \ No newline at end of file