Skip to content

Commit 43d4445

Browse files
author
Bogdan Marinescu
committed
Interrupt chaining: preliminary version
1 parent 1e8e509 commit 43d4445

File tree

18 files changed

+1290
-511
lines changed

18 files changed

+1290
-511
lines changed

libraries/mbed/api/CallChain.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef MBED_CALLCHAIN_H
2+
#define MBED_CALLCHAIN_H
3+
4+
#include "FunctionPointer.h"
5+
#include <string.h>
6+
7+
namespace mbed {
8+
9+
typedef FunctionPointer* pFunctionPointer_t;
10+
11+
class CallChain {
12+
public:
13+
CallChain(int size = 4);
14+
~CallChain();
15+
16+
pFunctionPointer_t add(void (*function)(void));
17+
template<typename T>
18+
pFunctionPointer_t add(T *object, void (T::*member)(void)) {
19+
return common_add(new FunctionPointer(object, member));
20+
}
21+
22+
pFunctionPointer_t add_front(void (*function)(void));
23+
template<typename T>
24+
pFunctionPointer_t add_front(T *object, void (T::*member)(void)) {
25+
return common_add_front(new FunctionPointer(object, member));
26+
}
27+
28+
int size() const;
29+
pFunctionPointer_t get(int i) const;
30+
int find(pFunctionPointer_t f) const;
31+
void clear();
32+
bool remove(pFunctionPointer_t f);
33+
void call();
34+
35+
#ifdef MBED_OPERATORS
36+
void operator ()(void) {
37+
call();
38+
}
39+
pFunctionPointer_t operator [](int i) const {
40+
return get(i);
41+
}
42+
#endif
43+
44+
private:
45+
void _check_size();
46+
pFunctionPointer_t common_add(pFunctionPointer_t pf);
47+
pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
48+
49+
pFunctionPointer_t* _chain;
50+
int _size;
51+
int _elements;
52+
};
53+
54+
} // namespace mbed
55+
56+
#endif
57+

libraries/mbed/api/FunctionPointer.h

Lines changed: 94 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,94 @@
1-
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2013 ARM Limited
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-
#ifndef MBED_FUNCTIONPOINTER_H
17-
#define MBED_FUNCTIONPOINTER_H
18-
19-
#include <string.h>
20-
21-
namespace mbed {
22-
23-
/** A class for storing and calling a pointer to a static or member void function
24-
*/
25-
class FunctionPointer {
26-
public:
27-
28-
/** Create a FunctionPointer, attaching a static function
29-
*
30-
* @param function The void static function to attach (default is none)
31-
*/
32-
FunctionPointer(void (*function)(void) = 0);
33-
34-
/** Create a FunctionPointer, attaching a member function
35-
*
36-
* @param object The object pointer to invoke the member function on (i.e. the this pointer)
37-
* @param function The address of the void member function to attach
38-
*/
39-
template<typename T>
40-
FunctionPointer(T *object, void (T::*member)(void)) {
41-
attach(object, member);
42-
}
43-
44-
/** Attach a static function
45-
*
46-
* @param function The void static function to attach (default is none)
47-
*/
48-
void attach(void (*function)(void) = 0);
49-
50-
/** Attach a member function
51-
*
52-
* @param object The object pointer to invoke the member function on (i.e. the this pointer)
53-
* @param function The address of the void member function to attach
54-
*/
55-
template<typename T>
56-
void attach(T *object, void (T::*member)(void)) {
57-
_object = static_cast<void*>(object);
58-
memcpy(_member, (char*)&member, sizeof(member));
59-
_membercaller = &FunctionPointer::membercaller<T>;
60-
_function = 0;
61-
}
62-
63-
/** Call the attached static or member function
64-
*/
65-
void call();
66-
67-
private:
68-
template<typename T>
69-
static void membercaller(void *object, char *member) {
70-
T* o = static_cast<T*>(object);
71-
void (T::*m)(void);
72-
memcpy((char*)&m, member, sizeof(m));
73-
(o->*m)();
74-
}
75-
76-
void (*_function)(void); // static function pointer - 0 if none attached
77-
void *_object; // object this pointer - 0 if none attached
78-
char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
79-
void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
80-
};
81-
82-
} // namespace mbed
83-
84-
#endif
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
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+
#ifndef MBED_FUNCTIONPOINTER_H
17+
#define MBED_FUNCTIONPOINTER_H
18+
19+
#include <string.h>
20+
21+
namespace mbed {
22+
23+
typedef void (*pvoidf_t)(void);
24+
25+
/** A class for storing and calling a pointer to a static or member void function
26+
*/
27+
class FunctionPointer {
28+
public:
29+
30+
/** Create a FunctionPointer, attaching a static function
31+
*
32+
* @param function The void static function to attach (default is none)
33+
*/
34+
FunctionPointer(void (*function)(void) = 0);
35+
36+
/** Create a FunctionPointer, attaching a member function
37+
*
38+
* @param object The object pointer to invoke the member function on (i.e. the this pointer)
39+
* @param function The address of the void member function to attach
40+
*/
41+
template<typename T>
42+
FunctionPointer(T *object, void (T::*member)(void)) {
43+
attach(object, member);
44+
}
45+
46+
/** Attach a static function
47+
*
48+
* @param function The void static function to attach (default is none)
49+
*/
50+
void attach(void (*function)(void) = 0);
51+
52+
/** Attach a member function
53+
*
54+
* @param object The object pointer to invoke the member function on (i.e. the this pointer)
55+
* @param function The address of the void member function to attach
56+
*/
57+
template<typename T>
58+
void attach(T *object, void (T::*member)(void)) {
59+
_object = static_cast<void*>(object);
60+
memcpy(_member, (char*)&member, sizeof(member));
61+
_membercaller = &FunctionPointer::membercaller<T>;
62+
_function = 0;
63+
}
64+
65+
/** Call the attached static or member function
66+
*/
67+
void call();
68+
69+
pvoidf_t get_function() const {
70+
return (pvoidf_t)_function;
71+
}
72+
73+
#ifdef MBED_OPERATORS
74+
void operator ()(void);
75+
#endif
76+
77+
private:
78+
template<typename T>
79+
static void membercaller(void *object, char *member) {
80+
T* o = static_cast<T*>(object);
81+
void (T::*m)(void);
82+
memcpy((char*)&m, member, sizeof(m));
83+
(o->*m)();
84+
}
85+
86+
void (*_function)(void); // static function pointer - 0 if none attached
87+
void *_object; // object this pointer - 0 if none attached
88+
char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
89+
void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
90+
};
91+
92+
} // namespace mbed
93+
94+
#endif

0 commit comments

Comments
 (0)