strat des robots

Fork of CRAC-Strat_2017 by CRAC Team

Committer:
ClementBreteau
Date:
Fri May 19 17:14:07 2017 +0000
Revision:
17:d1594579eec6
Parent:
0:ad97421fb1fb
strat du robot, 19-05-2017, 19h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antbig 0:ad97421fb1fb 1 /* mbed Microcontroller Library - FunctionPointer
antbig 0:ad97421fb1fb 2 * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
antbig 0:ad97421fb1fb 3 */
antbig 0:ad97421fb1fb 4
antbig 0:ad97421fb1fb 5 #ifndef MBED_FUNCTIONPOINTER_H
antbig 0:ad97421fb1fb 6 #define MBED_FUNCTIONPOINTER_H
antbig 0:ad97421fb1fb 7
antbig 0:ad97421fb1fb 8 #include <string.h>
antbig 0:ad97421fb1fb 9
antbig 0:ad97421fb1fb 10 namespace mbed {
antbig 0:ad97421fb1fb 11
antbig 0:ad97421fb1fb 12 /* Class FunctionPointer
antbig 0:ad97421fb1fb 13 * A class for storing and calling a pointer to a static or member void function
antbig 0:ad97421fb1fb 14 */
antbig 0:ad97421fb1fb 15 class FunctionPointer {
antbig 0:ad97421fb1fb 16
antbig 0:ad97421fb1fb 17 public:
antbig 0:ad97421fb1fb 18
antbig 0:ad97421fb1fb 19 /* Constructor FunctionPointer
antbig 0:ad97421fb1fb 20 * Create a FunctionPointer, attaching a static function
antbig 0:ad97421fb1fb 21 *
antbig 0:ad97421fb1fb 22 * Variables
antbig 0:ad97421fb1fb 23 * function - The void static function to attach (default is none)
antbig 0:ad97421fb1fb 24 */
antbig 0:ad97421fb1fb 25 FunctionPointer(void (*function)(void) = 0);
antbig 0:ad97421fb1fb 26
antbig 0:ad97421fb1fb 27 /* Constructor FunctionPointer
antbig 0:ad97421fb1fb 28 * Create a FunctionPointer, attaching a member function
antbig 0:ad97421fb1fb 29 *
antbig 0:ad97421fb1fb 30 * Variables
antbig 0:ad97421fb1fb 31 * object - The object pointer to invoke the member function on (i.e. the this pointer)
antbig 0:ad97421fb1fb 32 * function - The address of the void member function to attach
antbig 0:ad97421fb1fb 33 */
antbig 0:ad97421fb1fb 34 template<typename T>
antbig 0:ad97421fb1fb 35 FunctionPointer(T *object, void (T::*member)(void)) {
antbig 0:ad97421fb1fb 36 attach(object, member);
antbig 0:ad97421fb1fb 37 }
antbig 0:ad97421fb1fb 38
antbig 0:ad97421fb1fb 39 /* Function attach
antbig 0:ad97421fb1fb 40 * Attach a static function
antbig 0:ad97421fb1fb 41 *
antbig 0:ad97421fb1fb 42 * Variables
antbig 0:ad97421fb1fb 43 * function - The void static function to attach (default is none)
antbig 0:ad97421fb1fb 44 */
antbig 0:ad97421fb1fb 45 void attach(void (*function)(void) = 0);
antbig 0:ad97421fb1fb 46
antbig 0:ad97421fb1fb 47 /* Function attach
antbig 0:ad97421fb1fb 48 * Attach a member function
antbig 0:ad97421fb1fb 49 *
antbig 0:ad97421fb1fb 50 * Variables
antbig 0:ad97421fb1fb 51 * object - The object pointer to invoke the member function on (i.e. the this pointer)
antbig 0:ad97421fb1fb 52 * function - The address of the void member function to attach
antbig 0:ad97421fb1fb 53 */
antbig 0:ad97421fb1fb 54 template<typename T>
antbig 0:ad97421fb1fb 55 void attach(T *object, void (T::*member)(void)) {
antbig 0:ad97421fb1fb 56 _object = static_cast<void*>(object);
antbig 0:ad97421fb1fb 57 memcpy(_member, (char*)&member, sizeof(member));
antbig 0:ad97421fb1fb 58 _membercaller = &FunctionPointer::membercaller<T>;
antbig 0:ad97421fb1fb 59 _function = 0;
antbig 0:ad97421fb1fb 60 }
antbig 0:ad97421fb1fb 61
antbig 0:ad97421fb1fb 62 /* Function call
antbig 0:ad97421fb1fb 63 * Call the attached static or member function
antbig 0:ad97421fb1fb 64 */
antbig 0:ad97421fb1fb 65 void call();
antbig 0:ad97421fb1fb 66
antbig 0:ad97421fb1fb 67 private:
antbig 0:ad97421fb1fb 68
antbig 0:ad97421fb1fb 69 template<typename T>
antbig 0:ad97421fb1fb 70 static void membercaller(void *object, char *member) {
antbig 0:ad97421fb1fb 71 T* o = static_cast<T*>(object);
antbig 0:ad97421fb1fb 72 void (T::*m)(void);
antbig 0:ad97421fb1fb 73 memcpy((char*)&m, member, sizeof(m));
antbig 0:ad97421fb1fb 74 (o->*m)();
antbig 0:ad97421fb1fb 75 }
antbig 0:ad97421fb1fb 76
antbig 0:ad97421fb1fb 77 void (*_function)(void); // static function pointer - 0 if none attached
antbig 0:ad97421fb1fb 78 void *_object; // object this pointer - 0 if none attached
antbig 0:ad97421fb1fb 79 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
antbig 0:ad97421fb1fb 80 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
antbig 0:ad97421fb1fb 81
antbig 0:ad97421fb1fb 82 };
antbig 0:ad97421fb1fb 83
antbig 0:ad97421fb1fb 84 } // namespace mbed
antbig 0:ad97421fb1fb 85
antbig 0:ad97421fb1fb 86 #endif