Mbed OS Reference
Loading...
Searching...
No Matches
CThunk.h
1/* Copyright (c) 2014-2019 ARM Limited
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18/** \addtogroup platform-public-api */
19/** @{*/
20
21/**
22 * \defgroup platform_CThunk CThunk class
23 * @{
24 */
25
26/* General C++ Object Thunking class
27 *
28 * - allows direct callbacks to non-static C++ class functions
29 * - keeps track for the corresponding class instance
30 * - supports an optional context parameter for the called function
31 * - ideally suited for class object receiving interrupts (NVIC_SetVector)
32 */
33
34#ifndef __CTHUNK_H__
35#define __CTHUNK_H__
36
37#include "platform/internal/CThunkBase.h"
38
39/**
40 * Class for created a pointer with data bound to it
41 *
42 * @note Synchronization level: Not protected
43 */
44template<class T>
45class CThunk: private CThunkBase {
46public:
47 typedef void (T::*CCallbackSimple)(void);
48 typedef void (T::*CCallback)(void *context);
49
50 inline CThunk(T *instance)
51 {
52 init(instance, NULL, NULL);
53 }
54
55 inline CThunk(T *instance, CCallback callback)
56 {
57 init(instance, callback, NULL);
58 }
59
60 ~CThunk()
61 {
62 if (_entry != NULL) {
63 cthunk_free(_entry);
64 _entry = NULL;
65 }
66 }
67
68 inline CThunk(T *instance, CCallbackSimple callback)
69 {
70 init(instance, (CCallback)callback, NULL);
71 }
72
73 inline CThunk(T &instance, CCallback callback)
74 {
75 init(instance, callback, NULL);
76 }
77
78 inline CThunk(T &instance, CCallbackSimple callback)
79 {
80 init(instance, (CCallback)callback, NULL);
81 }
82
83 inline CThunk(T &instance, CCallback callback, void *context)
84 {
85 init(instance, callback, context);
86 }
87
88 inline void callback(CCallback callback)
89 {
90 _callback = callback;
91 }
92
93 inline void callback(CCallbackSimple callback)
94 {
95 _callback_simple = callback;
96 }
97
98 inline void context(void *context)
99 {
100 _context = context;
101 }
102
103 inline void context(uint32_t context)
104 {
105 _context = (void *)context;
106 }
107
108 inline uint32_t entry(void)
109 {
110 if (_entry == NULL) {
111 _entry = cthunk_alloc(this);
112 }
113 return (uint32_t)_entry;
114 }
115
116 /* get thunk entry point for connecting rhunk to an IRQ table */
117 inline operator CThunkEntry(void)
118 {
119 return (CThunkEntry)entry();
120 }
121
122 /* get thunk entry point for connecting rhunk to an IRQ table */
123 inline operator uint32_t(void)
124 {
125 return entry();
126 }
127
128 /* simple test function */
129 inline void call(void)
130 {
131 (((CThunkEntry)(entry()))());
132 }
133
134private:
135 T *_instance;
136 void *_context;
137 union {
138 CCallbackSimple _callback_simple;
139 CCallback _callback;
140 };
141
142 CThunkEntry _entry;
143
144 static void trampoline(CThunkBase *base)
145 {
146 CThunk<T> *self = static_cast<CThunk<T>*>(base);
147 T *instance = self->_instance;
148 void *context = self->_context;
149 CCallback callback = self->_callback;
150
151 if (instance && callback) {
152 (instance->*callback)(context);
153 }
154 }
155
156 inline void init(T *instance, CCallback callback, void *context)
157 {
158 _instance = instance;
159 _context = context;
160 _callback = callback;
161 _trampoline = &trampoline;
162 _entry = 0;
163 }
164};
165
166/**@}*/
167
168/**@}*/
169
170#endif/*__CTHUNK_H__*/
171
Class for created a pointer with data bound to it.
Definition: CThunk.h:45