Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C++

RMI for C++

Rate me:
Please Sign up or sign in to vote.
4.87/5 (113 votes)
6 Aug 2009CPOL8 min read 817.8K   4.6K   153  
User-friendly remote method invocation in C++.
//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005, Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//
// Distributed under the so-called MIT license, see accompanying file license.txt.
//*****************************************************************************

#ifndef _SF_SERIALIZESMARTPTR_HPP_
#define _SF_SERIALIZESMARTPTR_HPP_

#include <SF/Archive.hpp>

namespace SF {

    //********************************************************
    // smart pointer utilities

    template<typename T>
    class Serializer;

    // 1. Non-ref counted smart pointer. SmartPtr<T> must support reset() and operator->().

#ifndef __BORLANDC__

    template< template<typename> class SmartPtr, typename T >
    inline bool serializeSimpleSmartPtr(SmartPtr<T> **pps, SF::Archive &ar)
    {
        bool bRet = false;
        if (ar.isRead()) {
            if (ar.isFlagSet(Archive::POINTER)) *pps = new SmartPtr<T>;
            T *pt = NULL;
            ar & pt;
            (**pps).reset(pt);
        }
        else if (ar.isWrite()) {
            T *pt = NULL;
            if (*pps) pt = (**pps).operator->();
            ar & pt;
        }
        return true;
    }

#define SF_SERIALIZE_SIMPLE_SMARTPTR( SmartPtr )                                        \
    template<typename T>                                                                \
    class Serializer< SmartPtr<T> > {                                                    \
    public:                                                                             \
        SmartPtr<T> **ppt_;                                                                \
        Serializer(SmartPtr<T> **ppt) : ppt_(ppt) {}                                    \
        bool invoke(Archive &ar) { return serializeSimpleSmartPtr(ppt_, ar); }            \
    }; class RequireSemicolon

#else // __BORLANDC__

#define SF_SERIALIZE_SIMPLE_SMARTPTR( SmartPtr )                                        \
    template<typename T>                                                                \
    inline bool serializeSimpleSmartPtr(SmartPtr<T> **pps, SF::Archive &ar)                \
    {                                                                                    \
        bool bRet = false;                                                                \
        if (ar.isRead()) {                                                                \
            if (ar.isFlagSet(Archive::POINTER)) *pps = new SmartPtr<T>;                    \
            T *pt = NULL;                                                                \
            ar & pt;                                                                    \
            (**pps).reset(pt);                                                          \
        }                                                                                \
    else if (ar.isWrite()) {                                                            \
            T *pt = NULL;                                                                \
            if (*pps) pt = (**pps).operator->();                                        \
            ar & pt;                                                                    \
        }                                                                                \
        return true;                                                                    \
    }                                                                                    \
                                                                                        \
    template<typename T>                                                                \
    class Serializer< SmartPtr<T> > {                                                        \
    public:                                                                             \
        SmartPtr<T> **ppt_;                                                                \
        Serializer(SmartPtr<T> **ppt) : ppt_(ppt) {}                                            \
        bool invoke( Archive &ar ) { return serializeSimpleSmartPtr(ppt_, ar); }            \
    }; class RequireSemicolon

#endif

    // 2. Ref counted smart pointer. Must support operator=(), operator->(), and get().

#ifndef __BORLANDC__

