refactor ScopedCollection to use an init() function

This commit is contained in:
Fischlurch 2012-01-07 04:44:48 +01:00
parent e6888f7b83
commit 5cc034d26b
3 changed files with 65 additions and 36 deletions

View file

@ -47,7 +47,7 @@
** the container is being filled successively.
** - the "RAII style" usage strives to create all of the content objects
** right away, immediately after the memory allocation. This usage pattern
** avoids any kind of "lifecylce state". Either the container comes up sane
** avoids any kind of "lifecycle state". Either the container comes up sane
** and fully populated, or the ctor call fails and any already created
** objects are discarded.
** @note intentionally there is no operation to discard individual objects,
@ -69,13 +69,9 @@
#define LIB_SCOPED_COLLECTION_H
//#include "include/logging.h"
#include "lib/iter-adapter.hpp"
#include "lib/error.hpp"
//#include "lib/util.hpp"
#include "lib/iter-adapter.hpp"
//#include <vector>
//#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/scoped_array.hpp>
#include <boost/static_assert.hpp>
@ -139,7 +135,6 @@ namespace lib {
#define TYPE_SANITY_CHECK \
BOOST_STATIC_ASSERT ((boost::is_base_of<I,TY>::value || boost::is_same<I,TY>::value))
/** Abbreviation for placement new */
#define EMBEDDED_ELEMENT_CTOR(_CTOR_CALL_) \
TYPE_SANITY_CHECK; \
@ -244,18 +239,8 @@ namespace lib {
, capacity_(maxElements)
, elements_(new ElementHolder[maxElements])
{
try {
while (level_ < capacity_)
{
ElementHolder& storageFrame (elements_[level_]);
builder (storageFrame);
++level_;
}}
catch(...)
{
clear();
throw;
} }
populate_by (builder);
}
/** variation of RAII-style: using a builder function,
* which is a member of some object. This supports the
@ -271,18 +256,8 @@ namespace lib {
, capacity_(maxElements)
, elements_(new ElementHolder[maxElements])
{
try {
while (level_ < capacity_)
{
ElementHolder& storageFrame (elements_[level_]);
(instance->*builder) (storageFrame);
++level_;
}}
catch(...)
{
clear();
throw;
} }
populate_by (builder,instance);
}
/* == some pre-defined Builders == */
@ -316,6 +291,7 @@ namespace lib {
}
}
/** init all elements default constructed */
void
populate()
try {
@ -333,6 +309,53 @@ namespace lib {
throw;
}
/** init all elements at once,
* invoking a builder functor for each.
* @param builder to create the individual elements
* this functor is responsible to invoke the appropriate
* ElementHolder#create function, which places a new element
* into the storage frame passed as parameter.
*/
template<class CTOR>
void
populate_by (CTOR builder)
try {
while (level_ < capacity_)
{
ElementHolder& storageFrame (elements_[level_]);
builder (storageFrame);
++level_;
} }
catch(...)
{
WARN (progress, "Failure while populating ScopedCollection. "
"All elements will be discarded");
clear();
throw;
}
/** variation of element initialisation,
* invoking a member function of some manager object
* for each collection element to be created.
*/
template<class TY>
void
populate_by (void (TY::*builder) (ElementHolder&), TY * const instance)
try {
while (level_ < capacity_)
{
ElementHolder& storageFrame (elements_[level_]);
(instance->*builder) (storageFrame);
++level_;
} }
catch(...)
{
WARN (progress, "Failure while populating ScopedCollection. "
"All elements will be discarded");
clear();
throw;
}
/** push a new element of default type

View file

@ -68,8 +68,8 @@ namespace play {
using lib::transform;
using lib::iter_stl::eachElm;
using std::tr1::placeholders::_1;
using std::tr1::bind;
//using std::tr1::placeholders::_1;
//using std::tr1::bind;
using std::vector;
//using std::tr1::shared_ptr;
using boost::scoped_ptr;
@ -186,6 +186,11 @@ namespace play {
protected: /* == API for OutputSlot-Impl == */
void
init() ///< derived classes need to invoke this to build the actual connections
{
connections_.populate_by (&ConnectionStateManager::buildConnection, this);
}
typedef typename Connections::ElementHolder& ConnectionStorage;
@ -195,8 +200,7 @@ namespace play {
ConnectionStateManager(uint numChannels)
: connections_( numChannels
, bind (&ConnectionStateManager::buildConnection, this, _1 ))
: connections_(numChannels)
{ }
public:

View file

@ -261,7 +261,9 @@ namespace play {
public:
SimulatedOutputSequences (uint numChannels)
: _Base(numChannels)
{ }
{
init();
}
};