Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ReaderFunc.h Source File

ReaderFunc.h

00001 /**
00002  * ACKme WiConnect Host Library is licensed under the BSD licence: 
00003  * 
00004  * Copyright (c)2014 ACKme Networks.
00005  * All rights reserved. 
00006  * 
00007  * Redistribution and use in source and binary forms, with or without modification, 
00008  * are permitted provided that the following conditions are met: 
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * 3. The name of the author may not be used to endorse or promote products 
00016  * derived from this software without specific prior written permission. 
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED 
00019  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00020  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00021  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00023  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00026  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00027  * OF SUCH DAMAGE.
00028  */
00029 #pragma once
00030 
00031 #include "api/WiconnectTypes.h"
00032 #include "FunctionPointer.h"
00033 
00034 
00035 namespace wiconnect
00036 {
00037 
00038 typedef WiconnectResult (*_ReaderFunc)(void *user, void *data, int maxReadSize, int *bytesRead);
00039 
00040 /**
00041  * @ingroup api_core_types
00042  *
00043  * @brief Generic data reading callback function.
00044  */
00045 class ReaderFunc : public FunctionPointer
00046 {
00047 public:
00048 
00049     /*************************************************************************************************/
00050     ReaderFunc(_ReaderFunc func = 0)
00051     {
00052         _function = (void*)func;
00053         _membercaller = NULL;
00054         _object = NULL;
00055     }
00056 
00057     /*************************************************************************************************/
00058     template<typename T>
00059     ReaderFunc(T *object, WiconnectResult (T::*member)(void *user, void *data, int maxReadSize, int *bytesRead))
00060     {
00061         _object = static_cast<void*>(object);
00062         memcpy(_member, (char*)&member, sizeof(member));
00063         _membercaller = (void*)&ReaderFunc::membercaller<T>;
00064         _function = 0;
00065     }
00066 
00067     /*************************************************************************************************/
00068     WiconnectResult call(void *user, void *data, int maxReadSize, int *bytesRead)
00069     {
00070         if (_function)
00071         {
00072             return ((_ReaderFunc)_function)(user, data, maxReadSize, bytesRead);
00073         }
00074         else if (_object)
00075         {
00076             typedef WiconnectResult (*membercallerFunc)(void*, char*, void *user, void *data, int maxReadSize, int *bytesRead);
00077             return ((membercallerFunc)_membercaller)(_object, _member, user, data, maxReadSize, bytesRead);
00078         }
00079         else
00080         {
00081             return WICONNECT_ERROR;
00082         }
00083     }
00084 
00085 private:
00086 
00087     /*************************************************************************************************/
00088     template<typename T>
00089     static WiconnectResult membercaller(void *object, char *member, void *user, void *data, int maxReadSize, int *bytesRead)
00090     {
00091         T* o = static_cast<T*>(object);
00092         WiconnectResult (T::*m)(void *user, void *data, int maxReadSize, int *bytesRead);
00093         memcpy((char*)&m, member, sizeof(m));
00094         return (o->*m)(user, data, maxReadSize, bytesRead);
00095     }
00096 };
00097 
00098 }