Skip to content

Commit 46f7164

Browse files
committed
Make optional use std::optional if available
1 parent 43fd829 commit 46f7164

File tree

19 files changed

+477
-103
lines changed

19 files changed

+477
-103
lines changed

CMakeLists.txt

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(StandardClassLibrary)
2+
project(SupportClassLibrary)
33

44
set(CMAKE_CXX_STANDARD 11)
5+
set(CMAKE_CXX_FLAGS_INIT "-Wall -Werror -Wextra -Wpedantic -Wshadow")
6+
#add_compile_definitions(SCL_USE_IOSTREAM_EXTEND)
7+
8+
file(GLOB scl_src
9+
"include/*.h"
10+
"include/*.hpp"
11+
)
12+
add_library(SupportClassLibrary INTERFACE ${scl_src})
13+
target_include_directories(SupportClassLibrary INTERFACE ${CMAKE_SOURCE_DIR}/include)
14+
15+
add_executable(SCL_E main.cpp)
16+
target_link_libraries(SCL_E PRIVATE SupportClassLibrary)
17+
18+
#include/scl/scl.hpp include/scl/tools/tools.hpp include/scl/tools/make/make.hpp include/scl/tools/make/from.h include/scl/tools/make/ptr.h include/scl/tools/make/unique.h include/scl/tools/make/shared.h include/scl/tools/iostream/iostream.hpp include/scl/tools/iostream/nl.h include/scl/tools/iostream/log/log.hpp include/scl/tools/iostream/log/basic.h include/scl/tools/iostream/log/prefixed.h include/scl/macros.h include/scl/tools/iostream/log/surrounded.h include/scl/tools/iostream/extend.h include/scl/tools/meta/meta.hpp include/scl/tools/meta/void_t.h include/scl/tools/meta/enable_if.h include/scl/tools/meta/is_convertible.h include/scl/tools/meta/can_call.h include/scl/tools/meta/is_same.h include/scl/tools/meta/fn_cast.h include/scl/tools/meta/fn_type.h include/scl/tools/meta/type_check.h include/scl/tools/meta/is_base_of.h include/scl/tools/meta/type_mod.h include/scl/tools/meta/type_query.h include/scl/tools/meta/conditional.h include/scl/tools/make/atomic.h include/scl/tools/make/promise.h include/scl/concepts/concepts.hpp include/scl/concepts/is_concept.h include/scl/concepts/require.h include/scl/concepts/Same.h include/scl/concepts/DerivedFrom.h include/scl/concepts/ConvertibleTo.h include/scl/concepts/Integral.h include/scl/concepts/Signed.h include/scl/concepts/Unsigned.h include/scl/concepts/SignedIntegral.h include/scl/concepts/UnsignedIntegral.h include/scl/concepts/Swappable.h include/scl/concepts/SwappableWith.h include/scl/concepts/Destructible.h include/scl/concepts/Constructible.h include/scl/concepts/DefaultConstructible.h include/scl/concepts/MoveConstructible.h include/scl/concepts/CopyConstructible.h include/scl/concepts/MoveAssignable.h include/scl/concepts/CopyAssignable.h include/scl/concepts/Movable.h include/scl/concepts/Copyable.h include/scl/concepts/EqualityComparable.h include/scl/concepts/EqualityComparableWith.h include/scl/concepts/LessThanComparable.h include/scl/concepts/LessThanComparableWith.h include/scl/concepts/SemiRegular.h include/scl/concepts/Regular.h include/scl/concepts/Invocable.h include/scl/concepts/RegularInvocable.h include/scl/concepts/Boolean.h include/scl/concepts/InequalityComparable.h include/scl/concepts/InequalityComparableWith.h include/scl/concepts/Predicate.h include/scl/concepts/Relation.h include/scl/exceptions/exceptions.hpp include/scl/exceptions/Throwable.h include/scl/exceptions/Exception.h include/scl/exceptions/RuntimeException.h include/scl/utils/utils.hpp include/scl/utils/toString.h include/scl/tools/meta/exists.h include/scl/tools/iostream/toString.h include/scl/tools/meta/defines_scl_to_string.h include/scl/tools/meta/defines_std_to_string.h include/scl/tools/meta/defines_ostream_operator.h main.h include/scl/utils/Any.h include/scl/exceptions/InvalidArgument.h include/scl/exceptions/OutOfBound.h include/scl/exceptions/InvalidCast.h include/scl/exceptions/InvalidAnyCast.h include/scl/concepts/NonMovable.h include/scl/concepts/NonCopyable.h include/scl/utils/Optional.h include/scl/exceptions/EmptyOptionalAccess.h include/scl/tools/make/optional.h include/scl/tools/make/any.h include/scl/tools/meta/constexpr_assert.h include/scl/utils/Either.h include/scl/exceptions/InvalidEitherAccess.h include/scl/concepts/Iterator.h include/scl/tools/meta/swap.h include/scl/concepts/ValueSwappable.h include/scl/concepts/NullablePointer.h include/scl/concepts/AssignableWith.h include/scl/concepts/FunctionObject.h include/scl/concepts/Hash.h include/scl/concepts/CharTraits.h include/scl/concepts/BinaryPredicate.h include/scl/tools/meta/is_instance.h include/scl/tools/meta/real_const.h tests/libs/include/testutils/AdvancedTypes.h include/scl/utils/Placeholder.h include/scl/stream/operators/filter.h include/scl/stream/operators/uniqueBy.h include/scl/stream/details/iterator/StlAdapter.h include/scl/stream/operators/unique.h include/scl/async/async.hpp include/scl/async/with_traits.h include/scl/async/with.h include/scl/exceptions/InvalidResource.h include/scl/async/Mutexed.h include/scl/tools/make/mutexed.h include/scl/http/http.hpp include/scl/http/StatusCode.h include/scl/http/Version.h include/scl/exceptions/UninitializedMemoryAccess.h include/scl/stream/creators/istream.h include/scl/async/AsyncQueue.h include/scl/async/Channel.h include/scl/exceptions/NonNullViolation.h include/scl/detect/detect.hpp include/scl/detect/platform/platform.hpp include/scl/detect/platform/windows.h include/scl/detect/platform/unix.h include/scl/detect/platform/linux.h include/scl/detect/platform/platform.clean_macros.h include/scl/detect/platform/posix.h include/scl/detect/platform/apple.h include/scl/detect/platform/android.h include/scl/detect/platform/unified/unified.hpp include/scl/detect/platform/unified/windows.h include/scl/detect/platform/unified/unified.clean_macros.h include/scl/detect/platform/unified/posix.h include/scl/prelude/prelude.hpp include/scl/alias/alias.hpp include/scl/stream/details/deprecation.h include/scl/concepts/deprecation.h include/scl/utils/Result.h include/scl/exceptions/InvalidResultAccess.h include/scl/tools/make/result.h include/scl/utils/invoke.h include/scl/async/Active.h
519

