Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:45:46

0001 
0002 // Copyright (C) 2009-2012 Lorenzo Caminiti
0003 // Distributed under the Boost Software License, Version 1.0
0004 // (see accompanying file LICENSE_1_0.txt or a copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 // Home at http://www.boost.org/libs/functional/overloaded_function
0007 
0008 #ifndef DOXYGEN // Doxygen documentation only.
0009 
0010 #if !BOOST_PP_IS_ITERATING
0011 #   ifndef BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_HPP_
0012 #       define BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_HPP_
0013 
0014 #       include <boost/functional/overloaded_function/detail/base.hpp>
0015 #       include <boost/functional/overloaded_function/detail/function_type.hpp>
0016 #       include <boost/functional/overloaded_function/config.hpp>
0017 #       include <boost/typeof/typeof.hpp>
0018 #       include <boost/preprocessor/iteration/iterate.hpp>
0019 #       include <boost/preprocessor/repetition/enum.hpp>
0020 #       include <boost/preprocessor/repetition/repeat.hpp>
0021 #       include <boost/preprocessor/control/expr_iif.hpp>
0022 #       include <boost/preprocessor/control/expr_if.hpp>
0023 #       include <boost/preprocessor/comparison/greater.hpp>
0024 #       include <boost/preprocessor/comparison/less.hpp>
0025 #       include <boost/preprocessor/cat.hpp>
0026 #       include <boost/preprocessor/arithmetic/add.hpp>
0027 #       include <boost/preprocessor/arithmetic/sub.hpp>
0028 #       include <boost/preprocessor/tuple/eat.hpp>
0029 #       include <boost/preprocessor/logical/and.hpp>
0030 #       include <boost/preprocessor/logical/not.hpp>
0031 #       include <boost/preprocessor/facilities/expand.hpp>
0032 
0033 #define BOOST_FUNCTIONAL_f_type(z, n, unused) \
0034     BOOST_PP_CAT(F, n)
0035 
0036 #define BOOST_FUNCTIONAL_f_arg(z, n, unused) \
0037     BOOST_PP_CAT(f, n)
0038 
0039 #define BOOST_FUNCTIONAL_f_tparam(z, n, unused) \
0040     typename BOOST_FUNCTIONAL_f_type(z, n, ~) \
0041 
0042 #define BOOST_FUNCTIONAL_f_tparam_dflt(z, n, is_tspec) \
0043     BOOST_FUNCTIONAL_f_tparam(z, n, ~) \
0044     /* overload requires at least 2 functors so F0 and F1 not optional */ \
0045     BOOST_PP_EXPR_IIF(BOOST_PP_AND(BOOST_PP_NOT(is_tspec), \
0046             BOOST_PP_GREATER(n, 1)), \
0047         = void \
0048     )
0049 
0050 #define BOOST_FUNCTIONAL_f_arg_decl(z, n, unused) \
0051     BOOST_FUNCTIONAL_f_type(z, n, ~) /* no qualifier to deduce tparam */ \
0052     BOOST_FUNCTIONAL_f_arg(z, n, ~)
0053 
0054 #define BOOST_FUNCTIONAL_g_type(z, n, unused) \
0055     BOOST_PP_CAT(G, n)
0056 
0057 #define BOOST_FUNCTIONAL_g_arg(z, n, unused) \
0058     BOOST_PP_CAT(g, n)
0059 
0060 #define BOOST_FUNCTIONAL_g_tparam(z, n, unused) \
0061     typename BOOST_FUNCTIONAL_g_type(z, n, ~)
0062 
0063 #define BOOST_FUNCTIONAL_g_arg_decl(z, n, unused) \
0064     BOOST_FUNCTIONAL_g_type(z, n, ~) /* no qualifier to deduce tparam */ \
0065     BOOST_FUNCTIONAL_g_arg(z, n, ~)
0066 
0067 #define BOOST_FUNCTIONAL_base(z, n, unused) \
0068     ::boost::overloaded_function_detail::base< \
0069         BOOST_FUNCTIONAL_f_type(z, n, ~) \
0070     >
0071 
0072 #define BOOST_FUNCTIONAL_inherit(z, n, unused) \
0073     public BOOST_FUNCTIONAL_base(z, n, ~)
0074 
0075 #define BOOST_FUNCTIONAL_base_init(z, n, unused) \
0076     BOOST_FUNCTIONAL_base(z, n, ~)(BOOST_FUNCTIONAL_g_arg(z, n, ~))
0077 
0078 #define BOOST_FUNCTIONAL_using_operator_call(z, n, unused) \
0079     using BOOST_FUNCTIONAL_base(z, n, ~)::operator();
0080 
0081 #define BOOST_FUNCTIONAL_function_type(z, n, unused) \
0082     typename ::boost::overloaded_function_detail::function_type< \
0083         BOOST_FUNCTIONAL_f_type(z, n, ~) \
0084     >::type
0085 
0086 #       define BOOST_PP_ITERATION_PARAMS_1 \
0087             /* at least 2 func to overload so start from 2 to MAX */ \
0088             /* (cannot iterate [0, MAX-2) because error on Sun) */ \
0089             (3, (2, BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX, \
0090             "boost/functional/overloaded_function.hpp"))
0091 #       include BOOST_PP_ITERATE() // Iterate over function arity.
0092 
0093 #undef BOOST_FUNCTIONAL_f_type
0094 #undef BOOST_FUNCTIONAL_f_arg
0095 #undef BOOST_FUNCTIONAL_f_tparam
0096 #undef BOOST_FUNCTIONAL_f_arg_decl
0097 #undef BOOST_FUNCTIONAL_f_tparam_dflt
0098 #undef BOOST_FUNCTIONAL_g_type
0099 #undef BOOST_FUNCTIONAL_g_arg
0100 #undef BOOST_FUNCTIONAL_g_tparam
0101 #undef BOOST_FUNCTIONAL_g_arg_decl
0102 #undef BOOST_FUNCTIONAL_base
0103 #undef BOOST_FUNCTIONAL_inherit
0104 #undef BOOST_FUNCTIONAL_base_init
0105 #undef BOOST_FUNCTIONAL_using_operator_call
0106 #undef BOOST_FUNCTIONAL_function_type
0107 
0108 #   endif // #include guard
0109 
0110 #elif BOOST_PP_ITERATION_DEPTH() == 1
0111 #   define BOOST_FUNCTIONAL_overloads \
0112         /* iterate as OVERLOADS, OVERLOADS-1, OVERLOADS-2, ... */ \
0113         /* (add 2 because iteration started from 2 to MAX) */ \
0114         BOOST_PP_ADD(2, BOOST_PP_SUB( \
0115                 BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX, \
0116                 BOOST_PP_FRAME_ITERATION(1)))
0117 #   define BOOST_FUNCTIONAL_is_tspec \
0118         /* if template specialization */ \
0119         BOOST_PP_LESS(BOOST_FUNCTIONAL_overloads, \
0120                 BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX)
0121 
0122 // For type-of emulation: This must be included at this pp iteration level.
0123 #   include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
0124 
0125 namespace boost {
0126 
0127 template<
0128     BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_f_tparam_dflt,
0129             BOOST_FUNCTIONAL_is_tspec)
0130 >
0131 class overloaded_function
0132     // Template specialization.
0133     BOOST_PP_EXPR_IIF(BOOST_PP_EXPAND(BOOST_FUNCTIONAL_is_tspec), <)
0134     BOOST_PP_IIF(BOOST_FUNCTIONAL_is_tspec,
0135         BOOST_PP_ENUM
0136     ,
0137         BOOST_PP_TUPLE_EAT(3)
0138     )(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_f_type, ~)
0139     BOOST_PP_EXPR_IIF(BOOST_PP_EXPAND(BOOST_FUNCTIONAL_is_tspec), >)
0140     // Bases (overloads >= 2 so always at least 2 bases).
0141     : BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads,
0142             BOOST_FUNCTIONAL_inherit, ~)
0143 {
0144 public:
0145     template<
0146         BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_g_tparam, ~)
0147     > /* implicit */ inline overloaded_function(
0148             BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads,
0149                     BOOST_FUNCTIONAL_g_arg_decl, ~))
0150             // Overloads >= 2 so always at least 2 bases to initialize.
0151             : BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads,
0152                     BOOST_FUNCTIONAL_base_init, ~)
0153     {}
0154 
0155     BOOST_PP_REPEAT(BOOST_FUNCTIONAL_overloads, 
0156             BOOST_FUNCTIONAL_using_operator_call, ~)
0157 };
0158 
0159 template<
0160     BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_f_tparam, ~)
0161 >
0162 overloaded_function<
0163     BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_function_type, ~)
0164 > make_overloaded_function(
0165     BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_f_arg_decl, ~)
0166 ) {
0167     return overloaded_function<
0168         BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads,
0169                 BOOST_FUNCTIONAL_function_type, ~)
0170     >(BOOST_PP_ENUM(BOOST_FUNCTIONAL_overloads, BOOST_FUNCTIONAL_f_arg, ~));
0171 }
0172 
0173 } // namespace
0174 
0175 // For type-of emulation: Register overloaded function type (for _AUTO, etc).
0176 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::overloaded_function,
0177     BOOST_FUNCTIONAL_overloads)
0178 
0179 #   undef BOOST_FUNCTIONAL_overloads
0180 #   undef BOOST_FUNCTIONAL_is_tspec
0181 #endif // iteration
0182 
0183 // DOCUMENTATION //
0184 
0185 #else // DOXYGEN
0186 
0187 /** @file
0188 @brief Overload distinct function pointers, function references, and
0189 monomorphic function objects into a single function object.
0190 */
0191 
0192 namespace boost {
0193 
0194 /**
0195 @brief Function object to overload functions with distinct signatures.
0196 
0197 This function object aggregates together calls to functions of all the
0198 specified function types <c>F1</c>, <c>F2</c>, etc which must have distinct
0199 function signatures from one another.
0200 
0201 @Params
0202 @Param{F<em>i</em>,
0203 Each function type must be specified using the following syntax (which is
0204 Boost.Function's preferred syntax):
0205 @code
0206     result_type (argument1_type\, argumgnet2_type\, ...)
0207 @endcode
0208 }
0209 @EndParams
0210 
0211 In some cases, the @RefFunc{make_overloaded_function} function template can be
0212 useful to construct an overloaded function object without explicitly
0213 specifying the function types.
0214 
0215 At least two distinct function types must be specified (because there is
0216 nothing to overload between one or zero functions).
0217 The maximum number of functions to overload is given by the
0218 @RefMacro{BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX}
0219 configuration macro.
0220 The maximum number of function parameters for each of the specified function
0221 types is given by the
0222 @RefMacro{BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX}
0223 configuration macro.
0224 
0225 @See @RefSect{tutorial, Tutorial} section, @RefFunc{make_overloaded_function},
0226 @RefMacro{BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX},
0227 @RefMacro{BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX},
0228 Boost.Function.
0229 */
0230 template<typename F1, typename F2, ...>
0231 class overloaded_function {
0232 public:
0233     /**
0234     @brief Construct the overloaded function object.
0235 
0236     Any function pointer, function reference, and monomorphic function object
0237     that can be converted to a <c>boost::function</c> function object can be
0238     specified as parameter.
0239 
0240     @Note Unfortunately, it is not possible to support polymorphic function
0241     objects (as explained <a
0242     href="http://lists.boost.org/Archives/boost/2012/03/191744.php">here</a>).
0243     */
0244     overloaded_function(const boost::function<F1>&,
0245             const boost::function<F2>&, ...);
0246 
0247     /**
0248     @brief Call operator matching the signature of the function type specified
0249     as 1st template parameter.
0250 
0251     This will in turn invoke the call operator of the 1st function passed to
0252     the constructor.
0253     */
0254     typename boost::function_traits<F1>::result_type operator()(
0255             typename boost::function_traits<F1>::arg1_type,
0256             typename boost::function_traits<F1>::arg2_type,
0257             ...) const;
0258 
0259     /**
0260     @brief Call operator matching the signature of the function type specified
0261     as 2nd template parameter.
0262 
0263     This will in turn invoke the call operator of the 2nd function passed to
0264     the constructor.
0265 
0266     @Note Similar call operators are present for all specified function types
0267     <c>F1</c>, <c>F2</c>, etc (even if not exhaustively listed by this
0268     documentation).
0269     */
0270     typename boost::function_traits<F2>::result_type operator()(
0271             typename boost::function_traits<F2>::arg1_type,
0272             typename boost::function_traits<F2>::arg2_type,
0273             ...) const;
0274 };
0275 
0276 /**
0277 @brief Make an overloaded function object without explicitly specifying the
0278 function types.
0279 
0280 This function template creates and returns an @RefClass{overloaded_function}
0281 object that overloads all the specified functions <c>f1</c>, <c>f2</c>, etc.
0282 
0283 The function types are internally determined from the template parameter types
0284 so they do not need to be explicitly specified.
0285 Therefore, this function template usually has a more concise syntax when
0286 compared with @RefClass{overloaded_function}.
0287 This is especially useful when the explicit type of the returned
0288 @RefClass{overloaded_function} object does not need to be known (e.g., when
0289 used with Boost.Typeof's <c>BOOST_AUTO</c>, C++11 <c>auto</c>, or when the
0290 overloaded function object is handled using a function template parameter, see
0291 the @RefSect{tutorial, Tutorial} section).
0292 
0293 The maximum number of functions to overload is given by the
0294 @RefMacro{BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX}
0295 configuration macro.
0296 
0297 @Note In this documentation, <c>__function_type__</c> is a placeholder for a
0298 symbol that is specific to the implementation of this library.
0299 
0300 @See @RefSect{tutorial, Tutorial} section, @RefClass{overloaded_function},
0301 @RefMacro{BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX}.
0302 */
0303 template<typename F1, typename F2, ...>
0304 overloaded_function<
0305     __function_type__<F1>, __function_type__<F2>, ...
0306 > make_overloaded_function(F1 f1, F2 f2, ...);
0307 
0308 } // namespace
0309 
0310 #endif // DOXYGEN
0311