-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address all comments from Johannes' review
- Loading branch information
Showing
5 changed files
with
82 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
// Copyright 2022, University of Freiburg, | ||
// Chair of Algorithms and Data Structures. | ||
// Author: | ||
// 2022 - Johannes Kalmbach <[email protected]> | ||
// Copyright 2022 - 2024, University of Freiburg | ||
// Chair of Algorithms and Data Structures | ||
// Author: Johannes Kalmbach <[email protected]> | ||
|
||
#ifndef QLEVER_TRANSPARENTFUNCTORS_H | ||
#define QLEVER_TRANSPARENTFUNCTORS_H | ||
#pragma once | ||
|
||
#include <util/Forward.h> | ||
#include <util/TypeTraits.h> | ||
|
||
#include <utility> | ||
|
||
/// Contains several function object types with templated operator() that wrap | ||
/// overloaded functions from the standard library. This enables passing them as | ||
/// function parameters. | ||
|
||
/// Note that in theory all of them could be implemented shorter as captureless | ||
/// lambda expressions. We have chosen not to do this because the STL also does | ||
/// not choose this approach (see e.g. `std::less`, `std::plus`, etc.) and | ||
/// because global inline lambdas in header files might in theory cause ODR (one | ||
/// definition rule) problems, especially when using different compilers. | ||
// Contains several function object types with templated operator() that wrap | ||
// overloaded functions from the standard library. This enables passing them as | ||
// function parameters. | ||
// | ||
// NOTE: in theory all of them could be implemented shorter as captureless | ||
// lambda expressions. We have chosen not to do this because the STL also does | ||
// not choose this approach (see e.g. `std::less`, `std::plus`, etc.) and | ||
// because global inline lambdas in header files might in theory cause ODR (one | ||
// definition rule) problems, especially when using different compilers. | ||
|
||
namespace ad_utility { | ||
|
||
|
@@ -79,37 +77,60 @@ struct ToBoolImpl { | |
} | ||
}; | ||
|
||
// Implementation of `staticCast` (see below). | ||
template <typename T> | ||
struct StaticCastImpl { | ||
constexpr decltype(auto) operator()(auto&& x) const { | ||
return static_cast<T>(AD_FWD(x)); | ||
} | ||
}; | ||
|
||
// Implementation of `dereference` (see below). | ||
struct DereferenceImpl { | ||
constexpr decltype(auto) operator()(auto&& x) const { return *AD_FWD(x); } | ||
}; | ||
|
||
} // namespace detail | ||
|
||
/// Return the first element via perfect forwarding of any type for which | ||
/// `std::get<0>(x)` is valid. This holds e.g. for `std::pair`, `std::tuple`, | ||
/// and `std::array`. | ||
// Return the first element via perfect forwarding of any type for which | ||
// `std::get<0>(x)` is valid. This holds e.g. for `std::pair`, `std::tuple`, | ||
// and `std::array`. | ||
static constexpr detail::FirstImpl first; | ||
|
||
/// Return the second element via perfect forwarding of any type for which | ||
/// `std::get<1>(x)` is valid. This holds e.g. for `std::pair`, `std::tuple`, | ||
/// and `std::array`. | ||
// Return the second element via perfect forwarding of any type for which | ||
// `std::get<1>(x)` is valid. This holds e.g. for `std::pair`, `std::tuple`, | ||
// and `std::array`. | ||
static constexpr detail::SecondImpl second; | ||
|
||
/// Transparent functor for `std::holds_alternative` | ||
// Transparent functor for `std::holds_alternative` | ||
template <typename T> | ||
static constexpr detail::HoldsAlternativeImpl<T> holdsAlternative; | ||
|
||
/// Transparent functor for `std::get`. Currently only works for `std::variant` | ||
/// and not for `std::array` or `std::tuple`. | ||
// Transparent functor for `std::get`. Currently only works for `std::variant` | ||
// and not for `std::array` or `std::tuple`. | ||
template <typename T> | ||
static constexpr detail::GetImpl<T> get; | ||
|
||
/// Transparent functor for `std::get_if`. As an extension to `std::get_if`, | ||
/// `ad_utility::getIf` may also be called with a `variant` object or reference, | ||
/// not only with a pointer. | ||
// Transparent functor for `std::get_if`. As an extension to `std::get_if`, | ||
// `ad_utility::getIf` may also be called with a `variant` object or reference, | ||
// not only with a pointer. | ||
template <typename T> | ||
static constexpr detail::GetIfImpl<T> getIf; | ||
|
||
// Transparent functor that converts any type to `bool` via | ||
// `static_cast<bool>`. | ||
static constexpr detail::ToBoolImpl toBool; | ||
|
||
/// A functor that takes an arbitrary number of arguments by reference and does | ||
/// nothing. | ||
// Transparent functor that casts any type to `T` via `static_cast<T>`. | ||
template <typename T> | ||
static constexpr detail::StaticCastImpl<T> staticCast{}; | ||
|
||
// Transparent functor that dereferences a pointer or smart pointer. | ||
static constexpr detail::DereferenceImpl dereference; | ||
|
||
// Transparent functor that takes an arbitrary number of arguments by reference | ||
// and does nothing. We also use the type `Noop`, hence it is defined here and | ||
// not in the `detail` namespace above. | ||
struct Noop { | ||
void operator()(const auto&...) const { | ||
// This function deliberately does nothing (static analysis expects a | ||
|
@@ -118,16 +139,4 @@ struct Noop { | |
}; | ||
[[maybe_unused]] static constexpr Noop noop{}; | ||
|
||
template <typename T> | ||
struct StaticCast { | ||
constexpr decltype(auto) operator()(auto&& x) const { | ||
return static_cast<T>(AD_FWD(x)); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
static constexpr StaticCast<T> staticCast{}; | ||
|
||
} // namespace ad_utility | ||
|
||
#endif // QLEVER_TRANSPARENTFUNCTORS_H |