Click here to Skip to main content
15,884,176 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.4K   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.
// 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_SUBINTERFACE_HPP_INCLUDED
#define BOOST_IDL_SUBINTERFACE_HPP_INCLUDED

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

#include <boost/interfaces/access.hpp>
#include <boost/interfaces/detail/flags.hpp>
#include <boost/interfaces/detail/generate.hpp>
#include <boost/interfaces/detail/macros/template_decl.hpp>
#include <boost/interfaces/detail/macros/template_args.hpp>
#include <boost/interfaces/interface_fwd.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/aux_/na.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/transform_view.hpp>
#include <boost/preprocessor/control/expr_if.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>

namespace boost { namespace interfaces {       

//------------------Definition of helper templates----------------------------//   

namespace detail {

template<typename Subinterface, typename Superinterfaces>
struct subinterface_traits {
    template< typename Derived, 
              typename Flags = flags::default_, 
              typename Base = detail::empty_base >
    struct generator_impl {
        struct get_generator {
            template<typename Interface>
            struct apply {
                typedef typename Interface::template
                        generator<Derived, Flags, Base> type;
            };
        };
        typedef typename
                mpl::transform_view<
                    Superinterfaces, 
                    get_generator
                >::type                         generators;           
        typedef typename
                mpl::fold<
                    generators, 
                    empty_base, 
                    generate
                >::type                         type;
        typedef typename type::interface_table  interface_table;
    };
      
    template< typename Derived, 
              typename Flags = flags::default_, 
              typename Base = detail::empty_base >
    struct generator 
        : generator_impl<Derived, Flags, Base>::type 
    { };

    struct type : generator<Subinterface> { 
        template< typename Derived, 
                  typename Flags = flags::default_, 
                  typename Base = detail::empty_base >
        struct generator 
            : subinterface_traits<Subinterface, Superinterfaces>
                   ::template generator<Derived, Flags, Base>
            { };
    };
};

} // End namespace detail.

//------------------Definition of class template subinterface-----------------//

template<typename Subinterface, typename Superinterfaces>
class subinterface 
    : public detail::subinterface_traits<
                 Subinterface, Superinterfaces
             >::type        
{
private:
    friend class interface_core_access;
    typedef typename 
            detail::subinterface_traits<
                 Subinterface, Superinterfaces
             >::type                              base_type;
protected:
    typedef subinterface                          subinterface_;
public:
    typedef Superinterfaces                       superinterfaces;
    typedef typename base_type::interface_table   interface_table; 
};

//------------------Definition of subinterface macros-------------------------//

#define BOOST_SUBINTERFACE_BEGIN(name) \
    struct BOOST_PP_CAT(name, _subinterface_impl_) \
        : ::boost::mpl::identity< ::boost::mpl::list< \
    /**/

#define BOOST_SUBINTERFACE_TEMPLATE_BEGIN(name, arity) \
    BOOST_SUBINTERFACE_BEGIN(name) \
    /**/

#define BOOST_SUPERINTERFACE(name) name, 

#define BOOST_SUBINTERFACE_END_IMPL(name, arity) ::boost::mpl::na> > { }; \
    BOOST_IDL_TEMPLATE_DECL(arity, XXX_) \
    struct name : ::boost::interfaces::subinterface< \
                      name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_), \
                      BOOST_PP_EXPR_IF(arity, typename) \
                          BOOST_PP_CAT(name, _subinterface_impl_) \
                          BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::type \
                  > \
    { \
        friend class ::boost::interfaces::access; \
        typedef BOOST_PP_EXPR_IF(arity, typename) \
                ::boost::interfaces::subinterface< \
                      name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_), \
                      BOOST_PP_EXPR_IF(arity, typename) \
                          BOOST_PP_CAT(name, _subinterface_impl_) \
                          BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::type \
                > base_type; \
        typedef BOOST_PP_EXPR_IF(arity, typename) \
                base_type::interface_table interface_table; \
        template<typename XXX_> \
        struct interface_functions \
            : base_type::template interface_functions<XXX_>, \
             ::boost::interfaces::detail::table_holder<interface_table, XXX_> \
        { \
            using ::boost::interfaces::detail \
                  ::table_holder<interface_table, XXX_>::m_table_; \
        }; \
        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 YYY_) > \
    ::boost::type_traits::yes_type \
    is_interface_helper( name BOOST_IDL_TEMPLATE_ARGS(arity, YYY_)*, \
                         XXX_*, \
                         typename \
                         ::boost::interfaces::detail::enable_if_same< \
                            XXX_, \
                            name BOOST_IDL_TEMPLATE_ARGS(arity, YYY_) \
                         >::type* = 0 ); \
    /**/

