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