am db0c176d: am 8af07798: am 79c79d54: Merge "Remove broken line interpolation tests...
[platform/upstream/VK-GL-CTS.git] / framework / common / tcuMaybe.hpp
1 #ifndef _TCUMAYBE_HPP
2 #define _TCUMAYBE_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Tester Core
5  * ----------------------------------------
6  *
7  * Copyright 2015 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Template for values that may not exist.
24  *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27
28 namespace tcu
29 {
30
31 // \note Type T is always aligned to same alignment as deUint64.
32 // \note This type always uses at least sizeof(T*) + sizeof(deUint64) of memory.
33 template<typename T>
34 class Maybe
35 {
36 public:
37                                 Maybe                   (void);
38                                 ~Maybe                  (void);
39
40                                 Maybe                   (const T& val);
41         Maybe<T>&       operator=               (const T& val);
42
43                                 Maybe                   (const Maybe<T>& other);
44         Maybe<T>&       operator=               (const Maybe<T>& other);
45
46         const T&        get                             (void) const;
47         const T&        operator*               (void) const { return get(); }
48
49         const T*        operator->              (void) const;
50                                 operator bool   (void) const { return m_ptr; }
51
52 private:
53         T*                              m_ptr;
54
55         union
56         {
57                 deUint8         m_data[sizeof(T)];
58                 deUint64        m_align;
59         };
60 };
61
62 template<typename T>
63 Maybe<T> nothing (void)
64 {
65         return Maybe<T>();
66 }
67
68 template<typename T>
69 Maybe<T>::Maybe (void)
70         : m_ptr (DE_NULL)
71 {
72 }
73
74 template<typename T>
75 Maybe<T>::~Maybe (void)
76 {
77         if (m_ptr)
78                 m_ptr->~T();
79 }
80
81 template<typename T>
82 Maybe<T>::Maybe (const T& val)
83         : m_ptr (DE_NULL)
84 {
85         m_ptr = new(m_data)T(val);
86 }
87
88 template<typename T>
89 Maybe<T>& Maybe<T>::operator= (const T& val)
90 {
91         if (m_ptr)
92                 m_ptr->~T();
93
94         m_ptr = new(m_data)T(val);
95
96         return *this;
97 }
98
99 template<typename T>
100 Maybe<T>::Maybe (const Maybe<T>& other)
101         : m_ptr (DE_NULL)
102 {
103         if (other.m_ptr)
104                 m_ptr = new(m_data)T(*other.m_ptr);
105 }
106
107 template<typename T>
108 Maybe<T>& Maybe<T>::operator= (const Maybe<T>& other)
109 {
110         if (this == &other)
111                 return *this;
112
113         if (m_ptr)
114                 m_ptr->~T();
115
116         if (other.m_ptr)
117                 m_ptr = new(m_data)T(*other.m_ptr);
118         else
119                 m_ptr = DE_NULL;
120
121         return *this;
122 }
123
124 template<typename T>
125 const T* Maybe<T>::operator-> (void) const
126 {
127         DE_ASSERT(m_ptr);
128         return m_ptr;
129 }
130
131 template<typename T>
132 const T& Maybe<T>::get (void) const
133 {
134         DE_ASSERT(m_ptr);
135         return *m_ptr;
136 }
137
138 } // tcu
139
140 #endif // _TCUMAYBE_HPP