6-
add_compile_definitions(SCL_USE_IOSTREAM_EXTEND)
7-
add_executable(StandardClassLibrary main.cpp include/scl/scl.hpp include/scl/tools/tools.hpp include/scl/tools/make/make.hpp include/scl/tools/make/from.h include/scl/tools/make/ptr.h include/scl/tools/make/unique.h include/scl/tools/make/shared.h include/scl/tools/iostream/iostream.hpp include/scl/tools/iostream/nl.h include/scl/tools/iostream/log/log.hpp include/scl/tools/iostream/log/basic.h include/scl/tools/iostream/log/prefixed.h include/scl/macros.h include/scl/tools/iostream/log/surrounded.h include/scl/tools/iostream/extend.h include/scl/tools/meta/meta.hpp include/scl/tools/meta/void_t.h include/scl/tools/meta/enable_if.h include/scl/tools/meta/is_convertible.h include/scl/tools/meta/can_call.h include/scl/tools/meta/is_same.h include/scl/tools/meta/fn_cast.h include/scl/tools/meta/fn_type.h include/scl/tools/meta/type_check.h include/scl/tools/meta/is_base_of.h include/scl/tools/meta/type_mod.h include/scl/tools/meta/type_query.h include/scl/tools/meta/conditional.h include/scl/tools/make/atomic.h include/scl/tools/make/promise.h include/scl/concepts/concepts.hpp include/scl/concepts/is_concept.h include/scl/concepts/require.h include/scl/concepts/Same.h include/scl/concepts/DerivedFrom.h include/scl/concepts/ConvertibleTo.h include/scl/concepts/Integral.h include/scl/concepts/Signed.h include/scl/concepts/Unsigned.h include/scl/concepts/SignedIntegral.h include/scl/concepts/UnsignedIntegral.h include/scl/concepts/Swappable.h include/scl/concepts/SwappableWith.h include/scl/concepts/Destructible.h include/scl/concepts/Constructible.h include/scl/concepts/DefaultConstructible.h include/scl/concepts/MoveConstructible.h include/scl/concepts/CopyConstructible.h include/scl/concepts/MoveAssignable.h include/scl/concepts/CopyAssignable.h include/scl/concepts/Movable.h include/scl/concepts/Copyable.h include/scl/concepts/EqualityComparable.h include/scl/concepts/EqualityComparableWith.h include/scl/concepts/LessThanComparable.h include/scl/concepts/LessThanComparableWith.h include/scl/concepts/SemiRegular.h include/scl/concepts/Regular.h include/scl/concepts/Invocable.h include/scl/concepts/RegularInvocable.h include/scl/concepts/Boolean.h include/scl/concepts/InequalityComparable.h include/scl/concepts/InequalityComparableWith.h include/scl/concepts/Predicate.h include/scl/concepts/Relation.h include/scl/exceptions/exceptions.hpp include/scl/exceptions/Throwable.h include/scl/exceptions/Exception.h include/scl/exceptions/RuntimeException.h include/scl/utils/utils.hpp include/scl/utils/toString.h include/scl/tools/meta/exists.h include/scl/tools/iostream/toString.h include/scl/tools/meta/defines_scl_to_string.h include/scl/tools/meta/defines_std_to_string.h include/scl/tools/meta/defines_ostream_operator.h main.h include/scl/utils/Any.h include/scl/exceptions/InvalidArgument.h include/scl/exceptions/OutOfBound.h include/scl/exceptions/InvalidCast.h include/scl/exceptions/InvalidAnyCast.h include/scl/concepts/NonMovable.h include/scl/concepts/NonCopyable.h include/scl/utils/Optional.h include/scl/exceptions/EmptyOptionalAccess.h include/scl/tools/make/optional.h include/scl/tools/make/any.h include/scl/tools/meta/constexpr_assert.h include/scl/utils/Either.h include/scl/exceptions/InvalidEitherAccess.h include/scl/concepts/Iterator.h include/scl/tools/meta/swap.h include/scl/concepts/ValueSwappable.h include/scl/concepts/NullablePointer.h include/scl/concepts/AssignableWith.h include/scl/concepts/FunctionObject.h include/scl/concepts/Hash.h include/scl/concepts/CharTraits.h include/scl/concepts/BinaryPredicate.h include/scl/tools/meta/is_instance.h include/scl/tools/meta/real_const.h tests/libs/include/testutils/AdvancedTypes.h include/scl/utils/Placeholder.h include/scl/stream/operators/filter.h include/scl/stream/operators/uniqueBy.h include/scl/stream/details/iterator/StlAdapter.h include/scl/stream/operators/unique.h include/scl/async/async.hpp include/scl/async/with_traits.h include/scl/async/with.h include/scl/exceptions/InvalidResource.h include/scl/async/Mutexed.h include/scl/tools/make/mutexed.h include/scl/http/http.hpp include/scl/http/StatusCode.h include/scl/http/Version.h include/scl/exceptions/UninitializedMemoryAccess.h include/scl/stream/creators/istream.h include/scl/async/AsyncQueue.h include/scl/async/Channel.h include/scl/exceptions/NonNullViolation.h include/scl/detect/detect.hpp include/scl/detect/platform/platform.hpp include/scl/detect/platform/windows.h include/scl/detect/platform/unix.h include/scl/detect/platform/linux.h include/scl/detect/platform/platform.clean_macros.h include/scl/detect/platform/posix.h include/scl/detect/platform/apple.h include/scl/detect/platform/android.h include/scl/detect/platform/unified/unified.hpp include/scl/detect/platform/unified/windows.h include/scl/detect/platform/unified/unified.clean_macros.h include/scl/detect/platform/unified/posix.h include/scl/prelude/prelude.hpp include/scl/alias/alias.hpp include/scl/stream/details/deprecation.h include/scl/concepts/deprecation.h include/scl/utils/Result.h include/scl/exceptions/InvalidResultAccess.h include/scl/tools/make/result.h include/scl/utils/invoke.h include/scl/async/Active.h)
8-
target_include_directories(StandardClassLibrary PRIVATE ${CMAKE_SOURCE_DIR}/include)
920

