Fixposition SDK 0.0.0-heads/main-0-gfaec355
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: Common types and type helpers
12 *
13 * @page FPSDK_COMMON_TYPES Common types and type helpers
14 *
15 * **API**: fpsdk_common/types.hpp and fpsdk::common::types
16 *
17 */
18#ifndef __FPSDK_COMMON_TYPES_HPP__
19#define __FPSDK_COMMON_TYPES_HPP__
20
21/* LIBC/STL */
22#include <array>
23#include <cstdint>
24#include <type_traits>
25
26/* EXTERNAL */
27
28/* PACKAGE */
29
30namespace fpsdk {
31namespace common {
32/**
33 * @brief Common types
34 */
35namespace types {
36/* ****************************************************************************************************************** */
37
38/**
39 * @brief Convert enum class constant to the underlying integral type value
40 *
41 * @tparam T The enum class type
42 * @param[in] enum_val The enum constant
43 *
44 * @returns the integral value of the enum constant as the value of the underlying type
45 */
46template <typename T, typename = typename std::enable_if<std::is_enum<T>::value, T>::type>
47constexpr typename std::underlying_type<T>::type EnumToVal(T enum_val)
48{
49 return static_cast<typename std::underlying_type<T>::type>(enum_val);
50}
51
52/**
53 * @brief Get number of elements in array variable
54 *
55 * @tparam T Array type
56 * @param[in] arr The array
57 *
58 * @returns the number of elements in arr
59 *
60 * @code{cpp}
61 * std::array<int, 5> a5;
62 * const std::size_t n6 = NumOf(a6); // = 5, same as a.size(), but works compile-time
63 * int a3[3];
64 * const std::size_t n3 = NumOf(a3); // = 3, works compile-time
65 * @endcode
66 */
67template <typename T>
68constexpr std::size_t NumOf(const T& arr)
69{
70 return sizeof(arr) / sizeof(arr[0]);
71}
72
73/**
74 * @brief Get number of elements in array type
75 *
76 * @tparam T Array type
77 *
78 * @returns the number of elements in the array
79 *
80 * @code{cpp}
81 * using Arr5 = std::array<int, 5>;
82 * const std::size_t n6 = NumOf<Arr5>(); // = 5, works compile-time
83 * @endcode
84 */
85template <typename T>
86constexpr std::size_t NumOf()
87{
88 return std::tuple_size<T>{};
89}
90
91/**
92 * @brief Base class to prevent copy or move
93 *
94 * Other classes can be derived from this to prevent copying of moving instances.
95 */
96class NoCopyNoMove // clang-format off
97{
98 public:
99 NoCopyNoMove() { }
100 ~NoCopyNoMove() { }
101 protected:
102 NoCopyNoMove& operator=(const NoCopyNoMove&) = delete;
103 NoCopyNoMove(const NoCopyNoMove&) = delete;
104 NoCopyNoMove(NoCopyNoMove&&) = delete;
105 NoCopyNoMove& operator=(NoCopyNoMove&&) = delete;
106}; // clang-format on
107
108//! Mark variable as unused to silence compiler warnings \hideinitializer
109#ifndef UNUSED
110# define UNUSED(thing) (void)thing
111#endif
112
113//! Preprocessor stringification \hideinitializer
114#ifndef STRINGIFY
115# define STRINGIFY(x) _STRINGIFY(x)
116#endif
117
118//! Preprocessor concatenation \hideinitializer
119#ifndef CONCAT
120# define CONCAT(a, b) _CONCAT(a, b)
121#endif
122
123//! Size of struct member
124#ifndef SIZEOF_FIELD
125# define SIZEOF_FIELD(_type_, _member_) sizeof((((_type_*)NULL)->_member_))
126#endif
127
128// Helpers, which we don't want to document in Doxygen, for some of the above
129#ifndef _DOXYGEN_
130# define _STRINGIFY(x) #x
131# define _CONCAT(a, b) a##b
132#endif
133
134/* ****************************************************************************************************************** */
135} // namespace types
136} // namespace common
137} // namespace fpsdk
138#endif // __FPSDK_COMMON_TYPES_HPP__
Base class to prevent copy or move.
Definition types.hpp:97
constexpr std::size_t NumOf()
Get number of elements in array type.
Definition types.hpp:86
constexpr std::underlying_type< T >::type EnumToVal(T enum_val)
Convert enum class constant to the underlying integral type value.
Definition types.hpp:47
Fixposition SDK.