8 #ifndef __LIBCAMERA_CONTROLS_H__
9 #define __LIBCAMERA_CONTROLS_H__
14 #include <unordered_map>
17 #include <libcamera/span.h>
21 class ControlValidator;
42 struct control_type<void> {
47 struct control_type<bool> {
52 struct control_type<uint8_t> {
57 struct control_type<int32_t> {
62 struct control_type<int64_t> {
67 struct control_type<float> {
72 struct control_type<std::string> {
77 struct control_type<Rectangle> {
78 static constexpr
ControlType value = ControlTypeRectangle;
82 struct control_type<Size> {
83 static constexpr
ControlType value = ControlTypeSize;
86 template<
typename T, std::
size_t N>
87 struct control_type<Span<T, N>> :
public control_type<std::remove_cv_t<T>> {
98 template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
99 !std::is_same<std::string, std::remove_cv_t<T>>::value,
100 std::nullptr_t> =
nullptr>
104 set(details::control_type<std::remove_cv_t<T>>::value,
false,
105 &value, 1,
sizeof(T));
108 template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
109 std::is_same<std::string, std::remove_cv_t<T>>::value,
110 std::nullptr_t> =
nullptr>
117 set(details::control_type<std::remove_cv_t<T>>::value,
true,
118 value.data(), value.size(),
sizeof(
typename T::value_type));
130 Span<const uint8_t>
data()
const;
131 Span<uint8_t>
data();
138 return !(*
this == other);
142 template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
143 !std::is_same<std::string, std::remove_cv_t<T>>::value,
144 std::nullptr_t> =
nullptr>
147 assert(type_ == details::control_type<std::remove_cv_t<T>>::value);
150 return *
reinterpret_cast<const T *
>(
data().data());
153 template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
154 std::is_same<std::string, std::remove_cv_t<T>>::value,
155 std::nullptr_t> =
nullptr>
161 assert(type_ == details::control_type<std::remove_cv_t<T>>::value);
164 using V =
typename T::value_type;
165 const V *value =
reinterpret_cast<const V *
>(
data().data());
166 return { value, numElements_ };
170 template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
171 !std::is_same<std::string, std::remove_cv_t<T>>::value,
172 std::nullptr_t> =
nullptr>
173 void set(
const T &value)
175 set(details::control_type<std::remove_cv_t<T>>::value,
false,
176 reinterpret_cast<const void *
>(&value), 1,
sizeof(T));
179 template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
180 std::is_same<std::string, std::remove_cv_t<T>>::value,
181 std::nullptr_t> =
nullptr>
187 set(details::control_type<std::remove_cv_t<T>>::value,
true,
188 value.data(), value.size(),
sizeof(
typename T::value_type));
197 std::size_t numElements_ : 32;
216 unsigned int id()
const {
return id_; }
217 const std::string &
name()
const {
return name_; }
229 static inline bool operator==(
unsigned int lhs,
const ControlId &rhs)
231 return lhs == rhs.id();
234 static inline bool operator!=(
unsigned int lhs,
const ControlId &rhs)
236 return !(lhs == rhs);
239 static inline bool operator==(
const ControlId &lhs,
unsigned int rhs)
241 return lhs.id() == rhs;
244 static inline bool operator!=(
const ControlId &lhs,
unsigned int rhs)
246 return !(lhs == rhs);
256 :
ControlId(
id,
name, details::control_type<std::remove_cv_t<T>>::value)
280 return min_ == other.min_ && max_ == other.max_;
285 return !(*
this == other);
294 using ControlIdMap = std::unordered_map<unsigned int, const ControlId *>;
296 class ControlInfoMap :
private std::unordered_map<const ControlId *, ControlInfo>
299 using Map = std::unordered_map<const ControlId *, ControlInfo>;
311 using Map::mapped_type;
312 using Map::value_type;
313 using Map::size_type;
315 using Map::const_iterator;
327 mapped_type &
at(
unsigned int key);
328 const mapped_type &
at(
unsigned int key)
const;
329 size_type
count(
unsigned int key)
const;
330 iterator
find(
unsigned int key);
331 const_iterator
find(
unsigned int key)
const;
336 void generateIdmap();
344 using ControlListMap = std::unordered_map<unsigned int, ControlValue>;
359 bool empty()
const {
return controls_.empty(); }
360 std::size_t
size()
const {
return controls_.size(); }
364 bool contains(
unsigned int id)
const;
373 return val->
get<T>();
376 template<
typename T,
typename V>
386 template<
typename T,
typename V>
393 val->
set<T>(Span<const typename std::remove_cv_t<V>>{ value.begin(), value.size() });
409 ControlListMap controls_;
Control static metadata.
Definition: controls.h:209
ControlId(unsigned int id, const std::string &name, ControlType type)
Construct a ControlId instance.
Definition: controls.h:211
ControlType type() const
Retrieve the control data type.
Definition: controls.h:218
const std::string & name() const
Retrieve the control name.
Definition: controls.h:217
unsigned int id() const
Retrieve the control numerical ID.
Definition: controls.h:216
A map of ControlId to ControlInfo.
Definition: controls.h:297
mapped_type & at(unsigned int key)
Access specified element by numerical ID.
Definition: controls.cpp:645
const ControlIdMap & idmap() const
Retrieve the ControlId map.
Definition: controls.h:333
ControlInfoMap & operator=(const ControlInfoMap &other)=default
Copy assignment operator, replace the contents with a copy of other.
std::unordered_map< const ControlId *, ControlInfo > Map
The base std::unsorted_map<> container.
Definition: controls.h:299
iterator find(unsigned int key)
Find the element matching a numerical ID.
Definition: controls.cpp:681
ControlInfoMap(const ControlInfoMap &other)=default
Copy constructor, construct a ControlInfoMap from a copy of other.
size_type count(unsigned int key) const
Count the number of elements matching a numerical ID.
Definition: controls.cpp:665
Describe the limits of valid values for a Control.
Definition: controls.h:266
ControlInfo(const ControlValue &min=0, const ControlValue &max=0, const ControlValue &def=0)
Construct a ControlInfo with minimum and maximum range parameters.
Definition: controls.cpp:487
std::string toString() const
Provide a string representation of the ControlInfo.
Definition: controls.cpp:525
bool operator==(const ControlInfo &other) const
Compare ControlInfo instances for equality.
Definition: controls.h:278
bool operator!=(const ControlInfo &other) const
Compare ControlInfo instances for non equality.
Definition: controls.h:283
const ControlValue & max() const
Retrieve the maximum value of the control.
Definition: controls.h:273
const ControlValue & def() const
Retrieve the default value of the control.
Definition: controls.h:274
const ControlValue & min() const
Retrieve the minimum value of the control.
Definition: controls.h:272
Associate a list of ControlId with their values for an object.
Definition: controls.h:342
void clear()
Removes all controls from the list.
Definition: controls.h:361
const ControlInfoMap * infoMap() const
Retrieve the ControlInfoMap used to construct the ControlList.
Definition: controls.h:399
ControlListMap::const_iterator const_iterator
Const iterator for the controls contained within the list.
Definition: controls.h:352
iterator end()
Retrieve an iterator pointing to the past-the-end control in the list.
Definition: controls.h:355
bool contains(const ControlId &id) const
Check if the list contains a control with the specified id.
Definition: controls.cpp:850
void set(const Control< T > &ctrl, const V &value)
Set the control ctrl value to value.
Definition: controls.h:377
std::size_t size() const
Retrieve the number of controls in the list.
Definition: controls.h:360
ControlListMap::iterator iterator
Iterator for the controls contained within the list.
Definition: controls.h:351
bool empty() const
Identify if the list is empty.
Definition: controls.h:359
void set(const Control< T > &ctrl, const std::initializer_list< V > &value)
Set the control ctrl value to value.
Definition: controls.h:387
T get(const Control< T > &ctrl) const
Get the value of control ctrl.
Definition: controls.h:367
iterator begin()
Retrieve an iterator to the first Control in the list.
Definition: controls.h:354
const_iterator begin() const
Retrieve a const_iterator to the first Control in the list.
Definition: controls.h:356
ControlList()
Construct a ControlList not associated with any object.
Definition: controls.cpp:762
const_iterator end() const
Retrieve a const iterator pointing to the past-the-end control in the list.
Definition: controls.h:357
Interface for the control validator.
Definition: control_validator.h:17
Abstract type representing the value of a control.
Definition: controls.h:93
T get() const
Get the control value.
Definition: controls.h:159
bool isArray() const
Determine if the value stores an array.
Definition: controls.h:128
ControlValue & operator=(const ControlValue &other)
Replace the content of the ControlValue with a copy of the content of other.
Definition: controls.cpp:145
void reserve(ControlType type, bool isArray=false, std::size_t numElements=1)
Set the control type and reserve memory.
Definition: controls.cpp:354
bool operator==(const ControlValue &other) const
Compare ControlValue instances for equality.
Definition: controls.cpp:278
void set(const T &value)
Set the control value to value.
Definition: controls.h:185
ControlValue()
Construct an empty ControlValue.
Definition: controls.cpp:97
bool operator!=(const ControlValue &other) const
Compare ControlValue instances for non equality.
Definition: controls.h:136
ControlValue(const T &value)
Construct a ControlValue of type T.
Definition: controls.h:114
ControlType type() const
Retrieve the data type of the value.
Definition: controls.h:126
std::string toString() const
Assemble and return a string describing the value.
Definition: controls.cpp:207
bool isNone() const
Determine if the value is not initialised.
Definition: controls.h:127
std::size_t numElements() const
Retrieve the number of elements stored in the ControlValue.
Definition: controls.h:129
Span< const uint8_t > data() const
Retrieve the raw data of a control value.
Definition: controls.cpp:185
Describe a control and its intrinsic properties.
Definition: controls.h:251
T type
The Control template type T.
Definition: controls.h:253
Control(unsigned int id, const char *name)
Construct a Control instance.
Definition: controls.h:255
ControlType
Define the data type of a Control.
Definition: controls.h:23
@ ControlTypeNone
Definition: controls.h:24
@ ControlTypeFloat
Definition: controls.h:29
@ ControlTypeBool
Definition: controls.h:25
@ ControlTypeInteger32
Definition: controls.h:27
@ ControlTypeString
Definition: controls.h:30
@ ControlTypeInteger64
Definition: controls.h:28
@ ControlTypeByte
Definition: controls.h:26
std::unordered_map< unsigned int, const ControlId * > ControlIdMap
A map of numerical control ID to ControlId.
Definition: controls.h:294
Data structures related to geometric objects.