aboutsummaryrefslogtreecommitdiff
path: root/src/base.h
blob: 7ef78014d8e24bbf49bade21005662114575f732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once

// ReSharper disable once CppUnusedIncludeDirective
#include "global_macros.h"


#ifdef CRU_DEBUG
#include <string>
#include <vector>
#else
#include <folly/String.h>
#include <folly/FBVector.h>
#endif

#include <folly/Function.h>

#include <utility>
#include <type_traits>
#include <stdexcept>
#include <memory>
#include <string_view>
#include <chrono>

namespace cru
{
    template<typename T> struct is_shared_ptr : std::false_type {};
    template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
    template<typename T> constexpr bool is_shared_ptr_v = is_shared_ptr<T>::value;

    enum class FlowControl
    {
        Continue,
        Break
    };

#ifdef CRU_DEBUG
    using String = std::wstring;
    using MultiByteString = std::string;
#else
    using String = folly::basic_fbstring<wchar_t>;
    using MultiByteString = folly::fbstring;
#endif

    using StringView = std::wstring_view;
    using MultiByteStringView = std::string_view;

    template<typename FunctionType>
    using Function = folly::Function<FunctionType>;

    template<typename FunctionType>
    using FunctionPtr = std::shared_ptr<Function<FunctionType>>;

    using Action = Function<void()>;
    using ActionPtr = FunctionPtr<void()>;

    template<typename Type, typename... Args>
    Type CreatePtr(Args&&... args)
    {
        static_assert(is_shared_ptr_v<Type>);
        return std::make_shared<typename Type::element_type>(std::forward<Args>(args)...);
    }

    inline ActionPtr CreateActionPtr(Action&& action)
    {
        return std::make_shared<Action>(std::move(action));
    }

#ifdef CRU_DEBUG
    template<typename T>
    using Vector = std::vector<T>;
#else
    template<typename T>
    using Vector = folly::fbvector<T>;
#endif

    using FloatSecond = std::chrono::duration<double, std::chrono::seconds::period>;

    class Object
    {
    public:
        Object() = default;
        Object(const Object&) = default;
        Object& operator = (const Object&) = default;
        Object(Object&&) = default;
        Object& operator = (Object&&) = default;
        virtual ~Object() = default;
    };

    struct Interface
    {
        virtual ~Interface() = default;
    };

    [[noreturn]] inline void UnreachableCode()
    {
        throw std::logic_error("Unreachable code.");
    }

    struct ICancelable : virtual Interface
    {
        virtual void Cancel() = 0;
    };

    using CancelablePtr = std::shared_ptr<ICancelable>;

    MultiByteString ToUtf8String(const StringView& string);
}