1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 #ifndef TRACE_SOURCE_ACCESSOR_H
21 #define TRACE_SOURCE_ACCESSOR_H
34 * \brief control access to objects' trace sources
36 * This class abstracts the kind of trace source to which we want to connect
37 * and provides services to Connect and Disconnect a sink to a trace source.
39 class TraceSourceAccessor
42 TraceSourceAccessor ();
43 virtual ~TraceSourceAccessor ();
44 void Ref (void) const;
45 void Unref (void) const;
48 * \param obj the object instance which contains the target trace source.
49 * \param cb the callback to connect to the target trace source.
51 virtual bool ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const = 0;
53 * \param obj the object instance which contains the target trace source.
54 * \param context the context to bind to the user callback.
55 * \param cb the callback to connect to the target trace source.
57 virtual bool Connect (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
59 * \param obj the object instance which contains the target trace source.
60 * \param cb the callback to disconnect from the target trace source.
62 virtual bool DisconnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const = 0;
64 * \param obj the object instance which contains the target trace source.
65 * \param context the context which was bound to the user callback.
66 * \param cb the callback to disconnect from the target trace source.
68 virtual bool Disconnect (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
70 mutable uint32_t m_count;
74 * \param a the trace source
76 * Create a TraceSourceAccessor which will control access to the underlying
77 * trace source. This helper template method assumes that the underlying
78 * type implements a statically-polymorphic set of Connect and Disconnect
79 * methods and creates a dynamic-polymorphic class to wrap the underlying
80 * static-polymorphic class.
83 Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a);
89 template <typename T, typename SOURCE>
90 Ptr<const TraceSourceAccessor>
91 DoMakeTraceSourceAccessor (SOURCE T::*a)
93 struct Accessor : public TraceSourceAccessor
95 virtual bool ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const {
96 T *p = dynamic_cast<T*> (obj);
101 (p->*m_source).ConnectWithoutContext (cb);
104 virtual bool Connect (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
105 T *p = dynamic_cast<T*> (obj);
110 (p->*m_source).Connect (cb, context);
113 virtual bool DisconnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const {
114 T *p = dynamic_cast<T*> (obj);
119 (p->*m_source).DisconnectWithoutContext (cb);
122 virtual bool Disconnect (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
123 T *p = dynamic_cast<T*> (obj);
128 (p->*m_source).Disconnect (cb, context);
132 } *accessor = new Accessor ();
133 accessor->m_source = a;
134 return Ptr<const TraceSourceAccessor> (accessor, false);
137 template <typename T>
138 Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a)
140 return DoMakeTraceSourceAccessor (a);
146 #endif /* TRACE_SOURCE_ACCESSOR_H */