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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.