1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Copyright 2005 - 2016, The libsigc++ Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef SIGC_BOUND_ARGUMENT_H
#define SIGC_BOUND_ARGUMENT_H
#include <sigc++/limit_reference.h>
#include <sigc++/reference_wrapper.h>
namespace sigc
{
/** A bound_argument<Foo> object stores a bound (for instance, with sigc::bind(), or
* sigc::bind_return()) argument.
*
* If Foo is a wrapped reference to a class Bar (std::reference_wrapper<Bar>) then this
* object is implemented on top of a limit_reference. When the slot is
* invoked, the limit_reference::invoke() method provides the argument (a Bar&).
* When the slot is visited (e.g. visit_each<>()), we simply visit the limit_reference,
* which will visit the derived type, or a sigc::trackable base if necessary.
*
* Likewise, If Foo is a wrapped const reference to a class Bar (std::reference_wrapper<const Bar>)
* then this object is implemented on top of a const_limit_reference.
*
* If Foo is something else (such as an argument that is bound by value) bound_argument just
* stores a cop of that value, and both invoke() and visit() simply return it.
*
* This object is used by the bind_functor<> and bind_return_functor<> objects,
* depending on whether the argument is bound as a parameter or as a return value.
*
* The general template implementation is used for parameters that are passed by value.
* @e T_type The type of the bound argument.
*/
template<typename T_type>
class bound_argument
{
public:
/** Constructor.
* @param arg The argument to bind.
*/
bound_argument(const T_type& arg) : visited_(arg) {}
/** Retrieve the entity to visit in visit_each().
* @return The bound argument.
*/
inline const T_type& visit() const { return visited_; }
/** Retrieve the entity to pass to the bound functor or return.
* @return The bound argument.
*/
inline T_type& invoke() { return visited_; }
private:
/** The value of the argument.
*/
T_type visited_;
};
// Template specialization:
/** bound_argument object for a bound argument that is passed by bind() or
* returned by bind_return() by reference, specialized for std::reference_wrapper<> types.
* @e T_wrapped The type of the bound argument.
*/
template<typename T_wrapped>
class bound_argument<std::reference_wrapper<T_wrapped>>
{
public:
/** Constructor.
* @param arg The argument to bind.
*/
bound_argument(const std::reference_wrapper<T_wrapped>& arg) : visited_(unwrap(arg)) {}
/** Retrieve the entity to visit in visit_each().
* @return The limited_reference to the bound argument.
*/
inline const limit_reference<T_wrapped>& visit() const { return visited_; }
/** Retrieve the entity to pass to the bound functor or return.
* @return The bound argument.
*/
inline T_wrapped& invoke() { return visited_.invoke(); }
private:
/** The limited_reference to the bound argument.
*/
limit_reference<T_wrapped> visited_;
};
/** bound_argument object for a bound argument that is passed by bind() or
* returned by bind_return() by const reference, specialized for const reference_wrapper<> types.
* - @e T_wrapped The type of the bound argument.
*/
template<typename T_wrapped>
class bound_argument<std::reference_wrapper<const T_wrapped>>
{
public:
/** Constructor.
* @param arg The argument to bind.
*/
bound_argument(const std::reference_wrapper<const T_wrapped>& arg) : visited_(unwrap(arg)) {}
/** Retrieve the entity to visit in visit_each().
* @return The const_limited_reference to the bound argument.
*/
inline const limit_reference<const T_wrapped>& visit() const { return visited_; }
/** Retrieve the entity to pass to the bound functor or return.
* @return The bound argument.
*/
inline const T_wrapped& invoke() { return visited_.invoke(); }
private:
/** The const_limited_reference to the bound argument.
*/
limit_reference<const T_wrapped> visited_;
};
#ifndef DOXYGEN_SHOULD_SKIP_THIS
/** Implementation of visitor<>::do_visit_each<>() specialized for the bound_argument class.
* Call visit_each() on the entity returned by the bound_argument's visit()
* method.
* @tparam T_type The type of bound_argument.
* @tparam T_action The type of functor to invoke.
* @param action The functor to invoke.
* @param arg The visited instance.
*/
template<typename T_type>
struct visitor<bound_argument<T_type>>
{
template<typename T_action>
static void do_visit_each(const T_action& action, const bound_argument<T_type>& arg)
{
sigc::visit_each(action, arg.visit());
}
};
#endif // DOXYGEN_SHOULD_SKIP_THIS
} /* namespace sigc */
#endif /* SIGC_BOUND_ARGUMENT_H */
|