Lumiera  0.pre.03
»edityourfreedom«
functor-util.hpp
Go to the documentation of this file.
1 /*
2  FUNCTOR-UTIL.hpp - collection of helpers for dealing with functors and signals
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
38 #ifndef FUNCTOR_UTIL_H
39 #define FUNCTOR_UTIL_H
40 
41 #include "lib/hash-value.h"
42 
43 #include <functional>
44 #include <boost/functional/hash.hpp>
45 
46 
47 
48 namespace util {
49 
50  using std::function;
51  using std::bind;
52  using std::placeholders::_1;
53 
54  namespace { // hiding some nasty details...
55 
56  using lib::HashVal;
57  using boost::hash_combine;
58 
79  class HijackedFunction
80  : std::_Function_base
81  {
82  typedef void (*DummyInvoker) (void);
83  DummyInvoker invoker_;
84 
85  public:
86 
87  friend bool
88  operator== (HijackedFunction const& f1,
89  HijackedFunction const& f2)
90  {
91  return (f1.invoker_ == f2.invoker_)
92  && (f1._M_manager == f2._M_manager)
93  && (f1._M_functor._M_unused._M_const_object ==
94  f2._M_functor._M_unused._M_const_object );
95  } // note: we don't cover any member pointer offset
96 
97  friend HashVal
98  hash_value (HijackedFunction const& fun)
99  {
100  HashVal hash(0);
101  hash_combine (hash, fun.invoker_);
102  hash_combine (hash, fun._M_manager);
103  hash_combine (hash, fun._M_functor._M_unused._M_const_object);
104  return hash; // note: member pointer offset part uncovered
105  }
106  };
107 
108  }
109 
110 
121  template<typename SIG>
122  inline bool
123  rawComparison (function<SIG> const& f1,
124  function<SIG> const& f2)
125  {
126  typedef HijackedFunction const& Hij;
127 
128  return reinterpret_cast<Hij> (f1)
129  == reinterpret_cast<Hij> (f2);
130  }
131 
134  template<typename SIG1, typename SIG2>
135  inline bool
136  rawComparison (function<SIG1> const&,
137  function<SIG2> const&)
138  {
139  return false;
140  }
141 
143  inline bool
144  rawComparison (void* f1, void* f2)
145  {
146  typedef HijackedFunction * HijP;
147 
148  return (!f1 && !f2)
149  || *reinterpret_cast<HijP> (f1)
150  == *reinterpret_cast<HijP> (f2);
151  }
152 
153 
163  template<typename SIG>
164  inline HashVal
165  rawHashValue (function<SIG> const& fun)
166  {
167  typedef HijackedFunction const& Hij;
168 
169  return hash_value (reinterpret_cast<Hij> (fun));
170  }
171 
172 } // namespace util
173 
174 namespace std {
175 
181  template<typename SIG>
182  inline lib::HashVal
183  hash_value (function<SIG> const& fun)
184  {
185  return util::rawHashValue (fun);
186  }
187 
188 }
189 
190 #endif /*FUNCTOR_UTIL_H*/
#define hash
NoUsableHashDefinition hash_value(...)
declared for metaprogramming only, never defined
STL namespace.
HashVal rawHashValue(function< SIG > const &fun)
workaround to calculate a hash value for a given function object.
bool rawComparison(function< SIG > const &f1, function< SIG > const &f2)
temporary workaround: tr1/functional should define public comparison operators for functor objects...
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
bool operator==(_Fmt const &left, _Fmt const &right)