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

types/LogFunc.h

Committer:
dan_ackme
Date:
2014-08-12
Revision:
11:ea484e1b7fc4
Parent:
1:6ec9998427ad
Child:
13:2b51f5267c92

File content as of revision 11:ea484e1b7fc4:

/*
 * Copyright 2014, ACKme Networks
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks;
 * the contents of this file may not be disclosed to third parties, copied
 * or duplicated in any form, in whole or in part, without the prior
 * written permission of ACKme Networks.
 */

#pragma once

#include "WiconnectTypes.h"
#include "FunctionPointer.h"

namespace wiconnect
{

typedef int (*_LogFunc)(const char *str);


/**
 * @ingroup types_core
 *
 * @brief Logging callback function.
 */
class LogFunc : public FunctionPointer
{
public:
    /*************************************************************************************************/
    LogFunc(_LogFunc func = 0)
    {
        _function = (void*)func;
        _membercaller = NULL;
        _object = NULL;
    }

    /*************************************************************************************************/
    template<typename T>
    LogFunc(T *object, WiconnectResult (T::*member)(const char *str))
    {
        _object = static_cast<void*>(object);
        memcpy(_member, (char*)&member, sizeof(member));
        _membercaller = (void*)&LogFunc::membercaller<T>;
        _function = 0;
    }

    /*************************************************************************************************/
    int call(const char *str)
    {
        if (_function)
        {
            return ((_LogFunc)_function)(str);
        }
        else if (_object)
        {
            typedef int (*membercallerFunc)(void*, char*, const char *str);
            return ((membercallerFunc)_membercaller)(_object, _member, str);
        }
        else
        {
            return -1;
        }
    }

private:

    /*************************************************************************************************/
    template<typename T>
    static int membercaller(void *object, char *member, const char *str)
    {
        T* o = static_cast<T*>(object);
        int (T::*m)(const char *str);
        memcpy((char*)&m, member, sizeof(m));
        return (o->*m)(str);
    }
};




}