#define BOOST_SUBINTERFACE_END(name) BOOST_SUBINTERFACE_END_IMPL(name, 0) 

#define BOOST_SUBINTERFACE_TEMPLATE_END(name, arity) \
    BOOST_SUBINTERFACE_END_IMPL(name, arity) \
    /**/

} } // End namespaces interfaces, boost.

#endif // #ifndef BOOST_IDL_SUBINTERFACE_HPP_INCLUDED

//// (C) Copyright Jonathan Turkanis 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_SUBINTERFACE_HPP_INCLUDED
//#define BOOST_IDL_SUBINTERFACE_HPP_INCLUDED
//
//#if defined(_MSC_VER) && (_MSC_VER >= 1020)
//# pragma once
//#endif
//
//#include <boost/interfaces/access.hpp>
//#include <boost/interfaces/detail/flags.hpp>
//#include <boost/interfaces/detail/generate.hpp>
//#include <boost/interfaces/detail/template_params.hpp>
//#include <boost/interfaces/interface_fwd.hpp>
//#include <boost/mpl/at.hpp>
//#include <boost/mpl/aux_/na.hpp>
//#include <boost/mpl/fold.hpp>
//#include <boost/mpl/identity.hpp>
//#include <boost/mpl/list.hpp>
//#include <boost/mpl/range_c.hpp>
//#include <boost/mpl/size.hpp>
//#include <boost/mpl/transform_view.hpp>
//#include <boost/preprocessor/control/expr_if.hpp>
//#include <boost/preprocessor/punctuation/comma_if.hpp>
//#include <boost/preprocessor/repetition/enum_params.hpp>
//
//namespace boost { namespace interfaces {       
//
////------------------Definition of helper templates----------------------------//   
//
//namespace detail {
//
//template<typename Subinterface, typename Superinterfaces>
//struct subinterface_traits {
//    template< typename Derived, 
//              typename Flags = flags::default_, 
//              typename Base = detail::empty_base >
//    struct generator_impl {
//        struct get_generator {
//            template<typename Interface>
//            struct apply {
//                typedef typename Interface::template
//                        generator<Derived, Flags, Base> type;
//            };
//        };
//        typedef typename
//                mpl::transform_view<
//                    Superinterfaces, 
//                    get_generator
//                >::type                         generators;           
//        typedef typename
//                mpl::fold<
//                    generators, 
//                    empty_base, 
//                    generate
//                >::type                         type;
//        typedef typename type::interface_table  interface_table;
//    };
//      
//    template< typename Derived, 
//              typename Flags = flags::default_, 
//              typename Base = detail::empty_base >
//    struct generator 
//        : generator_impl<Derived, Flags, Base>::type 
//    { };
//
//    struct type : generator<Subinterface> { 
//        template< typename Derived, 
//                  typename Flags = flags::default_, 
//                  typename Base = detail::empty_base >
//        struct generator 
//            : subinterface_traits<Subinterface, Superinterfaces>
//                   ::template generator<Derived, Flags, Base>
//            { };
//    };
//};
//
//} // End namespace detail.
//
////------------------Definition of class template subinterface-----------------//
//
//template<typename Subinterface, typename Superinterfaces>
//class subinterface 
//    : public detail::subinterface_traits<
//                 Subinterface, Superinterfaces
//             >::type        
//{
//private:
//    friend class interface_core_access;
//    typedef typename 
//            detail::subinterface_traits<
//                 Subinterface, Superinterfaces
//             >::type                              base_type;
//protected:
//    typedef subinterface                          subinterface_;
//public:
//    typedef Superinterfaces                       superinterfaces;
//    typedef typename base_type::interface_table   interface_table; 
//};
//
////------------------Definition of subinterface macros-------------------------//
//
//#define BOOST_SUBINTERFACE_BEGIN(name) \
//    struct BOOST_PP_CAT(name, _subinterface_impl_) \
//        : ::boost::mpl::identity< ::boost::mpl::list< \
//    /**/
//
//#define BOOST_SUBINTERFACE_TEMPLATE_BEGIN(name, arity) \
//    BOOST_SUBINTERFACE_BEGIN(name) \
//    /**/
//
//#define BOOST_SUPERINTERFACE(name) name, 
//
//#define BOOST_SUBINTERFACE_END_IMPL(name, arity) ::boost::mpl::na> > { }; \
//    BOOST_IDL_TEMPLATE_DECL(arity, XXX_) \
//    struct name : ::boost::interfaces::subinterface< \
//                      name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_), \
//                      BOOST_PP_EXPR_IF(arity, typename) \
//                          BOOST_PP_CAT(name, _subinterface_impl_) \
//                          BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::type \
//                  > \
//    { \
//        friend class ::boost::interfaces::access; \
//        typedef BOOST_PP_EXPR_IF(arity, typename) \
//                ::boost::interfaces::subinterface< \
//                      name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_), \
//                      BOOST_PP_EXPR_IF(arity, typename) \
//                          BOOST_PP_CAT(name, _subinterface_impl_) \
//                          BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::type \
//                > base_type; \
//        typedef BOOST_PP_EXPR_IF(arity, typename) \
//                base_type::interface_table interface_table; \
//        template<typename XXX_> \
//        struct interface_functions \
//            : base_type::template interface_functions<XXX_> \
//        { static const interface_table m_table_; }; \
//        BOOST_IDL_CTORS(name, XXX_) \
//        BOOST_IDL_CORE_INTERFACE_SUPPORT(name) \
//    }; \
//    BOOST_IDL_TEMPLATE_DECL(arity, XXX_) \
//    template<typename XXX_> \
//    const BOOST_PP_EXPR_IF(arity, typename) name \
//        BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::interface_table \
//        name BOOST_IDL_TEMPLATE_ARGS(arity, XXX_)::template  \
//            interface_functions<XXX_>::m_table_ = \
//                ::boost::mpl::identity<XXX_>(); \
//    template< typename XXX_ BOOST_PP_COMMA_IF(arity) \
//              BOOST_PP_ENUM_PARAMS(arity, typename YYY_) > \
//    ::boost::type_traits::yes_type \
//    is_interface_helper( name BOOST_IDL_TEMPLATE_ARGS(arity, YYY_)*, \
//                         XXX_*, \
//                         typename \
//                         ::boost::interfaces::detail::enable_if_same< \
//                            XXX_, \
//                            name BOOST_IDL_TEMPLATE_ARGS(arity, YYY_) \
//                         >::type* = 0 ); \
//    /**/
//
//#define BOOST_SUBINTERFACE_END(name) BOOST_SUBINTERFACE_END_IMPL(name, 0) 
//
//#define BOOST_SUBINTERFACE_TEMPLATE_END(name, arity) \
//    BOOST_SUBINTERFACE_END_IMPL(name, arity) \
//    /**/
//
//} } // End namespaces interfaces, boost.
//
//#endif // #ifndef BOOST_IDL_SUBINTERFACE_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