CppSerdes  1.0
A serialization/deserialization library designed with embedded systems in mind
bitcpy_sized_pointer.h
Go to the documentation of this file.
1 #ifndef _BITCPY_SIZED_POINTER_H_
8 #define _BITCPY_SIZED_POINTER_H_
9 
10 #include <stdint.h>
11 #include <type_traits>
12 
14 namespace serdes
15 {
18  template <typename T_array>
20  {
22  T_array *const value;
23 
25  const size_t size;
26 
30  sized_pointer(T_array *const v, const size_t N) noexcept : value{v}, size{N * static_cast<size_t>(v != nullptr)} {}
31 
35  template <size_t N>
36  sized_pointer(T_array (&v)[N]) noexcept : value{v}, size{N} {}
37 
39  size_t bit_capacity() const noexcept
40  {
41  return size * sizeof(T_array) * 8u;
42  }
43  };
44 
47  template <>
48  struct sized_pointer<void>
49  {
51  void *const value;
52 
54  const size_t size;
55 
57  const uint_fast8_t element_size;
58 
63  template <typename T>
64  sized_pointer(T *const v, const size_t N) noexcept : value{const_cast<void *const>(reinterpret_cast<volatile const void *const>(v))}, size{N * static_cast<size_t>(v != nullptr)}, element_size{sizeof(T)} {}
65 
70  template <typename T, size_t N>
71  sized_pointer(T (&v)[N]) noexcept : value{const_cast<void *const>(reinterpret_cast<volatile const void *const>(v))}, size{N}, element_size{sizeof(T)}
72  {
73  }
74 
76  size_t bit_capacity() const noexcept
77  {
78  return size * element_size * 8u;
79  }
80  };
81 
82  // implimentation details
83  namespace detail
84  {
85  template <typename>
86  struct is_sized_pointer : std::false_type
87  {
88  };
89 
90  template <typename T>
91  struct is_sized_pointer<sized_pointer<T>> : std::true_type
92  {
93  };
94  }
95 }
96 
97 #endif // _BITCPY_SIZED_POINTER_H_
const size_t size
size of the array
Definition: bitcpy_sized_pointer.h:25
CppSerdes library namespace.
Definition: bitcpy_common.h:69
T_array *const value
the underlying pointer
Definition: bitcpy_sized_pointer.h:22
sized_pointer(T_array(&v)[N]) noexcept
Construct a new sized pointer object from the array itself.
Definition: bitcpy_sized_pointer.h:36
sized_pointer(T *const v, const size_t N) noexcept
Construct a new sized pointer object from a raw pointer and a passed size.
Definition: bitcpy_sized_pointer.h:64
const size_t size
size of the array
Definition: bitcpy_sized_pointer.h:54
size_t bit_capacity() const noexcept
returns the number of bits in the array
Definition: bitcpy_sized_pointer.h:76
const uint_fast8_t element_size
Number of bytes per element of the original array type, same as sizeof(T_original) ...
Definition: bitcpy_sized_pointer.h:57
sized_pointer(T_array *const v, const size_t N) noexcept
Construct a new sized pointer object from a raw pointer and a passed size.
Definition: bitcpy_sized_pointer.h:30
void *const value
the underlying pointer reinterpreted as a void* type
Definition: bitcpy_sized_pointer.h:51
size_t bit_capacity() const noexcept
returns the number of bits in the array
Definition: bitcpy_sized_pointer.h:39
sized_pointer(T(&v)[N]) noexcept
Construct a new sized pointer object from the array itself.
Definition: bitcpy_sized_pointer.h:71
Holds a pointer to an array (with its type information) with a constant size.
Definition: bitcpy_sized_pointer.h:19