Click here to Skip to main content
15,881,803 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.3K   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 David Abrahams 2004.
// 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.)

// Disclaimer: Not a Boost library.

#ifndef BOOST_IDL_DETAIL_IDL_END_HPP_INCLUDED
#define BOOST_IDL_DETAIL_IDL_HPP_INCLUDED

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

#include <boost/interfaces/interface_fwd.hpp>
#include <boost/interfaces/is_interface.hpp>
#include <boost/interfaces/access.hpp>
#include <boost/interfaces/detail/generate.hpp>
#include <boost/interfaces/detail/interface_base.hpp>
#include <boost/interfaces/detail/macros/befriend_template.hpp>
#include <boost/interfaces/detail/macros/interface_support.hpp>
#include <boost/interfaces/detail/macros/template_args.hpp>
#include <boost/interfaces/detail/macros/template_decl.hpp>
#include <boost/interfaces/detail/util.hpp>    // disable_if_same, always_false, 
#include <boost/mpl/bool.hpp>                  // table_holder, augmented_table.
#include <boost/mpl/fold.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/control/expr_if.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/type_traits/detail/yes_no_type.hpp>

#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>

#define BOOST_INTERFACE_END_INTERFACE_TABLE() \
     typedef BOOST_IDL_AUGMENTED_TABLE( \
              typename base_type_::interface_table \
            ) interface_table; \
    /**/

#define BOOST_INTERFACE_END_IMPL(name, arity) \
        ::boost::mpl::na > > { }; \
      public: \
      struct index { \
          template<typename N> \
          struct apply \
            : tracker_< ::boost::mpl::int_<N::value>, int > \
              { }; \
      }; \
    }; \
  }; \
  BOOST_IDL_TEMPLATE_DECL(arity, XXX_) \
  class name \
      : public ::boost::interfaces::detail::interface_base< \
                 BOOST_PP_CAT(name, _interface_impl_) \
                   BOOST_IDL_TEMPLATE_ARGS(arity, XXX_), \
                 __LINE__ - BOOST_PP_CAT(name, _interface_impl_) \
                   BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::start_line_ \
               >::BOOST_PP_EXPR_IF(arity, template) generator< \
                 name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_) \
               > \
  { \
  private: \
    typedef BOOST_PP_CAT(name, _interface_impl_) \
            BOOST_IDL_TEMPLATE_ARGS(arity, XXX_) implementation_type; \
    typedef BOOST_PP_EXPR_IF(arity, typename) \
            ::boost::interfaces::detail::interface_base< \
                 implementation_type, \
                 __LINE__ - implementation_type::start_line_ \
               >::BOOST_PP_EXPR_IF(arity, template) generator< \
                 name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_) \
            > base_type_; \
    friend class ::boost::interfaces::access; \
  public: \
    BOOST_PP_EXPR_IF(arity, BOOST_INTERFACE_END_INTERFACE_TABLE()) \
    template<typename XXX_> \
    struct interface_functions \
        : base_type_::BOOST_PP_EXPR_IF(arity, template) interface_functions<XXX_>, \
          ::boost::interfaces::detail::table_holder<interface_table, XXX_> \
        { }; \
    template< typename DDD_, \
              typename CCC_ = ::boost::mpl::false_, \
              typename BBB_ = ::boost::interfaces::detail::empty_base > \
    struct generator \
      : ::boost::interfaces::detail::interface_base< \
           BOOST_PP_CAT(name, _interface_impl_) \
             BOOST_IDL_TEMPLATE_ARGS(arity, XXX_), \
            __LINE__ - BOOST_PP_CAT(name, _interface_impl_) \
              BOOST_IDL_TEMPLATE_ARGS(arity, XXX_) ::start_line_ \
         >::BOOST_PP_EXPR_IF(arity, template) generator<DDD_, CCC_, BBB_> \
      { }; \
    BOOST_IDL_CTORS(name, XXX_) \
    BOOST_IDL_CORE_INTERFACE_SUPPORT(name) \
  }; \
  template< typename XXX_ BOOST_PP_COMMA_IF(arity) \
            BOOST_PP_ENUM_PARAMS(arity, typename XXX_) > \
  ::boost::type_traits::yes_type \
  is_interface_helper( name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)*, \
                       XXX_*, \
                       typename \
                       ::boost::interfaces::detail::enable_if_same< \
                         XXX_, \
                         name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_) \
                       >::type* = 0 ); \
  /**/

#define BOOST_IDL_END(name) BOOST_INTERFACE_END_IMPL(name, 0) 

#define BOOST_IDL_END_TEMPLATE(name, arity) \
    BOOST_INTERFACE_END_IMPL(name, arity)         \
    /**/

#endif // #ifndef BOOST_IDL_DETAIL_IDL_END_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