-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
Added camera plugin with basic functionality #338
Changes from 88 commits
452ba9c
780c1e1
4916523
f50af93
eba1144
81f33bd
8f99d44
e8c7091
0a95d07
6ca5d05
604288a
5fba828
8261818
0240dff
1960f18
6bffd15
638cac0
a6e18fc
20be737
fcc6787
8b1c662
91a762b
3ee1f72
151f449
08b49b4
8a6ca92
31cf390
821e790
dccb346
fc0bbfa
9f9646f
c168dd3
59c5f5c
67a19a7
097a29a
59aa9b5
ae84ed3
2c70a9b
bd7618b
8830330
2069406
f97e10e
62eaadd
ead13c9
54e7e56
3043d75
d6f4fc8
b1c1b9d
6136eb9
b4227a7
65db7fc
9b7a36b
7f9e3ae
6015f27
877e620
8207824
18a755c
a3d3d92
4fed230
f162c42
bbcd979
4965f56
e003bc5
b541d79
4329a23
3ebd5b9
770cd9d
cd5f905
80460c0
eaf1cf5
1781bfe
6211461
6249eaa
cd7b264
c3a2682
1367076
3bba620
efdfa43
1ff5fdd
6f40598
ff2580d
35a7e83
a94ff6d
993bcf5
6f63912
8c54323
7594450
c8a5c17
bcb5009
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#pragma once | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
#include <typeinfo> | ||
#include <cassert> | ||
#include "log.h" | ||
|
||
namespace dronecore { | ||
|
||
// Any class taken from: | ||
// https://codereview.stackexchange.com/questions/20058/c11-any-class | ||
|
||
|
||
template <class T> | ||
using StorageType = typename std::decay<T>::type; | ||
|
||
struct Any { | ||
bool is_null() const { return !ptr; } | ||
bool not_null() const { return ptr; } | ||
|
||
template<typename U> Any(U &&value) | ||
: ptr(new Derived<StorageType<U>>(std::forward<U>(value))) {} | ||
|
||
template<class U> bool is() const | ||
{ | ||
typedef StorageType<U> T; | ||
|
||
auto derived = dynamic_cast<Derived<T>*>(ptr); | ||
|
||
return derived; | ||
} | ||
|
||
template<class U> | ||
StorageType<U> &as() const | ||
{ | ||
typedef StorageType<U> T; | ||
|
||
auto derived = dynamic_cast<Derived<T>*>(ptr); | ||
|
||
if (!derived) { | ||
// FIXME: We don't have exceptions, so this is commented out | ||
// and we'll abort instead. | ||
//throw bad_cast(); | ||
LogErr() << "Need to abort because of a bad_cast"; | ||
abort(); | ||
} | ||
|
||
return derived->value; | ||
} | ||
|
||
template<class U> | ||
operator U() const | ||
{ | ||
return as<StorageType<U>>(); | ||
} | ||
|
||
Any() | ||
: ptr(nullptr) {} | ||
|
||
Any(Any &that) | ||
: ptr(that.clone()) {} | ||
|
||
Any(Any &&that) | ||
: ptr(that.ptr) | ||
{ | ||
that.ptr = nullptr; | ||
} | ||
|
||
Any(const Any &that) | ||
: ptr(that.clone()) {} | ||
|
||
Any(const Any &&that) | ||
: ptr(that.clone()) {} | ||
|
||
Any &operator=(const Any &a) | ||
{ | ||
if (ptr == a.ptr) { | ||
return *this; | ||
} | ||
|
||
auto old_ptr = ptr; | ||
|
||
ptr = a.clone(); | ||
|
||
delete old_ptr; | ||
|
||
return *this; | ||
} | ||
|
||
Any &operator=(Any &&a) | ||
{ | ||
if (ptr == a.ptr) { | ||
return *this; | ||
} | ||
|
||
std::swap(ptr, a.ptr); | ||
|
||
return *this; | ||
} | ||
|
||
~Any() | ||
{ | ||
delete ptr; | ||
} | ||
|
||
private: | ||
struct Base { | ||
virtual ~Base() {} | ||
|
||
virtual Base *clone() const = 0; | ||
}; | ||
|
||
template<typename T> | ||
struct Derived : Base { | ||
template<typename U> Derived(U &&value_tmp) : value(std::forward<U>(value_tmp)) { } | ||
|
||
T value; | ||
|
||
Base *clone() const { return new Derived<T>(value); } | ||
}; | ||
|
||
Base *clone() const | ||
{ | ||
if (ptr) { | ||
return ptr->clone(); | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
Base *ptr; | ||
}; | ||
|
||
} // namespace dronecore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
#include <string> | ||
#include "any.h" | ||
#include <gtest/gtest.h> | ||
|
||
using namespace dronecore; | ||
|
||
TEST(Any, StringAndInt) | ||
{ | ||
Any n; | ||
ASSERT_TRUE(n.is_null()); | ||
|
||
std::string s1 = "foo"; | ||
|
||
Any a1 = s1; | ||
|
||
ASSERT_TRUE(a1.not_null()); | ||
ASSERT_TRUE(a1.is<std::string>()); | ||
ASSERT_TRUE(!a1.is<int>()); | ||
|
||
Any a2(a1); | ||
|
||
ASSERT_TRUE(a2.not_null()); | ||
ASSERT_TRUE(a2.is<std::string>()); | ||
ASSERT_TRUE(!a2.is<int>()); | ||
|
||
std::string s2 = a2; | ||
|
||
ASSERT_TRUE(s1 == s2); | ||
} | ||
|
||
TEST(Any, Casts) | ||
{ | ||
const int some_number = 42; | ||
Any n; | ||
ASSERT_TRUE(n.is_null()); | ||
|
||
int i = some_number; | ||
n = i; | ||
|
||
ASSERT_TRUE(n.not_null()); | ||
ASSERT_EQ(n.as<int>(), some_number); | ||
|
||
// We can't actually cast using `as`. | ||
ASSERT_FLOAT_EQ(float(n.as<int>()), float(some_number)); | ||
} | ||
|
||
TEST(Any, Copys) | ||
{ | ||
const float some_float = 0.7f; | ||
|
||
Any n1; | ||
Any n2; | ||
ASSERT_TRUE(n1.is_null()); | ||
ASSERT_TRUE(n2.is_null()); | ||
|
||
n1 = some_float; | ||
n2 = n1; | ||
ASSERT_TRUE(n1.is<float>()); | ||
ASSERT_TRUE(n2.is<float>()); | ||
ASSERT_TRUE(n1.as<float>() == n2.as<float>()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ void CallEveryHandler::remove(const void *cookie) | |
auto it = _entries.find(const_cast<void *>(cookie)); | ||
if (it != _entries.end()) { | ||
_entries.erase(const_cast<void *>(cookie)); | ||
_iterator_invalidated = true; | ||
} | ||
} | ||
|
||
|
@@ -81,6 +82,13 @@ void CallEveryHandler::run_once() | |
_entries_mutex.lock(); | ||
} | ||
} | ||
|
||
// We leave the loop. | ||
// FIXME: there should be a nicer way to do this. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Just trying, maybe it's not better)
... instead of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yer I like it, thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with your suggestion is that the |
||
if (_iterator_invalidated) { | ||
_iterator_invalidated = false; | ||
break; | ||
} | ||
} | ||
_entries_mutex.unlock(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't wanna know :D