Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos EthernetInterface FatFileSystemCpp MCP23S17 SDFileSystem mbed
Fork of HTTPServerHelloWorld by
mbed/FunctionPointer.h@3:5dc0023e6284, 2014-01-31 (annotated)
- Committer:
- wyunreal
- Date:
- Fri Jan 31 23:19:28 2014 +0000
- Revision:
- 3:5dc0023e6284
First approach of EthernetService class
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wyunreal | 3:5dc0023e6284 | 1 | /* mbed Microcontroller Library - FunctionPointer |
| wyunreal | 3:5dc0023e6284 | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
| wyunreal | 3:5dc0023e6284 | 3 | * sford |
| wyunreal | 3:5dc0023e6284 | 4 | */ |
| wyunreal | 3:5dc0023e6284 | 5 | |
| wyunreal | 3:5dc0023e6284 | 6 | #ifndef MBED_FUNCTIONPOINTER_H |
| wyunreal | 3:5dc0023e6284 | 7 | #define MBED_FUNCTIONPOINTER_H |
| wyunreal | 3:5dc0023e6284 | 8 | |
| wyunreal | 3:5dc0023e6284 | 9 | #include <string.h> |
| wyunreal | 3:5dc0023e6284 | 10 | |
| wyunreal | 3:5dc0023e6284 | 11 | namespace mbed { |
| wyunreal | 3:5dc0023e6284 | 12 | |
| wyunreal | 3:5dc0023e6284 | 13 | /* Class FunctionPointer |
| wyunreal | 3:5dc0023e6284 | 14 | * A class for storing and calling a pointer to a static or member void function |
| wyunreal | 3:5dc0023e6284 | 15 | */ |
| wyunreal | 3:5dc0023e6284 | 16 | class FunctionPointer { |
| wyunreal | 3:5dc0023e6284 | 17 | |
| wyunreal | 3:5dc0023e6284 | 18 | public: |
| wyunreal | 3:5dc0023e6284 | 19 | |
| wyunreal | 3:5dc0023e6284 | 20 | /* Constructor FunctionPointer |
| wyunreal | 3:5dc0023e6284 | 21 | * Create a FunctionPointer, attaching a static function |
| wyunreal | 3:5dc0023e6284 | 22 | * |
| wyunreal | 3:5dc0023e6284 | 23 | * Variables |
| wyunreal | 3:5dc0023e6284 | 24 | * function - The void static function to attach (default is none) |
| wyunreal | 3:5dc0023e6284 | 25 | */ |
| wyunreal | 3:5dc0023e6284 | 26 | FunctionPointer(void (*function)(void) = 0); |
| wyunreal | 3:5dc0023e6284 | 27 | |
| wyunreal | 3:5dc0023e6284 | 28 | /* Constructor FunctionPointer |
| wyunreal | 3:5dc0023e6284 | 29 | * Create a FunctionPointer, attaching a member function |
| wyunreal | 3:5dc0023e6284 | 30 | * |
| wyunreal | 3:5dc0023e6284 | 31 | * Variables |
| wyunreal | 3:5dc0023e6284 | 32 | * object - The object pointer to invoke the member function on (i.e. the this pointer) |
| wyunreal | 3:5dc0023e6284 | 33 | * function - The address of the void member function to attach |
| wyunreal | 3:5dc0023e6284 | 34 | */ |
| wyunreal | 3:5dc0023e6284 | 35 | template<typename T> |
| wyunreal | 3:5dc0023e6284 | 36 | FunctionPointer(T *object, void (T::*member)(void)) { |
| wyunreal | 3:5dc0023e6284 | 37 | attach(object, member); |
| wyunreal | 3:5dc0023e6284 | 38 | } |
| wyunreal | 3:5dc0023e6284 | 39 | |
| wyunreal | 3:5dc0023e6284 | 40 | /* Function attach |
| wyunreal | 3:5dc0023e6284 | 41 | * Attach a static function |
| wyunreal | 3:5dc0023e6284 | 42 | * |
| wyunreal | 3:5dc0023e6284 | 43 | * Variables |
| wyunreal | 3:5dc0023e6284 | 44 | * function - The void static function to attach (default is none) |
| wyunreal | 3:5dc0023e6284 | 45 | */ |
| wyunreal | 3:5dc0023e6284 | 46 | void attach(void (*function)(void) = 0); |
| wyunreal | 3:5dc0023e6284 | 47 | |
| wyunreal | 3:5dc0023e6284 | 48 | /* Function attach |
| wyunreal | 3:5dc0023e6284 | 49 | * Attach a member function |
| wyunreal | 3:5dc0023e6284 | 50 | * |
| wyunreal | 3:5dc0023e6284 | 51 | * Variables |
| wyunreal | 3:5dc0023e6284 | 52 | * object - The object pointer to invoke the member function on (i.e. the this pointer) |
| wyunreal | 3:5dc0023e6284 | 53 | * function - The address of the void member function to attach |
| wyunreal | 3:5dc0023e6284 | 54 | */ |
| wyunreal | 3:5dc0023e6284 | 55 | template<typename T> |
| wyunreal | 3:5dc0023e6284 | 56 | void attach(T *object, void (T::*member)(void)) { |
| wyunreal | 3:5dc0023e6284 | 57 | _object = static_cast<void*>(object); |
| wyunreal | 3:5dc0023e6284 | 58 | memcpy(_member, (char*)&member, sizeof(member)); |
| wyunreal | 3:5dc0023e6284 | 59 | _membercaller = &FunctionPointer::membercaller<T>; |
| wyunreal | 3:5dc0023e6284 | 60 | _function = 0; |
| wyunreal | 3:5dc0023e6284 | 61 | } |
| wyunreal | 3:5dc0023e6284 | 62 | |
| wyunreal | 3:5dc0023e6284 | 63 | /* Function call |
| wyunreal | 3:5dc0023e6284 | 64 | * Call the attached static or member function |
| wyunreal | 3:5dc0023e6284 | 65 | */ |
| wyunreal | 3:5dc0023e6284 | 66 | void call(); |
| wyunreal | 3:5dc0023e6284 | 67 | |
| wyunreal | 3:5dc0023e6284 | 68 | private: |
| wyunreal | 3:5dc0023e6284 | 69 | |
| wyunreal | 3:5dc0023e6284 | 70 | template<typename T> |
| wyunreal | 3:5dc0023e6284 | 71 | static void membercaller(void *object, char *member) { |
| wyunreal | 3:5dc0023e6284 | 72 | T* o = static_cast<T*>(object); |
| wyunreal | 3:5dc0023e6284 | 73 | void (T::*m)(void); |
| wyunreal | 3:5dc0023e6284 | 74 | memcpy((char*)&m, member, sizeof(m)); |
| wyunreal | 3:5dc0023e6284 | 75 | (o->*m)(); |
| wyunreal | 3:5dc0023e6284 | 76 | } |
| wyunreal | 3:5dc0023e6284 | 77 | |
| wyunreal | 3:5dc0023e6284 | 78 | void (*_function)(void); // static function pointer - 0 if none attached |
| wyunreal | 3:5dc0023e6284 | 79 | void *_object; // object this pointer - 0 if none attached |
| wyunreal | 3:5dc0023e6284 | 80 | char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
| wyunreal | 3:5dc0023e6284 | 81 | void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
| wyunreal | 3:5dc0023e6284 | 82 | |
| wyunreal | 3:5dc0023e6284 | 83 | }; |
| wyunreal | 3:5dc0023e6284 | 84 | |
| wyunreal | 3:5dc0023e6284 | 85 | } // namespace mbed |
| wyunreal | 3:5dc0023e6284 | 86 | |
| wyunreal | 3:5dc0023e6284 | 87 | #endif |
