Skip to content

Commit aae6653

Browse files
committed
Factored out enum_ops
1 parent 08845b7 commit aae6653

File tree

4 files changed

+27
-249
lines changed

4 files changed

+27
-249
lines changed

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 3.29)
2+
project(adobe-source-libraries CXX)
23
include(FindGit)
34
include(CMakeParseArguments)
45

@@ -10,19 +11,24 @@ if(WIN32)
1011
set(CMAKE_OBJECT_PATH_MAX 500)
1112
endif()
1213

13-
project(adobe_source_libraries CXX)
14-
1514
################################################################################
1615

1716
# Enable CPM caching to avoid re-downloading dependencies. Must be before CPM.cmake include.
18-
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/.cpm-cache CACHE PATH "Directory to cache CPM packages" FORCE)
17+
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/.cache/cpm CACHE PATH "Directory to cache CPM packages" FORCE)
1918

2019
include(cmake/CPM.cmake)
2120

2221
################################################################################
2322
# Dependencies
2423

25-
CPMAddPackage("gh:stlab/[email protected]")
24+
# https://github.com/stlab/copy-on-write/releases
25+
CPMAddPackage("gh:stlab/[email protected]")
26+
27+
# https://github.com/stlab/enum-ops/releases
28+
CPMAddPackage(gh:stlab/[email protected])
29+
# CPMAddPackage(NAME enum-ops
30+
# URL "${CMAKE_SOURCE_DIR}/../enum-ops"
31+
# )
2632

2733
################################################################################
2834

@@ -43,8 +49,6 @@ if(CMAKE_EXPORT_COMPILE_COMMANDS AND PROJECT_IS_TOP_LEVEL)
4349
)
4450
endif()
4551

