Click here to Skip to main content
15,885,546 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 815.9K   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_SERIALIZEFUNDAMENTAL_HPP_
#define _SF_SERIALIZEFUNDAMENTAL_HPP_

#include <SF/Archive.hpp>
#include <SF/DataPtr.hpp>
#include <SF/I_Stream.hpp>
#include <SF/Serializer.hpp>
#include <SF/Tools.hpp>

namespace SF {

    // serialize fundamental types

    template<typename T>
    inline void serializeFundamental(SF::Archive &ar, T &t, const unsigned int, unsigned int count = 1)
    {
        typedef typename boost::remove_const<T>::type U;
        BOOST_STATIC_ASSERT( boost::is_fundamental<U>::value );
        U *pu = const_cast<U *>(&t);
        void *pvt = pu;

        if (ar.isRead()) 
        {
            I_Encoding &encoding = dynamic_cast<I_WithEncoding *>(ar.getStream())->getEncoding();
            DataPtr data;
            dynamic_cast<WithFormatRead *>(ar.getStream())->get(data);
            if (count > 1 && count != encoding.getCount(data,typeid(U)) )
            {
                SF_THROW(SF::Exception, "Static array size mismatch" )(typeid(U).name())(count)(encoding.getCount(data,typeid(T)));
            }
            encoding.toObject(data, pvt, typeid(T), count );
        }
        else if (ar.isWrite()) 
        {
            I_Encoding &encoding = dynamic_cast<I_WithEncoding *>(ar.getStream())->getEncoding();
            DataPtr data;
            encoding.toData(data, pvt, typeid(U), count );
            dynamic_cast<WithFormatWrite *>(ar.getStream())->put(data);
        }
    }

#define DEFINE_FUNDAMENTAL_SERIALIZATION(type)                             \
    inline void serialize(SF::Archive &ar, type &t, unsigned int version)  \
    {                                                                      \
        serializeFundamental(ar, t, version);                              \
    }

    FOR_EACH_FUNDAMENTAL_TYPE( DEFINE_FUNDAMENTAL_SERIALIZATION )

#undef DEFINE_FUNDAMENTAL_SERIALIZATION


} // namespace SF

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