WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors
2// SPDX-License-Identifier: Zlib
3
4#ifndef WPI_MEMORY_DETAIL_UTILITY_HPP
5#define WPI_MEMORY_DETAIL_UTILITY_HPP
6
7// implementation of some functions from <utility> to prevent dependencies on it
8
9#include <type_traits>
10
11#include "../config.hpp"
12
13#if WPI_HOSTED_IMPLEMENTATION
14#include <utility>
15#endif
16
17namespace wpi
18{
19 namespace memory
20 {
21 namespace detail
22 {
23 // move - taken from http://stackoverflow.com/a/7518365
24 template <typename T>
25 typename std::remove_reference<T>::type&& move(T&& arg) noexcept
26 {
27 return static_cast<typename std::remove_reference<T>::type&&>(arg);
28 }
29 // forward - taken from http://stackoverflow.com/a/27501759
30 template <class T>
31 T&& forward(typename std::remove_reference<T>::type& t) noexcept
32 {
33 return static_cast<T&&>(t);
34 }
35 template <class T>
36 T&& forward(typename std::remove_reference<T>::type&& t) noexcept
37 {
38 static_assert(!std::is_lvalue_reference<T>::value,
39 "Can not forward an rvalue as an lvalue.");
40 return static_cast<T&&>(t);
41 }
42
43 namespace swap_
44 {
45#if WPI_HOSTED_IMPLEMENTATION
46 using std::swap;
47#else
48 template <typename T>
49 void swap(T& a, T& b)
50 {
51 T tmp = move(a);
52 a = move(b);
53 b = move(tmp);
54 }
55#endif
56 } // namespace swap_
57
58 // ADL aware swap
59 template <typename T>
60 void adl_swap(T& a, T& b) noexcept
61 {
62 using swap_::swap;
63 swap(a, b);
64 }
65
66// fancier syntax for enable_if
67// used as (template) parameter
68// also useful for doxygen
69// define PREDEFINED: WPI_REQUIRES(x):=
70#define WPI_REQUIRES(Expr) typename std::enable_if<(Expr), int>::type = 0
71
72// same as above, but as return type
73// also useful for doxygen:
74// defined PREDEFINED: WPI_REQUIRES_RET(x,r):=r
75#define WPI_REQUIRES_RET(Expr, ...) typename std::enable_if<(Expr), __VA_ARGS__>::type
76
77// fancier syntax for enable_if on non-templated member function
78#define WPI_ENABLE_IF(Expr) \
79 template <typename Dummy = std::true_type, WPI_REQUIRES(Dummy::value && (Expr))>
80
81// fancier syntax for general expression SFINAE
82// used as (template) parameter
83// also useful for doxygen:
84// define PREDEFINED: WPI_SFINAE(x):=
85#define WPI_SFINAE(Expr) decltype((Expr), int()) = 0
86
87// avoids code repetition for one-line forwarding functions
88#define WPI_AUTO_RETURN(Expr) \
89 decltype(Expr) \
90 { \
91 return Expr; \
92 }
93
94// same as above, but requires certain type
95#define WPI_AUTO_RETURN_TYPE(Expr, T) \
96 decltype(Expr) \
97 { \
98 static_assert(std::is_same<decltype(Expr), T>::value, \
99 #Expr " does not have the return type " #T); \
100 return Expr; \
101 }
102
103 // whether or not a type is an instantiation of a template
104 template <template <typename...> class Template, typename T>
105 struct is_instantiation_of : std::false_type
106 {
107 };
108
109 template <template <typename...> class Template, typename... Args>
110 struct is_instantiation_of<Template, Template<Args...>> : std::true_type
111 {
112 };
113 } // namespace detail
114 } // namespace memory
115} // namespace wpi
116
117#endif //WPI_MEMORY_DETAIL_UTILITY_HPP
Configuration macros.
detail namespace with internal helper functions
Definition input_adapters.h:32
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.h:5258
void swap(T &a, T &b)
Definition utility.hpp:49
void adl_swap(T &a, T &b) noexcept
Definition utility.hpp:60
T && forward(typename std::remove_reference< T >::type &t) noexcept
Definition utility.hpp:31
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
void swap(free_memory_list &a, free_memory_list &b) noexcept
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Returns a named argument to be used in a formatting function.
Definition base.h:2775