Zivid C++ API 2.16.0+46cdaba6-1
Array1D.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of the Zivid API
3 *
4 * Copyright 2015-2025 (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
47
48#include <memory>
49#include <string>
50
51namespace Zivid
52{
53
54#ifndef NO_DOC
56 namespace Detail
57 {
58 ZIVID_UTILS_EXPORT std::string array1DToString(std::size_t size);
59 } // namespace Detail
60#endif
61
70 template<typename DataFormat>
71 class Array1D
72 {
73 public:
75 using ValueType = DataFormat;
76
80 using ConstIterator = const DataFormat *;
81
84 : m_size{ 0 }
85 {}
86
88 size_t size() const
89 {
90 return m_size;
91 }
92
94 bool isEmpty() const
95 {
96 return size() == 0;
97 }
98
101 const DataFormat *data() const
102 {
103 return m_data.get();
104 }
105
112 const DataFormat &operator()(size_t idx) const
113 {
114#ifdef __clang__
115# if __has_warning("-Wunsafe-buffer-usage")
116# pragma clang diagnostic push
117# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
118# endif
119#endif
120 return *(data() + idx);
121#ifdef __clang__
122# if __has_warning("-Wunsafe-buffer-usage")
123# pragma clang diagnostic pop
124# endif
125#endif
126 }
127
130 {
131 return data();
132 }
133
136 {
137#ifdef __clang__
138# if __has_warning("-Wunsafe-buffer-usage")
139# pragma clang diagnostic push
140# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
141# endif
142#endif
143 return data() + size();
144#ifdef __clang__
145# if __has_warning("-Wunsafe-buffer-usage")
146# pragma clang diagnostic pop
147# endif
148#endif
149 }
150
153 {
154 return begin();
155 }
156
159 {
160 return end();
161 }
162
164 std::string toString() const
165 {
166 return Detail::array1DToString(size());
167 }
168
169 private:
170 friend class Array1DFactory;
171
172 Array1D(std::size_t size, std::unique_ptr<DataFormat[]> data)
173 : m_size{ size }
174 , m_data{ std::move(data) }
175 {}
176
177 size_t m_size;
178 std::shared_ptr<const DataFormat[]> m_data;
179 };
180
182 template<typename T>
183 std::ostream &operator<<(std::ostream &stream, const Array1D<T> &array)
184 {
185 return stream << array.toString();
186 }
187
189 template<typename T>
191 {
192 return array.cbegin();
193 }
194
196 template<typename T>
198 {
199 return array.begin();
200 }
201
203 template<typename T>
205 {
206 return array.cend();
207 }
208
210 template<typename T>
212 {
213 return array.end();
214 }
215} // namespace Zivid
#define ZIVID_UTILS_EXPORT
Definition UtilsExport.h:56
One-dimensional container of data.
Definition Array1D.h:72
const DataFormat & operator()(size_t idx) const
Constant reference to an element given by an index.
Definition Array1D.h:112
const DataFormat * data() const
Pointer to the first data element of the array.
Definition Array1D.h:101
const DataFormat * ConstIterator
The iterator type for immutable access. It iterates over individual Array1D elements.
Definition Array1D.h:80
ConstIterator end() const
Iterator to the end of the array.
Definition Array1D.h:135
DataFormat ValueType
The type of the elements stored in the Array.
Definition Array1D.h:75
ConstIterator cbegin() const
Iterator to the beginning of the array.
Definition Array1D.h:152
size_t size() const
Get the number of elements in the array.
Definition Array1D.h:88
std::string toString() const
Get array information as string.
Definition Array1D.h:164
bool isEmpty() const
Check if the array is empty.
Definition Array1D.h:94
ConstIterator cend() const
Iterator to the end of the array.
Definition Array1D.h:158
ConstIterator begin() const
Iterator to the beginning of the array.
Definition Array1D.h:129
friend class Array1DFactory
Definition Array1D.h:170
Array1D()
Create an empty Array1D.
Definition Array1D.h:83
The main Zivid namespace. All Zivid code is found here.
Definition Application.h:84
Array1D< T >::ConstIterator end(const Array1D< T > &array)
Iterator to the end of the array.
Definition Array1D.h:211
Array1D< T >::ConstIterator cend(const Array1D< T > &array)
Iterator to the end of the array.
Definition Array1D.h:204
Array1D< T >::ConstIterator begin(const Array1D< T > &array)
Iterator to the beginning of the array.
Definition Array1D.h:197
Array1D< T >::ConstIterator cbegin(const Array1D< T > &array)
Iterator to the beginning of the array.
Definition Array1D.h:190
std::ostream & operator<<(std::ostream &stream, const Array1D< T > &array)
Serialize array information to a stream.
Definition Array1D.h:183