Skip to content

Commit

Permalink
Creating separate Initialize and Finalize referenced functions for Ob…
Browse files Browse the repository at this point in the history
…jectPool.

This causes some AutoFilter tests to fail.
  • Loading branch information
GabrielHare committed Jul 30, 2014
1 parent c89c2ef commit 05e3e51
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions autowiring/ObjectPool.h
Expand Up @@ -16,7 +16,10 @@ T* DefaultCreate(void) {
}

template<typename T>
void DefaultReset(T&){}
void DefaultInitialize(T&){}

template<typename T>
void DefaultFinalize(T&){}

/// <summary>
/// Allows the management of a pool of objects based on an embedded factory
Expand All @@ -43,25 +46,28 @@ class ObjectPool
size_t limit = ~0,
size_t maxPooled = ~0,
const std::function<T*()>& alloc = &DefaultCreate<T>,
const std::function<void(T&)>& rx = &DefaultReset<T>
const std::function<void(T&)>& initial = &DefaultInitialize<T>,
const std::function<void(T&)>& final = &DefaultFinalize<T>
) :
m_monitor(std::make_shared<ObjectPoolMonitor>(this)),
m_poolVersion(0),
m_maxPooled(maxPooled),
m_limit(limit),
m_outstanding(0),
m_rx(rx),
m_alloc(alloc)
m_alloc(alloc),
m_initial(initial),
m_final(final)
{}

/// <param name="limit">The maximum number of objects this pool will allow to be outstanding at any time</param>
ObjectPool(
const std::function<T*()>& alloc,
const std::function<void(T&)>& rx = &DefaultReset<T>,
const std::function<void(T&)>& initial = &DefaultInitialize<T>,
const std::function<void(T&)>& final = &DefaultFinalize<T>,
size_t limit = ~0,
size_t maxPooled = ~0
) :
ObjectPool(limit, maxPooled, alloc, rx)
ObjectPool(limit, maxPooled, alloc, initial, final)
{}

ObjectPool(ObjectPool&& rhs)
Expand Down Expand Up @@ -95,8 +101,9 @@ class ObjectPool
size_t m_limit;
size_t m_outstanding;

// Resetter:
std::function<void(T&)> m_rx;
// Resetters:
std::function<void(T&)> m_initial;
std::function<void(T&)> m_final;

// Allocator:
std::function<T*()> m_alloc;
Expand Down Expand Up @@ -144,7 +151,7 @@ class ObjectPool
m_objs.size() < m_maxPooled
) {
// Reset the object and put it back in the pool:
m_rx(*unique);
m_final(*unique);
m_objs.push_back(Wrap(unique.release()));
}

Expand All @@ -170,11 +177,14 @@ class ObjectPool
lk.unlock();

// We failed to recover an object, create a new one:
return Wrap(m_alloc());
auto obj = Wrap(m_alloc());
m_initial(*obj);
return obj;
}

// Remove, return:
auto obj = m_objs.back();
m_initial(*obj);
m_objs.pop_back();
return obj;
}
Expand Down Expand Up @@ -406,8 +416,9 @@ class ObjectPool
m_maxPooled = rhs.m_maxPooled;
m_limit = rhs.m_limit;
m_outstanding = rhs.m_outstanding;
std::swap(m_rx, rhs.m_rx);
std::swap(m_alloc, rhs.m_alloc);
std::swap(m_initial, rhs.m_initial);
std::swap(m_final, rhs.m_final);

// Now we can take ownership of this monitor object:
m_monitor->SetOwner(this);
Expand Down

0 comments on commit 05e3e51

Please sign in to comment.