Skip to content

Commit

Permalink
Merge pull request #48 from leapmotion/ref-cleanup
Browse files Browse the repository at this point in the history
General cleanups
  • Loading branch information
Gabriel Hare committed Aug 16, 2014
2 parents 33bcd3a + 852e9d4 commit 3b20c77
Show file tree
Hide file tree
Showing 25 changed files with 205 additions and 151 deletions.
32 changes: 16 additions & 16 deletions autowiring/AutoPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,27 +357,27 @@ class AutoPacket:
/// </remarks>
template<class T, class... Ts>
void DecorateImmediate(const T& immed, const Ts&... immeds) {
// These are the things we're going to be working with while we perform immediate decoration:
static const std::type_info* sc_typeInfo [] = {&typeid(T), &typeid(Ts)...};
const void* pvImmeds [] = {&immed, &immeds...};
DecorationDisposition* pTypeSubs[1 + sizeof...(Ts)];

// None of the inputs may be shared pointers--if any of the inputs are shared pointers, they must be attached
// to this packet via Decorate, or else dereferenced and used that way.
static_assert(
!is_shared_ptr<T>::value &&
!is_any<is_shared_ptr<Ts>...>::value,
!is_any<is_shared_ptr<T>, is_shared_ptr<Ts>...>::value,
"DecorateImmediate must not be used to attach a shared pointer, use Decorate on such a decoration instead"
);

// These are the things we're going to be working with while we perform immediate decoration:
static const std::type_info* s_argTypes [] = {&typeid(T), &typeid(Ts)...};
static const size_t s_arity = 1 + sizeof...(Ts);
const void* pvImmeds [] = {&immed, &immeds...};
DecorationDisposition* pTypeSubs[s_arity];

// Perform standard decoration with a short initialization:
{
std::lock_guard<std::mutex> lk(m_lock);
for(size_t i = 0; i <= sizeof...(Ts); i++) {
pTypeSubs[i] = &m_decorations[*sc_typeInfo[i]];
for(size_t i = 0; i < s_arity; i++) {
pTypeSubs[i] = &m_decorations[*s_argTypes[i]];
if(pTypeSubs[i]->wasCheckedOut) {
std::stringstream ss;
ss << "Cannot perform immediate decoration with type " << sc_typeInfo[i]->name()
ss << "Cannot perform immediate decoration with type " << s_argTypes[i]->name()
<< ", the requested decoration already exists";
throw std::runtime_error(ss.str());
}
Expand All @@ -393,26 +393,26 @@ class AutoPacket:
// Pulse satisfaction:
MakeAtExit([this, &pTypeSubs] {
// Mark entries as unsatisfiable:
for(auto pEntry : pTypeSubs) {
for(DecorationDisposition* pEntry : pTypeSubs) {
pEntry->satisfied = false;
pEntry->m_pImmediate = nullptr;
}

// Now trigger a rescan to hit any deferred, unsatisfiable entries:
static const std::type_info* lamda_typeInfos [] = {&typeid(T), &typeid(Ts)...};
for(auto ti : lamda_typeInfos)
for(const std::type_info* ti : s_argTypes)
MarkUnsatisfiable(*ti);
}),
PulseSatisfaction(pTypeSubs, 1 + sizeof...(Ts));
PulseSatisfaction(pTypeSubs, s_arity);
}

/// <summary>
/// Adds a function to be called as an AutoFilter for this packet only.
/// </summary>
template<class Ret, class... Args>
void AddRecipient(std::function<Ret(Args...)> f) {
std::shared_ptr<MicroAutoFilter<Ret, Args...>> filter(new MicroAutoFilter<Ret, Args...>(f));
InitializeRecipient(MakeAutoFilterDescriptor(filter));
InitializeRecipient(
MakeAutoFilterDescriptor(std::make_shared<MicroAutoFilter<Ret, Args...>>(f))
);
}

/// <returns>
Expand Down
8 changes: 4 additions & 4 deletions autowiring/Bolt.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class Bolt:
{
public:
const t_TypeInfoVector GetContextSigils(void) const override {
static const std::type_info* sc_types[] = {
static const std::type_info* s_types[] = {
&typeid(Sigil)...,
nullptr
};
return sc_types;
return s_types;
}

static_assert(!is_any_same<void, Sigil...>::value, "Can't use 'void' as a sigil type");
Expand All @@ -35,7 +35,7 @@ class Bolt<>:
{
public:
const t_TypeInfoVector GetContextSigils(void) const override {
static const std::type_info* sc_types[] = {nullptr};
return sc_types;
static const std::type_info* s_types[] = {nullptr};
return s_types;
}
};
30 changes: 14 additions & 16 deletions autowiring/ContextCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ class ContextCreator:
template<class Fn>
void Enumerate(Fn&& fn) {
std::lock_guard<std::mutex> lk(m_contextLock);
for(auto q = m_contexts.begin(); q != m_contexts.end(); q++) {
auto ctxt = q->second.lock();
if(ctxt)
if(!fn(q->first, ctxt))
return;
for(const auto& entry : m_contexts) {
auto ctxt = entry.second.lock();
if(ctxt && !fn(entry.first, ctxt))
return;
}
}

Expand All @@ -56,8 +55,8 @@ class ContextCreator:
Container Enumerate(void) {
Container container;
std::lock_guard<std::mutex> lk(m_contextLock);
for(auto q = m_contexts.begin(); q != m_contexts.end(); q++) {
auto ctxt = q->second.lock();
for(const auto& entry : m_contexts) {
auto ctxt = entry.second.lock();
if(ctxt)
container.insert(container.end(), ctxt);
}
Expand All @@ -69,10 +68,10 @@ class ContextCreator:
/// </summary>
std::shared_ptr<CoreContext> FindContext(const Key& key) {
std::lock_guard<std::mutex> lk(m_contextLock);
typename t_mpType::iterator q = m_contexts.find(key);
if(q == m_contexts.end())
auto entry = m_contexts.find(key);
if(entry == m_contexts.end())
return std::shared_ptr<CoreContext>();
return q->second.lock();
return entry.second.lock();
}

/// <summary>
Expand Down Expand Up @@ -122,7 +121,7 @@ class ContextCreator:
/// The contaner could, in fact, have elements in it at the time control is returned to the caller.
/// </remarks>
void Clear(bool wait) {
ContextCreatorBase::Clear(wait, m_contexts, [] (typename t_mpType::iterator q) {return q->second.lock();});
ContextCreatorBase::Clear(wait, m_contexts, [] (const typename t_mpType::value_type& q) {return q.second.lock();});
}

/// <summary>
Expand Down Expand Up @@ -240,10 +239,9 @@ class ContextCreator<Sigil, void>:
template<class Fn>
void Enumerate(Fn&& fn) {
std::lock_guard<std::mutex> lk(m_contextLock);
for(auto q = m_contextList.begin(); q != m_contextList.end(); q++) {
auto ctxt = q->lock();
if(ctxt)
if(!fn(ctxt))
for(const auto& weak_ctxt : m_contextList) {
auto ctxt = weak_ctxt.lock();
if(ctxt && !fn(ctxt))
return;
}
}
Expand All @@ -264,7 +262,7 @@ class ContextCreator<Sigil, void>:
void Clear(bool wait) {
(std::lock_guard<std::mutex>)m_contextLock,
m_clearSentinal++;
ContextCreatorBase::Clear(wait, m_contextList, [] (typename t_contextList::iterator q) { return q->lock();});
ContextCreatorBase::Clear(wait, m_contextList, [] (const typename t_contextList::value_type& q) { return q.lock();});
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions autowiring/ContextCreatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ContextCreatorBase {
if(!wait) {
// Trivial signal-clear-return:
std::lock_guard<std::mutex> lk(m_contextLock);
for(auto q = ctr.begin(); q != ctr.end(); q++) {
auto locked = locker(q);
for(const auto& ctxt : ctr) {
auto locked = locker(ctxt);
if(locked)
locked->SignalShutdown();
}
Expand All @@ -44,8 +44,8 @@ class ContextCreatorBase {
// Copy out and clear:
{
std::lock_guard<std::mutex> lk(m_contextLock);
for(auto q = ctr.begin(); q != ctr.end(); q++) {
auto locked = locker(q);
for(const auto& ctxt : ctr) {
auto locked = locker(ctxt);
if(locked)
locked->SignalShutdown();
}
Expand All @@ -54,8 +54,8 @@ class ContextCreatorBase {
}

// Signal everyone first, then wait in a second pass:
for(auto q = ctrCopy.begin(); q != ctrCopy.end(); q++) {
auto locked = locker(q);
for(const auto& ctxt : ctrCopy) {
auto locked = locker(ctxt);
if(locked)
locked->Wait();
}
Expand Down
9 changes: 4 additions & 5 deletions autowiring/ContextMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ class ContextMap
template<class Fn>
void Enumerate(Fn&& fn) {
std::lock_guard<std::mutex> lk(m_lk);
for(auto q = m_contexts.begin(); q != m_contexts.end(); q++) {
auto ctxt = q->second.lock();
if(ctxt)
if(!fn(q->first, ctxt))
return;
for(const auto& entry : m_contexts) {
auto ctxt = entry.second.lock();
if(ctxt && !fn(entry.first, ctxt))
return;
}
}

Expand Down
5 changes: 2 additions & 3 deletions autowiring/EventOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ class EventOutputStreamBase {
static std::map<std::string, MemFn> my_map = local;
if(str == "Query")
{
for(auto it = my_map.begin(); it != my_map.end(); ++it)
{
if((it->second) == memfn) return it->first;
for(const auto& entry : my_map) {
if((entry.second) == memfn) return entry.first;
}
return "";
}
Expand Down
6 changes: 2 additions & 4 deletions autowiring/InvokeRelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ class InvokeRelay<Deferred (T::*)(Args...)> {
// Context not yet started
return;

const auto& dq = erp->GetDispatchQueue();
std::lock_guard<std::mutex> lk(erp->GetDispatchQueueLock());

for(auto q = dq.begin(); q != dq.end(); q++)
(**q).AddExisting(new CurriedInvokeRelay<T, Args...>(dynamic_cast<T&>(**q), fnPtr, args...));
for(DispatchQueue* q : erp->GetDispatchQueue())
q->AddExisting(new CurriedInvokeRelay<T, Args...>(dynamic_cast<T&>(*q), fnPtr, args...));
}
};

Expand Down
6 changes: 2 additions & 4 deletions autowiring/MicroBolt.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ struct MicroBolt:
// NOTE: Injection of T into all matching contexts may result in
// multiple calls to Inject<T>() if a matching context
// is created during traversal.
std::shared_ptr<CoreContext> rootContext = CoreContext::CurrentContext();
auto listSigils = {ContextEnumeratorT<Sigils>(rootContext)...};
for (auto findSigil : listSigils)
for (auto context : findSigil)
for(auto findSigil : {ContextEnumeratorT<Sigils>(CoreContext::CurrentContext())...})
for(auto context : findSigil)
context->template Inject<T>();
}
void ContextCreated(void) override;
Expand Down
6 changes: 3 additions & 3 deletions src/autonet/AutoNetServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void AutoNetServerImpl::NewObject(CoreContext& ctxt, const AnySharedPointer& obj
// Check if type receives any events
{
Json::array listenerTypes;
for(auto& event : m_EventTypes) {
for(const auto& event : m_EventTypes) {
if(event->IsSameAs(objectPtr.get()))
listenerTypes.push_back(demangle(event->Type()));
}
Expand Down Expand Up @@ -240,15 +240,15 @@ void AutoNetServerImpl::HandleSubscribe(websocketpp::connection_hdl hdl) {
m_Subscribers.insert(hdl);

Json::array types;
for(auto type : m_AllTypes) {
for(const auto& type : m_AllTypes) {
types.push_back(type.first);
}

SendMessage(hdl, "subscribed", types);
AutoGlobalContext()->BuildCurrentState();

// Send breakpoint message
for(auto& breakpoint : m_breakpoints) {
for(const auto& breakpoint : m_breakpoints) {
SendMessage(hdl, "breakpoint", breakpoint);
}
}
Expand Down
61 changes: 61 additions & 0 deletions src/autotesting/AutowiringEnclosure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "stdafx.h"
#include "AutowiringEnclosure.h"

struct TestInfoProxy {
TestInfoProxy(const testing::TestInfo& info) :
m_info(info)
{}

const testing::TestInfo& m_info;
};

void AutowiringEnclosure::OnTestStart(const testing::TestInfo& info) {
AutoRequired<AutowiringEnclosureExceptionFilter> filter;

// The context proper. This is automatically assigned as the current
// context when SetUp is invoked.
AutoCreateContext create;
create->Construct<TestInfoProxy>(info);

// Add exception filter in this context:
create->Inject<AutowiringEnclosureExceptionFilter>();

// Now make it current and let the test run:
create->SetCurrent();
}

void AutowiringEnclosure::OnTestEnd(const testing::TestInfo& info) {
// Verify we can grab the test case back out and that the pointer is correct:
Autowired<TestInfoProxy> ti;
Autowired<AutowiringEnclosureExceptionFilter> ecef;
auto ctxt = ecef ? ecef->GetContext() : nullptr;

// Unconditionally reset the global context as the current context
AutoGlobalContext()->SetCurrent();

// Global initialization tests are special, we don't bother checking closure principle on them:
if(!strcmp("GlobalInitTest", info.test_case_name()))
return;

// Need to make sure we got back our exception filter before continuing:
ASSERT_TRUE(ecef.IsAutowired()) << "Failed to find the enclosed context exception filter; unit test may have incorrectly reset the enclosing context before returning";

// Now try to tear down the test context enclosure:
ctxt->SignalShutdown();

// Do not allow teardown to take more than 250 milliseconds or so
if(!ctxt->Wait(std::chrono::milliseconds(250))) {
// Critical error--took too long to tear down
assert(false);
}

static const char s_autothrow[] = "AUTOTHROW_";
if(!strncmp(s_autothrow, info.name(), ARRAYCOUNT(s_autothrow) - 1))
// Throw expected, end here
return;

// If an exception occurred somewhere, report it:
ASSERT_FALSE(ecef->m_excepted)
<< "An unhandled exception occurred in this context" << std::endl
<< "[" << (ecef->m_ti ? ecef->m_ti->name() : "unknown") << "] " << ecef->m_what;
}
10 changes: 5 additions & 5 deletions src/autowiring/AutoPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void AutoPacket::MarkUnsatisfiable(const std::type_info& info) {

// Update satisfaction inside of lock
decoration = &dFind->second;
for(auto& satCounter : decoration->m_subscribers) {
for(const auto& satCounter : decoration->m_subscribers) {
if(satCounter.second)
// Entry is mandatory, leave it unsatisfaible
continue;
Expand Down Expand Up @@ -115,7 +115,7 @@ void AutoPacket::UpdateSatisfaction(const std::type_info& info) {

// Update satisfaction inside of lock
decoration = &dFind->second;
for(auto& satCounter : decoration->m_subscribers)
for(const auto& satCounter : decoration->m_subscribers)
if(satCounter.first->Decrement(satCounter.second))
callQueue.push_back(satCounter.first);
}
Expand Down Expand Up @@ -159,8 +159,8 @@ void AutoPacket::PulseSatisfaction(DecorationDisposition* pTypeSubs[], size_t nI
{
std::lock_guard<std::mutex> lk(m_lock);
for(size_t i = nInfos; i--;) {
for(auto& satCounter : pTypeSubs[i]->m_subscribers) {
auto& cur = satCounter.first;
for(const auto& satCounter : pTypeSubs[i]->m_subscribers) {
SatCounter* cur = satCounter.first;
if (satCounter.second) {
++cur->remaining;
}
Expand Down Expand Up @@ -194,7 +194,7 @@ void AutoPacket::Initialize(void) {

// Call all subscribers with no required or optional arguments:
// NOTE: This may result in decorations that cause other subscribers to be called.
for (auto* call : callCounters)
for (SatCounter* call : callCounters)
call->CallAutoFilter(*this);

// Initial satisfaction of the AutoPacket:
Expand Down
5 changes: 2 additions & 3 deletions src/autowiring/BasicThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ void BasicThread::Stop(bool graceful) {

void BasicThread::ForceCoreThreadReidentify(void) {
for(const auto& ctxt : ContextEnumerator(AutoGlobalContext())) {
auto threadListCpy = ctxt->CopyBasicThreadList();
for(auto q = threadListCpy.begin(); q != threadListCpy.end(); q++)
(**q).SetCurrentThreadName();
for(const auto& thread : ctxt->CopyBasicThreadList())
thread->SetCurrentThreadName();
}
}

Expand Down
Loading

0 comments on commit 3b20c77

Please sign in to comment.