os/ossrv/ossrv_pub/boost_apis/boost/pending/detail/property.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//  (C) Copyright Jeremy Siek 2004 
sl@0
     2
//  Distributed under the Boost Software License, Version 1.0. (See
sl@0
     3
//  accompanying file LICENSE_1_0.txt or copy at
sl@0
     4
//  http://www.boost.org/LICENSE_1_0.txt)
sl@0
     5
sl@0
     6
#ifndef BOOST_DETAIL_PROPERTY_HPP
sl@0
     7
#define BOOST_DETAIL_PROPERTY_HPP
sl@0
     8
sl@0
     9
#include <utility> // for std::pair
sl@0
    10
#include <boost/type_traits/same_traits.hpp> // for is_same
sl@0
    11
sl@0
    12
namespace boost {
sl@0
    13
sl@0
    14
  namespace detail {
sl@0
    15
sl@0
    16
    template <class PropertyTag1, class PropertyTag2>
sl@0
    17
    struct same_property {
sl@0
    18
      enum { value = is_same<PropertyTag1,PropertyTag2>::value };
sl@0
    19
    };
sl@0
    20
sl@0
    21
    struct error_property_not_found { };
sl@0
    22
sl@0
    23
    template <int TagMatched>
sl@0
    24
    struct property_value_dispatch {
sl@0
    25
      template <class PropertyTag, class T, class Tag>
sl@0
    26
      inline static T& get_value(PropertyTag& p, T*, Tag) {
sl@0
    27
        return p.m_value; 
sl@0
    28
      }
sl@0
    29
      template <class PropertyTag, class T, class Tag>
sl@0
    30
      inline static const T& const_get_value(const PropertyTag& p, T*, Tag) {
sl@0
    31
        return p.m_value; 
sl@0
    32
      }
sl@0
    33
    };
sl@0
    34
sl@0
    35
    template <class PropertyList>
sl@0
    36
    struct property_value_end {
sl@0
    37
      template <class T> struct result { typedef T type; };
sl@0
    38
sl@0
    39
      template <class T, class Tag>
sl@0
    40
      inline static T& get_value(PropertyList& p, T* t, Tag tag) {
sl@0
    41
        typedef typename PropertyList::next_type Next;
sl@0
    42
        typedef typename Next::tag_type Next_tag;
sl@0
    43
        enum { match = same_property<Next_tag,Tag>::value };
sl@0
    44
        return property_value_dispatch<match>
sl@0
    45
          ::get_value(static_cast<Next&>(p), t, tag);
sl@0
    46
      }
sl@0
    47
      template <class T, class Tag>
sl@0
    48
      inline static const T& const_get_value(const PropertyList& p, T* t, Tag tag) {
sl@0
    49
        typedef typename PropertyList::next_type Next;
sl@0
    50
        typedef typename Next::tag_type Next_tag;
sl@0
    51
        enum { match = same_property<Next_tag,Tag>::value };
sl@0
    52
        return property_value_dispatch<match>
sl@0
    53
          ::const_get_value(static_cast<const Next&>(p), t, tag);
sl@0
    54
      }
sl@0
    55
    };
sl@0
    56
    template <>
sl@0
    57
    struct property_value_end<no_property> {
sl@0
    58
      template <class T> struct result { 
sl@0
    59
        typedef detail::error_property_not_found type; 
sl@0
    60
      };
sl@0
    61
sl@0
    62
      // Stop the recursion and return error
sl@0
    63
      template <class T, class Tag>
sl@0
    64
      inline static detail::error_property_not_found&
sl@0
    65
      get_value(no_property&, T*, Tag) {
sl@0
    66
        static error_property_not_found s_prop_not_found;
sl@0
    67
        return s_prop_not_found;
sl@0
    68
      }
sl@0
    69
      template <class T, class Tag>
sl@0
    70
      inline static const detail::error_property_not_found&
sl@0
    71
      const_get_value(const no_property&, T*, Tag) {
sl@0
    72
        static error_property_not_found s_prop_not_found;
sl@0
    73
        return s_prop_not_found;
sl@0
    74
      }
sl@0
    75
    };
sl@0
    76
sl@0
    77
    template <>
sl@0
    78
    struct property_value_dispatch<0> {
sl@0
    79
      template <class PropertyList, class T, class Tag>
sl@0
    80
      inline static typename property_value_end<PropertyList>::template result<T>::type&
sl@0
    81
      get_value(PropertyList& p, T* t, Tag tag) {
sl@0
    82
        return property_value_end<PropertyList>::get_value(p, t, tag);
sl@0
    83
      }
sl@0
    84
      template <class PropertyList, class T, class Tag>
sl@0
    85
      inline static const typename property_value_end<PropertyList>::template result<T>::type&
sl@0
    86
      const_get_value(const PropertyList& p, T* t, Tag tag) {
sl@0
    87
        return property_value_end<PropertyList>::const_get_value(p, t, tag);
sl@0
    88
      }
sl@0
    89
    };
sl@0
    90
sl@0
    91
    template <class PropertyList>
sl@0
    92
    struct build_property_tag_value_alist
sl@0
    93
    {
sl@0
    94
      typedef typename PropertyList::next_type NextProperty;
sl@0
    95
      typedef typename PropertyList::value_type Value;
sl@0
    96
      typedef typename PropertyList::tag_type Tag;
sl@0
    97
      typedef typename build_property_tag_value_alist<NextProperty>::type Next;
sl@0
    98
      typedef std::pair< std::pair<Tag,Value>, Next> type;
sl@0
    99
    };
sl@0
   100
    template <>
sl@0
   101
    struct build_property_tag_value_alist<no_property>
sl@0
   102
    {
sl@0
   103
      typedef no_property type;
sl@0
   104
    };
sl@0
   105
sl@0
   106
#if !defined BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
sl@0
   107
    template <class TagValueAList, class Tag>
sl@0
   108
    struct extract_value {
sl@0
   109
      typedef error_property_not_found type;
sl@0
   110
    };
sl@0
   111
    template <class Value, class Tag1, class Tag2, class Rest>
sl@0
   112
    struct extract_value< std::pair<std::pair<Tag1,Value>,Rest>, Tag2> {
sl@0
   113
      typedef typename extract_value<Rest,Tag2>::type type;
sl@0
   114
    };
sl@0
   115
    template <class Value, class Tag, class Rest>
sl@0
   116
    struct extract_value< std::pair<std::pair<Tag,Value>,Rest>, Tag> {
sl@0
   117
      typedef Value type;
sl@0
   118
    };
sl@0
   119
#else
sl@0
   120
    // VC++ workaround:
sl@0
   121
    // The main idea here is to replace partial specialization with
sl@0
   122
    // nested template member classes. Of course there is the
sl@0
   123
    // further complication that the outer class of the nested
sl@0
   124
    // template class cannot itself be a template class.
sl@0
   125
    // Hence the need for the ev_selector. -JGS
sl@0
   126
sl@0
   127
    struct recursive_extract;
sl@0
   128
    struct end_extract;
sl@0
   129
sl@0
   130
    template <class TagValueAList>
sl@0
   131
    struct ev_selector { typedef recursive_extract type; };
sl@0
   132
    template <>
sl@0
   133
    struct ev_selector<no_property> { typedef end_extract type; };
sl@0
   134
sl@0
   135
    struct recursive_extract {
sl@0
   136
      template <class TagValueAList, class Tag1>
sl@0
   137
      struct bind_ {
sl@0
   138
        typedef typename TagValueAList::first_type AListFirst;
sl@0
   139
        typedef typename AListFirst::first_type Tag2;
sl@0
   140
        typedef typename AListFirst::second_type Value;
sl@0
   141
        enum { match = same_property<Tag1,Tag2>::value };
sl@0
   142
        typedef typename TagValueAList::second_type Next;
sl@0
   143
        typedef typename ev_selector<Next>::type Extractor;
sl@0
   144
        typedef typename boost::ct_if< match, Value, 
sl@0
   145
          typename Extractor::template bind_<Next,Tag1>::type
sl@0
   146
        >::type type;
sl@0
   147
      };
sl@0
   148
    };
sl@0
   149
    struct end_extract {
sl@0
   150
      template <class AList, class Tag1>
sl@0
   151
      struct bind_ {
sl@0
   152
        typedef error_property_not_found type;
sl@0
   153
      };
sl@0
   154
    };
sl@0
   155
#endif //!defined BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
sl@0
   156
sl@0
   157
  } // namespace detail 
sl@0
   158
} // namespace boost
sl@0
   159
sl@0
   160
#endif // BOOST_DETAIL_PROPERTY_HPP