tmp.hh 1.57 KB
Newer Older
1
2
#pragma once

3
#include <memory>
4
5
6
7
8
9
10
11
#include <utility>

namespace polymesh
{
// small template metaprogramming
namespace tmp
{
template <class FuncT, class ArgT>
Philip Trettner's avatar
Philip Trettner committed
12
struct decayed_result_of
13
{
Philip Trettner's avatar
Philip Trettner committed
14
    using type = typename std::decay<decltype(std::declval<FuncT>()(std::declval<ArgT>()))>::type;
15
16
};

Philip Trettner's avatar
Philip Trettner committed
17
18
19
20
21
22
23
24
25
template <class FuncT, class ArgT>
using decayed_result_type_of = typename decayed_result_of<FuncT, ArgT>::type;

template <class FuncT, class ArgT>
struct result_of
{
    using type = decltype(std::declval<FuncT>()(std::declval<ArgT>()));
};

26
27
template <class FuncT, class ArgT>
using result_type_of = typename result_of<FuncT, ArgT>::type;
Philip Trettner's avatar
Philip Trettner committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

template <class T>
using enable_if_const_t = typename std::enable_if<std::is_const<T>::value, std::nullptr_t>::type;
template <class T>
using enable_if_mutable_t = typename std::enable_if<!std::is_const<T>::value, std::nullptr_t>::type;

template <bool Condition, class TrueType, class FalseType>
struct if_then_else;

template <class TrueType, class FalseType>
struct if_then_else<true, TrueType, FalseType>
{
    using result = TrueType;
};
template <class TrueType, class FalseType>
struct if_then_else<false, TrueType, FalseType>
{
    using result = FalseType;
};

template <class TargetT, class TestT>
using ref_if_mut = typename if_then_else<std::is_const<TestT>::value, TargetT, typename std::add_lvalue_reference<TargetT>::type>::result;

// std::add_lvalue_reference
// template <class T>
53
54
55
56
57
58
59

/// For C++11
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
60
61
}
}