I'm trying to create an baseclass that will serve as a parent for reference
counted objects handled by boost::intrusiv e_ptr<>. The documentation
didn't provide much in the way of describing what the functions
intrusive_ptr_a dd_ref and intrusive_ptr_r elease should do, nor even what
their signatures should be. There is one comment that has me a bit unsure
about whether my approach is a good one:"On compilers that support
argument-dependent lookup, intrusive_ptr_a dd_ref and intrusive_ptr_r elease
should be defined in the namespace that corresponds to their parameter;
otherwise, the definitions need to go in namespace boost."
Note that my baseclass is util::Reference d, and my functions are
util::intrusive _ptr_add_ref, and util::intrusive _ptr_release. My hope is
that I can derive from these in other namespaces and rely on the fact that
the baseclass is in util to avoid the unspecified problems that would arise
if the parameter is not in a namespace corresponding to the functions.
/*************** *************** *************** *************** ***************
* Copyright (C) 2005 by Steven T. Hatton *
* ha*****@globals ymmetry.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*************** *************** *************** *************** ***************/
#ifndef UTIL_REFERENCED _IF_H
#define UTIL_REFERENCED _IF_H
#include <boost/intrusive_ptr.h pp>
#include <boost/mem_fn.hpp>
namespace util {
class Referenced_IF {
friend void intrusive_ptr_a dd_ref(const Referenced_IF* r);
friend void intrusive_ptr_r elease(const Referenced_IF* r);
mutable unsigned _r;
void increment() const { ++_r; }
void decrement() const { if(!--_r) delete this; }
protected:
virtual ~Referenced_IF( ) {}
};
typedef boost::intrusiv e_ptr<Reference d_IF> iref_ptr;
inline void intrusive_ptr_a dd_ref(const Referenced_IF* r)
{ r->increment(); }
inline void intrusive_ptr_r elease(const Referenced_IF* r)
{ r->decrement(); }
}
#endif
//---------------------------------------------------------
The following seems to work. Can anybody see anything blatantly wrong with
it?
#ifndef CONTROL_SIGNALL ISTENER_IF_H
#define CONTROL_SIGNALL ISTENER_IF_H
#include <util/Referenced_IF.h >
namespace control {
class SignalListener_ IF: public util::Reference d_IF {
public:
virtual void signal()=0;
protected:
virtual ~SignalListener _IF() {}
};
}
#endif
//---------------------------------------------------------
#ifndef SIGNALGENERATOR _IF_HH
#define SIGNALGENERATOR _IF_HH
#include <list>
#include <algorithm>
#include <functional>
#include "SignalListener _IF.h"
namespace control {
/*!\brief The SignalGenerator _IF class template provides an interface for
signal generators.
SignalGenerator _IF maintains a list of \c SignalListener_ IF observers,
and notifies them
when an event occurs.
*/
class SignalGenerator _IF {
public:
typedef SignalListener_ IF Listener_T;
typedef boost::intrusiv e_ptr<SignalLis tener_IF> Listener_ptr_T;
/*!
Adds \a listener to the list of observers. Does nothing if \a listener
is already present.
*/
void attachListener( Listener_T* listener) {
using namespace std;
Listener_ptr_T lptr(listener);
if(find(_listen ers.begin(), _listeners.end( ), listener) ==
_listeners.end( ))
_listeners.push _back(listener) ;
}
/*!
Removes \a listener from the list of observers. Does nothing if \a
listener is not present.
*/
void detachListener( Listener_T* listener)
{ _listeners.remo ve(listener); }
protected:
/*!
Derived classes should call this to notify all listeners that an event
has occurred.
*/
void _generateSignal () { std::for_each(_ listeners.begin (),
_listeners.end( ), boost::mem_fn(& Listener_T::sig nal)); }
std::list<Liste ner_ptr_T> _listeners;
};
}
#endif
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell