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
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
//*****************************************************************************
// 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_STREAM_HPP_
#define _SF_STREAM_HPP_

#include <iosfwd>
#include <map>
#include <string>

#include <boost/noncopyable.hpp>

#include <SF/Archive.hpp>
#include <SF/DataPtr.hpp>
#include <SF/Encoding.hpp>
#include <SF/I_Stream.hpp>

// TODO: placed here for users convenience, maybe put it somewhere else
#include <SF/SerializeFundamental.hpp>
#include <SF/SerializeDynamicArray.hpp>
#include <SF/SerializeStaticArray.hpp>

namespace SF {

    //**************************************************
    // Encoding of object data

    template<typename E>
    class Encoding : public I_Encoding
    {
    public:
        virtual UInt32 getCount(DataPtr &data, const std::type_info &type)
        { 
            return CountElements<E>()(data, type); 
        }

        virtual void toData(DataPtr &data, void *pvObject, const std::type_info &type, int nCount)
        {
            EncodeElements<E>()(data, pvObject, type, nCount);
        }

        virtual void toObject(DataPtr &data, void *pvObject, const std::type_info &type, int nCount)
        { 
            DecodeElements<E>()(data, pvObject, type, nCount);
        }
    };

    template<typename E>
    class WithEncoding : public I_WithEncoding
    {
    public:
        virtual I_Encoding &getEncoding() { return e; }
    private:
        E e;
    };

    //**************************************************
    // Context handling

    // Context

    class ContextRead : public I_ContextRead
    {
    public:
        ContextRead();
        virtual ~ContextRead();
        virtual void add(SF::UInt32 nid, const ObjectId &id);
        virtual void add(void *ptr, const std::type_info &objType, void *pObj);
        virtual bool query(SF::UInt32 nid, ObjectId &id);
        virtual bool query(void *ptr, const std::type_info &objType, void *&pObj);
        virtual void clear();
    private:
        bool bEnabled_;
        std::map<UInt32, ObjectId> nid_id_;
        std::map<std::string, std::map< void *, void * > > type_ptr_obj_;
    };

    class ContextWrite : public I_ContextWrite
    {
    public:
        ContextWrite();
        virtual ~ContextWrite();
        virtual void setEnabled(bool enabled);
        virtual bool getEnabled();
        virtual void add(const ObjectId &id, UInt32 &nid);
        virtual bool query(const ObjectId &id, UInt32 &nid);
        virtual void clear();
    private:
        bool bEnabled_;
        UInt32 currentId_;
        std::map<ObjectId, UInt32> id_nid_;
    };

    class WithContextRead : public I_WithContextRead
    {
    public:
        virtual ~WithContextRead();
        virtual I_ContextRead &getContext();
    private:
        ContextRead context;
    };

    class WithContextWrite : public I_WithContextWrite
    {
    public:
        virtual ~WithContextWrite();
        virtual I_ContextWrite &getContext();
        void enableContext() { getContext().setEnabled(true); }
        void disableContext() { getContext().setEnabled(false); }
        void clearContext() { getContext().clear(); }
    private:
        ContextWrite context;
    };

    //**************************************************
    // Stream local storage

    class LocalStorage : public I_LocalStorage, boost::noncopyable
    {
    public:
        LocalStorage();
        virtual ~LocalStorage();
        virtual void set(const std::type_info &objType, void *pObj);
        virtual void *get(const std::type_info &objType);
    private:
        std::map< std::string, void * > type_ptr_;
    };

    class WithLocalStorage : public I_WithLocalStorage
    {
    public:
        virtual ~WithLocalStorage();
        virtual LocalStorage &getLocalStorage();
    private:
        LocalStorage localStorage_;
    };

    //****************************************************

    class WithSemanticsRead : public I_Stream
    {
    public:
        virtual ~WithSemanticsRead() {}

        // input dispatching

        template<typename T>
        WithSemanticsRead &operator>>(T &t)
        {
            Archive(Archive::READ, static_cast<I_Stream *>(this) ) & t;
            return *this;
        }

        template<typename T>
        WithSemanticsRead &operator>>(const T &t)
        {
            Archive(Archive::READ, static_cast<I_Stream *>(this) ) & t;
            return *this;
        }
        
    };

    class WithSemanticsWrite : public I_Stream
    {
    public:
        virtual ~WithSemanticsWrite() {}

        template<typename T>
        WithSemanticsWrite &operator<<(T &t)
        {
            Archive(Archive::WRITE, static_cast<I_Stream *>(this) ) & t;
            return *this;
        }

        template<typename T>
        WithSemanticsWrite &operator<<(const T &t)
        {
            Archive(Archive::WRITE, static_cast<I_Stream *>(this) ) & t;
            return *this;
        }

    };


    // stream base classes

    typedef Encoding<Text> EncodingText;
    typedef Encoding<BinaryNative> EncodingBinaryNative;
    typedef Encoding<BinaryPortable> EncodingBinaryPortable;

    typedef WithEncoding<EncodingText> WithEncodingText;
    typedef WithEncoding<EncodingBinaryNative> WithEncodingBinaryNative;
    typedef WithEncoding<EncodingBinaryPortable> WithEncodingBinaryPortable;

    class Node;

    class IStream :
        public WithSemanticsRead,
        public WithContextRead,
        public WithFormatRead,
        public WithLocalStorage,
        boost::noncopyable
    {
    public:
        IStream(std::istream &is);
    private:
        bool begin(Node &node);
        bool get(DataPtr &value);
        void end();
        UInt32 read_int(UInt32 &n);
        UInt32 read_byte(Byte8 &byte);
        UInt32 read(Byte8 *pBytes, UInt32 nLength);
        void putback_byte(Byte8 byte);
        std::istream &is;
    };

    class OStream :
        public WithSemanticsWrite,
        public WithContextWrite,
        public WithFormatWrite,
        public WithLocalStorage,
        boost::noncopyable
    {
    public:
        OStream(std::ostream &os);
    private:
        void begin(const Node &node);
        void put(const DataPtr &value);
        void end();
        UInt32 write_int(UInt32 n);
        UInt32 write_byte(Byte8 byte);
        UInt32 write_chararray(const Char8 *pChar, UInt32 nLength);
        UInt32 write(const Byte8 *pBytes, UInt32 nLength);
        std::ostream &os;
    };

} // namespace SF


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

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 | Mobile
Web04 | 2.8.140709.1 | Last Updated 6 Aug 2009
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid