Skip to content

Commit

Permalink
Another approach for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Jul 28, 2024
1 parent 79aae56 commit fb0ae4a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions include/semver/semver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SOFTWARE.
#ifndef Z4KN4FEIN_SEMVER_H
#define Z4KN4FEIN_SEMVER_H

#ifndef SEMVER_MODULE
#include <ostream>
#include <string>
#include <regex>
Expand All @@ -38,6 +39,13 @@ SOFTWARE.
#include <string_view>
#endif
#endif
#endif

#ifdef SEMVER_MODULE
#define SEMVER_EXPORT export
#else
#define SEMVER_EXPORT
#endif

namespace semver
{
Expand All @@ -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) { }
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
48 changes: 36 additions & 12 deletions module/semver.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,42 @@ SOFTWARE.

module;

#include <semver/semver.hpp>
#include <ostream>
#include <string>
#include <regex>
#include <utility>
#include <vector>

// conditionally include <format> and its dependency <string_view> for C++20
#ifdef __cpp_lib_format
#if __cpp_lib_format >= 201907L
#include <format>
#include <string_view>
#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;
}
#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 <semver/semver.hpp>

#ifdef __clang__
#pragma clang diagnostic pop
#endif

#ifdef _MSC_VER
#pragma warning(pop)
#endif

0 comments on commit fb0ae4a

Please sign in to comment.