Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/licence-cpp-formatted
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Reaver Library Licence
*
* Copyright © 2016 Michał "Griwes" Dominiak
* Copyright © 2017 Michał "Griwes" Dominiak
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
Expand Down
112 changes: 112 additions & 0 deletions include/reaver/function_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Reaver Library Licence
*
* Copyright © 2017 Michał "Griwes" Dominiak
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation is required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
**/

#pragma once

namespace reaver
{
inline namespace _v1
{
enum class ref_qualifier
{
none,
lvalue,
rvalue
};

template<typename, bool is_const = false, bool is_volatile = false, ref_qualifier = ref_qualifier::none>
struct function_traits;

template<typename Ret, typename... Args, bool IsConst, bool IsVolatile, ref_qualifier RefQualifier>
struct function_traits<Ret(Args...), IsConst, IsVolatile, RefQualifier>
{
using return_type = Ret;
constexpr static bool is_const = IsConst;
constexpr static bool is_volatile = IsVolatile;
constexpr static ref_qualifier reference_qualifier = RefQualifier;

template<template<typename...> typename Template, typename... AdditionalArguments>
using explode = Template<AdditionalArguments..., Ret, Args...>;
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) const> : function_traits<Ret(Args...), true, false>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) volatile> : function_traits<Ret(Args...), false, true>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) const volatile> : function_traits<Ret(Args...), true, true>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) &> : function_traits<Ret(Args...), true, false, ref_qualifier::lvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) const &> : function_traits<Ret(Args...), true, false, ref_qualifier::lvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) volatile &> : function_traits<Ret(Args...), false, true, ref_qualifier::lvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) const volatile &> : function_traits<Ret(Args...), true, true, ref_qualifier::lvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) &&> : function_traits<Ret(Args...), true, false, ref_qualifier::rvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) const &&> : function_traits<Ret(Args...), true, false, ref_qualifier::rvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) volatile &&> : function_traits<Ret(Args...), false, true, ref_qualifier::rvalue>
{
};

template<typename Ret, typename... Args>
struct function_traits<Ret(Args...) const volatile &&> : function_traits<Ret(Args...), true, true, ref_qualifier::rvalue>
{
};

template<typename F>
using return_type = typename function_traits<F>::return_type;

template<typename F, template<typename...> typename Template, typename... AdditionalArguments>
using explode = typename function_traits<F>::template explode<Template, AdditionalArguments...>;
}
}
29 changes: 29 additions & 0 deletions include/reaver/sfinae_function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Reaver Library Licence
*
* Copyright © 2017 Michał "Griwes" Dominiak
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation is required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
**/

#pragma once

#define SFINAE_FUNCTION(...) \
noexcept(noexcept(__VA_ARGS__))->decltype(__VA_ARGS__) \
{ \
return __VA_ARGS__; \
}
58 changes: 58 additions & 0 deletions include/reaver/typeclass/functor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Reaver Library Licence
*
* Copyright © 2017 Michał "Griwes" Dominiak
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation is required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
**/

#pragma once

#include "typeclass.h"

namespace reaver
{
inline namespace prelude
{
inline namespace _v1
{
template<template<typename...> typename F>
struct functor
{
TYPECLASS_INSTANCE(typename U);
};

template<template<typename...> typename F,
typename A,
typename... Args,
typename Fun,
decltype(tc_instance<functor<F>, F<A, Args...>>::fmap(std::declval<F<A, Args...>>(), std::declval<Fun>()), int())...>
auto fmap(F<A, Args...> f, Fun && fn)
{
return tc_instance<functor<F>, F<A, Args...>>::fmap(std::move(f), std::forward<Fun>(fn));
}

// clang-format off
DEFAULT_INSTANCE_TEMPLATE((template<typename...> typename F), (F), functor, U)
{
template<typename Fun, typename... Args>
static auto fmap(F<Args...>, Fun && fn) = delete;
};
// clang-format on
}
}
}
60 changes: 60 additions & 0 deletions include/reaver/typeclass/hashable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Reaver Library Licence
*
* Copyright © 2017 Michał "Griwes" Dominiak
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation is required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
**/

#pragma once

#include "sfinae_function.h"
#include "typeclass.h"

namespace reaver
{
inline namespace prelude
{
inline namespace _v1
{
// TODO: use this
struct hashable_definition
{
using hash = std::size_t() const;
};

struct hashable
{
TYPECLASS_INSTANCE(typename T);
};

template<typename T>
auto hash(const T & t) SFINAE_FUNCTION(tc_instance<hashable, T>::hash(t));

// clang-format off
DEFAULT_INSTANCE(hashable, T)
{
// prefer std::hash specialization over T::hash_value
template<typename U = T, typename... Ts>
static auto hash(const U & t, Ts...) SFINAE_FUNCTION(t.hash_value());

template<typename U = T>
static auto hash(const U & t) SFINAE_FUNCTION(std::hash<U>()(t));
};
}
}
}
68 changes: 68 additions & 0 deletions include/reaver/typeclass/swappable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Reaver Library Licence
*
* Copyright © 2017 Michał "Griwes" Dominiak
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation is required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
**/

#pragma once

#include "sfinae_function.h"
#include "typeclass.h"

namespace reaver
{
inline namespace prelude
{
inline namespace _v1
{
struct swappable
{
TYPECLASS_INSTANCE(typename T);
};

template<typename T>
auto swap(T & a, T & b) SFINAE_FUNCTION(tc_instance<swappable, T>::swap(a, b));

// clang-format off
DEFAULT_INSTANCE(swappable, T)
{
template<typename U, typename = void>
struct has_member_swap : std::false_type {};

template<typename U>
struct has_member_swap<U, std::void_t<decltype(std::declval<U &>().swap(std::declval<U &>()))>> : std::true_type {};

template<typename U = T, typename std::enable_if<!has_member_swap<U>::value, int>::type...>
static void swap(T & a, T & b)
noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T>)
{
auto tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}

template<typename U = T, typename std::enable_if<has_member_swap<U>::value, int>::type...>
static void swap(T & a, T & b) noexcept(noexcept(a.swap(b)))
{
a.swap(b);
}
};
}
}
}
Loading