Merge branch 'feature/either2' into feature/library_intermediate
This commit is contained in:
commit
57fc1f1cb8
211
src/cpp-utils/either.h
Normal file
211
src/cpp-utils/either.h
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef MESSMER_CPPUTILS_EITHER_H
|
||||||
|
#define MESSMER_CPPUTILS_EITHER_H
|
||||||
|
|
||||||
|
#include <boost/optional.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
namespace cpputils {
|
||||||
|
|
||||||
|
template<class Left, class Right>
|
||||||
|
class either final {
|
||||||
|
public:
|
||||||
|
template<class Head, class... Tail, std::enable_if_t<std::is_constructible<Left, Head, Tail...>::value && !std::is_constructible<Right, Head, Tail...>::value>* = nullptr>
|
||||||
|
either(Head&& construct_left_head_arg, Tail&&... construct_left_tail_args) noexcept(noexcept(std::declval<either<Left, Right>>()._construct_left(std::forward<Head>(construct_left_head_arg), std::forward<Tail>(construct_left_tail_args)...)))
|
||||||
|
: _side(Side::left) {
|
||||||
|
_construct_left(std::forward<Head>(construct_left_head_arg), std::forward<Tail>(construct_left_tail_args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Head, class... Tail, std::enable_if_t<!std::is_constructible<Left, Head, Tail...>::value && std::is_constructible<Right, Head, Tail...>::value>* = nullptr>
|
||||||
|
either(Head&& construct_right_head_arg, Tail&&... construct_right_tail_args) noexcept(noexcept(std::declval<either<Left, Right>>()._construct_right(std::forward<Head>(construct_right_head_arg), std::forward<Tail>(construct_right_tail_args)...)))
|
||||||
|
: _side(Side::right) {
|
||||||
|
_construct_right(std::forward<Head>(construct_right_head_arg), std::forward<Tail>(construct_right_tail_args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO Try allowing copy-construction when Left/Right types are std::is_convertible
|
||||||
|
either(const either<Left, Right> &rhs) noexcept(noexcept(std::declval<either<Left, Right>>()._construct_left(rhs._left)) && noexcept(std::declval<either<Left, Right>>()._construct_right(rhs._right)))
|
||||||
|
: _side(rhs._side) {
|
||||||
|
if(_side == Side::left) {
|
||||||
|
_construct_left(rhs._left);
|
||||||
|
} else {
|
||||||
|
_construct_right(rhs._right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
either(either<Left, Right> &&rhs) noexcept(noexcept(_construct_left(std::move(rhs._left))) && noexcept(_construct_right(std::move(rhs._right))))
|
||||||
|
: _side(rhs._side) {
|
||||||
|
if(_side == Side::left) {
|
||||||
|
_construct_left(std::move(rhs._left));
|
||||||
|
} else {
|
||||||
|
_construct_right(std::move(rhs._right));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~either() {
|
||||||
|
_destruct();
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO Try allowing copy-assignment when Left/Right types are std::is_convertible
|
||||||
|
either<Left, Right> &operator=(const either<Left, Right> &rhs) noexcept(noexcept(_construct_left(rhs._left)) && noexcept(_construct_right(rhs._right))) {
|
||||||
|
_destruct();
|
||||||
|
_side = rhs._side;
|
||||||
|
if (_side == Side::left) {
|
||||||
|
_construct_left(rhs._left);
|
||||||
|
} else {
|
||||||
|
_construct_right(rhs._right);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
either<Left, Right> &operator=(either<Left, Right> &&rhs) noexcept(noexcept(_construct_left(std::move(rhs._left))) && noexcept(_construct_right(std::move(rhs._right)))) {
|
||||||
|
_destruct();
|
||||||
|
_side = rhs._side;
|
||||||
|
if (_side == Side::left) {
|
||||||
|
_construct_left(std::move(rhs._left));
|
||||||
|
} else {
|
||||||
|
_construct_right(std::move(rhs._right));
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO fold, map_left, map_right, left_or_else(val), right_or_else(val), left_or_else(func), right_or_else(func)
|
||||||
|
|
||||||
|
bool is_left() const noexcept {
|
||||||
|
return _side == Side::left;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_right() const noexcept {
|
||||||
|
return _side == Side::right;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Left &left() const& {
|
||||||
|
ASSERT(is_left(), "Tried to get left side of an either which is right.");
|
||||||
|
return _left;
|
||||||
|
}
|
||||||
|
Left &left() & {
|
||||||
|
return const_cast<Left&>(const_cast<const either<Left, Right>*>(this)->left());
|
||||||
|
}
|
||||||
|
Left &&left() && {
|
||||||
|
return std::move(left());
|
||||||
|
}
|
||||||
|
|
||||||
|
const Right &right() const& {
|
||||||
|
ASSERT(is_right(), "Tried to get right side of an either which is left.");
|
||||||
|
return _right;
|
||||||
|
}
|
||||||
|
Right &right() & {
|
||||||
|
return const_cast<Right&>(const_cast<const either<Left, Right>*>(this)->right());
|
||||||
|
}
|
||||||
|
Right &&right() && {
|
||||||
|
return std::move(right());
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::optional<const Left&> left_opt() const& noexcept {
|
||||||
|
if (_side == Side::left) {
|
||||||
|
return _left;
|
||||||
|
} else {
|
||||||
|
return boost::none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boost::optional<Left&> left_opt() & noexcept {
|
||||||
|
if (_side == Side::left) {
|
||||||
|
return _left;
|
||||||
|
} else {
|
||||||
|
return boost::none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// left_opt()&& not offered because optional<Left&&> doesn't work
|
||||||
|
|
||||||
|
boost::optional<const Right&> right_opt() const& noexcept {
|
||||||
|
if (_side == Side::right) {
|
||||||
|
return _right;
|
||||||
|
} else {
|
||||||
|
return boost::none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boost::optional<Right&> right_opt() & noexcept {
|
||||||
|
if (_side == Side::right) {
|
||||||
|
return _right;
|
||||||
|
} else {
|
||||||
|
return boost::none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// right_opt()&& not offered because optional<Right&&> doesn't work
|
||||||
|
|
||||||
|
private:
|
||||||
|
union {
|
||||||
|
Left _left;
|
||||||
|
Right _right;
|
||||||
|
};
|
||||||
|
enum class Side : uint8_t {left, right} _side;
|
||||||
|
|
||||||
|
explicit either(Side side) noexcept : _side(side) {}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void _construct_left(Args&&... args) noexcept(noexcept(new Left(std::forward<Args>(args)...))) {
|
||||||
|
new(&_left)Left(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
template<typename... Args>
|
||||||
|
void _construct_right(Args&&... args) noexcept(noexcept(new Right(std::forward<Args>(args)...))) {
|
||||||
|
new(&_right)Right(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
void _destruct() noexcept {
|
||||||
|
if (_side == Side::left) {
|
||||||
|
_left.~Left();
|
||||||
|
} else {
|
||||||
|
_right.~Right();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Left_, typename Right_, typename... Args>
|
||||||
|
friend either<Left_, Right_> make_left(Args&&... args) /* TODO noexcept(noexcept(std::declval<either<Left, Right>>()._construct_left(std::forward<Args>(args)...))) */;
|
||||||
|
|
||||||
|
template<typename Left_, typename Right_, typename... Args>
|
||||||
|
friend either<Left_, Right_> make_right(Args&&... args) /* TODO noexcept(noexcept(std::declval<either<Left, Right>>()._construct_right(std::forward<Args>(args)...))) */;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class Left, class Right>
|
||||||
|
inline bool operator==(const either<Left, Right> &lhs, const either<Left, Right> &rhs) noexcept(noexcept(std::declval<Left>() == std::declval<Left>()) && noexcept(std::declval<Right>() == std::declval<Right>())) {
|
||||||
|
if (lhs.is_left() != rhs.is_left()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (lhs.is_left()) {
|
||||||
|
return lhs.left() == rhs.left();
|
||||||
|
} else {
|
||||||
|
return lhs.right() == rhs.right();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Left, class Right>
|
||||||
|
inline bool operator!=(const either<Left, Right> &lhs, const either<Left, Right> &rhs) noexcept(noexcept(operator==(lhs, rhs))) {
|
||||||
|
return !operator==(lhs, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Left, class Right>
|
||||||
|
inline std::ostream &operator<<(std::ostream &stream, const either<Left, Right> &value) {
|
||||||
|
if (value.is_left()) {
|
||||||
|
stream << "Left(" << value.left() << ")";
|
||||||
|
} else {
|
||||||
|
stream << "Right(" << value.right() << ")";
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Left, typename Right, typename... Args>
|
||||||
|
inline either<Left, Right> make_left(Args&&... args) /* TODO noexcept(noexcept(std::declval<either<Left, Right>>()._construct_left(std::forward<Args>(args)...))) */ {
|
||||||
|
either<Left, Right> result(either<Left, Right>::Side::left);
|
||||||
|
result._construct_left(std::forward<Args>(args)...);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Left, typename Right, typename... Args>
|
||||||
|
inline either<Left, Right> make_right(Args&&... args) /* TODO noexcept(noexcept(std::declval<either<Left, Right>>()._construct_right(std::forward<Args>(args)...))) */ {
|
||||||
|
either<Left, Right> result(either<Left, Right>::Side::right);
|
||||||
|
result._construct_right(std::forward<Args>(args)...);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -8,6 +8,7 @@
|
|||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <Winhttp.h>
|
#include <Winhttp.h>
|
||||||
|
#include <VersionHelpers.h>
|
||||||
|
|
||||||
using boost::none;
|
using boost::none;
|
||||||
using boost::optional;
|
using boost::optional;
|
||||||
@ -212,7 +213,8 @@ namespace cpputils {
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
cpputils::unique_ref<WinHttpSession> create_session() {
|
cpputils::unique_ref<WinHttpSession> create_session() {
|
||||||
HttpHandleRAII session_handle = WinHttpOpen(L"cpputils::HttpClient", WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
const DWORD dwAccessType = IsWindows8Point1OrGreater() ? WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY : WINHTTP_ACCESS_TYPE_DEFAULT_PROXY;
|
||||||
|
HttpHandleRAII session_handle = WinHttpOpen(L"cpputils::HttpClient", dwAccessType, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||||
if(nullptr == session_handle.handle) {
|
if(nullptr == session_handle.handle) {
|
||||||
throw std::runtime_error("Error calling WinHttpOpen. Error code: " + std::to_string(GetLastError()));
|
throw std::runtime_error("Error calling WinHttpOpen. Error code: " + std::to_string(GetLastError()));
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,9 @@ using std::cerr;
|
|||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
if (!IsWindows10OrGreater()) {
|
if (!IsWindows7SP1OrGreater()) {
|
||||||
std::cerr << "CryFS is currently only supported on Windows 10 (or later)." << std::endl;
|
std::cerr << "CryFS is currently only supported on Windows 7 SP1 (or later)." << std::endl;
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -54,6 +54,7 @@ set(SOURCES
|
|||||||
system/HomedirTest.cpp
|
system/HomedirTest.cpp
|
||||||
system/EnvTest.cpp
|
system/EnvTest.cpp
|
||||||
value_type/ValueTypeTest.cpp
|
value_type/ValueTypeTest.cpp
|
||||||
|
either_test.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME}_exit_status process/exit_status.cpp)
|
add_executable(${PROJECT_NAME}_exit_status process/exit_status.cpp)
|
||||||
|
1261
test/cpp-utils/either_test.cpp
Normal file
1261
test/cpp-utils/either_test.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@ -13,6 +13,8 @@ using cpputils::value_type::OrderedIdValueType;
|
|||||||
using cpputils::value_type::QuantityValueType;
|
using cpputils::value_type::QuantityValueType;
|
||||||
using cpputils::value_type::FlagsValueType;
|
using cpputils::value_type::FlagsValueType;
|
||||||
|
|
||||||
|
// TODO Test that noexcept flags are set correctly
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
struct MyIdValueType : IdValueType<MyIdValueType, int64_t> {
|
struct MyIdValueType : IdValueType<MyIdValueType, int64_t> {
|
||||||
|
Loading…
Reference in New Issue
Block a user