Boost C++ Libraries Boost.Bimap Home Libraries People FAQ More

PrevUpHomeNext

functor_bag.hpp

Go to the documentation of this file.
00001 // Boost.Bimap
00002 //
00003 // Copyright (c) 2006-2007 Matias Capeletto
00004 //
00005 // Distributed under the Boost Software License, Version 1.0.
00006 // (See accompanying file LICENSE_1_0.txt or copy at
00007 // http://www.boost.org/LICENSE_1_0.txt)
00008 
00009 /// \file container_adaptor/detail/functor_bag.hpp
00010 /// \brief Defines a EBO optimizacion helper for functors.
00011 
00012 #ifndef BOOST_BIMAP_CONTAINER_ADAPTOR_DETAIL_FUNCTOR_BAG_HPP
00013 #define BOOST_BIMAP_CONTAINER_ADAPTOR_DETAIL_FUNCTOR_BAG_HPP
00014 
00015 #if defined(_MSC_VER) && (_MSC_VER>=1200)
00016 #pragma once
00017 #endif
00018 
00019 #include <boost/config.hpp>
00020 
00021 #if defined(BOOST_MSVC)
00022 // This bogus warning will appear when add_const is applied to a
00023 // const volatile reference because we can't detect const volatile
00024 // references with MSVC6.
00025 #   pragma warning(push)
00026 #   pragma warning(disable:4181)
00027 // warning C4181: qualifier applied to reference type ignored
00028 #endif
00029 
00030 #include <boost/mpl/placeholders.hpp>
00031 
00032 #include <boost/type_traits/add_reference.hpp>
00033 #include <boost/type_traits/is_base_of.hpp>
00034 
00035 #include <boost/mpl/inherit_linearly.hpp>
00036 #include <boost/mpl/inherit.hpp>
00037 
00038 namespace boost {
00039 namespace bimaps {
00040 namespace container_adaptor {
00041 namespace detail {
00042 
00043 /// \brief EBO optimizacion helper for functors
00044 /**
00045 
00046 This class is a generalization of a helper class explained in an article by
00047 Nathan C. Myers.\n
00048 See it at \link http://www.cantrip.org/emptyopt.html
00049                                                                                     **/
00050 
00051 template < class Data, class FunctorList >
00052 struct data_with_functor_bag :
00053 
00054     public mpl::inherit_linearly<
00055 
00056         FunctorList,
00057         mpl::if_< is_base_of< mpl::_2, mpl::_1 >,
00058         //   {
00059                  mpl::_1,
00060         //   }
00061         //   else
00062         //   {
00063                  mpl::inherit< mpl::_1, mpl::_2 >
00064         //   }
00065         >
00066 
00067     >::type
00068 {
00069     Data data;
00070     data_with_functor_bag() {}
00071     data_with_functor_bag(BOOST_DEDUCED_TYPENAME add_reference<Data>::type const d) 
00072         : data(d) {}
00073 
00074     template< class Functor >
00075     Functor& functor()
00076     {
00077         return *(static_cast<Functor*>(this));
00078     }
00079 
00080     template< class Functor >
00081     const Functor& functor() const
00082     {
00083         return *(static_cast<Functor const *>(this));
00084     }
00085 };
00086 
00087 } // namespace detail
00088 } // namespace container_adaptor
00089 } // namespace bimaps
00090 } // namespace boost
00091 
00092 #if defined(BOOST_MSVC)
00093 #   pragma warning(pop)
00094 #endif
00095 
00096 #endif // BOOST_BIMAP_CONTAINER_ADAPTOR_DETAIL_FUNCTOR_BAG_HPP
00097 
00098 
Copyright 2006 Matias Capeletto

PrevUpHomeNext