https://github.com/patrickroberts/pr
An assortment of standalone C++ utilities
https://github.com/patrickroberts/pr
Last synced: 4 months ago
JSON representation
An assortment of standalone C++ utilities
- Host: GitHub
- URL: https://github.com/patrickroberts/pr
- Owner: patrickroberts
- License: mit
- Created: 2025-01-12T02:29:58.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-11T05:17:48.000Z (about 1 year ago)
- Last Synced: 2025-04-11T06:25:19.172Z (about 1 year ago)
- Language: C++
- Homepage:
- Size: 65.4 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `pr`
An assortment of standalone C++ utilities
## [`include/pr/context.hpp`](include/pr/context.hpp)
Synopsis
```cpp
namespace pr {
template
concept /*storable*/ = std::same_as>;
template *storable*/ T>
inline thread_local constinit T * /*context*/ = nullptr;
template
concept /*makeable*/ =
/*storable*/> and not std::is_rvalue_reference_v;
template
class /*provider*/ {
using value_type = std::remove_reference_t;
// `mutable` prevents UB when `make_context` initializes a `const auto`
[[no_unique_address]] mutable T inner_; // exposition-only
value_type *outer_; // exposition-only
public:
template
requires std::constructible_from
explicit /*provider*/(ArgsT &&...args) noexcept(
std::is_nothrow_constructible_v);
/*provider*/(const /*provider*/ &) = delete;
/*provider*/(/*provider*/ &&) = delete;
~/*provider*/() noexcept;
};
template *makeable*/ T, class... ArgsT>
requires std::constructible_from
[[nodiscard]] auto make_context(ArgsT &&...args) noexcept(
std::is_nothrow_constructible_v) -> /*provider*/;
template
concept /*gettable*/ = /*storable*/>;
template *gettable*/ T>
[[nodiscard]] auto get_context() noexcept -> T *;
} // namespace pr
```
---
pr::make_context
```cpp
template *makeable*/ T, class... ArgsT>
requires std::constructible_from
[[nodiscard]] auto make_context(ArgsT &&...args) noexcept(
std::is_nothrow_constructible_v) -> /*provider*/;
```
Constructs and returns `/*provider*/`, whose constructor initializes its members as if by `inner_(std::forward(args)...), outer_(std::exchange(/*context*/, std::addressof(inner_)))`. Its destructor restores `/*context*/` to the value of `outer_`. `T` must be a cv-unqualified non-reference or lvalue-reference type, or the instantiation is ill-formed, which can result in substitution failure when the call appears in the immediate context of a template instantiation.
---
pr::get_context
```cpp
template *gettable*/ T>
[[nodiscard]] auto get_context() noexcept -> T *;
```
Returns `/*context*/`, whose value has been initialized by thread-local calls to `pr::make_context(...)` or `pr::make_context(...)`, or `nullptr` otherwise. `T` must be an optionally const-qualified non-reference type, or the instantiation is ill-formed, which can result in substitution failure when the call appears in the immediate context of a template instantiation.
## [`include/pr/shared_view.hpp`](include/pr/shared_view.hpp)
Synopsis
```cpp
namespace pr {
namespace ranges {
template
concept copyable_view = /* see description */;
template
concept shared_range = /* see description */;
template
requires std::movable
class shared_view;
namespace views {
inline constexpr /* unspecified */ shared = /* unspecified */;
} // namespace views
} // namespace ranges
namespace views = ranges::views;
} // namespace pr
template
inline constexpr bool
std::ranges::enable_borrowed_range> =
std::ranges::enable_borrowed_range;
```
---
pr::ranges::copyable_view
#### Concept
```cpp
namespace pr::ranges {
template
concept copyable_view = std::ranges::view and std::copyable;
}
```
The `pr::ranges::copyable_view` concept is a refinement of `std::ranges::view` for which `std::copyable` is satisfied.
---
pr::ranges::shared_range
#### Concept
```cpp
namespace pr::ranges {
template
concept shared_range =
std::ranges::viewable_range and
std::copyable>;
}
```
The `pr::ranges::shared_range` concept is a refinement of `std::ranges::viewable_range` for which `std::copyable` is satisfied by `std::views::all_t`.
---
pr::ranges::views::shared
#### Call signature
```cpp
template
requires shared_range or std::movable
[[nodiscard]] constexpr auto shared(R &&range) -> copyable_view auto;
```
Given an expression `e` of type `T`, the expression `pr::views::shared(e)` is expression-equivalent to:
- `std::views::all(e)`, if it is a well-formed expression and `std::views::all_t` models `std::copyable`;
- `pr::ranges::shared_view{e}` otherwise.
---
pr::ranges::shared_view
```cpp
template
requires std::movable
class shared_view
: public std::ranges::view_interface>
```
A view that has shared ownership of a range. It wraps a shared pointer to that range.
Data members
| Member object | Definition |
| ---------------------------------------- | -------------------------------------------------------------------------- |
| `std::shared_ptr range_ptr` (private) | A shared pointer to the underlying range. (exposition-only member object*) |
Member functions
#### `pr::ranges::shared_view::shared_view`
| | |
| ------------------------------------------------------- | -------- |
| `shared_view() requires std::default_initializable;` | (1) |
| `explicit shared_view(R &&base);` | (2) |
Constructs a `shared_view`.
1) Default constructor. Initializes `range_ptr` as if by `range_ptr(std::make_shared())`.
2) Initializes the underlying `range_ptr` with `std::make_shared(std::move(base))`.
---
#### `pr::ranges::shared_view::base`
| |
| ------------------------------------------------- |
| `[[nodiscard]] auto base() const noexcept -> R &` |
Returns `*range_ptr`.
---
#### `pr::ranges::shared_view::begin`
| |
| ----------------------------------------------------------------- |
| `[[nodiscard]] auto begin() const -> std::ranges::iterator_t;` |
Returns `std::ranges::begin(*range_ptr)`.
---
#### `pr::ranges::shared_view::end`
| |
| --------------------------------------------------------------- |
| `[[nodiscard]] auto end() const -> std::ranges::iterator_t;` |
Returns `std::ranges::end(*range_ptr)`.
Helper templates
```cpp
template
inline constexpr bool
std::ranges::enable_borrowed_range> =
std::ranges::enable_borrowed_range;
```
This specialization of `std::ranges::enable_borrowed_range` makes `shared_view` satisfy `borrowed_range` when the underlying range satisfies it.