os/ossrv/ossrv_pub/boost_apis/boost/lambda/casts.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// - casts.hpp -- BLambda Library -------------
sl@0
     2
//
sl@0
     3
// Copyright (C) 2000 Gary Powell (powellg@amazon.com)
sl@0
     4
// Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
sl@0
     5
//
sl@0
     6
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     7
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     8
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     9
//
sl@0
    10
// For more information, see http://www.boost.org
sl@0
    11
sl@0
    12
// -----------------------------------------------
sl@0
    13
sl@0
    14
#if !defined(BOOST_LAMBDA_CASTS_HPP)
sl@0
    15
#define BOOST_LAMBDA_CASTS_HPP
sl@0
    16
sl@0
    17
#include <typeinfo>
sl@0
    18
sl@0
    19
namespace boost { 
sl@0
    20
namespace lambda {
sl@0
    21
sl@0
    22
template<class T> class cast_action;
sl@0
    23
sl@0
    24
template<class T> class static_cast_action;
sl@0
    25
template<class T> class dynamic_cast_action;
sl@0
    26
template<class T> class const_cast_action;
sl@0
    27
template<class T> class reinterpret_cast_action;
sl@0
    28
sl@0
    29
class typeid_action;
sl@0
    30
class sizeof_action;
sl@0
    31
sl@0
    32
// Cast actions
sl@0
    33
sl@0
    34
template<class T> class cast_action<static_cast_action<T> > 
sl@0
    35
{
sl@0
    36
public:
sl@0
    37
  template<class RET, class Arg1>
sl@0
    38
  static RET apply(Arg1 &a1) {
sl@0
    39
    return static_cast<RET>(a1);
sl@0
    40
  }
sl@0
    41
};
sl@0
    42
sl@0
    43
template<class T> class cast_action<dynamic_cast_action<T> > {
sl@0
    44
public:
sl@0
    45
  template<class RET, class Arg1>
sl@0
    46
  static RET apply(Arg1 &a1) {
sl@0
    47
    return dynamic_cast<RET>(a1);
sl@0
    48
  }
sl@0
    49
};
sl@0
    50
sl@0
    51
template<class T> class cast_action<const_cast_action<T> > {
sl@0
    52
public:
sl@0
    53
  template<class RET, class Arg1>
sl@0
    54
  static RET apply(Arg1 &a1) {
sl@0
    55
    return const_cast<RET>(a1);
sl@0
    56
  }
sl@0
    57
};
sl@0
    58
sl@0
    59
template<class T> class cast_action<reinterpret_cast_action<T> > {
sl@0
    60
public:
sl@0
    61
  template<class RET, class Arg1>
sl@0
    62
  static RET apply(Arg1 &a1) {
sl@0
    63
    return reinterpret_cast<RET>(a1);
sl@0
    64
  }
sl@0
    65
};
sl@0
    66
sl@0
    67
  // typedid action
sl@0
    68
class typeid_action {
sl@0
    69
public:
sl@0
    70
  template<class RET, class Arg1>
sl@0
    71
  static RET apply(Arg1 &a1) {
sl@0
    72
    return typeid(a1);
sl@0
    73
  }
sl@0
    74
};
sl@0
    75
sl@0
    76
// sizeof action
sl@0
    77
class sizeof_action
sl@0
    78
{
sl@0
    79
public:
sl@0
    80
  template<class RET, class Arg1>
sl@0
    81
  static RET apply(Arg1 &a1) {
sl@0
    82
    return sizeof(a1);
sl@0
    83
  }
sl@0
    84
};
sl@0
    85
sl@0
    86
sl@0
    87
// return types of casting lambda_functors (all "T" type.)
sl@0
    88
sl@0
    89
template<template <class> class cast_type, class T, class A>
sl@0
    90
struct return_type_N<cast_action< cast_type<T> >, A> { 
sl@0
    91
  typedef T type;
sl@0
    92
};
sl@0
    93
sl@0
    94
// return type of typeid_action
sl@0
    95
template<class A>
sl@0
    96
struct return_type_N<typeid_action, A> { 
sl@0
    97
  typedef std::type_info const & type;
sl@0
    98
};
sl@0
    99
sl@0
   100
// return type of sizeof_action
sl@0
   101
sl@0
   102
template<class A>
sl@0
   103
struct return_type_N<sizeof_action, A> { 
sl@0
   104
  typedef std::size_t type;
sl@0
   105
};
sl@0
   106
sl@0
   107
sl@0
   108
// the four cast & typeid overloads.
sl@0
   109
// casts can take ordinary variables (not just lambda functors)
sl@0
   110
sl@0
   111
// static_cast 
sl@0
   112
template <class T, class Arg1>
sl@0
   113
inline const lambda_functor<
sl@0
   114
  lambda_functor_base<
sl@0
   115
    action<1, cast_action<static_cast_action<T> > >, 
sl@0
   116
    tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   117
  > 
sl@0
   118
>
sl@0
   119
ll_static_cast(const Arg1& a1) { 
sl@0
   120
  return 
sl@0
   121
    lambda_functor_base<
sl@0
   122
      action<1, cast_action<static_cast_action<T> > >, 
sl@0
   123
      tuple<typename const_copy_argument <const Arg1>::type> 
sl@0
   124
    >
sl@0
   125
  ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
sl@0
   126
}
sl@0
   127
sl@0
   128
// dynamic_cast
sl@0
   129
template <class T, class Arg1>
sl@0
   130
inline const lambda_functor<
sl@0
   131
  lambda_functor_base<
sl@0
   132
    action<1, cast_action<dynamic_cast_action<T> > >, 
sl@0
   133
    tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   134
  > 
sl@0
   135
>
sl@0
   136
ll_dynamic_cast(const Arg1& a1) { 
sl@0
   137
  return 
sl@0
   138
    lambda_functor_base<
sl@0
   139
      action<1, cast_action<dynamic_cast_action<T> > >, 
sl@0
   140
      tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   141
    > 
sl@0
   142
  ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
sl@0
   143
}
sl@0
   144
sl@0
   145
// const_cast
sl@0
   146
template <class T, class Arg1>
sl@0
   147
inline const lambda_functor<
sl@0
   148
  lambda_functor_base<
sl@0
   149
    action<1, cast_action<const_cast_action<T> > >, 
sl@0
   150
    tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   151
  > 
sl@0
   152
>
sl@0
   153
ll_const_cast(const Arg1& a1) { 
sl@0
   154
  return 
sl@0
   155
      lambda_functor_base<
sl@0
   156
        action<1, cast_action<const_cast_action<T> > >, 
sl@0
   157
        tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   158
      > 
sl@0
   159
      ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
sl@0
   160
}
sl@0
   161
sl@0
   162
// reinterpret_cast
sl@0
   163
template <class T, class Arg1>
sl@0
   164
inline const lambda_functor<
sl@0
   165
  lambda_functor_base<
sl@0
   166
    action<1, cast_action<reinterpret_cast_action<T> > >, 
sl@0
   167
    tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   168
  > 
sl@0
   169
>
sl@0
   170
ll_reinterpret_cast(const Arg1& a1) { 
sl@0
   171
  return 
sl@0
   172
      lambda_functor_base<
sl@0
   173
        action<1, cast_action<reinterpret_cast_action<T> > >, 
sl@0
   174
        tuple<typename const_copy_argument <const Arg1>::type> 
sl@0
   175
      > 
sl@0
   176
      ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
sl@0
   177
}
sl@0
   178
sl@0
   179
// typeid
sl@0
   180
// can be applied to a normal variable as well (can refer to a polymorphic
sl@0
   181
// class object)
sl@0
   182
template <class Arg1>
sl@0
   183
inline const lambda_functor<
sl@0
   184
  lambda_functor_base<
sl@0
   185
    action<1, typeid_action>, 
sl@0
   186
    tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   187
  > 
sl@0
   188
>
sl@0
   189
ll_typeid(const Arg1& a1) { 
sl@0
   190
  return 
sl@0
   191
      lambda_functor_base<
sl@0
   192
        action<1, typeid_action>, 
sl@0
   193
        tuple<typename const_copy_argument <const Arg1>::type>
sl@0
   194
      > 
sl@0
   195
      ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
sl@0
   196
}
sl@0
   197
sl@0
   198
// sizeof(expression)
sl@0
   199
// Always takes a lambda expression (if not, built in sizeof will do)
sl@0
   200
template <class Arg1>
sl@0
   201
inline const lambda_functor<
sl@0
   202
  lambda_functor_base<
sl@0
   203
    action<1, sizeof_action>, 
sl@0
   204
    tuple<lambda_functor<Arg1> >
sl@0
   205
  > 
sl@0
   206
>
sl@0
   207
ll_sizeof(const lambda_functor<Arg1>& a1) { 
sl@0
   208
  return 
sl@0
   209
      lambda_functor_base<
sl@0
   210
        action<1, sizeof_action>, 
sl@0
   211
        tuple<lambda_functor<Arg1> >
sl@0
   212
      > 
sl@0
   213
      ( tuple<lambda_functor<Arg1> >(a1));
sl@0
   214
}
sl@0
   215
sl@0
   216
} // namespace lambda 
sl@0
   217
} // namespace boost
sl@0
   218
sl@0
   219
#endif