1021
add_subdirectory(tests)

include/scl/detect/detect.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ namespace scl{
88
namespace detect{}
99
}
1010

11-
#include <scl/detect/platform/platform.hpp>
11+
#include <scl/detect/platform/platform.hpp>
12+
#include <scl/detect/version/version.hpp>

include/scl/detect/version.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#include <scl/macros.h>
3+
4+
#define SCL_VERSION_CPP11 true
5+
6+
#ifdef SCL_CPP14
7+
#define SCL_VERSION_CPP14 true
8+
#else
9+
#define SCL_VERSION_CPP14 false
10+
#endif
11+
12+
#ifdef SCL_CPP17
13+
#define SCL_VERSION_CPP17 true
14+
#else
15+
#define SCL_VERSION_CPP17 false
16+
#endif
17+
18+
#ifdef SCL_CPP20
19+
#define SCL_VERSION_CPP20 true
20+
#else
21+
#define SCL_VERSION_CPP20 false
22+
#endif
23+
24+
namespace scl{
25+
namespace detect{
26+
/**
27+
* @namespace scl::detect
28+
* Namespace for detecting C++ versions
29+
*/
30+
namespace version{
31+
constexpr bool cpp11 = SCL_VERSION_CPP11;
32+
constexpr bool cpp14 = SCL_VERSION_CPP14;
33+
constexpr bool cpp17 = SCL_VERSION_CPP17;
34+
constexpr bool cpp20 = SCL_VERSION_CPP20;
35+
};
36+
}
37+
}

