diff --git a/CMakeLists.txt b/CMakeLists.txt index e9cf0c3..6c5ecaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,28 @@ endif() +option(TSL_ORDERED_MAP_BUILD_MODULE "Build the C++20 module for tsl::ordered_map" OFF) + +if(TSL_ORDERED_MAP_BUILD_MODULE) + if(CMAKE_CXX_STANDARD LESS 20) + message(FATAL_ERROR "TSL_ORDERED_MAP_BUILD_MODULE requires C++20 or later (CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})") + endif() + + add_library(ordered_map_module) + add_library(tsl::ordered_map_module ALIAS ordered_map_module) + + target_sources(ordered_map_module + PUBLIC FILE_SET CXX_MODULES FILES + "${CMAKE_CURRENT_SOURCE_DIR}/src/tsl.ordered_map.cppm") + + target_link_libraries(ordered_map_module PUBLIC tsl::ordered_map) + set_target_properties(ordered_map_module PROPERTIES CXX_STANDARD 20) + + install(TARGETS ordered_map_module + EXPORT tsl-ordered-mapTargets + FILE_SET CXX_MODULES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +endif() + include(CMakePackageConfigHelpers) ## Install include directory and potential natvis file diff --git a/README.md b/README.md index 7d1eb34..8ae87a7 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Two classes are provided: `tsl::ordered_map` and `tsl::ordered_set`. - Support for efficient serialization and deserialization (see [example](#serialization) and the `serialize/deserialize` methods in the [API](https://tessil.github.io/ordered-map/classtsl_1_1ordered__map.html) for details). - The library can be used with exceptions disabled (through `-fno-exceptions` option on Clang and GCC, without an `/EH` option on MSVC or simply by defining `TSL_NO_EXCEPTIONS`). `std::terminate` is used in replacement of the `throw` instruction when exceptions are disabled. - API closely similar to `std::unordered_map` and `std::unordered_set`. +- Support for C++ modules (requires C++20 or later) ### Differences compared to `std::unordered_map` `tsl::ordered_map` tries to have an interface similar to `std::unordered_map`, but some differences exist. @@ -82,6 +83,8 @@ cmake --build . ./tsl_ordered_map_tests ``` +To enable C++ modules, the version must be at least C++20 on any compiler supporting C++ modules (minimal versions usually Clang 18, GCC 15, and MSVC 17). When configuring CMake, pass `-DTSL_ORDERED_MAP_BUILD_MODULE=ON` to enable the `TSL_ORDERED_MAP_BUILD_MODULE` option. This creates a module `tsl.ordered_map`, which exports `tsl::ordered_map` and `tsl::ordered_set`. + ### Usage The API can be found [here](https://tessil.github.io/ordered-map/). diff --git a/include/tsl/ordered_hash.h b/include/tsl/ordered_hash.h index 614ec94..5fb7e6d 100644 --- a/include/tsl/ordered_hash.h +++ b/include/tsl/ordered_hash.h @@ -117,7 +117,7 @@ const T& clamp(const T& v, const T& lo, const T& hi) { } template -static T numeric_cast(U value, +T numeric_cast(U value, const char* error_message = "numeric_cast() failed.") { T ret = static_cast(value); if (static_cast(ret) != value) { @@ -145,7 +145,7 @@ static_assert(std::numeric_limits::max() >= "slz_size_type must be >= std::size_t"); template -static T deserialize_value(Deserializer& deserializer) { +T deserialize_value(Deserializer& deserializer) { // MSVC < 2017 is not conformant, circumvent the problem by removing the // template keyword #if defined(_MSC_VER) && _MSC_VER < 1910 diff --git a/src/tsl.ordered_map.cppm b/src/tsl.ordered_map.cppm new file mode 100644 index 0000000..a3eb3e7 --- /dev/null +++ b/src/tsl.ordered_map.cppm @@ -0,0 +1,34 @@ +/** + * MIT License + * + * Copyright (c) 2017 Thibaut Goetghebuer-Planchon + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +module; + +#include "tsl/ordered_map.h" +#include "tsl/ordered_set.h" + +export module tsl.ordered_map; + +export namespace tsl { + using tsl::ordered_map; + using tsl::ordered_set; +}