linuxdcpp-team team mailing list archive
-
linuxdcpp-team team
-
Mailing list archive
-
Message #03381
[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2436: simplify ScopedFunctor
------------------------------------------------------------
revno: 2436
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Mon 2011-02-21 21:57:49 +0100
message:
simplify ScopedFunctor
modified:
dcpp/ScopedFunctor.h
--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk
Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/ScopedFunctor.h'
--- dcpp/ScopedFunctor.h 2011-02-11 23:08:25 +0000
+++ dcpp/ScopedFunctor.h 2011-02-21 20:57:49 +0000
@@ -22,25 +22,25 @@
namespace dcpp {
/* helper class to run a functor when the object goes out of scope.
-the additional #defines facilitate the following:
-- compilers can't always deduce the type of some functors (especially lambdas), so we use decltype.
-- use __COUNTER__ to generate a unique object name.
+the additional #defines generate a unique object name thanks to the __COUNTER__ macro.
for example, the call:
- ScopedFunctor([] { printf("hello"); })
+ ScopedFunctor([] { printf("hello"); });
will print "hello" when the current scope ends. */
+
template<typename F>
struct ScopedFunctor {
- explicit ScopedFunctor(const F& f_) : f(f_) { }
+ explicit ScopedFunctor(F f_) : f(f_) { }
~ScopedFunctor() { f(); }
private:
- const F& f;
+ F f;
};
+
+template<typename F>
+ScopedFunctor<F> makeScopedFunctor(F f) { return ScopedFunctor<F>(f); }
+
#define ScopedFunctor_gen(ScopedFunctor_name, ScopedFunctor_counter) ScopedFunctor_name##ScopedFunctor_counter
#define ScopedFunctor_(ScopedFunctor_f, ScopedFunctor_counter) \
- auto ScopedFunctor_gen(ScopedFunctor_f_, ScopedFunctor_counter) = ScopedFunctor_f; \
- ScopedFunctor<decltype(ScopedFunctor_gen(ScopedFunctor_f_, ScopedFunctor_counter))> \
- ScopedFunctor_gen(ScopedFunctor_object, ScopedFunctor_counter) \
- (ScopedFunctor_gen(ScopedFunctor_f_, ScopedFunctor_counter))
+ auto ScopedFunctor_gen(ScopedFunctor_object, ScopedFunctor_counter) = makeScopedFunctor(ScopedFunctor_f)
#define ScopedFunctor(ScopedFunctor_f) ScopedFunctor_(ScopedFunctor_f, __COUNTER__)
} // namespace dcpp