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

RCF - Interprocess Communication for C++

, 25 Oct 2011 CPOL
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
rcf-04.zip
RCF-0.4
demo
vs2003
RCF
Client
Server
doc
html
class_r_c_f_1_1_exception.png
class_r_c_f_1_1_filter.png
class_r_c_f_1_1_filter_factory.png
class_r_c_f_1_1_filter_service.png
class_r_c_f_1_1_identity_filter.png
class_r_c_f_1_1_identity_filter_factory.png
class_r_c_f_1_1_i___client_transport.png
class_r_c_f_1_1_i___endpoint.png
class_r_c_f_1_1_i___service.png
class_r_c_f_1_1_i___session_manager.png
class_r_c_f_1_1_multicast_client_transport.png
class_r_c_f_1_1_object_factory_service.png
class_r_c_f_1_1_open_ssl_encryption_filter.png
class_r_c_f_1_1_open_ssl_encryption_filter_factory.png
class_r_c_f_1_1_publishing_service.png
class_r_c_f_1_1_rcf_server.png
class_r_c_f_1_1_remote_exception.png
class_r_c_f_1_1_subscription_service.png
class_r_c_f_1_1_tcp_endpoint.png
class_r_c_f_1_1_udp_endpoint.png
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.png
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.png
doxygen.png
ftv2blank.png
ftv2doc.png
ftv2folderclosed.png
ftv2folderopen.png
ftv2lastnode.png
ftv2link.png
ftv2mlastnode.png
ftv2mnode.png
ftv2node.png
ftv2plastnode.png
ftv2pnode.png
ftv2vertline.png
tab_b.gif
tab_l.gif
tab_r.gif
latex
annotated.tex
class_r_c_f_1_1_client_stub.tex
class_r_c_f_1_1_exception.eps
class_r_c_f_1_1_exception.tex
class_r_c_f_1_1_filter.eps
class_r_c_f_1_1_filter.tex
class_r_c_f_1_1_filter_description.tex
class_r_c_f_1_1_filter_factory.eps
class_r_c_f_1_1_filter_factory.tex
class_r_c_f_1_1_filter_service.eps
class_r_c_f_1_1_filter_service.tex
class_r_c_f_1_1_identity_filter.eps
class_r_c_f_1_1_identity_filter.tex
class_r_c_f_1_1_identity_filter_factory.eps
class_r_c_f_1_1_identity_filter_factory.tex
class_r_c_f_1_1_i___client_transport.eps
class_r_c_f_1_1_i___client_transport.tex
class_r_c_f_1_1_i___endpoint.eps
class_r_c_f_1_1_i___endpoint.tex
class_r_c_f_1_1_i___proactor.tex
class_r_c_f_1_1_i___rcf_client.tex
class_r_c_f_1_1_i___server_transport.tex
class_r_c_f_1_1_i___server_transport_ex.tex
class_r_c_f_1_1_i___service.eps
class_r_c_f_1_1_i___service.tex
class_r_c_f_1_1_i___session.tex
class_r_c_f_1_1_i___session_manager.eps
class_r_c_f_1_1_i___session_manager.tex
class_r_c_f_1_1_multicast_client_transport.eps
class_r_c_f_1_1_multicast_client_transport.tex
class_r_c_f_1_1_object_factory_service.eps
class_r_c_f_1_1_object_factory_service.tex
class_r_c_f_1_1_open_ssl_encryption_filter.eps
class_r_c_f_1_1_open_ssl_encryption_filter.tex
class_r_c_f_1_1_open_ssl_encryption_filter_factory.eps
class_r_c_f_1_1_open_ssl_encryption_filter_factory.tex
class_r_c_f_1_1_publishing_service.eps
class_r_c_f_1_1_publishing_service.tex
class_r_c_f_1_1_rcf_server.eps
class_r_c_f_1_1_rcf_server.tex
class_r_c_f_1_1_remote_exception.eps
class_r_c_f_1_1_remote_exception.tex
class_r_c_f_1_1_subscription_service.eps
class_r_c_f_1_1_subscription_service.tex
class_r_c_f_1_1_tcp_endpoint.eps
class_r_c_f_1_1_tcp_endpoint.tex
class_r_c_f_1_1_udp_endpoint.eps
class_r_c_f_1_1_udp_endpoint.tex
class_r_c_f_1_1_zlib_stateful_compression_filter.tex
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.eps
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.tex
class_r_c_f_1_1_zlib_stateless_compression_filter.tex
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.eps
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.tex
dirs.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2F.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2FRCF_2F.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2FRCF_2FProtocol_2F.tex
doxygen.sty
Helvetica.ttf
hierarchy.tex
Makefile
refman.tex
struct_r_c_f_1_1_filter_id_comparison.tex
include
RCF
Marshal.inl
Protocol
RcfServer.inl
ServerStub.inl
test
util
Platform
Machine
SPARC
x86
OS
Unix
Windows
Threads
SF
src
RCF
Protocol
SF
test
borland
Jamfile
Jamrules
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
client.pem
server.pem
rcf-09c.zip
RCF-0.9c
demo
vs2003
RCF
Client
Server
include
RCF
Marshal.inl
Protocol
RcfServer.inl
ServerStub.inl
test
util
Platform
Machine
SPARC
x86
OS
Unix
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
bcc
Jamfile
Jamrules
data
caCertA.pem
caCertB.pem
certA.pem
certB.pem
ssCert1.pem
ssCert2.pem
Jamfile
Jamrules
vc6
Jamfile
Jamrules
//******************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005 - 2007. All rights reserved.
// Consult your license for conditions of use.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//******************************************************************************

