Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some convenience operators to observable #994

Merged
merged 7 commits into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/autowiring/observable.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
// Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved.
#pragma once
#include "marshaller.h"
#include "signal.h"
Expand Down Expand Up @@ -74,7 +74,6 @@ class observable {
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These operators don't seem to be hit at all via the unit test. I mean, I can remove them and the test still compiles and runs.

I suspect it's because operator const T&(void) const { return val; } already does everything we need.

Still investigating.



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

}

namespace std {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition, this will be handy

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());
}
};
}
41 changes: 40 additions & 1 deletion src/autowiring/test/ObservableTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
// 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);
}