Skip to content

Commit

Permalink
Merge pull request #27 from ignitionrobotics/implptr_get
Browse files Browse the repository at this point in the history
Implptr Get function
  • Loading branch information
nkoenig authored Nov 23, 2021
2 parents e9c439b + 6419a1b commit bb3f23a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/ignition/utils/ImplPtr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ namespace ignition
/// \return Immutable access to the contained object's members.
public: const T *operator->() const;

/// \brief Non-const member access function. This const-unqualified
/// operator ensures that logical const-correctness is followed by the
/// consumer class.
/// \return Mutable access to the contained object's members.
public: T *Get();

/// \brief Const member access function. This const-qualified operator
/// ensures that logical const-correctness is followed by the consumer
/// class.
/// \return Immutable access to the contained object's members.
public: const T *Get() const;

/// \internal \brief Create a clone of this ImplPtr's contents. This is
/// for internal use only. The copy constructor and copy assignment
/// operators should suffice for consumers.
Expand Down
15 changes: 15 additions & 0 deletions include/ignition/utils/detail/ImplPtr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace ignition
{
//////////////////////////////////////////////////
template <class T, class CopyConstruct, class CopyAssign>
// cppcheck-suppress syntaxError
template <class C, class A>
CopyMoveDeleteOperations<T, CopyConstruct, CopyAssign>::
CopyMoveDeleteOperations(C &&_construct, A &&_assign)
Expand Down Expand Up @@ -110,6 +111,20 @@ namespace ignition
return ptr.get();
}

//////////////////////////////////////////////////
template <class T, class Deleter, class Operations>
T *ImplPtr<T, Deleter, Operations>::Get()
{
return ptr.get();
}

//////////////////////////////////////////////////
template <class T, class Deleter, class Operations>
const T *ImplPtr<T, Deleter, Operations>::Get() const
{
return ptr.get();
}

//////////////////////////////////////////////////
template <class T, typename... Args>
ImplPtr<T> MakeImpl(Args &&..._args)
Expand Down
34 changes: 34 additions & 0 deletions test/integration/implptr/ImplPtr_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ TEST(ImplPtr, CopyConstruct)
EXPECT_EQ(object.GetString(), other.GetString());
}

/////////////////////////////////////////////////
TEST(ImplPtr, GetAccessor)
{
CopyableObject object(28, "golden_string");
int value = object.ThreadIncrementInt();
EXPECT_EQ(value, 1);
EXPECT_EQ(object.GetInt(), 1);

CopyableObject copied;
copied = object;
value = copied.ThreadIncrementInt();
EXPECT_EQ(value, 1);
EXPECT_EQ(copied.GetInt(), 1);

EXPECT_EQ(copied.ThreadIncrementInt(), object.ThreadIncrementInt());
EXPECT_EQ(copied.GetInt(), 1);
EXPECT_EQ(object.GetInt(), 1);
}

/////////////////////////////////////////////////
TEST(ImplPtr, CopyAssign)
{
Expand Down Expand Up @@ -111,6 +130,21 @@ TEST(UniqueImplPtr, MoveConstruct)
EXPECT_EQ(other.GetString(), moved.GetString());
}

/////////////////////////////////////////////////
TEST(UniqueImplPtr, GetAccessor)
{
MovableObject object(28, "golden_string");
int value = object.ThreadIncrementInt();
EXPECT_EQ(value, 1);
EXPECT_EQ(object.GetInt(), 1);

MovableObject moved;
moved = std::move(object);
value = moved.ThreadIncrementInt();
EXPECT_EQ(value, 1);
EXPECT_EQ(moved.GetInt(), 1);
}

/////////////////////////////////////////////////
TEST(UniqueImplPtr, MoveAssign)
{
Expand Down
24 changes: 24 additions & 0 deletions test/integration/implptr/implptr_test_classes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
#include "implptr_test_classes.hh"

#include <string>
#include <thread>

using namespace ignition;
using namespace ignition::implptr_test_classes;

class ignition::implptr_test_classes::ObjectPrivate
{
public: void TestFunc()
{
this->ivalue += 1;
}

public: int ivalue;
public: std::string svalue;
};
Expand Down Expand Up @@ -60,6 +66,15 @@ void CopyableObject::SetString(const std::string &_value)
(*dataPtr).svalue = _value;
}

//////////////////////////////////////////////////
int CopyableObject::ThreadIncrementInt()
{
this->SetInt(0);
auto thread = std::thread(&ObjectPrivate::TestFunc, this->dataPtr.Get());
thread.join();
return this->GetInt();
}

//////////////////////////////////////////////////
MovableObject::MovableObject(const int _ivalue, const std::string &_svalue)
: dataPtr(utils::MakeUniqueImpl<ObjectPrivate>(_ivalue, _svalue))
Expand Down Expand Up @@ -91,6 +106,15 @@ void MovableObject::SetString(const std::string &_value)
(*dataPtr).svalue = _value;
}

//////////////////////////////////////////////////
int MovableObject::ThreadIncrementInt()
{
this->SetInt(0);
auto thread = std::thread(&ObjectPrivate::TestFunc, this->dataPtr.get());
thread.join();
return this->GetInt();
}

//////////////////////////////////////////////////
class CopyableObjectAlt::Implementation
{
Expand Down
11 changes: 11 additions & 0 deletions test/integration/implptr/implptr_test_classes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ignition/utils/ImplPtr.hh>
#include <ignition/utils/Export.hh>

#include <functional>
#include <string>

namespace ignition
Expand Down Expand Up @@ -49,6 +50,11 @@ namespace ignition
/// \brief Set the string value held by the pimpl
public: void SetString(const std::string &_value);

/// \brief Uses a thread to increment the internal integer value from
/// 0 to 1.
/// \return The final internal integer value, which should be 1.
public: int ThreadIncrementInt();

/// \brief Pointer to implementation
/// This demonstrates using an implementation class that is
/// forward-declared outside of this class.
Expand All @@ -74,6 +80,11 @@ namespace ignition
/// \brief Set the string value held by the pimpl
public: void SetString(const std::string &_value);

/// \brief Uses a thread to increment the internal integer value from
/// 0 to 1.
/// \return The final internal integer value, which should be 1.
public: int ThreadIncrementInt();

/// \brief Pointer to implementation
/// This demonstrates using an implementation class that is
/// forward-declared outside of this class.
Expand Down

0 comments on commit bb3f23a

Please sign in to comment.