Zivid C++ API  2.3.1+1a22cbf1-1
Defining the Future of 3D Machine Vision
Detail.h
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * This file is part of the Zivid 3D Camera API
4  *
5  * Copyright 2015-2022 (C) Zivid AS
6  * All rights reserved.
7  *
8  * Zivid Software License, v1.0
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of Zivid AS nor the names of its contributors may be used
21  * to endorse or promote products derived from this software without specific
22  * prior written permission.
23  *
24  * 4. This software, with or without modification, must not be used with any
25  * other 3D camera than from Zivid AS.
26  *
27  * 5. Any software provided in binary form under this license must not be
28  * reverse engineered, decompiled, modified and/or disassembled.
29  *
30  * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
31  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
34  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
42  * Info: http://www.zivid.com
43  ******************************************************************************/
44 
45 #pragma once
46 
47 #include <type_traits>
48 #include <utility>
49 
50 namespace 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(std::forward<DataModel>(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()) return false;
118  if(!hasValue()) return true;
119  return value() == other.value();
120  }
121 
122  bool operator!=(const Optional<T> &other) const
123  {
124  return !operator==(other);
125  }
126 
127  bool operator<(const Optional<T> &other) const
128  {
129  if(!hasValue() && !other.hasValue()) return false;
130  if(hasValue() && !other.hasValue()) return false;
131  if(!hasValue() && other.hasValue()) return true;
132  return m_value < other.m_value;
133  }
134 
135  bool operator>(const Optional<T> &other) const
136  {
137  if(!hasValue() && !other.hasValue()) return false;
138  if(hasValue() && !other.hasValue()) return true;
139  if(!hasValue() && other.hasValue()) return false;
140  return m_value > other.m_value;
141  }
142 
143  private:
144  bool m_hasValue;
145  T m_value;
146  };
147 
148  template<typename T>
149  struct Befriend
150  {
151  static void setFromString(T &target, const std::string &value)
152  {
153  target.setFromString(value);
154  }
155 
156  static void setFromString(T &target, const std::string &path, const std::string &value)
157  {
158  target.setFromString(path, value);
159  }
160 
161  static std::string getString(const T &target, const std::string &path)
162  {
163  return target.getString(path);
164  }
165  };
166 
167  template<typename T>
168  void setFromString(T &target, const std::string &value)
169  {
170  Befriend<T>::setFromString(target, value);
171  }
172 
173  template<typename T>
174  void setFromString(T &target, const std::string &path, const std::string &value)
175  {
176  Befriend<T>::setFromString(target, path, value);
177  }
178 
179  template<typename T>
180  std::string getString(const T &target, const std::string &path)
181  {
182  return Befriend<T>::getString(target, path);
183  }
184  } // namespace Detail
185 #endif
186  } // namespace DataModel
187 } // namespace Zivid
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:55