Developer World
Spresense SDK Library v3.2.0-ebc0364
type_holder.h
1/****************************************************************************
2 * modules/include/memutils/message/type_holder.h
3 *
4 * Copyright 2018 Sony Semiconductor Solutions Corporation
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17 * the names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36#ifndef TYPE_HOLDER_H_INCLUDED
37#define TYPE_HOLDER_H_INCLUDED
38
39#include <stdio.h> /* printf, size_t */
40#include "memutils/common_utils/common_types.h"
41#include "memutils/common_utils/common_assert.h"
42#include "memutils/message/AssertInfo.h"
43
44/* Returns the class identifier corresponding to type. */
45
46#define GET_TYPE_ID(type) (&TypeHolder<type>::type_size)
47
48template<typename T> class TypeHolder;
49
50/*****************************************************************
51 * Base class for processing TypeHolder<T> class in common
52 *****************************************************************/
54public:
55 typedef size_t (*id_t)(); /* Type identifier. */
56
57 virtual ~TypeHolderBase() {}
58
59 template<typename T>
60 bool is_type() const { return this->id() == GET_TYPE_ID(T); }
61
62 template<typename T>
63 T& get() {
64 D_ASSERT2(this->is_type<T>(),
65 AssertParamLog(AssertIdTypeUnmatch, (uint32_t)this->id(), (uint32_t)GET_TYPE_ID(T)));
66 return static_cast<TypeHolder<T>*>(this)->get();
67 }
68
69 template<typename T>
70 const T& get() const {
71 D_ASSERT2(this->is_type<T>(),
72 AssertParamLog(AssertIdTypeUnmatch, (uint32_t)this->id(), (uint32_t)GET_TYPE_ID(T)));
73 return static_cast<const TypeHolder<T>*>(this)->get();
74 }
75
76 template<typename T>
77 T& get_any(bool size_check = true) {
78 if (size_check) {
79 D_ASSERT2(sizeof(T) <= this->size(),
80 AssertParamLog(AssertIdSizeError, sizeof(T), this->size()));
81 }
82 return static_cast<TypeHolder<T>*>(this)->get();
83 }
84
85 template<typename T>
86 const T& get_any(bool size_check = true) const {
87 if (size_check) {
88 D_ASSERT2(sizeof(T) <= this->size(),
89 AssertParamLog(AssertIdSizeError, sizeof(T), this->size()));
90 }
91 return static_cast<const TypeHolder<T>*>(this)->get();
92 }
93
94 size_t size() const { return this->id()(); }
95
96 void dump() const {
97 size_t sz = this->size();
98 const unsigned char* data = &get_any<unsigned char>();
99 printf("size:%u, id:%p, data: ", sz, this->id());
100 for (size_t i = 0; i < MIN(sz, 16); ++i) {
101 printf("%02x ", data[i]);
102 }
103 printf("\n");
104 }
105
106 virtual id_t id() const = 0;
107
108#ifdef USE_TYPE_HOLDER_ACTION_API
109 /* Subclass of TypeHolder class, overriding it if necessary. */
110
111 virtual void* action(void*) { return 0; }
112 virtual void* action(void*) const { return 0; }
113#endif
114}; /* TypeBase */
115
116/*****************************************************************
117 * Class that holds an instance of an arbitrary type and type information
118 * To uniquely identify the storage type,
119 * the template argument must be of type only
120 *****************************************************************/
121template<typename T>
123public:
124 /* A unique function is generated for each class. */
125
126 static size_t type_size() { return sizeof(T); }
127
128 TypeHolder() : m_held() {}
129 explicit TypeHolder(const T& data) : m_held(data) {}
130
131 T& get() { return m_held; }
132 const T& get() const { return m_held; }
133
134 /* Use the address of a unique function for each class
135 * for class identification.
136 */
137
138 virtual id_t id() const { return &type_size; }
139
140private:
141 T m_held;
142}; /* TypeHolder */
143
144#endif /* TYPE_HOLDER_H_INCLUDED */
Definition: type_holder.h:53
Definition: type_holder.h:122
Definition: AssertInfo.h:210