joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FunctionPointerBase.h Source File

FunctionPointerBase.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_FUNCTIONPOINTERBASE_H
00017 #define MBED_FUNCTIONPOINTERBASE_H
00018 
00019 #include <string.h>
00020 #include <stdint.h>
00021 #include <stddef.h>
00022 #include <stdarg.h>
00023 namespace mbed {
00024 
00025 template<typename R>
00026 class FunctionPointerBase {
00027 public:
00028     operator bool(void) const {
00029         return (_membercaller != NULL) && (_object != NULL);
00030     }
00031 
00032     /**
00033      * Clears the current function pointer assignment
00034      * After clear(), this instance will point to nothing (NULL)
00035      */
00036     virtual void clear() {
00037         _membercaller = NULL;
00038         _object = NULL;
00039     }
00040 
00041 protected:
00042     struct ArgOps {
00043         void (*constructor)(void *, va_list);
00044         void (*copy_args)(void *, void *);
00045         void (*destructor)(void *);
00046     };
00047     void * _object; // object Pointer/function pointer
00048     R (*_membercaller)(void *, uintptr_t *, void *);
00049     // aligned raw member function pointer storage - converted back by registered _membercaller
00050     uintptr_t _member[4];
00051     static const struct ArgOps _nullops;
00052 
00053 protected:
00054     FunctionPointerBase():_object(NULL), _membercaller(NULL) {}
00055     FunctionPointerBase(const FunctionPointerBase<R> & fp) {
00056         copy(&fp);
00057     }
00058     virtual ~FunctionPointerBase() {
00059     }
00060 
00061     /**
00062      * Calls the member pointed to by object::member or (function)object
00063      * @param arg
00064      * @return
00065      */
00066     inline R call(void* arg) {
00067         return _membercaller(_object, _member, arg);
00068     }
00069 
00070     void copy(const FunctionPointerBase<R> * fp) {
00071         _object = fp->_object;
00072         memcpy (_member, fp->_member, sizeof(_member));
00073         _membercaller = fp->_membercaller;
00074     }
00075 private:
00076     static void _null_constructor(void * dest, va_list args) {(void) dest;(void) args;}
00077     static void _null_copy_args(void *dest , void* src) {(void) dest; (void) src;}
00078     static void _null_destructor(void *args) {(void) args;}
00079 
00080 };
00081 template<typename R>
00082 const struct FunctionPointerBase<R>::ArgOps FunctionPointerBase<R>::_nullops = {
00083     FunctionPointerBase<R>::_null_constructor,
00084     FunctionPointerBase<R>::_null_copy_args,
00085     FunctionPointerBase<R>::_null_destructor
00086 };
00087 
00088 }
00089 #endif