Skip to content

Commit

Permalink
Merge pull request #61 from leapmotion/bug-errmsg
Browse files Browse the repository at this point in the history
Adding more descriptive static assertions to AutoFilter
  • Loading branch information
gtremper committed Aug 19, 2014
2 parents 039a83b + 72cccaf commit 862df7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 16 additions & 6 deletions autowiring/has_autofilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
template<class W, class Selector>
struct has_valid_autofilter {
// Evaluates to false for W::AutoFilter(void), else to true.
static const bool value = Decompose<decltype(&W::AutoFilter)>::N != 0;
static const int N = Decompose<decltype(&W::AutoFilter)>::N;
static const bool value = Decompose<decltype(&W::AutoFilter)>::N != 0;
};

// Specialization pertains only when W has no AutoFilter method.
template<class W>
struct has_valid_autofilter<W, std::false_type> {
static const bool value = false;
static const int N = -1;
static const bool value = false;
};

//===========================================================
Expand All @@ -43,8 +45,11 @@ struct has_unambiguous_autofilter
template<class U>
static std::true_type select(unnamed_constant<decltype(&U::AutoFilter), &U::AutoFilter>*);

// Conveninece typedef used externally:
typedef has_valid_autofilter<T, decltype(select<T>(nullptr))> has_valid;

// Evaluates to true only if T includes a unique AutoFilter method with at least one argument.
static const bool value = has_valid_autofilter<T, decltype(select<T>(nullptr))>::value;
static const bool value = has_valid::value;
};

class AutoPacket;
Expand All @@ -66,10 +71,15 @@ struct has_autofilter {
static const bool value = has_unambiguous_autofilter<T>::value;

// This class has at least one AutoFilter method
struct detect_ambiguous_autofilter : T, test_valid_autofilter {};
struct detect_ambiguous_autofilter: T, test_valid_autofilter {};

// Validate the case most likely to have problems
static_assert(has_unambiguous_autofilter<T>::has_valid::N != 0, "Cannot define AutoFilter(void), your AutoFilter routine must take at least one argument");

// Ensures a compiler error when the identification of T::AutoFilter is ambiguous.
// This cannot be in has_unambiguous_autofilter, since that would be recursive.
static_assert(value || has_unambiguous_autofilter<detect_ambiguous_autofilter>::value,
"Cannot define more than 1 AutoFilter method. Cannot define AutoFilter(void)");
static_assert(
value || has_unambiguous_autofilter<detect_ambiguous_autofilter>::value,
"Cannot define more than one AutoFilter method and all AutoFilter methods must be public"
);
};
1 change: 0 additions & 1 deletion src/autowiring/test/AutoFilterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ TEST_F(AutoFilterTest, VerifyAntiDecorate) {
/// </summary>
template<typename T>
struct good_autofilter {

// Evaluates to false when T does not include an AutoFilter method with at least one argument.
static const bool value = has_unambiguous_autofilter<T>::value;

Expand Down

0 comments on commit 862df7a

Please sign in to comment.