-
-
Notifications
You must be signed in to change notification settings - Fork 511
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 #338 from dronecore/add-camera
Added camera plugin with basic functionality
- Loading branch information
Showing
38 changed files
with
5,611 additions
and
126 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
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
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
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,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 |
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,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>()); | ||
} |
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.