Skip to content

Commit

Permalink
merge develop
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h authored Aug 15, 2024
2 parents 870e2e1 + ae813d0 commit bcdbbd3
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code_testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
uses: dice-group/cpp-conan-release-reusable-workflow/.github/actions/add_conan_provider@main

- name: Configure CMake
run: cmake -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined" -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=On -DBUILD_EXAMPLES=On -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -G Ninja -B build .
run: cmake -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined" -DCMAKE_BUILD_TYPE=Debug -DWITH_SVECTOR=ON -DWITH_BOOST=ON -DBUILD_TESTING=On -DBUILD_EXAMPLES=On -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -G Ninja -B build .
env:
CC: ${{ steps.install_cc.outputs.cc }}
CXX: ${{ steps.install_cc.outputs.cxx }}
Expand Down
36 changes: 30 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.24)

project(
dice-template-library
VERSION 1.7.0
VERSION 1.8.0
DESCRIPTION
"This template library is a collection of template-oriented code that we, the Data Science Group at UPB, found pretty handy. It contains: `switch_cases` (Use runtime values in compile-time context), `integral_template_tuple` (Create a tuple-like structure that instantiates a template for a range of values), `integral_template_variant` (A wrapper type for `std::variant` guarantees to only contain variants of the form `T<IX>` and `for_{types,values,range}` (Compile time for loops for types, values or ranges))."
HOMEPAGE_URL "https://dice-research.org/")
Expand All @@ -12,13 +12,21 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.hpp.in ${CMAKE_CURRENT_

option(BUILD_TESTING "build tests" OFF)
option(BUILD_EXAMPLES "build examples" OFF)
option(WITH_SVECTOR "use ankerl/svector to provide flex_array with flex_array_implementation::sbo_vector" OFF)
option(WITH_BOOST "use boost to provide offset_ptr_stl_allocator in polymorphic_allocator.hpp" OFF)

if (PROJECT_IS_TOP_LEVEL)
set(CONAN_INSTALL_ARGS "${CONAN_INSTALL_ARGS};-o=boost/*:header_only=True")

if (BUILD_TESTING)
if (BUILD_TESTING OR BUILD_EXAMPLES)
set(CONAN_INSTALL_ARGS "${CONAN_INSTALL_ARGS};-o=&:with_test_deps=True")
endif ()

if (WITH_SVECTOR)
set(CONAN_INSTALL_ARGS "${CONAN_INSTALL_ARGS};-o=&:with_svector=True")
endif ()

if (WITH_BOOST)
set(CONAN_INSTALL_ARGS "${CONAN_INSTALL_ARGS};-o=&:with_boost=True;-o=boost/*:header_only=True")
endif ()
endif ()

# conan requires cmake build type to be specified and it is generally a good idea
Expand All @@ -35,6 +43,22 @@ target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

if (WITH_SVECTOR)
find_package(svector REQUIRED)

target_link_libraries(${PROJECT_NAME} INTERFACE
svector::svector
)
endif ()

if (WITH_BOOST)
find_package(Boost REQUIRED COMPONENTS)

target_link_libraries(${PROJECT_NAME} INTERFACE
Boost::headers
)
endif ()

set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 20
CXX_EXTENSIONS OFF
Expand All @@ -44,12 +68,12 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
include(cmake/install_interface_library.cmake)
install_interface_library(${PROJECT_NAME} ${PROJECT_NAME} ${PROJECT_NAME} "include")

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
include(CTest)
enable_testing()
add_subdirectory(tests)
endif()

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_EXAMPLES)
if(PROJECT_IS_TOP_LEVEL AND BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It contains:
- `polymorphic_allocator`: Like `std::pmr::polymorphic_allocator` but with static dispatch
- `DICE_DEFER`/`DICE_DEFER_TO_SUCCES`/`DICE_DEFER_TO_FAIL`: On-the-fly RAII for types that do not support it natively (similar to go's `defer` keyword)
- `overloaded`: Composition for `std::variant` visitor lambdas
- `flex_array`: A combination of `std::array` and `std::span`
- `flex_array`: A combination of `std::array`, `std::span` and a `vector` with small buffer optimization
- `tuple_algorithms`: Some algorithms for iterating tuples
- `generator`: The reference implementation of `std::generator` from [P2502R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2502r2.pdf)
- `channel`: A single producer, single consumer queue
Expand Down Expand Up @@ -73,8 +73,8 @@ Usage examples can be found [here](examples/examples_defer.cpp).
Some algorithms for iterating tuples, for example `tuple_fold` a fold/reduce implementation for tuples.

### `flex_array`
A combination of `std::array` and `std::span` where the size is either statically known or a runtime variable
depending on the `extent` template parameter
A combination of `std::array`, `std::span` and a `vector` with small buffer optimization where the size is either
statically known or a runtime variable depending on the `extent`/`max_extent` template parameters

### `generator`
The reference implementation of `std::generator` from [P2502R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2502r2.pdf).
Expand Down Expand Up @@ -106,7 +106,7 @@ add
FetchContent_Declare(
dice-template-library
GIT_REPOSITORY "https://github.com/dice-group/dice-template-library.git"
GIT_TAG v1.7.0
GIT_TAG v1.8.0
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(dice-template-library)
Expand All @@ -125,7 +125,7 @@ target_link_libraries(your_target
### conan

You can use it with [conan](https://conan.io/).
To do so, you need to add `dice-template-library/1.7.0` to the `[requires]` section of your conan file.
To do so, you need to add `dice-template-library/1.8.0` to the `[requires]` section of your conan file.

## Build and Run Tests and Examples

Expand Down
20 changes: 18 additions & 2 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ class DiceTemplateLibrary(ConanFile):
no_copy_source = True
options = {
"with_test_deps": [True, False],
"with_svector": [True, False],
"with_boost": [True, False],
}
default_options = {
"with_test_deps": False,
"with_svector": False,
"with_boost": False,
}

def requirements(self):
if self.options.with_svector:
self.requires("svector/1.0.3", transitive_headers=True)

if self.options.with_boost:
self.requires("boost/1.84.0", transitive_headers=True)

if self.options.with_test_deps:
self.test_requires("boost/1.83.0")
self.test_requires("doctest/2.4.11")

def layout(self):
Expand All @@ -36,7 +45,7 @@ def layout(self):
def build(self):
if not self.conf.get("tools.build:skip_test", default=False):
cmake = CMake(self)
cmake.configure()
cmake.configure(variables={"WITH_SVECTOR": self.options.with_svector, "WITH_BOOST": self.options.with_boost})
cmake.build()

def package_id(self):
Expand Down Expand Up @@ -67,6 +76,13 @@ def package(self):
def package_info(self):
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []
self.cpp_info.requires = []

if self.options.with_svector:
self.cpp_info.requires += ["svector::svector"]

if self.options.with_boost:
self.cpp_info.requires += ["boost::headers"]

self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_target_name", f"{self.name}::{self.name}")
Expand Down
13 changes: 13 additions & 0 deletions examples/example_flex_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ struct square {
}
};

#if __has_include(<ankerl/svector.h>)
struct arbitrary_high_dimensional_thing {
flex_array<size_t, 2, dynamic_extent> extents;
};
#endif // __has_include

struct shape {
std::variant<point, line, square> shape_;

Expand Down Expand Up @@ -67,4 +73,11 @@ int main() {
print_extents(point1);
print_extents(line1);
print_extents(square1);

#if __has_include(<ankerl/svector.h>)
arbitrary_high_dimensional_thing thing{.extents = {1, 2, 3, 4, 5, 6, 7, 8}};
for (auto const ext : thing.extents) {
std::cout << ext << " ";
}
#endif // __has_include
}
15 changes: 12 additions & 3 deletions include/dice/template-library/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace dice::template_library {
closed_.test_and_set(std::memory_order_release);
}
queue_not_empty_.notify_one(); // notify pop() so that it does not get stuck
queue_not_full_.notify_all(); // notify emplace()
}

/**
Expand All @@ -90,7 +91,14 @@ namespace dice::template_library {

{
std::unique_lock lock{queue_mutex_};
queue_not_full_.wait(lock, [this]() noexcept { return queue_.size() < max_cap_; });
queue_not_full_.wait(lock, [this]() noexcept { return queue_.size() < max_cap_ || closed_.test(std::memory_order_relaxed); });

if (closed_.test(std::memory_order_relaxed)) [[unlikely]] {
// relaxed is enough because we hold the lock
// wait was exited because closed_ was true (channel closed)
return false;
}

queue_.emplace_back(std::forward<Args>(args)...);
}

Expand All @@ -112,7 +120,8 @@ namespace dice::template_library {

{
std::unique_lock lock{queue_mutex_};
if (queue_.size() >= max_cap_) {
if (queue_.size() >= max_cap_ || closed_.test(std::memory_order_relaxed)) {
// relaxed is enough because we hold the lock
return false;
}

Expand Down Expand Up @@ -171,7 +180,7 @@ namespace dice::template_library {
*/
[[nodiscard]] std::optional<value_type> pop() noexcept(std::is_nothrow_move_constructible_v<value_type>) {
std::unique_lock lock{queue_mutex_};
queue_not_empty_.wait(lock, [this]() noexcept { return !queue_.empty() || closed_.test(std::memory_order_acquire); });
queue_not_empty_.wait(lock, [this]() noexcept { return !queue_.empty() || closed_.test(std::memory_order_relaxed); });

if (queue_.empty()) [[unlikely]] {
// implies closed_ == true
Expand Down
Loading

0 comments on commit bcdbbd3

Please sign in to comment.