Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C++

RCF - Interprocess Communication for C++

Rate me:
Please Sign up or sign in to vote.
4.94/5 (147 votes)
25 Oct 2011CPOL20 min read 4.7M   8.4K   331  
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
#ifndef INCLUDE_SF_SERIALIZEFUNDAMENTAL_HPP
#define INCLUDE_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 SF_DEFINE_FUNDAMENTAL_SERIALIZATION(type)                               \
    inline void serialize(SF::Archive &ar, type &t, unsigned int version)       \
    {                                                                           \
        serializeFundamental(ar, t, version);                                   \
    }

    SF_FOR_EACH_FUNDAMENTAL_TYPE( SF_DEFINE_FUNDAMENTAL_SERIALIZATION )

#undef SF_DEFINE_FUNDAMENTAL_SERIALIZATION


} // namespace SF

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