SPH
Object.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <cstdint>
9 #include <utility>
10 
11 #define NAMESPACE_SPH_BEGIN namespace Sph {
12 #define NAMESPACE_SPH_END }
13 
15 
16 
18 #ifdef __GNUC__
19 #ifdef __clang__
20 #define SPH_CLANG
21 #else
22 #define SPH_GCC
23 #endif
24 #endif
25 
27 #ifdef SPH_DEBUG
28 #define INLINE inline
29 #define INL
30 #else
31 #define INLINE __attribute__((always_inline)) inline
32 #define INL __attribute__((always_inline))
33 #endif
34 
35 #define NO_INLINE __attribute__((noinline))
36 
37 #define UNUSED(x)
38 
42 #define MARK_USED(x) (void)sizeof(x)
43 
44 #define SPH_FALLTHROUGH [[fallthrough]];
45 
46 #define DEPRECATED __attribute__((deprecated))
47 
49 #define SPH_LIKELY(x) __builtin_expect(bool(x), 1)
50 #define SPH_UNLIKELY(x) __builtin_expect(bool(x), 0)
51 
52 
54 struct Noncopyable {
55  Noncopyable() = default;
56 
57  Noncopyable(const Noncopyable&) = delete;
58  Noncopyable(Noncopyable&&) = default;
59  Noncopyable& operator=(const Noncopyable&) = delete;
61 };
62 
64 struct Immovable {
65  Immovable() = default;
66 
67  Immovable(const Immovable&) = delete;
68  Immovable(Immovable&&) = delete;
69  Immovable& operator=(const Immovable&) = delete;
71 };
72 
73 
80 class Local {
81  void* operator new(std::size_t) = delete;
82  void* operator new[](std::size_t) = delete;
83  void operator delete(void*) = delete;
84  void operator delete[](void*) = delete;
85 };
86 
88 struct Polymorphic {
89  virtual ~Polymorphic() {}
90 };
91 
93 template <typename T>
94 class Badge {
95 private:
96  friend T;
97 
98  Badge() {}
99 };
100 
101 namespace Detail {
102 template <std::size_t N1, std::size_t N2>
104  template <typename TVisitor>
105  INLINE static void action(TVisitor&& visitor) {
106  visitor.template visit<N1>();
107  StaticForType<N1 + 1, N2>::action(std::forward<TVisitor>(visitor));
108  }
109 };
110 template <std::size_t N>
111 struct StaticForType<N, N> {
112  template <typename TVisitor>
113  INLINE static void action(TVisitor&& visitor) {
114  visitor.template visit<N>();
115  }
116 };
117 } // namespace Detail
118 
122 template <std::size_t N1, std::size_t N2, typename TVisitor>
123 INLINE void staticFor(TVisitor&& visitor) {
124  Detail::StaticForType<N1, N2>::action(std::forward<TVisitor>(visitor));
125 }
126 
127 namespace Detail {
129 template <typename... TArgs>
130 void expandPack(TArgs&&...) {}
131 } // namespace Detail
132 
133 template <typename TVisitor, typename... TArgs>
134 INLINE void staticForEach(TVisitor&& visitor, TArgs&&... args) {
135  Detail::expandPack(visitor(std::forward<TArgs>(args))...);
136 }
137 
138 
139 namespace Detail {
140 template <std::size_t I, std::size_t N>
142  template <typename TValue, typename... TOthers>
143  INLINE static decltype(auto) action(TValue&& UNUSED(value), TOthers&&... others) {
144  return SelectNthType<I + 1, N>::action(std::forward<TOthers>(others)...);
145  }
146 };
147 template <std::size_t N>
148 struct SelectNthType<N, N> {
149  template <typename TValue, typename... TOthers>
150  INLINE static decltype(auto) action(TValue&& value, TOthers&&... UNUSED(others)) {
151  return std::forward<TValue>(value);
152  }
153 };
154 } // namespace Detail
155 
158 template <std::size_t N, typename TValue, typename... TOthers>
159 INLINE decltype(auto) selectNth(TValue&& value, TOthers&&... others) {
160  return Detail::SelectNthType<0, N>::action(std::forward<TValue>(value), std::forward<TOthers>(others)...);
161 }
162 
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
INLINE void staticFor(TVisitor &&visitor)
Definition: Object.h:123
#define NAMESPACE_SPH_BEGIN
Definition: Object.h:11
INLINE void staticForEach(TVisitor &&visitor, TArgs &&... args)
Definition: Object.h:134
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
decltype(auto) INLINE selectNth(TValue &&value, TOthers &&... others)
Definition: Object.h:159
constexpr int N
Helper class used to allow calling a function only from within T.
Definition: Object.h:94
Object intended to only be constructed on stack.
Definition: Object.h:80
void expandPack(TArgs &&...)
Definition: Object.h:130
static decltype(auto) INLINE action(TValue &&UNUSED(value), TOthers &&... others)
Definition: Object.h:143
static INLINE void action(TVisitor &&visitor)
Definition: Object.h:113
static INLINE void action(TVisitor &&visitor)
Definition: Object.h:105
Object with deleted copy and move constructor and copy and move operator.
Definition: Object.h:64
Immovable()=default
Immovable & operator=(Immovable &&)=delete
Immovable(Immovable &&)=delete
Immovable(const Immovable &)=delete
Immovable & operator=(const Immovable &)=delete
Object with deleted copy constructor and copy operator.
Definition: Object.h:54
Noncopyable & operator=(const Noncopyable &)=delete
Noncopyable(Noncopyable &&)=default
Noncopyable(const Noncopyable &)=delete
Noncopyable()=default
Noncopyable & operator=(Noncopyable &&)=default
Base class for all polymorphic objects.
Definition: Object.h:88
virtual ~Polymorphic()
Definition: Object.h:89