    template< template<typename> class SmartPtr, typename T >
    inline bool serializeRefCountedSmartPtr(SmartPtr<T> **pps, SF::Archive &ar)
    {
        SF_ASSERT( ar.isRead() || ar.isWrite() );
        if (ar.isRead()) {
            if (ar.isFlagSet(Archive::POINTER)) *pps = new SmartPtr<T>;
            T *pt = NULL;
            if ((ar & pt).isOk()) {
                typedef ObjectId IdT;
                I_ContextRead &ctx = dynamic_cast<WithContextRead *>(ar.getStream())->getContext();
                void *pv = NULL;
                if (pt && ctx.query( pt, typeid(SmartPtr<T>), pv )) {
                    SmartPtr<T> *ps_prev = reinterpret_cast< SmartPtr<T> * >(pv);
                    **pps = *ps_prev;
                }
                else if (pt) {
                    ctx.add( pt, typeid(SmartPtr<T>), *pps );
                    //**pps = SmartPtr<T>(pt); // causes ICE with codewarrior 9.0
                    SmartPtr<T> spt(pt);
                    **pps = spt;
                }
                else {
                    //**pps = SmartPtr<T>(pt); // causes ICE with codewarrior 9.0
                    SmartPtr<T> spt(pt);
                    **pps = spt;
                }
                return true;
            }
            else
                return false;
        }
        else /*if (ar.isWrite())*/ {
            T *pt = NULL;
            if (*pps) pt = (**pps).get();
            return (ar & pt).isOk();
        }
    }

#define SF_SERIALIZE_REFCOUNTED_SMARTPTR( SmartPtr )                                    \
    template<typename T>                                                                \
    class Serializer< SmartPtr<T> > {                                                        \
    public:                                                                             \
        SmartPtr<T> **ppt_;                                                                \
        Serializer(SmartPtr<T> **ppt) : ppt_(ppt) {}                                            \
        bool invoke( Archive &ar ) { return serializeRefCountedSmartPtr(ppt_, ar); }        \
    }; class RequireSemicolon

#else // __BORLANDC__

#define SF_SERIALIZE_REFCOUNTED_SMARTPTR( SmartPtr )                                                \
    template<typename T >                                                                            \
    inline bool serializeRefCountedSmartPtr(SmartPtr<T> **pps, SF::Archive &ar)                        \
    {                                                                                                \
        if (ar.isRead()) {                                                                            \
            if (ar.isFlagSet(Archive::POINTER)) *pps = new SmartPtr<T>;                                \
            T *pt = NULL;                                                                            \
            if ((ar & pt).isOk()) {                                                                    \
                typedef ObjectId IdT;                                                               \
                I_ContextRead &ctx = dynamic_cast<WithContextRead *>(ar.getStream())->getContext();    \
                void *pv = NULL;                                                                    \
                if (pt && ctx.query( pt, typeid(SmartPtr<T>), pv )) {                                \
                    SmartPtr<T> *ps_prev = reinterpret_cast< SmartPtr<T> * >(pv);                    \
                    **pps = *ps_prev;                                                                \
                }                                                                                    \
                    else if (pt) {                                                                    \
                    ctx.add( pt, typeid(SmartPtr<T>), *pps );                                        \
                    **pps = SmartPtr<T>(pt);                                                        \
                }                                                                                    \
                else {                                                                                \
                    **pps = SmartPtr<T>(pt);                                                        \
                }                                                                                    \
                return true;                                                                        \
            }                                                                                        \
            else                                                                                    \
                return false;                                                                        \
        }                                                                                            \
        else if (ar.isWrite()) {                                                                    \
            T *pt = NULL;                                                                            \
            if (*pps) pt = (**pps).get();                                                            \
            return (ar & pt).isOk();                                                                \
        }                                                                                            \
    }                                                                                                \
                                                                                                    \
    template<typename T>                                                                            \
    class Serializer< SmartPtr<T> > {                                                                    \
    public:                                                                                         \
        SmartPtr<T> **ppt_;                                                                            \
        Serializer(SmartPtr<T> **ppt) : ppt_(ppt) {}                                                        \
        bool invoke( Archive &ar ) { return serializeRefCountedSmartPtr(ppt_, ar); }                    \
    }; class RequireSemicolon

#endif

} // namespace SF

#endif // ! _SF_SERIALIZERSMARTPTR_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)


Written By
Australia Australia
Software developer, from Sweden and now living in Canberra, Australia, working on distributed C++ applications. When he is not programming, Jarl enjoys skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

Comments and Discussions