BitStream
Loading...
Searching...
No Matches
parameter.h
Go to the documentation of this file.
1#pragma once
2
3#include "assert.h"
4#include "platform.h"
5
6#include <utility>
7#include <type_traits>
8
9namespace bitstream
10{
11#ifdef BS_DEBUG_BREAK
12 template<typename T>
13 class out
14 {
15 public:
16 BS_CONSTEXPR out(T& value) noexcept :
17 m_Value(value),
18 m_Constructed(false) {}
19
20 out(const out&) = delete;
21
22 out(out&&) = delete;
23
24 BS_CONSTEXPR ~out()
25 {
26 if (!m_Constructed)
28 }
29
30 template<typename U, typename = std::enable_if_t<std::is_assignable_v<T&, U>>>
31 BS_CONSTEXPR out& operator=(U&& arg) noexcept(std::is_nothrow_assignable_v<T&, U>)
32 {
33 m_Value = std::forward<U>(arg);
34
35 m_Constructed = true;
36
37 return *this;
38 }
39
40 BS_CONSTEXPR T* operator->() noexcept
41 {
42 m_Constructed = true;
43 return &m_Value;
44 }
45
46 BS_CONSTEXPR T& operator*() noexcept
47 {
48 m_Constructed = true;
49 return m_Value;
50 }
51
52 private:
53 T& m_Value;
54 bool m_Constructed;
55 };
56#else
57 template<typename T>
58 class out
59 {
60 public:
61 BS_CONSTEXPR out(T& value) noexcept :
62 m_Value(value) {}
63
64 out(const out&) = delete;
65
66 out(out&&) = delete;
67
68 template<typename U, typename = std::enable_if_t<std::is_assignable_v<T&, U>>>
69 BS_CONSTEXPR out& operator=(U&& arg) noexcept(std::is_nothrow_assignable_v<T&, U>)
70 {
71 m_Value = std::forward<U>(arg);
72
73 return *this;
74 }
75
76 BS_CONSTEXPR T* operator->() noexcept { return &m_Value; }
77
78 BS_CONSTEXPR T& operator*() noexcept { return m_Value; }
79
80 private:
81 T& m_Value;
82 };
83#endif
84
88 template<typename T>
89 using in = std::conditional_t<(sizeof(T) <= 16 && std::is_trivially_copy_constructible_v<T>), std::add_const_t<T>, std::add_lvalue_reference_t<std::add_const_t<T>>>;
90
94 template<typename Stream, typename T>
95 using inout = std::conditional_t<Stream::writing, in<T>, std::add_lvalue_reference_t<T>>;
96
97
101 template<typename Lambda>
103 {
104 public:
105 constexpr finally(Lambda func) noexcept :
106 m_Lambda(func) {}
107
109 {
110 m_Lambda();
111 }
112
113 private:
114 Lambda m_Lambda;
115 };
116
117 template<typename Lambda>
118 finally(Lambda func) -> finally<Lambda>;
119}
#define BS_BREAKPOINT()
Definition assert.h:17
Test type.
Definition parameter.h:103
~finally()
Definition parameter.h:108
Definition parameter.h:59
out(out &&)=delete
BS_CONSTEXPR T * operator->() noexcept
Definition parameter.h:76
BS_CONSTEXPR T & operator*() noexcept
Definition parameter.h:78
out(const out &)=delete
BS_CONSTEXPR out(T &value) noexcept
Definition parameter.h:61
BS_CONSTEXPR out & operator=(U &&arg) noexcept(std::is_nothrow_assignable_v< T &, U >)
Definition parameter.h:69
Definition bounded_range.h:28
std::conditional_t<(sizeof(T)<=16 &&std::is_trivially_copy_constructible_v< T >), std::add_const_t< T >, std::add_lvalue_reference_t< std::add_const_t< T > > > in
Passes by const or const reference depending on size.
Definition parameter.h:89
std::conditional_t< Stream::writing, in< T >, std::add_lvalue_reference_t< T > > inout
Passes by reference.
Definition parameter.h:95
#define BS_CONSTEXPR
Definition platform.h:10