Skip to content

Commit

Permalink
Merge pull request #34 from leapmotion/ref-header-organization
Browse files Browse the repository at this point in the history
Header organization
  • Loading branch information
codemercenary committed Aug 7, 2014
2 parents 230b121 + 46b0670 commit 49e3596
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 165 deletions.
3 changes: 1 addition & 2 deletions autowiring/AutoCheckout.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include <algorithm>

class AutoPacket;

Expand Down Expand Up @@ -66,4 +65,4 @@ class AutoCheckout {
completion = rhs.completion;
return *this;
}
};
};
1 change: 0 additions & 1 deletion autowiring/AutoFuture.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ class AutoFuture
return true;
}
};

2 changes: 1 addition & 1 deletion autowiring/AutoInjectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AutoInjectableExpression:
{}

void operator()(AutoFuture* pFuture) const override {
auto added = CallByUnpackingTuple(typename gen_index_tuple<sizeof...(Args)>::type());
auto added = CallByUnpackingTuple(typename make_index_tuple<sizeof...(Args)>::type());
if(pFuture)
*pFuture += added;
}
Expand Down
4 changes: 2 additions & 2 deletions autowiring/AutoNetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <map>
#include <set>

struct EventIdentifierBase;
struct TypeIdentifierBase;

class AutoNetServer:
public CoreThread,
Expand Down Expand Up @@ -153,7 +153,7 @@ class AutoNetServer:
std::map<int, CoreContext*> m_ContextPtrs;

// All event types
std::set<std::shared_ptr<EventIdentifierBase>> m_EventTypes;
std::set<std::shared_ptr<TypeIdentifierBase>> m_EventTypes;

// All ContextMembers
std::map<std::string, std::function<void(void)>> m_AllTypes;
Expand Down
13 changes: 2 additions & 11 deletions autowiring/Autowired.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Decompose.h"
#include "Deferred.h"
#include "GlobalCoreContext.h"
#include "TypeRegistry.h"
#include MEMORY_HEADER
#include ATOMIC_HEADER

Expand Down Expand Up @@ -121,8 +120,6 @@ class Autowired:
AutowirableSlot<T>(ctxt ? ctxt->template ResolveAnchor<T>() : ctxt),
m_pFirstChild(nullptr)
{
// Add this type to the TypeRegistry
(void) RegType<T>::r;
if(ctxt)
ctxt->Autowire(*this);
}
Expand Down Expand Up @@ -254,21 +251,15 @@ class AutoRequired:
// !!!!! Read comment in Autowired if you get a compiler error here !!!!!
AutoRequired(const std::shared_ptr<CoreContext>& ctxt = CoreContext::CurrentContext()):
std::shared_ptr<T>(ctxt->template Inject<T>())
{
// Add this type to the TypeRegistry
(void) RegType<T>::r;
}
{}

/// <summary>
/// Construct overload, for types which take constructor arguments
/// </summary>
template<class... Args>
AutoRequired(const std::shared_ptr<CoreContext>& ctxt, Args&&... args) :
std::shared_ptr<T>(ctxt->template Construct<T>(std::forward<Args>(args)...))
{
// Add this type to the TypeRegistry
(void) RegType<T>::r;
}
{}