46-
project(adobe-source-libraries CXX)
47-
4852
# https://github.com/boostorg/boost/releases
4953
CPMAddPackage(
5054
NAME Boost
@@ -64,6 +68,7 @@ function(target_link_boost target)
6468
target_link_libraries(${target} PUBLIC Boost::multiprecision)
6569
target_link_libraries(${target} PUBLIC Boost::date_time)
6670
target_link_libraries(${target} PUBLIC stlab::copy-on-write)
71+
target_link_libraries(${target} PUBLIC stlab::enum-ops)
6772
endfunction(target_link_boost)
6873

6974
function(target_link_boost_test target)

CMakePresets.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"cacheVariables": {
1111
"CMAKE_BUILD_TYPE": "DEBUG",
1212
"CMAKE_CXX_STANDARD": "20",
13-
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
14-
"CPM_SOURCE_CACHE": "${sourceParentDir}/.cache/CPM"
13+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
1514
},
1615
"architecture": {
1716
"value": "x64",
@@ -34,8 +33,7 @@
3433
"cacheVariables": {
3534
"CMAKE_BUILD_TYPE": "DEBUG",
3635
"CMAKE_CXX_STANDARD": "17",
37-
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
38-
"CPM_SOURCE_CACHE": "${sourceParentDir}/.cache/CPM"
36+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
3937
},
4038
"architecture": {
4139
"value": "x64",
@@ -58,8 +56,7 @@
5856
"cacheVariables": {
5957
"CMAKE_BUILD_TYPE": "DEBUG",
6058
"CMAKE_CXX_STANDARD": "20",
61-
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
62-
"CPM_SOURCE_CACHE": "${sourceParentDir}/.cache/CPM"
59+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
6360
},
6461
"vendor": {
6562
"microsoft.com/VisualStudioSettings/CMake/1.0": {
@@ -77,8 +74,7 @@
7774
"binaryDir": "${sourceParentDir}/build/asl-${presetName}",
7875
"cacheVariables": {
7976
"CMAKE_BUILD_TYPE": "DEBUG",
80-
"CMAKE_CXX_STANDARD": "17",
81-
"CPM_SOURCE_CACHE": "${sourceParentDir}/.cache/CPM"
77+
"CMAKE_CXX_STANDARD": "17"
8278
}
8379
},
8480
{
@@ -89,8 +85,7 @@
8985
"cacheVariables": {
9086
"CMAKE_CXX_STANDARD": "17",
9187
"CMAKE_BUILD_TYPE": "DEBUG",
92-
"CMAKE_CXX_CLANG_TIDY": "clang-tidy",
93-
"CPM_SOURCE_CACHE": "${sourceParentDir}/.cache/CPM"
88+
"CMAKE_CXX_CLANG_TIDY": "clang-tidy"
9489
}
9590
},
9691
{
@@ -174,7 +169,7 @@
174169
"displayName": "Build Documentation",
175170
"description": "Build the documentation",
176171
"configurePreset": "docs",
177-
"targets": ["docs"]
172+
"targets": "docs"
178173
}
179174
],
180175
"testPresets": [

adobe/enum_ops.hpp

Lines changed: 8 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -8,244 +8,22 @@
88
#ifndef ADOBE_ENUM_OPS_HPP
99
#define ADOBE_ENUM_OPS_HPP
1010

11-
/**************************************************************************************************/
12-
13-
#include <type_traits>
14-
15-
/**************************************************************************************************/
16-
17-
/*!
18-
\defgroup enum_ops Typesafe Integers and Bit Fields (enums)
19-
\ingroup utility
20-
21-
\section Description Description
22-
23-
\c enum_ops provides optional typesafe bitset and arithmetic operations for enumeration types.
24-
Without these typesafe operations, the compiler will promote the operand(s) to the appropriate
25-
integral type, and the result will be an integral type. When the typesafe operations have been
26-
defined for an enumeration type, \c E, the result will be of type \c E exactly when the
27-
operand(s) are of type \c E.
28-
29-
\c ADOBE_DEFINE_BITSET_OPS(E) or auto adobe_enable_bitmask_enum(E) -> std::true_type;
30-
enables the bitset operations <code>~, |, &, ^, |=, &=, ^= </code>
31-
for enumeration type \c E.
32-
33-
\c ADOBE_DEFINE_ARITHMETIC_OPS(E) or auto adobe_enable_arithmetic_enum(E) -> std::true_type;
34-
enables the typesafe arithmetic operations <code>+, -, *, /,
35-
%, +=, *=, -=, /=, \%=</code> for enumeration type \c E.
36-
37-
\section Definition Definition
38-
39-
Defined in \link enum_ops.hpp <code>adobe/enum_ops.hpp</code> \endlink
40-
41-
\section Example Example
42-
43-
The following is an example of code that will compile:
44-
\dontinclude enum_ops_example.cpp
45-
\skip start_of_example
46-
\until end_of_example
47-
48-
The following is contains an example of code that will not compile
49-
since the typesafe operators have not been defined.
50-
51-
\dontinclude enum_ops_example_fail.cpp
52-
\skip start_of_example
53-
\until end_of_example
54-
*/
55-
56-
/**************************************************************************************************/
57-
58-
namespace adobe {
59-
60-
/**************************************************************************************************/
61-
62-
auto adobe_enable_bitmask_enum(...) -> std::false_type;
63-
auto adobe_enable_arithmetic_enum(...) -> std::false_type;
11+
#include <stlab/enum_ops.hpp>
6412

6513
/**************************************************************************************************/
6614

67-
namespace implementation {
15+
/// \file enum_ops.hpp
16+
/// \brief Bitwise and arithmetic operator helpers for enums.
17+
/// \ingroup utility
6818

6919
/**************************************************************************************************/
7020

71-
#if !defined(ADOBE_NO_DOCUMENTATION)
72-
template <typename T>
73-
constexpr bool has_enabled_bitmask = decltype(adobe_enable_bitmask_enum(std::declval<T>()))::value;
74-
template <typename T>
75-
constexpr bool has_enabled_arithmetic =
76-
decltype(adobe_enable_arithmetic_enum(std::declval<T>()))::value;
77-
#endif
78-
79-
/**************************************************************************************************/
80-
81-
} // namespace implementation
82-
83-
/**************************************************************************************************/
21+
/// \deprecated Use stlab_enable_bitmask_enum instead.
22+
#define ADOBE_DEFINE_BITSET_OPS(EnumType) auto stlab_enable_bitmask_enum(EnumType)->std::true_type;
8423

85-
} // namespace adobe
86-
87-
/**************************************************************************************************/
88-
89-
// this exist to mantain backwards compatability with the old ops
90-
#define ADOBE_DEFINE_BITSET_OPS(EnumType) \
91-
constexpr auto adobe_enable_bitmask_enum(EnumType)->std::true_type;
92-
93-
94-
template <typename T>
95-
constexpr auto operator&(const T lhs, const T rhs)
96-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
97-
using underlying = std::underlying_type_t<T>;
98-
return static_cast<T>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs)); // NOLINT
99-
}
100-
101-
template <typename T>
102-
constexpr auto operator~(const T a)
103-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
104-
using underlying = std::underlying_type_t<T>;
105-
return static_cast<T>(~static_cast<underlying>(a)); // NOLINT
106-
}
107-
108-
template <typename T>
109-
constexpr auto operator|(const T lhs, const T rhs)
110-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
111-
using underlying = std::underlying_type_t<T>;
112-
return static_cast<T>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); // NOLINT
113-
}
114-
115-
template <typename T>
116-
constexpr auto operator^(const T lhs, const T rhs)
117-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
118-
using underlying = std::underlying_type_t<T>;
119-
return static_cast<T>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs)); // NOLINT
120-
}
121-
122-
template <typename T>
123-
constexpr auto operator^=(T& lhs, const T rhs)
124-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
125-
return lhs = lhs ^ rhs;
126-
}
127-
128-
template <typename T>
129-
constexpr auto operator&=(T& lhs, const T rhs)
130-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
131-
return lhs = lhs & rhs;
132-
}
133-
134-
template <typename T>
135-
constexpr auto operator|=(T& lhs, const T rhs)
136-
-> std::enable_if_t<adobe::implementation::has_enabled_bitmask<T>, T> {
137-
return lhs = lhs | rhs;
138-
}
139-
140-
// this exist to mantain backwards compatability with the old ops
24+
/// \deprecated Use stlab_enable_arithmetic_enum instead.
14125
#define ADOBE_DEFINE_ARITHMETIC_OPS(EnumType) \
142-
constexpr auto adobe_enable_arithmetic_enum(EnumType)->std::true_type;
143-
template <typename T>
144-
constexpr auto operator+(const T a)
145-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
146-
using underlying = std::underlying_type_t<T>;
147-
return static_cast<T>(+static_cast<underlying>(a)); // NOLINT
148-
}
149-
150-
template <typename T>
151-
constexpr auto operator-(const T a)
152-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
153-
using underlying = std::underlying_type_t<T>;
154-
return static_cast<T>(-static_cast<underlying>(a)); // NOLINT
155-
}
156-
157-
template <typename T>
158-
constexpr auto operator+(const T lhs, const T rhs)
159-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
160-
using underlying = std::underlying_type_t<T>;
161-
return static_cast<T>(static_cast<underlying>(lhs) + static_cast<underlying>(rhs)); // NOLINT
162-
}
163-
164-
template <typename T>
165-
constexpr auto operator-(const T lhs, const T rhs)
166-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
167-
using underlying = std::underlying_type_t<T>;
168-
return static_cast<T>(static_cast<underlying>(lhs) - static_cast<underlying>(rhs)); // NOLINT
169-
}
170-
171-
template <typename T>
172-
constexpr auto operator*(const T lhs, const T rhs)
173-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
174-
using underlying = std::underlying_type_t<T>;
175-
return static_cast<T>(static_cast<underlying>(lhs) * static_cast<underlying>(rhs)); // NOLINT
176-
}
177-
178-
template <typename T>
179-
constexpr auto operator/(const T lhs, const T rhs)
180-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
181-
using underlying = std::underlying_type_t<T>;
182-
return static_cast<T>(static_cast<underlying>(lhs) / static_cast<underlying>(rhs)); // NOLINT
183-
}
184-
185-
template <typename T>
186-
constexpr auto operator%(const T lhs, const T rhs)
187-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
188-
using underlying = std::underlying_type_t<T>;
189-
return static_cast<T>(static_cast<underlying>(lhs) % static_cast<underlying>(rhs)); // NOLINT
190-
}
191-
192-
template <typename T>
193-
constexpr auto operator+=(T& lhs, const T rhs)
194-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
195-
return lhs = lhs + rhs;
196-
}
197-
198-
template <typename T>
199-
constexpr auto operator-=(T& lhs, const T rhs)
200-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
201-
return lhs = lhs - rhs;
202-
}
203-
204-
template <typename T>
205-
constexpr auto operator*=(T& lhs, const T rhs)
206-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
207-
return lhs = lhs * rhs;
208-
}
209-
210-
template <typename T>
211-
constexpr auto operator/=(T& lhs, const T rhs)
212-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
213-
return lhs = lhs / rhs;
214-
}
215-
216-
template <typename T>
217-
constexpr auto operator%=(T& lhs, const T rhs)
218-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
219-
return lhs = lhs % rhs;
220-
}
221-
222-
template <typename T>
223-
constexpr auto operator++(T& lhs)
224-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
225-
return lhs += static_cast<T>(1); // NOLINT
226-
}
227-
228-
template <typename T>
229-
constexpr auto operator++(T& lhs, int)
230-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
231-
T result = lhs;
232-
lhs += static_cast<T>(1); // NOLINT
233-
return result;
234-
}
235-
236-
template <typename T>
237-
constexpr auto operator--(T& lhs)
238-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
239-
return lhs -= static_cast<T>(1); // NOLINT
240-
}
241-
242-
template <typename T>
243-
constexpr auto operator--(T& lhs, int)
244-
-> std::enable_if_t<adobe::implementation::has_enabled_arithmetic<T>, T> {
245-
T result = lhs;
246-
lhs -= static_cast<T>(1); // NOLINT
247-
return result;
248-
}
26+
auto stlab_enable_arithmetic_enum(EnumType)->std::true_type;
24927

25028
/**************************************************************************************************/
25129

test/enum_ops/enum_ops_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(enum_arith_ops) {
107107
BOOST_CHECK(x == 1);
108108

109109
// *
110-
x = num_2 * num_3;
110+
x = num_2 * 3;
111111
BOOST_CHECK(x == 6);
112112

113113
// /
@@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(enum_arith_ops) {
130130

131131
// *=
132132
x = num_2;
133-
x *= num_3;
133+
x *= 3;
134134
BOOST_CHECK(x == 6);
135135

136136
// /=

0 commit comments

Comments
 (0)