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
#include <SF/DataPtr.hpp>

#include <SF/Tools.hpp>

namespace SF {

    // TODO: need to be able to pass a preallocated buffer to DataPtr<T>, and use that if its big enough
    DataPtr::DataPtr() : 
        ptr_(buffer_), 
        length_(), 
        allocatedLength_(), 
        whichDeleter_(), 
        pfn_deleter_() 
    {}

    // TODO: ptr_ should never be NULL, so take action if the following 2 ctors recieve NULL pointers
    DataPtr::DataPtr(const T *sz) : 
        ptr_( const_cast<T*>(sz) ), 
        length_( length(sz) ), 
        allocatedLength_(length_+1), 
        whichDeleter_(0), 
        pfn_deleter_(NULL) 
    { 
        // Weak attach
        SF_ASSERT(ptr_); 
    }

    DataPtr::DataPtr(const T *sz, UInt32 length) : 
        ptr_( const_cast<T*>(sz) ), 
        length_(length), 
        allocatedLength_(length_), 
        whichDeleter_(), 
        pfn_deleter_(NULL) 
    { 
        // Weak attach
        SF_ASSERT(ptr_); 
    }

    DataPtr::DataPtr(const DataPtr &rhs) : 
        ptr_(rhs.ptr_), 
        length_(rhs.length_), 
        allocatedLength_(rhs.allocatedLength_), 
        whichDeleter_(0), 
        pfn_deleter_(NULL) 
    {
        // Weak copy
    } 

    DataPtr &DataPtr::operator=(const DataPtr &rhs) 
    {
        // Weak copy
        ptr_ = rhs.ptr_; 
        length_ = rhs.length_; 
        allocatedLength_ = rhs.allocatedLength_; 
        whichDeleter_ = 0; 
        pfn_deleter_ = NULL; 
        return *this; 
    } 
    
    DataPtr::~DataPtr() 
    { 
        release(); 
    }

    void DataPtr::assign(const T *sz, UInt32 length) 
    { 
        allocate(length); 
        memmove( get(), sz, length*sizeof(T) ); 
    }

    void DataPtr::assign(const T *sz) 
    { 
        assign(sz, length(sz)); 
    }
    
    void DataPtr::assign(const std::string &s) 
    { 
        assign(s.c_str(), s.length()); 
    }

    void DataPtr::release() 
    {
        switch (whichDeleter_) 
        {
        case 0: 
            break;
        case 1: 
            delete ptr_;  
            break;
        case 2: 
            delete [] ptr_; 
            break;
        case 3: 
            pfn_deleter_(ptr_); 
            break;
        default: 
            SF_ASSERT(0);
        }
        ptr_ = NULL; 
        length_ = 0; 
        whichDeleter_ = 0;
        pfn_deleter_ = NULL;
    }

    UInt32 DataPtr::allocate(UInt32 length) 
    { 
        release();
        if (length <= 63) 
        {
            ptr_ = buffer_;
            length_ = length;
            ptr_[length_] = 0;
            allocatedLength_ = length_ + 1;
            whichDeleter_ = 0;
            pfn_deleter_ = NULL;
        }
        else 
        {
            ptr_ = new T[length + 1];
            allocatedLength_ = length + 1;
            ptr_[length] = 0;
            length_ = length;
            whichDeleter_ = 2;
            pfn_deleter_ = NULL;
        }
        return length; 
    }

    void DataPtr::terminatebufferwithzero() const
    {
        if (ptr_ && ptr_[length_] != 0) 
        {
            if (allocatedLength_ > length_) 
            {
                ptr_[length_] = 0;
            }
            else if (allocatedLength_ == length_) 
            {
                // TODO: reallocate, this time with extra space for the trailing zero.
                SF_ASSERT(0);                
            }
        }
    }

    void DataPtr::update_length() 
    {
        if (ptr_)
            length_ = length(ptr_);
    }

    DataPtr::T *DataPtr::get() const 
    { 
        return ptr_; 
    }
    
    UInt32 DataPtr::length() const 
    { 
        return length_; 
    }
    
    bool DataPtr::empty() const 
    { 
        return length_ == 0; 
    }
    
    std::string DataPtr::cpp_str() const 
    { 
        return std::string(static_cast<char *>(ptr_), length_); 
    }

    UInt32 DataPtr::length(const T *sz)
    {
        if (sz) 
        {
            UInt32 i=0;
            while (sz[i])
                i++;
            return i;
        }
        else
            return 0;
    }

    bool operator<(const DataPtr &lhs, const DataPtr &rhs) 
    {
        typedef Byte8 T;
        SF_ASSERT(lhs.get());
        SF_ASSERT(rhs.get());
        if (lhs.length() < rhs.length())
            return true;
        else if (lhs.length() > rhs.length())
            return false;
        else {
            SF_ASSERT(lhs.length() == rhs.length());
            return memcmp(lhs.get(), rhs.get(), lhs.length()*sizeof(T)) < 0; 
        }
    }

    bool operator==(const DataPtr &lhs, const DataPtr &rhs) 
    { 
        typedef Byte8 T;
        SF_ASSERT(lhs.get());
        SF_ASSERT(rhs.get());
        return 
            lhs.length() == rhs.length() && 
            0 == memcmp(lhs.get(), rhs.get(), lhs.length()*sizeof(T)); 
    }

    bool operator!=(const DataPtr &lhs, const DataPtr &rhs) 
    { 
        return ! (lhs == rhs); 
    }

} // namespace SF

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