#ifndef INCLUDE_SF_SERIALIZER_HPP
#define INCLUDE_SF_SERIALIZER_HPP

#include <boost/mpl/assert.hpp>
#include <boost/mpl/or.hpp>

#include <RCF/TypeTraits.hpp>

#include <SF/Archive.hpp>
#include <SF/I_Stream.hpp>
#include <SF/Registry.hpp>
#include <SF/SerializeFundamental.hpp>
#include <SF/SerializePolymorphic.hpp>
#include <SF/SfNew.hpp>
#include <SF/Tools.hpp>


namespace boost {
    namespace serialization {
        template<class Base, class Derived>
        const Base & base_object(const Derived & d);
    }
}

#if defined(_MSC_VER) && _MSC_VER < 1310

#include <boost/type_traits.hpp>
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(long double)
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(__int64)
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(unsigned __int64)
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(std::string)
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(std::wstring)

#endif

namespace SF {

    // Generic serializer, subclassed by all other serializers

    class SerializerBase : boost::noncopyable
    {
    private:
        bool invokeRead(Archive &ar);
        bool invokeWrite(Archive &ar);

        // Following are overridden to provide type-specific operations.
        virtual std::string getTypeName() = 0;
        virtual void newObject(Archive &ar) = 0;
        virtual bool isDerived() = 0;
        virtual std::string getDerivedTypeName() = 0;
        virtual void getSerializerPolymorphic(const std::string &derivedTypeName) = 0;
        virtual bool invokeSerializerPolymorphic(SF::Archive &) = 0;
        virtual void serializeContents(Archive &ar) = 0;
        virtual void addToInputContext(I_Stream *, const UInt32 &) = 0;
        virtual bool queryInputContext(I_Stream *, const UInt32 &) = 0;
        virtual void addToOutputContext(I_Stream *, UInt32 &) = 0;
        virtual bool queryOutputContext(I_Stream *, UInt32 &) = 0;
        virtual void setFromId() = 0;
        virtual void setToNull() = 0;
        virtual bool isNull() = 0;
        virtual bool isNonAtomic() = 0;

    public:
        SerializerBase();
        virtual ~SerializerBase();
        bool invoke(Archive &ar);
    };

    //---------------------------------------------------------------------
    // Type-specific serializers

    // this pragma concerns Serializer<T>::newObject, but needs to be up here, probably because Serializer<T> is a template
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4675 )  // warning C4675: resolved overload was found by argument-dependent lookup
#endif

    template<typename T>
    class Serializer : public SerializerBase
    {
    public:
        Serializer(T **ppt) :
            ppt(ppt),
            pf(RCF_DEFAULT_INIT),
            id()
        {}

    private:
        typedef ObjectId IdT;
        T **ppt;
        I_SerializerPolymorphic *pf;
        IdT id;

        std::string getTypeName()
        {
            //return SF::Registry::getSingleton().template getTypeName<T>();
            return SF::Registry::getSingleton().getTypeName( (T *) 0);
        }

        void newObject(Archive &ar)
        {
            *ppt = sfNew((T *) NULL, (T **) NULL, ar);
        }

        bool isDerived()
        {
            if (*ppt && typeid(T) != typeid(**ppt))
            {
                if (!SF::Registry::getSingleton().isTypeRegistered( typeid(**ppt) ))
                {
                    RCF_THROW(RCF::Exception(RCF::SfError_TypeRegistration))(typeid(T))(typeid(**ppt));
                }
                return true;
            }
            return false;
        }

        std::string getDerivedTypeName()
        {
            return SF::Registry::getSingleton().getTypeName( typeid(**ppt) );
        }

        void getSerializerPolymorphic(const std::string &derivedTypeName)
        {
            //pf = & SF::Registry::getSingleton().template getSerializerPolymorphic<T>(derivedTypeName);
            pf = & SF::Registry::getSingleton().getSerializerPolymorphic( (T *) 0, derivedTypeName);
        }

        bool invokeSerializerPolymorphic(SF::Archive &ar)
        {
            RCF_ASSERT(pf);
            void **ppvb = (void **) (ppt); // not even reinterpret_cast wants to touch this
            return pf->invoke(ppvb, ar);
        }

        void serializeContents(Archive &ar)
        {
            preserialize(ar, *ppt, 0);
        }

        void addToInputContext(SF::I_Stream *stream, const UInt32 &nid)
        {
            I_ContextRead &ctx = dynamic_cast<I_WithContextRead *>(stream)->getContext();
            ctx.add(nid, IdT( (void *) (*ppt), &typeid(T)));
        }

        bool queryInputContext(SF::I_Stream *stream, const UInt32 &nid)
        {
            I_ContextRead &ctx = dynamic_cast<I_WithContextRead *>(stream)->getContext();
            return ctx.query(nid, id);
        }

        void addToOutputContext(SF::I_Stream *stream, UInt32 &nid)
        {
            I_ContextWrite &ctx = dynamic_cast<I_WithContextWrite *>(stream)->getContext();
            ctx.add( IdT( (void *) *ppt, &typeid(T)), nid);
        }

        bool queryOutputContext(SF::I_Stream *stream, UInt32 &nid)
        {
            I_ContextWrite &ctx = dynamic_cast<I_WithContextWrite *>(stream)->getContext();
            return ctx.query( IdT( (void *) *ppt, &typeid(T)), nid);
        }

        void setFromId()
        {
            *ppt = reinterpret_cast<T *>(id.first);
        }

        void setToNull()
        {
            *ppt = NULL;
        }

        bool isNull()
        {
            return *ppt == NULL;
        }

        bool isNonAtomic()
        {
            bool isFundamental = RCF::IsFundamental<T>::value;
            return !isFundamental;
        }

    };