operator bool(void) const {
return IsAutowired();
Expand Down
4 changes: 4 additions & 0 deletions autowiring/AutowiringEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
struct AnySharedPointer;
class CoreContext;

/// <summary>
/// These events are broadcast internally by Autowiring
/// for visualizing internal behavior.
/// </summary
class AutowiringEvents {
public:
virtual void NewContext(CoreContext&)=0;
Expand Down
1 change: 0 additions & 1 deletion autowiring/BasicThreadStateBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ struct BasicThreadStateBlock:
// The current thread, if running
std::thread m_thisThread;
};

2 changes: 1 addition & 1 deletion autowiring/Bolt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "Decompose.h"
#include "is_any.h"
#include "BoltBase.h"

/// <summary>
Expand Down
17 changes: 16 additions & 1 deletion autowiring/CoreContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ExceptionFilter.h"
#include "TeardownNotifier.h"
#include "EventRegistry.h"
#include "TypeRegistry.h"
#include "TypeUnifier.h"

#include <list>
Expand Down Expand Up @@ -315,7 +316,7 @@ class CoreContext:
pBoltBase(autowiring::fast_pointer_cast<BoltBase>(value)),
receivesEvents([this]{
for (auto evt = g_pFirstEventEntry; evt; evt = evt->pFlink) {
auto identifier = evt->NewEventIdentifier();
auto identifier = evt->NewTypeIdentifier();
if (identifier->IsSameAs(pObject.get()))
return true;
}
Expand Down Expand Up @@ -521,6 +522,9 @@ class CoreContext:
/// </summary>
template<typename T, typename... Args>
std::shared_ptr<T> Construct(Args&&... args) {
// Add this type to the TypeRegistry
(void) RegType<T>::r;

// If T doesn't inherit Object, then we need to compose a unifying type which does
typedef typename SelectTypeUnifier<T>::type TActual;
static_assert(std::is_base_of<Object, TActual>::value, "Constructive type does not implement Object as expected");
Expand Down Expand Up @@ -934,6 +938,16 @@ class CoreContext:
}
};

/// <summary>
/// Forward-declarable version of CoreContext::InjectCurrent
/// </summary>
namespace autowiring {
template<typename T>
void InjectCurrent(void){
CoreContext::InjectCurrent<T>();
}
}

/// <summary>
/// Constant type optimization for named sigil types
/// </summary>
Expand All @@ -960,3 +974,4 @@ template<typename T, typename... Sigil>
void CoreContext::AutoRequireMicroBolt(void) {
Inject<MicroBolt<T, Sigil...>>();
}

19 changes: 0 additions & 19 deletions autowiring/Decompose.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@
#include TYPE_TRAITS_HEADER
#include <typeinfo>

/// <summary>
/// Extended version of is_same
/// </summary>
/// <remarks>
/// Inherits from true_type if T is the same as any of Us...
/// </remarks>
template<typename T, typename... Us>
struct is_any_same;

template<typename T>
struct is_any_same<T> {
static const bool value = false;
};

template<typename T, typename U, typename... Us>
struct is_any_same<T, U, Us...> {
static const bool value = std::is_same<T, U>::value || is_any_same<T, Us...>::value;
};

/// <summary>
/// Provides some static reflection support for member function pointers
/// </summary>
Expand Down
49 changes: 2 additions & 47 deletions autowiring/Deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
/// Support class for deserializing byte-streams input as std::string into a variety of formats
/// </summary>
namespace Auto {
namespace autowiring {
struct SerializableSigil{};

struct Serialize:
Expand Down Expand Up @@ -40,49 +40,4 @@ namespace Auto {
return t;
}
};


/// <summary>
/// Utility type which enables the composition of a sequence [0, sizeof...(Ts))
/// </summary>
template<unsigned ...Indicies>
struct index_tuple {

/// Generate an index_tuple with an additional element.
template<unsigned N>
struct append {
typedef index_tuple<Indicies..., N> type;
};
};

template<typename ...Members>
struct make_index_tuple; //So linux doesn't bitch

/// Unary metafunction that generates an index_tuple containing [0, Size)
template<typename Head, typename ...Tail>
struct make_index_tuple<Head, Tail...>{
typedef typename make_index_tuple<Tail...>::type::template append<sizeof...(Tail)>::type type;
};

// Terminal case of the recursive metafunction.
template<>
struct make_index_tuple<>{
typedef index_tuple<> type;
};

static_assert(
std::is_same<
make_index_tuple<>::type,
index_tuple<>
>::value,
"Index tuple base case was not correctly specialized"
);

static_assert(
std::is_same<
make_index_tuple<int, int, int>::type,
index_tuple<0, 1, 2>
>::value,
"Index tuple conversion function did not properly index a sample type"
);
}
}
9 changes: 5 additions & 4 deletions autowiring/EventInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once
#include "Decompose.h"
#include "Deserialize.h"
#include "index_tuple.h"
#include <string>
#include <sstream>
#include <deque>
Expand Down Expand Up @@ -39,14 +40,14 @@ struct Expression<R(W::*)(ToBindArgs...) >: public ExpressionBase
/// parameter pack expansion.
/// </summary>
void DeserializeAndForward(std::deque<std::string> & d){
DeserializeAndForward(d, typename Auto::make_index_tuple<ToBindArgs...>::type());
DeserializeAndForward(d, typename make_index_tuple<sizeof...(ToBindArgs)>::type());
}

template<unsigned... I>
void DeserializeAndForward(std::deque<std::string> & d, Auto::index_tuple<I...>){
template<int... I>
void DeserializeAndForward(std::deque<std::string> & d, index_tuple<I...>){
auto it = d.begin();
AutoFired<W> sender;
sender(m_memfunc)(Auto::deser<ToBindArgs>::deserialize(it[I])...);
sender(m_memfunc)(autowiring::deser<ToBindArgs>::deserialize(it[I])...);
}
};

Expand Down
7 changes: 5 additions & 2 deletions autowiring/EventOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ class EventOutputStreamBase {

template <class Arg>
//stub out such that if ARGUMENT defines a static method called AutoSerialize which returns an std::string,
typename std::enable_if<std::is_base_of<Auto::Serialize, Arg>::value, void >::type
typename std::enable_if<std::is_base_of<autowiring::Serialize, Arg>::value, void >::type
SerializeMethod(Arg & arg){
m_OutputStream << "\xD8" << arg.AutoSerialize();
}

template <class Arg1>
//SFINAE STUB OUT: replace with check_if overloads <<
typename std::enable_if<!std::is_same<Arg1, std::basic_string<char> const *>::value && !std::is_base_of<Auto::Serialize, Arg1>::value, void >::type
typename std::enable_if<
!std::is_same<Arg1, std::basic_string<char> const *>::value &&
!std::is_base_of<autowiring::Serialize, Arg1>::value,
void>::type
SerializeMethod(Arg1 & arg1){
assert(false);
//static_assert(false, "Fundamental belief about serialized argument types violated");
Expand Down
31 changes: 5 additions & 26 deletions autowiring/EventRegistry.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include <typeinfo>
#include "TypeIdentifier.h"
#include TYPE_TRAITS_HEADER
#include STL_TUPLE_HEADER
#include MEMORY_HEADER
Expand All @@ -9,27 +9,6 @@ template<class T>
class JunctionBox;

class JunctionBoxBase;
class Object;

// Checks if an Object* listens to a event T;
struct EventIdentifierBase {
virtual bool IsSameAs(const Object* obj) = 0;
virtual const std::type_info& Type() = 0;
};

template<typename T>
struct EventIdentifier:
public EventIdentifierBase
{
// true if "obj" is an event receiver for T
bool IsSameAs(const Object* obj){
return !!dynamic_cast<const T*>(obj);
}

const std::type_info& Type(){
return typeid(T);
}
};

struct EventRegistryEntry {
EventRegistryEntry(const std::type_info& ti);
Expand All @@ -53,7 +32,7 @@ struct EventRegistryEntry {
/// <summary>
/// Used to create a type identifier value, for use with AutoNet
/// </summary>
virtual std::shared_ptr<EventIdentifierBase> NewEventIdentifier(void) const = 0;
virtual std::shared_ptr<TypeIdentifierBase> NewTypeIdentifier(void) const = 0;
};

template<class T>
Expand All @@ -73,9 +52,9 @@ struct EventRegistryEntryT:
);
}

std::shared_ptr<EventIdentifierBase> NewEventIdentifier(void) const override {
return std::static_pointer_cast<EventIdentifierBase>(
std::make_shared<EventIdentifier<T>>()
std::shared_ptr<TypeIdentifierBase> NewTypeIdentifier(void) const override {
return std::static_pointer_cast<TypeIdentifierBase>(
std::make_shared<TypeIdentifier<T>>()
);
}
};
Expand Down
15 changes: 2 additions & 13 deletions autowiring/InvokeRelay.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "is_any.h"
#include "index_tuple.h"
#include "JunctionBox.h"

class Deferred;

template<typename T>
class JunctionBox;

// Generate and index tuple
template<int ...>
struct index_tuple {};

template<int N, int... S>
struct gen_index_tuple: gen_index_tuple<N - 1, N - 1, S...> {};

template<int... S>
struct gen_index_tuple<0, S...> {
typedef index_tuple<S...> type;
};

/// <summary>
/// A fully bound member function call
/// </summary>
Expand Down Expand Up @@ -55,7 +44,7 @@ class CurriedInvokeRelay:

public:
void operator()(void) override {
CallByUnpackingTuple(typename gen_index_tuple<sizeof...(Args)>::type());
CallByUnpackingTuple(typename make_index_tuple<sizeof...(Args)>::type());
}
};

Expand Down
2 changes: 1 addition & 1 deletion autowiring/SharedPointerSlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,4 @@ struct SharedPointerSlotT:
T& operator*(void) const {
return *get();
}
};
};
Loading

0 comments on commit 49e3596

Please sign in to comment.