Skip to content

Commit

Permalink
Merge pull request #994 from leapmotion/feature-observableops
Browse files Browse the repository at this point in the history
Add some convenience operators to observable
  • Loading branch information
yeswalrus authored Sep 29, 2016
2 parents 78bf34d + a851a7d commit 42bfbea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/autowiring/observable.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class observable {
}
};


template<typename T>
struct marshaller<observable<T>> :
marshaller_base
Expand All @@ -100,3 +99,16 @@ struct marshaller<observable<T>> :
};

}

namespace std {
template<typename T>
struct hash<autowiring::observable<T>> {
hash(void) = default;

hash<T> interior;

auto operator()(const autowiring::observable<T>& value) const -> decltype(interior(value.get())) {
return interior(value.get());
}
};
}
39 changes: 39 additions & 0 deletions src/autowiring/test/ObservableTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include <autowiring/observable.h>
#include <set>
#include <unordered_set>

class ObservableTest:
public testing::Test
Expand Down Expand Up @@ -36,3 +38,40 @@ TEST_F(ObservableTest, BeforeAndAfter) {
ASSERT_EQ(8, obBefore) << "\"Before\" value in onBeforeChanged was not correct";
ASSERT_EQ(9, obAfter) << "\"AfFter\" value in onBeforeChanged was not correct";
}

TEST_F(ObservableTest, SetOfObservable) {
std::unordered_set<autowiring::observable<int>> a;
a.insert(4449);
a.insert(44410);
a.insert(44411);
ASSERT_EQ(1, a.count(4449));
ASSERT_EQ(1, a.count(44410));
ASSERT_EQ(1, a.count(44411));

std::set<autowiring::observable<int>> b;
b.insert(9);
b.insert(12);
b.insert(44);
ASSERT_EQ(1, b.count(9));
ASSERT_EQ(1, b.count(12));
ASSERT_EQ(1, b.count(44));
}

TEST_F(ObservableTest, MathOperators) {
const autowiring::observable<int> one(1);
const autowiring::observable<int> two(2);

// plus, minus, multiply, divide
ASSERT_EQ(3, one + 2);
ASSERT_EQ(1, two - 1);
ASSERT_EQ(2.0, one * 2.0);
ASSERT_EQ(two, two / one);

// all comparison operators
ASSERT_TRUE(one == 1);
ASSERT_TRUE(one != 2);
ASSERT_TRUE(one < 2);
ASSERT_TRUE(one >= 1);
ASSERT_FALSE(one > two);
ASSERT_FALSE(two <= one);
}

0 comments on commit 42bfbea

Please sign in to comment.