mbed library sources(for async_print)

Dependents:   AsyncPrint

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/FunctionPointer.h@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:fd0d7bdfcdc2 1 /* mbed Microcontroller Library
emilmont 2:143cac498751 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 0:fd0d7bdfcdc2 3 *
emilmont 2:143cac498751 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 2:143cac498751 5 * you may not use this file except in compliance with the License.
emilmont 2:143cac498751 6 * You may obtain a copy of the License at
mbed_official 0:fd0d7bdfcdc2 7 *
emilmont 2:143cac498751 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:fd0d7bdfcdc2 9 *
emilmont 2:143cac498751 10 * Unless required by applicable law or agreed to in writing, software
emilmont 2:143cac498751 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 2:143cac498751 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 2:143cac498751 13 * See the License for the specific language governing permissions and
emilmont 2:143cac498751 14 * limitations under the License.
mbed_official 0:fd0d7bdfcdc2 15 */
mbed_official 0:fd0d7bdfcdc2 16 #ifndef MBED_FUNCTIONPOINTER_H
mbed_official 0:fd0d7bdfcdc2 17 #define MBED_FUNCTIONPOINTER_H
mbed_official 0:fd0d7bdfcdc2 18
mbed_official 0:fd0d7bdfcdc2 19 #include <string.h>
mbed_official 0:fd0d7bdfcdc2 20
emilmont 2:143cac498751 21 namespace mbed {
mbed_official 0:fd0d7bdfcdc2 22
mbed_official 0:fd0d7bdfcdc2 23 /** A class for storing and calling a pointer to a static or member void function
mbed_official 0:fd0d7bdfcdc2 24 */
mbed_official 0:fd0d7bdfcdc2 25 class FunctionPointer {
mbed_official 0:fd0d7bdfcdc2 26 public:
mbed_official 0:fd0d7bdfcdc2 27
mbed_official 0:fd0d7bdfcdc2 28 /** Create a FunctionPointer, attaching a static function
emilmont 2:143cac498751 29 *
mbed_official 0:fd0d7bdfcdc2 30 * @param function The void static function to attach (default is none)
mbed_official 0:fd0d7bdfcdc2 31 */
mbed_official 0:fd0d7bdfcdc2 32 FunctionPointer(void (*function)(void) = 0);
mbed_official 0:fd0d7bdfcdc2 33
mbed_official 0:fd0d7bdfcdc2 34 /** Create a FunctionPointer, attaching a member function
emilmont 2:143cac498751 35 *
mbed_official 0:fd0d7bdfcdc2 36 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
emilmont 2:143cac498751 37 * @param function The address of the void member function to attach
mbed_official 0:fd0d7bdfcdc2 38 */
mbed_official 0:fd0d7bdfcdc2 39 template<typename T>
mbed_official 0:fd0d7bdfcdc2 40 FunctionPointer(T *object, void (T::*member)(void)) {
mbed_official 0:fd0d7bdfcdc2 41 attach(object, member);
mbed_official 0:fd0d7bdfcdc2 42 }
emilmont 2:143cac498751 43
mbed_official 0:fd0d7bdfcdc2 44 /** Attach a static function
emilmont 2:143cac498751 45 *
mbed_official 0:fd0d7bdfcdc2 46 * @param function The void static function to attach (default is none)
mbed_official 0:fd0d7bdfcdc2 47 */
mbed_official 0:fd0d7bdfcdc2 48 void attach(void (*function)(void) = 0);
emilmont 2:143cac498751 49
mbed_official 0:fd0d7bdfcdc2 50 /** Attach a member function
emilmont 2:143cac498751 51 *
mbed_official 0:fd0d7bdfcdc2 52 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
emilmont 2:143cac498751 53 * @param function The address of the void member function to attach
mbed_official 0:fd0d7bdfcdc2 54 */
mbed_official 0:fd0d7bdfcdc2 55 template<typename T>
mbed_official 0:fd0d7bdfcdc2 56 void attach(T *object, void (T::*member)(void)) {
mbed_official 0:fd0d7bdfcdc2 57 _object = static_cast<void*>(object);
mbed_official 0:fd0d7bdfcdc2 58 memcpy(_member, (char*)&member, sizeof(member));
mbed_official 0:fd0d7bdfcdc2 59 _membercaller = &FunctionPointer::membercaller<T>;
mbed_official 0:fd0d7bdfcdc2 60 _function = 0;
mbed_official 0:fd0d7bdfcdc2 61 }
emilmont 2:143cac498751 62
mbed_official 0:fd0d7bdfcdc2 63 /** Call the attached static or member function
mbed_official 0:fd0d7bdfcdc2 64 */
mbed_official 0:fd0d7bdfcdc2 65 void call();
mbed_official 0:fd0d7bdfcdc2 66
mbed_official 0:fd0d7bdfcdc2 67 private:
mbed_official 0:fd0d7bdfcdc2 68 template<typename T>
mbed_official 0:fd0d7bdfcdc2 69 static void membercaller(void *object, char *member) {
mbed_official 0:fd0d7bdfcdc2 70 T* o = static_cast<T*>(object);
mbed_official 0:fd0d7bdfcdc2 71 void (T::*m)(void);
mbed_official 0:fd0d7bdfcdc2 72 memcpy((char*)&m, member, sizeof(m));
mbed_official 0:fd0d7bdfcdc2 73 (o->*m)();
mbed_official 0:fd0d7bdfcdc2 74 }
emilmont 2:143cac498751 75
mbed_official 0:fd0d7bdfcdc2 76 void (*_function)(void); // static function pointer - 0 if none attached
mbed_official 0:fd0d7bdfcdc2 77 void *_object; // object this pointer - 0 if none attached
mbed_official 0:fd0d7bdfcdc2 78 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
mbed_official 0:fd0d7bdfcdc2 79 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
mbed_official 0:fd0d7bdfcdc2 80 };
mbed_official 0:fd0d7bdfcdc2 81
mbed_official 0:fd0d7bdfcdc2 82 } // namespace mbed
mbed_official 0:fd0d7bdfcdc2 83
mbed_official 0:fd0d7bdfcdc2 84 #endif