#ifdef _MSC_VER
#pragma warning( pop )
#endif
    /*
    template<typename T> struct GetIndirection                         { typedef boost::mpl::int_<0> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T *>                    { typedef boost::mpl::int_<1> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T const *>              { typedef boost::mpl::int_<1> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T * const>              { typedef boost::mpl::int_<1> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T const * const>        { typedef boost::mpl::int_<1> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T **>                   { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T **const>              { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T *const*>              { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T const**>              { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T *const*const>         { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T const**const>         { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T const*const*>         { typedef boost::mpl::int_<2> Level; typedef T Base; };
    template<typename T> struct GetIndirection<T const*const*const>    { typedef boost::mpl::int_<2> Level; typedef T Base; };
    */

    template<typename PPT>
    struct GetIndirection
    {
        typedef typename boost::remove_pointer<PPT>::type PT;
        typedef typename boost::is_pointer<PPT>::type is_single;
        typedef typename boost::is_pointer<PT>::type is_double;

        typedef
        typename boost::mpl::if_<
            is_double,
            boost::mpl::int_<2>,
            typename boost::mpl::if_<
                is_single,
                boost::mpl::int_<1>,
                boost::mpl::int_<0>
            >::type
        >::type Level;

        typedef
        typename boost::remove_cv<
            typename boost::remove_pointer<
                typename boost::remove_cv<
                    typename boost::remove_pointer<
                        typename boost::remove_cv<PPT>::type
                    >::type
                >::type
            >::type
        >::type Base;

    };

    template<typename T>
    inline bool invokeCustomSerializer(T **ppt, Archive &ar, RCF_PFTO_HACK int)
    {
        BOOST_MPL_ASSERT(( boost::mpl::not_< RCF::IsPointer<T> > ));
        return Serializer<T>(ppt).invoke(ar);
    }

    template<typename U, typename T>
    inline bool invokeSerializer(U *, T *, boost::mpl::int_<0> *, const U &u, Archive &ar)
    {
        BOOST_MPL_ASSERT(( boost::mpl::not_< RCF::IsPointer<T> > ));
        T *pt = const_cast<T *>(&u);
        return invokeCustomSerializer( (T **) (&pt), ar, 0);
    }

    template<typename U, typename T>
    inline bool invokeSerializer(U *, T *, boost::mpl::int_<1> *, const U &u, Archive &ar)
    {
        BOOST_MPL_ASSERT(( boost::mpl::not_< RCF::IsPointer<T> > ));
        return invokeCustomSerializer( (T **) (&u), ar, 0);
    }

    template<typename U, typename T>
    inline bool invokeSerializer(U *, T *, boost::mpl::int_<2> *, const U &u, Archive &ar)
    {
        BOOST_MPL_ASSERT(( boost::mpl::not_< RCF::IsPointer<T> > ));
        return invokeCustomSerializer( (T**) (u), ar, 0);
    }

    template<typename U>
    inline bool invokeSerializer(U u, Archive &ar)
    {
        typedef typename GetIndirection<U>::Level Level;
        typedef typename GetIndirection<U>::Base T;
        BOOST_MPL_ASSERT(( boost::mpl::not_< RCF::IsPointer<T> > ));
        //BOOST_MPL_ASSERT(( boost::mpl::not_< boost::is_const<T> > ));
        return invokeSerializer( (U *) 0, (T *) 0, (Level *) 0, u, ar);
    }

    template<typename U>
    inline bool invokePtrSerializer(U u, Archive &ar)
    {
        typedef typename GetIndirection<U>::Level Level;
        const int levelOfIndirection = Level::value;
        RCF_ASSERT( levelOfIndirection == 1 || levelOfIndirection == 2);
        ar.setFlag( SF::Archive::POINTER, levelOfIndirection == 2 );
        return invokeSerializer(u,ar);
    }

    int getMyRuntimeVersion();

    template<typename T>
    inline void serializeEnum(::SF::Archive &ar, T &t, const unsigned int)
    {
        int rcfRuntimeVersion = getMyRuntimeVersion();
        if (rcfRuntimeVersion >= 2)
        {
            ar & SF::Archive::Flag(SF::Archive::NO_BEGIN_END);
        }

        if (ar.isRead())
        {
            boost::int32_t n = 0;
            ar & n;
            t = T(n);
        }
        else /* if (ar.isWrite())) */
        {
            boost::int32_t n = t;
            ar & n;
        }
    }

    template<typename T>
    inline void serializeInternal(Archive &archive, T &t, const unsigned int version)
    {
        t.serialize(archive, version);
    }

    template<typename T>
    inline void serializeFundamentalOrNot(Archive &archive, T &t, const unsigned int version, boost::mpl::true_ *)
    {
        serializeFundamental(archive, t, version);
    }

    template<typename T>
    inline void serializeFundamentalOrNot(Archive &archive, T &t, const unsigned int version, boost::mpl::false_ *)
    {
        serializeInternal(archive, t, version);
    }

    template<typename T>
    inline void serializeEnumOrNot(Archive &archive, T &t, const unsigned int version, boost::mpl::true_ *)
    {
        serializeEnum(archive, t, version);
    }

    template<typename T>
    inline void serializeEnumOrNot(Archive &archive, T &t, const unsigned int version, boost::mpl::false_ *)
    {
        typedef typename RCF::IsFundamental<T>::type type;
        serializeFundamentalOrNot(archive, t, version, (type *) NULL);
    }

    template<typename Archive, typename T>
    inline void serialize(Archive &archive, T &t, const unsigned RCF_PFTO_HACK int version)
    {
        typedef typename boost::is_enum<T>::type type;
        serializeEnumOrNot(archive, t, version, (type *) NULL);
    }

    template<typename Archive, typename T>
    inline void preserialize(Archive &ar, T *&pt, const unsigned RCF_PFTO_HACK int version)
    {
        BOOST_MPL_ASSERT(( boost::mpl::not_< RCF::IsPointer<T> > ));
        typedef typename RCF::RemoveCv<T>::type U;
        serialize(ar, (U &) *pt, static_cast<const unsigned int>(version));
    }

}

#endif // ! INCLUDE_SF_SERIALIZER_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
Web03 | 2.8.1411023.1 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid