Zivid C++ API 2.9.0+4dbba385-1
Defining the Future of 3D Machine Vision
Detail.h
Go to the documentation of this file.
1
2
3/*******************************************************************************
4
5 * This file is part of the Zivid 3D Camera API
6
7 *
8
9 * Copyright 2015-2023 (C) Zivid AS
10
11 * All rights reserved.
12
13 *
14
15 * Zivid Software License, v1.0
16
17 *
18
19 * Redistribution and use in source and binary forms, with or without
20
21 * modification, are permitted provided that the following conditions are met:
22
23 *
24
25 * 1. Redistributions of source code must retain the above copyright notice,
26
27 * this list of conditions and the following disclaimer.
28
29 *
30
31 * 2. Redistributions in binary form must reproduce the above copyright notice,
32
33 * this list of conditions and the following disclaimer in the documentation
34
35 * and/or other materials provided with the distribution.
36
37 *
38
39 * 3. Neither the name of Zivid AS nor the names of its contributors may be used
40
41 * to endorse or promote products derived from this software without specific
42
43 * prior written permission.
44
45 *
46
47 * 4. This software, with or without modification, must not be used with any
48
49 * other 3D camera than from Zivid AS.
50
51 *
52
53 * 5. Any software provided in binary form under this license must not be
54
55 * reverse engineered, decompiled, modified and/or disassembled.
56
57 *
58
59 * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
60
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62
63 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
64
65 * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
66
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68
69 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
70
71 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
72
73 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
74
75 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
76
77 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78
79 *
80
81 * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
82
83 * Info: http://www.zivid.com
84
85 ******************************************************************************/
86
87
88
89#pragma once
90
91#include <stdexcept>
92#include <type_traits>
93#include <utility>
94
95namespace Zivid
96{
97 namespace DataModel
98 {
99#ifndef NO_DOC
101 namespace Detail
102 {
103 template<typename DataModel>
104 void invokeSetWithEachArgument(DataModel && /*dm*/)
105 {
106 // Base case, do nothing
107 }
108
109 template<typename DataModel, typename Arg, typename... Args>
110 void invokeSetWithEachArgument(DataModel &&dm, Arg &&arg, Args &&...args)
111 {
112 dm.set(std::forward<Arg>(arg));
113 invokeSetWithEachArgument(std::forward<DataModel>(dm), std::forward<Args>(args)...);
114 }
115
116 // Note: This Optional class will default construct an object of the contained type when the
117 // optional is unset.
118 // This class should not be accessible directly via the public API.
119 template<typename T>
120 class Optional
121 {
122 static_assert(std::is_default_constructible<T>::value, "T needs to be default_constructible");
123
124 public:
125 constexpr Optional()
126 : m_hasValue{ false }
127 , m_value{}
128 {}
129
130 explicit constexpr Optional(const T &value)
131 : m_hasValue{ true }
132 , m_value{ value }
133 {}
134
135 explicit constexpr Optional(T &&value)
136 : m_hasValue{ true }
137 , m_value{ std::move(value) }
138 {}
139
140 const T &value() const
141 {
142 if(!hasValue())
143 {
144 throw std::runtime_error("value() called on unset optional");
145 }
146 return m_value;
147 }
148
149 bool hasValue() const
150 {
151 return m_hasValue;
152 }
153
154 void reset()
155 {
156 m_hasValue = false;
157 m_value = T{};
158 }
159
160 bool operator==(const Optional<T> &other) const
161 {
162 if(hasValue() != other.hasValue())
163 {
164 return false;
165 }
166 if(!hasValue())
167 {
168 return true;
169 }
170 return value() == other.value();
171 }
172
173 bool operator!=(const Optional<T> &other) const
174 {
175 return !operator==(other);
176 }
177
178 bool operator<(const Optional<T> &other) const
179 {
180 if(!hasValue() && !other.hasValue())
181 {
182 return false;
183 }
184 if(hasValue() && !other.hasValue())
185 {
186 return false;
187 }
188 if(!hasValue() && other.hasValue())
189 {
190 return true;
191 }
192 return m_value < other.m_value;
193 }
194
195 bool operator>(const Optional<T> &other) const
196 {
197 if(!hasValue() && !other.hasValue())
198 {
199 return false;
200 }
201 if(hasValue() && !other.hasValue())
202 {
203 return true;
204 }
205 if(!hasValue() && other.hasValue())
206 {
207 return false;
208 }
209 return m_value > other.m_value;
210 }
211
212 bool operator<=(const Optional<T> &other) const
213 {
214 return (operator==(other) || operator<(other));
215 }
216
217 bool operator>=(const Optional<T> &other) const
218 {
219 return (operator==(other) || operator>(other));
220 }
221
222 private:
223 bool m_hasValue;
224 T m_value;
225 };
226
227 template<typename T>
228 struct Befriend
229 {
230 static void setFromString(T &target, const std::string &value)
231 {
232 target.setFromString(value);
233 }
234
235 static void setFromString(T &target, const std::string &path, const std::string &value)
236 {
237 target.setFromString(path, value);
238 }
239
240 static std::string getString(const T &target, const std::string &path)
241 {
242 return target.getString(path);
243 }
244 };
245
246 template<typename T>
247 void setFromString(T &target, const std::string &value)
248 {
249 Befriend<T>::setFromString(target, value);
250 }
251
252 template<typename T>
253 void setFromString(T &target, const std::string &path, const std::string &value)
254 {
255 Befriend<T>::setFromString(target, path, value);
256 }
257
258 template<typename T>
259 std::string getString(const T &target, const std::string &path)
260 {
261 return Befriend<T>::getString(target, path);
262 }
263 } // namespace Detail
264#endif
265 } // namespace DataModel
266} // namespace Zivid
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:99