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  cthunk_free(_entry);
68  _entry = NULL;
69  }
70 
71  inline CThunk(T *instance, CCallbackSimple callback)
72  {
73  init(instance, (CCallback)callback, NULL);
74  }
75 
76  inline CThunk(T &instance, CCallback callback)
77  {
78  init(instance, callback, NULL);
79  }
80 
81  inline CThunk(T &instance, CCallbackSimple callback)
82  {
83  init(instance, (CCallback)callback, NULL);
84  }
85 
86  inline CThunk(T &instance, CCallback callback, void *context)
87  {
88  init(instance, callback, context);
89  }
90 
91  inline void callback(CCallback callback)
92  {
93  _callback = callback;
94  }
95 
96  inline void callback(CCallbackSimple callback)
97  {
98  _callback_simple = callback;
99  }
100 
101  inline void context(void *context)
102  {
103  _context = context;
104  }
105 
106  inline void context(uint32_t context)
107  {
108  _context = (void *)context;
109  }
110 
111  inline uint32_t entry(void)
112  {
113  if (_entry == NULL) {
114  _entry = cthunk_alloc(this);
115  }
116  return (uint32_t)_entry;
117  }
118 
119  /* get thunk entry point for connecting rhunk to an IRQ table */
120  inline operator CThunkEntry(void)
121  {
122  return (CThunkEntry)entry();
123  }
124 
125  /* get thunk entry point for connecting rhunk to an IRQ table */
126  inline operator uint32_t(void)
127  {
128  return entry();
129  }
130 
131  /* simple test function */
132  inline void call(void)
133  {
134  (((CThunkEntry)(entry()))());
135  }
136 
137 private:
138  T *_instance;
139  void *_context;
140  union {
141  CCallbackSimple _callback_simple;
142  CCallback _callback;
143  };
144 
145  CThunkEntry _entry;
146 
147  static void trampoline(CThunkBase *base)
148  {
149  CThunk<T> *self = static_cast<CThunk<T>*>(base);
150  T *instance = self->_instance;
151  void *context = self->_context;
152  CCallback callback = self->_callback;
153 
154  if (instance && callback) {
155  (instance->*callback)(context);
156  }
157  }
158 
159  inline void init(T *instance, CCallback callback, void *context)
160  {
161  _instance = instance;
162  _context = context;
163  _callback = callback;
164  _trampoline = &trampoline;
165  _entry = 0;
166  }
167 };
168 
169 /**@}*/
170 
171 /**@}*/
172 
173 #endif/*__CTHUNK_H__*/
174 
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.