Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RMI for C++

, 6 Aug 2009 CPOL
User-friendly remote method invocation in C++.
rcf.zip
RCF
demo
vs2003
RCF
Client
Server
doc
include
RCF
Connection.inl
Marshal.inl
Multiplexer.inl
RCF.inl
RcfServer.inl
ServerStub.inl
Protocol
util
Platform
Compiler
bcc55
cw80
gcc32
icl70
msvc71
Library
bcc55
cw80
gcc32
msvc60
msvc71
Machine
SPARC
x86
OS
Cygwin
Unix
Windows
System
NonWindows
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
Jamfile
Jamrules
borland
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
//*****************************************************************************
// Copyright (c) 2005. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef _UTIL_INITDEINIT_HPP_
#define _UTIL_INITDEINIT_HPP_

#include "AutoRun.hpp"

namespace util {

    typedef void(*InitCallback)();
    typedef void(*DeinitCallback)();

    static const int InitCallbackMaxCount = 25;
    static const int DeinitCallbackMaxCount = 25;

    class InitCallbackArray
    {
    public:
        static InitCallback *get()
        {
            static InitCallback initCallbacks[InitCallbackMaxCount];
            return initCallbacks;
        }
        static bool &getInitCallbacksInvoked()
        {
            static bool initCallbacksInvoked;
            return initCallbacksInvoked;
        }
    };

    class DeinitCallbackArray
    {
    public:
        static DeinitCallback *getStatic()
        {
            static DeinitCallback deinitCallbacks[DeinitCallbackMaxCount];
            return deinitCallbacks;
        }
        static DeinitCallback *&getAllocated()
        {
            static DeinitCallback *deinitCallbacks;
            return deinitCallbacks;
        }
    };

    inline InitCallback *getInitCallbackArray()
    {
        return InitCallbackArray::get();
    }

    inline DeinitCallback *getDeinitCallbackArray()
    {
        return DeinitCallbackArray::getAllocated() ?
            DeinitCallbackArray::getAllocated() :
            DeinitCallbackArray::getStatic();
    }

    inline bool getInitCallbacksInvoked()
    {
        return InitCallbackArray::getInitCallbacksInvoked();
    }

    inline void setInitCallbacksInvoked(bool initCallbacksInvoked)
    {
        InitCallbackArray::getInitCallbacksInvoked() = initCallbacksInvoked;
    }

    template<typename Callback>
    inline bool addCallback(Callback *callbackArray, Callback callback, const int callbacksMaxCount)
    {
        for (int i=0; i<callbacksMaxCount; ++i)
        {
            if (0 == callbackArray[i])
            {
                callbackArray[i] = callback;
                return true;
            }
            else if (callback == callbackArray[i])
            {
                return true;
            }
        }
        return false;
    }

    template<typename Callback>
    inline void invokeCallbacks(Callback *callbackArray,  const int callbacksMaxCount)
    {
        int i=0;
        while (i<callbacksMaxCount && callbackArray[i])
        {
            Callback callback = callbackArray[i];
            (*callback)();
            i++;
        }
    }
    
    // TODO: only following functions should be publicly accessible

    template<typename T>
    inline bool addInitCallback(T (*initCallback)())
    {
        if (getInitCallbacksInvoked())
        {
            initCallback();
        }
        addCallback(getInitCallbackArray(), InitCallback(initCallback), InitCallbackMaxCount);
        return true;
    }

    inline bool addDeinitCallback(DeinitCallback deinitCallback)
    {
        addCallback(getDeinitCallbackArray(), DeinitCallback(deinitCallback), DeinitCallbackMaxCount);
        return true;
    }

    inline void invokeInitCallbacks()
    {
        invokeCallbacks(getInitCallbackArray(), InitCallbackMaxCount);
        setInitCallbacksInvoked(true);
    }

    inline void invokeDeinitCallbacks()
    {
        invokeCallbacks(getDeinitCallbackArray(), DeinitCallbackMaxCount);
        setInitCallbacksInvoked(false);
    }

} // namespace util


// TODO: throughly specify how these macros interact with multiple translation units

#define UTIL_ON_INIT_DEINIT_IMPL(statements, functorName, whichAction)                                             \
class functorName {                                                                                                                               \
public:                                                                                                                                                  \
    functorName() { static bool first = true; if (first) { first = false; whichAction(&functorName::invoke); } }          \
    static void invoke() { statements; }                                                                                                     \
};                                                                                                                                                          \
static functorName BOOST_PP_CAT(functorName, Inst);

#define UTIL_ON_INIT(statements)                            UTIL_ON_INIT_NAMED(statements, selfRegisteringInitFunctor)
#define UTIL_ON_DEINIT(statements)                          UTIL_ON_DEINIT_NAMED(statements, selfRegisteringDeinitFunctor)
#define UTIL_ON_INIT_NAMED(statements, name)                UTIL_ON_INIT_DEINIT_IMPL(statements, BOOST_PP_CAT(name, __LINE__), ::util::addInitCallback )
#define UTIL_ON_DEINIT_NAMED(statements, name)              UTIL_ON_INIT_DEINIT_IMPL(statements, BOOST_PP_CAT(name, __LINE__), ::util::addDeinitCallback )
#define UTIL_ON_INIT_DEINIT(initFunc, deinitFunc)           UTIL_ON_INIT(initFunc) UTIL_ON_DEINIT(deinitFunc)

#endif // ! _UTIL_INITDEINIT_HPP_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jarl Lindrud

Australia Australia
Software developer, ex-resident of Sweden and now living in Canberra, Australia, working on distributed C++ applications. Jarl enjoys programming, but prefers skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 6 Aug 2009
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid