Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C++

Using Interfaces for Object Oriented Primitives

Rate me:
Please Sign up or sign in to vote.
4.65/5 (11 votes)
7 Dec 20047 min read 57.5K   369   27  
An introduction to the OOTL (Object Oriented Template Library). Describes how the OOTL uses a bleeding-edge technique of defining interface types in C++ to provide lightweight object-oriented primitives with run-time polymorphism through an IObject interface.
// (C) Copyright Jonathan Turkanis 2004.
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
// (C) Copyright 2001, 2002, 2003 Peter Dimov
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)

// Based on <boost/shared_ptr.hpp>

// Disclaimer: Not a Boost library.

#ifndef BOOST_INTERFACES_SHARED_INTERFACE_PTR_HPP_INCLUDED
#define BOOST_INTERFACES_SHARED_INTERFACE_PTR_HPP_INCLUDED

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include <boost/config.hpp>     // Member template friends, BOOST_MSVC.

#include <algorithm>            // swap.
#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
#include <boost/detail/shared_count.hpp>
#include <boost/interfaces/interface_access.hpp>
#include <boost/interfaces/interface_fwd.hpp>

namespace boost { namespace interfaces {

template<typename Interface>
class shared_interface_ptr 

#ifndef BOOST_INTERFACES_NO_ACCESS_CONTROL
    : private Interface 
#else
    : public Interface 
#endif

{
private:
    typedef typename Interface::interface_table  interface_table;
    typedef interface_core_access                access;
    #ifndef BOOST_INTERFACES_NO_ACCESS_CONTROL
        template<typename T>
        friend class shared_interface_ptr;
    #endif
public:
    typedef Interface                            element_type;

        // Constructors

    shared_interface_ptr() : pn() { }

    template<typename Subinterface>
    shared_interface_ptr(const shared_interface_ptr<Subinterface>& p) 
        : Interface(p), pn(p.pn)
        { }

    template<class T>
    explicit shared_interface_ptr(T * t) 
        : Interface(*t), pn(t, checked_deleter<T>())
        { }

    template<class T, class D> 
    shared_interface_ptr(T* t, D d)
        : Interface(t), pn(t, d)
        { }

          // Assignment

    template<class T>
    shared_ptr & operator=(const shared_interface_ptr<T>& p)
    {
        Interface::operator=(p);
        pn = p.pn;
        return *this;
    }

          // Smart pointer interface

    element_type* get() const 
    { return &const_cast<Interface&>(self()); }

    element_type& operator*() const 
    { return const_cast<Interface&>(self()); }

    element_type* operator->() const { return get(); }

    void reset()
    { shared_interface_ptr<T>().swap(*this); }

    template<class T> 
    void reset(T* t)
    {
        BOOST_ASSERT(t == 0 || t != get_pointer());
        this_type(p).swap(*this);
    }

    template<class T, class D> 
    void reset(T* t, D d)
    {
         shared_interface_ptr<T>(p, d).swap(*this);
    }

    void swap(shared_interface_ptr<T> & other)
    {
        std::swap(*self(), *other.self());
        pn.swap(other.pn);
    }
private:
    element_type& self() { return *this; }
    const element_type& self() const{ return *this; }

    const void* get_pointer() const
    { return access::get_interface_pointer_(self()); }

    const interface_table* get_table() const
    { return access::get_interface_table_(self()); }

    template<typename Subinterface>
    const void* get_pointer(const Subinterface& other) const
    { return access::get_interface_pointer_(other); }

    template<typename Subinterface>
    const typename Subinterface::interface_table* 
    get_table(const Subinterface& other) const
    { return access::get_interface_table_(other); }

BOOST_INTERFACES_PRIVATE_ACCESS_SPECIFIER()
    detail::shared_count pn;   // reference counter
};

} } // End namespace interfaces, boost.

#endif // #ifndef BOOST_INTERFACES_SHARED_INTERFACE_PTR_HPP_INCLUDED

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Ara 3D
Canada Canada
I am the designer of the Plato programming language and I am the founder of Ara 3D. I can be reached via email at cdiggins@gmail.com

Comments and Discussions