-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from leapmotion/ref-functionaldecoration
AutoPacket functional decoration
- Loading branch information
Showing
6 changed files
with
394 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved. | ||
#pragma once | ||
|
||
#include "is_auto_filter.h" | ||
#include "Deferred.h" | ||
|
||
/// <summary> | ||
/// Transmutes a function returning void to an instance that can be called by AutoPacket. | ||
/// The specializations distinguish between void and Deferred return types. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default constructor yields an AutoFilter that does nothing. | ||
/// </remarks> | ||
template<class Ret, class... Args> | ||
struct MicroAutoFilter { | ||
// This case pertains only when the return value is not recognized | ||
static_assert(is_auto_filter_return<Ret>{}, | ||
"The return is not an allowed type for AutoFilter methods"); | ||
}; | ||
template<class... Args> | ||
struct MicroAutoFilter<void, Args...> { | ||
MicroAutoFilter(const std::function<void(Args...)>& filter) : m_filter(filter) { | ||
static_assert(all_auto_filter_args<Args...>::value, | ||
"At least one argument is not an allowed type for AutoFilter methods"); | ||
} | ||
|
||
void AutoFilter(Args... args) { | ||
if (m_filter) | ||
return m_filter(std::move(args)...); | ||
} | ||
|
||
protected: | ||
std::function<void(Args...)> m_filter; | ||
}; | ||
template<class... Args> | ||
struct MicroAutoFilter<Deferred, Args...> { | ||
MicroAutoFilter(const std::function<void(Args...)>& filter) : m_filter(filter) { | ||
static_assert(all_auto_filter_args<Args...>::value, | ||
"At least one argument is not an allowed type for AutoFilter methods"); | ||
} | ||
|
||
Deferred AutoFilter(Args... args) { | ||
if (m_filter) | ||
return m_filter(args...); | ||
return Deferred(this); | ||
} | ||
|
||
protected: | ||
std::function<void(Args...)> m_filter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved. | ||
#pragma once | ||
|
||
#include <functional> | ||
#include TYPE_TRAITS_HEADER | ||
|
||
class Deferred; | ||
template<class T, bool auto_ready> class auto_out; | ||
|
||
/// <summary> | ||
/// Determines whether Arg is an instance of auto_out | ||
/// </summary> | ||
template<class Arg> | ||
struct is_auto_out : | ||
std::false_type | ||
{}; | ||
template<class Arg, bool auto_ready> | ||
struct is_auto_out<auto_out<Arg, auto_ready>> : | ||
std::true_type | ||
{}; | ||
|
||
/// <summary> | ||
/// Determines whether Arg is an allowed AutoFilter argument: | ||
/// - const input& output | ||
/// - auto_out<ouput>& | ||
/// </summary> | ||
/// <remarks> | ||
/// Only strict declaration types are supported. | ||
/// The argument type "const AutoPacket&" is valid but "AutoPacket&" is not. | ||
/// </remarks> | ||
template<class Arg> | ||
struct is_auto_filter_arg : | ||
std::integral_constant<bool, | ||
(std::is_reference<Arg>{} && std::is_const<typename std::remove_reference<Arg>::type>{}) || | ||
is_auto_out<typename std::remove_reference<Arg>::type>{} | ||
> | ||
{}; | ||
|
||
/// <summary> | ||
/// Determines whether Arg is an allowed AutoFilter argument list. | ||
/// </summary> | ||
/// <remarks> | ||
/// Only strict declaration types are supported. | ||
/// At least one argument is required in order for the function to be valid. | ||
/// If calling on instantiation is desired declare "const AutoPacket&" | ||
/// as the only argument. | ||
/// </remarks> | ||
template<class... Args> | ||
struct all_auto_filter_args : | ||
std::false_type | ||
{}; | ||
template<class Head, class... Tail> | ||
struct all_auto_filter_args<Head, Tail...> : | ||
std::integral_constant<bool, is_auto_filter_arg<Head>{} && all_auto_filter_args<Tail...>{}> | ||
{}; | ||
template<class Head> | ||
struct all_auto_filter_args<Head> : | ||
is_auto_filter_arg<Head> | ||
{}; | ||
|
||
/// <summary> | ||
/// Determines whether the return value of a function is allowed for an AutoFilter: | ||
/// - void | ||
/// - Deferred | ||
/// </summary> | ||
template<class Ret> | ||
struct is_auto_filter_return : | ||
std::integral_constant<bool, | ||
std::is_same<void, Ret>{} || | ||
std::is_same<Deferred, Ret>{} | ||
> | ||
{}; | ||
|
||
///<summary> | ||
/// Determines whether T has the type of an autofilter method | ||
///</summary> | ||
template <class T> | ||
struct is_auto_filter : | ||
std::false_type | ||
{}; | ||
template <class Ret, class... Args> | ||
struct is_auto_filter<std::function<Ret(Args...)>> : | ||
std::integral_constant<bool, is_auto_filter_return<Ret>{} && all_auto_filter_args<Args...>{}> | ||
{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.