Skip to content

Commit

Permalink
Support assignment in Some
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 7, 2016
1 parent ae0e85a commit ed0cc9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/shogun/base/some.h
Expand Up @@ -31,6 +31,7 @@ namespace shogun
Some(const Some<T>& other);
Some(Some<T>&& other);
Some& operator=(const Some<T>& other);
Some& operator=(T* other);
~Some();

/** Casts the underlying object back to raw pointer
Expand All @@ -45,6 +46,7 @@ namespace shogun
T* operator->();
private:
using std::shared_ptr<T>::get;
using std::shared_ptr<T>::reset;
};

template <typename T>
Expand All @@ -67,6 +69,13 @@ namespace shogun
{
}
template <typename T>
Some<T>& Some<T>::operator=(T* other)
{
this->reset(other);
SG_REF(this->get());
return *this;
}
template <typename T>
Some<T>::operator T*()
{
T* ptr = this->get();
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/base/Some_unittest.cc
Expand Up @@ -27,6 +27,17 @@ TEST(Some,basic)
SG_UNREF(raw);
}

TEST(Some,reassignment)
{
auto kernel = some<CGaussianKernel>();
CGaussianKernel* raw = new CGaussianKernel();
EXPECT_EQ(1, kernel->ref_count());
EXPECT_EQ(0, raw->ref_count());
kernel = raw;
EXPECT_TRUE(kernel->equals(raw));
EXPECT_EQ(1, kernel->ref_count());
}

TEST(Some,get)
{
auto kernel = some<CGaussianKernel>();
Expand Down

0 comments on commit ed0cc9e

Please sign in to comment.