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

Implptr Get function #27

Merged
merged 10 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
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
14 changes: 14 additions & 0 deletions include/ignition/utils/detail/ImplPtr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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
18 changes: 18 additions & 0 deletions test/integration/implptr/ImplPtr_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ 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);
}

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

/////////////////////////////////////////////////
TEST(UniqueImplPtr, GetAccessor)
{
MovableObject object(28, "golden_string");
azeey marked this conversation as resolved.
Show resolved Hide resolved
int value = object.ThreadIncrementInt();
EXPECT_EQ(value, 1);
EXPECT_EQ(object.GetInt(), 1);
}

/////////////////////////////////////////////////
TEST(UniqueImplPtr, MoveAssign)
{
Expand Down
25 changes: 25 additions & 0 deletions test/integration/implptr/implptr_test_classes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@

#include "implptr_test_classes.hh"

#include <future>
azeey marked this conversation as resolved.
Show resolved Hide resolved
#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 +67,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 +107,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