1 /* Copyright 2006-2014 Joaquin M Lopez Munoz.
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * See http://www.boost.org/libs/flyweight for library home page.
7  */
8 
9 #ifndef BOOST_FLYWEIGHT_DETAIL_PERFECT_FWD_HPP
10 #define BOOST_FLYWEIGHT_DETAIL_PERFECT_FWD_HPP
11 
12 #if defined(_MSC_VER)
13 #pragma once
14 #endif
15 
16 /* C++03-compatible implementation of perfect forwarding.
17  * Usage:
18  *
19  *  # define NAME ...
20  *  # define BODY(args) {...BOOST_FLYWEIGHT_FORWARD(args)...}
21  *  BOOST_FLYWEIGHT_PERFECT_FWD(name,body)
22  *
23  * where NAME includes the return type and qualifiers (if any) and BODY(args)
24  * is expected to fo the forwarding through BOOST_FLYWEIGHT_FORWARD(args).
25  *
26  * In compilers capable of perfect forwarding, the real thing is provided
27  * (just one variadic args overload is generated). Otherwise the machinery
28  * generates n+1 overloads, if rvalue refs are supported, or else 2^(n+1)-1
29  * overloads accepting any combination of lvalue refs and const lvalue refs,
30  * up to BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS args.
31  *
32  * BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(name,body) is a variation omitting the
33  * overloads with zero args --when perfect forwarding is available, this second
34  * macro is exactly the same as the original.
35  */
36 
37 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
38 #include <boost/preprocessor/cat.hpp>
39 #include <boost/preprocessor/repetition/enum.hpp>
40 #include <boost/preprocessor/repetition/enum_params.hpp>
41 #include <boost/preprocessor/seq/seq.hpp>
42 
43 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
44 #include <utility>
45 #endif
46 
47 #define BOOST_FLYWEIGHT_FORWARD_FORWARD_AUX(z,n,_) \
48 std::forward<BOOST_PP_CAT(T,n)>(BOOST_PP_CAT(t,n))
49 
50 #define BOOST_FLYWEIGHT_FORWARD_FORWARD(n) \
51 BOOST_PP_ENUM(n,BOOST_FLYWEIGHT_FORWARD_FORWARD_AUX,~)
52 
53 #define BOOST_FLYWEIGHT_FORWARD_ENUM(n) BOOST_PP_ENUM_PARAMS(n,t)
54 
55 #define BOOST_FLYWEIGHT_FORWARD_PASS(arg) arg
56 
57 #define BOOST_FLYWEIGHT_FORWARD(args)\
58 BOOST_PP_CAT(BOOST_FLYWEIGHT_FORWARD_,BOOST_PP_SEQ_HEAD(args))( \
59 BOOST_PP_SEQ_HEAD(BOOST_PP_SEQ_TAIL(args)))
60 
61 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)||\
62     defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
63 
64 #if !defined(BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS)
65 #define BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS 5
66 #endif
67 
68 #if BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS<0
69 #error BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS must be >=0
70 #endif
71 
72 #if BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS<=5
73 #include <boost/flyweight/detail/pp_perfect_fwd.hpp>
74 #else
75 #include <boost/flyweight/detail/dyn_perfect_fwd.hpp>
76 #endif
77 
78 #else
79 
80 /* real perfect forwarding */
81 
82 #define BOOST_FLYWEIGHT_PERFECT_FWD(name,body) \
83 template<typename... Args>name(Args&&... args) \
84 body((PASS)(std::forward<Args>(args)...))
85 
86 #define BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS  \
87 BOOST_FLYWEIGHT_PERFECT_FWD
88 
89 #endif
90 #endif
91