include/scl/exceptions/Exception.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace scl{
2323
* @tparam T being the reason's type
2424
* @param reason being the error message
2525
*/
26-
template <class T>
27-
Exception(T&& reason) : _reason{std::forward<T>(reason)} {
26+
Exception(const char* reason) : _reason{reason} {
2827
}
2928

3029
/**

include/scl/exceptions/RuntimeException.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace scl{
1010
/**
1111
* Base class for exceptions that could never be checked at compile-time
1212
*/
13-
class RuntimeException : public Throwable, public std::runtime_error{
13+
class RuntimeException : public Throwable{
1414
protected:
1515
/**
1616
* @var _reason
@@ -24,8 +24,7 @@ namespace scl{
2424
* @tparam T being the reason's type
2525
* @param reason being the error message
2626
*/
27-
template <class T>
28-
RuntimeException(T&& reason) : _reason{std::forward<T>(reason)}, runtime_error{std::forward<T>(reason)} {
27+
RuntimeException(const char* reason) : _reason{reason} {
2928
}
3029

3130
/**

include/scl/http/StatusCode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ namespace scl{
375375
std::string operator()(const scl::http::StatusCode& status){
376376
return STATUS + std::to_string(status.status())
377377
+ SEP + status.name()
378-
+ CF + std::to_string(status.version()) + SPECS;
378+
+ CF + scl::utils::toString(status.version()) + SPECS;
379379
}
380380
};
381381

include/scl/http/Version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace scl{
8484
}
8585

8686
public:
87-
friend class scl::utils::ToString<Version>;
87+
friend struct scl::utils::ToString<Version>;
8888

8989
#define VERSION(name) static const Version name
9090
VERSION(V1_0);

include/scl/http/http.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ namespace scl{
88
namespace http{}
99
}
1010

11-
#include <scl/http/StatusCode.h>
11+
#include <scl/http/StatusCode.h>
12+
#include <scl/http/Version.h>

include/scl/macros.h

+3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
return stmt;\
1212
}
1313

14+
#define SCL_NODISCARD
15+
1416
#ifdef SCL_CPP20
1517
#define SCL_CPP17
1618
#endif
1719

1820
#ifdef SCL_CPP17
1921
#define SCL_CPP14
22+
#define SCL_NODISCARD [[nodiscard]]
2023
#endif

include/scl/stream/terminators/pack/toSet.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace scl {
1313
* Class that allows packing to a std::set
1414
* @tparam T being the stream value type
1515
* @tparam Compare being the type of comparator used to insert elements
16-
* @tparam Allocator being the type of the allocator used for std::vector
16+
* @tparam Allocator being the type of the allocator used for std::set
1717
*/
1818
template <class T, class Compare = std::less<T>, class Allocator = std::allocator<T>>
1919
class SetPacker : public scl::stream::details::iterator::EndStreamIterator<std::set<T, Compare, Allocator>, T>{
@@ -111,8 +111,8 @@ namespace scl {
111111
* @return a vector containing the elements from the stream
112112
*/
113113
template <class T, class Compare, class Allocator>
114-
typename pack::details::SetPacker<T>::result_type operator|(const Stream<T>& lhs, const pack::details::to_set_toolbox_alloc<Compare, Allocator>& _){
115-
auto packer = pack::details::SetPacker<T>{lhs.it()};
114+
typename pack::details::SetPacker<T, Compare, Allocator>::result_type operator|(const Stream<T>& lhs, const pack::details::to_set_toolbox_alloc<Compare, Allocator>& _){
115+
auto packer = pack::details::SetPacker<T, Compare, Allocator>{lhs.it()};
116116
return packer.process();
117117
}
118118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#pragma once
2+
#include <scl/stream/details/deprecation.h>
3+
#include <unordered_set>
4+
#include <scl/stream/Stream.h>
5+
#include <scl/stream/details/iterator/EndStreamIterator.h>
6+
7+
namespace scl {
8+
namespace stream {
9+
namespace terminators {
10+
namespace pack {
11+
namespace details {
12+
/**
13+
* Class that allows packing to a std::unordered_set
14+
* @tparam T being the stream value type
15+
* @tparam Hash being the type of hash function
16+
* @tparam KeyEqual being the type of the key comparator
17+
* @tparam Allocator being the type of the allocator used for std::unordered_set
18+
*/
19+
template <class T, class Hash == std::hash<T>, class KeyEqual = std::equal_to<T>, class Allocator = std::allocator<T>>
20+
class UnorderedSetPacker : public scl::stream::details::iterator::EndStreamIterator<std::unordered_set<T, Hash, KeyEqual, Allocator>, T>{
21+
public:
22+
using iterator_type = stream::details::iterator::EndStreamIterator<std::unordered_set<T, Hash, KeyEqual, Allocator>, T>;
23+
using value_type = typename iterator_type::value_type;
24+
using payload_type = typename iterator_type::payload_type;
25+
using result_type = typename iterator_type::result_type;
26+
using parent_iterator_type = typename iterator_type::parent_iterator_type;
27+
using parent_type = typename iterator_type::parent_type;
28+
29+
using hash_type = Hash;
30+
using key_equal_type = KeyEqual;
31+
using allocator_type = Allocator;
32+
33+
explicit UnorderedSetPacker(parent_type p) : iterator_type{std::move(p)} {};
34+
35+
result_type process(){
36+
result_type ret;
37+
38+
for(auto&& payload : *this){
39+
if(payload.isValid())
40+
ret.insert(*payload.value());
41+
}
42+
43+
return ret;
44+
}
45+
};
46+
47+
/**
48+
* Tag type to pack into a std::set w/ default template parameters
49+
*/
50+
struct to_unordered_set_toolbox{};
51+
52+
/**
53+
* Tag type to pack into a std::set w/ control over template parameters
54+
* @tparam Hash being the type of the hash function
55+
* @tparam KeyEqual being the type of the key comparator
56+
* @tparam Allocator being the type of allocator used to allocate memory
57+
*/
58+
template <class Hash, class KeyEqual, class Allocator>
59+
struct to_unordered_set_toolbox_alloc{};
60+
}
61+
62+
details::to_unordered_set_toolbox toUnorderedSet(){ return {}; }
63+
64+
/**
65+
* Tag type to pack into a std::set w/ control over template parameters
66+
* @tparam Hash being the type of the hash function
67+
* @tparam KeyEqual being the type of the key comparator
68+
* @tparam Allocator being the type of allocator used to allocate memory
69+
*/
70+
template <class Hash, class KeyEqual, class Allocator>
71+
details::to_unordered_set_toolbox_alloc<Hash, KeyEqual, Allocator> toUnorderedSet(){
72+
return {};
73+
}
74+
}
75+
76+
/**
77+
* Pipe operator for packing into an unordered set
78+
* @tparam T being the value type of the stream
79+
* @param lhs being the stream to pack
80+
* @param _ being the packer type tag
81+
* @return an unordered set containing the elements from the stream
82+
*/
83+
template <class T>
84+
typename pack::details::UnorderedSetPacker<T>::result_type operator|(const Stream<T>& lhs, const pack::details::to_unordered_set_toolbox& _){
85+
auto packer = pack::details::UnorderedSetPacker<T>{lhs.it()};
86+
return packer.process();
87+
}
88+
89+
/**
90+
* Pipe operator or packing into an unordered set using a specific allocator and comparator
91+
* @tparam T being the value type of the stream
92+
* @tparam Hash being the type of the hash function
93+
* @tparam KeyEqual being the type of the key comparator
94+
* @tparam Allocator being the type of allocator used to allocate memory
95+
* @param lhs being the stream to pack
96+
* @param _ being the packer type tag
97+
* @return an unordered set containing the elements from the stream
98+
*/
99+
template <class T, class Compare, class Allocator>
100+
typename pack::details::UnorderedSetPacker<T, Hash, KeyEqual, Allocator>::result_type operator|(const Stream<T>& lhs, const pack::details::to_unordered_set_toolbox_alloc<Hash, KeyEqual, Allocator>& _){
101+
auto packer = pack::details::UnorderedSetPacker<T, Hash, KeyEqual, Allocator>{lhs.it()};
102+
return packer.process();
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)