text
stringlengths
0
2.2M
// might not use the 'bslma' allocator protocol.
//
// First, we define a wrapper class that holds an object and a functor and
// calls the functor (called the listener) each time the wrapped object is
// changed. We store the object directly as a member variable, instead of
// using an uninitialized buffer, to avoid a separate construction step:
//..
template <class TYPE, class FUNC>
class MyTriggeredWrapper {
// This class is a wrapper around an object of the specified 'TYPE'
// that triggers a call to an object, called the "listener", of the
// specified 'FUNC' invocable type whenever the wrapped object is
// changed.
// DATA
TYPE d_value;
FUNC d_listener;
public:
// CREATORS
explicit MyTriggeredWrapper(const FUNC& f,
bslma::Allocator *basicAllocator = 0);
MyTriggeredWrapper(const TYPE& v,
const FUNC& f,
bslma::Allocator *basicAllocator = 0);
// Create an object with the specified 'f' as the listener to be
// called when a change is triggered. Optionally specify 'v' as
// the wrapped value; otherwise the wrapped value is default
// constructed. Optionally specify 'basicAllocator' to supply
// memory; otherwise the current default allocator is used. If
// 'TYPE' is not allocator aware, 'basicAllocator' is ignored.
MyTriggeredWrapper(const MyTriggeredWrapper& original,
bslma::Allocator *basicAllocator = 0);
// Create a copy of the specified 'original'. Optionally specify
// 'basicAllocator' to supply memory; otherwise the current
// default allocator is used.
~MyTriggeredWrapper()
// Destroy the wrapped object and listener.
{
}
// MANIPULATORS
MyTriggeredWrapper& operator=(const TYPE& rhs);
MyTriggeredWrapper& operator=(const MyTriggeredWrapper& rhs);
// Assign the wrapped value to the value of the specified 'rhs',
// invoke the listener with the new value, and return a reference
// providing modifiable access to this object. Note that the
// listener itself is not assigned.
void setValue(const TYPE& value);
// Set the wrapped value to the specified 'value' and invoke the
// listener with the new value.
// ACCESSORS
const TYPE& value() const
// Return a reference providing read-only access to the wrapped
// value.
{
return d_value;
}
const FUNC& listener() const
// Return a reference providing read-only access to the listener.
{
return d_listener;
}
};
//..
// Next, we define the constructors such that they initialize 'd_value' using
// the specified allocator if and only if 'TYPE' accepts an allocator. The
// 'bslma::ConstructionUtil::make' family of functions encapsulate all of the
// metaprogramming that detects whether or not 'TYPE' uses an allocator and,
// if so, which construction protocol it uses (allocator at the front or at
// the back of the argument list), making all three constructors straight-
// forward:
//..
template <class TYPE, class FUNC>
MyTriggeredWrapper<TYPE, FUNC>::MyTriggeredWrapper(
const FUNC& f,
bslma::Allocator *basicAllocator)
: d_value(bslma::ConstructionUtil::make<TYPE>(basicAllocator))
, d_listener(f)
{
}
template <class TYPE, class FUNC>
MyTriggeredWrapper<TYPE, FUNC>::MyTriggeredWrapper(
const TYPE& v,
const FUNC& f,
bslma::Allocator *basicAllocator)
: d_value(bslma::ConstructionUtil::make<TYPE>(basicAllocator, v))
, d_listener(f)
{
}
template <class TYPE, class FUNC>
MyTriggeredWrapper<TYPE, FUNC>::MyTriggeredWrapper(
const MyTriggeredWrapper& other,