Skip to content

Commit

Permalink
Make KeyEvent rule-of-five compliant (#224)
Browse files Browse the repository at this point in the history
* Added copy constructor
* Added assign operator
* Added move constructor and move assignment

Signed-off-by: ahcorde <ahcorde@gmail.com>
  • Loading branch information
ahcorde authored Jun 9, 2021
1 parent 153f320 commit c6bddd8
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 22 deletions.
18 changes: 18 additions & 0 deletions events/include/ignition/common/KeyEvent.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ namespace ignition
/// \brief Constructor.
public: KeyEvent();

/// \brief Copy constructor.
/// \param[in] _other Other key event
public: KeyEvent(const KeyEvent &_other);

// \brief Move constructor.
/// \param[in] _other Other key event
public: KeyEvent(KeyEvent &&_other);

/// \brief Destructor
public: ~KeyEvent();

Expand Down Expand Up @@ -92,6 +100,16 @@ namespace ignition
/// \param[in] _alt Status of the alt key
public: void SetAlt(const bool _alt);

/// \brief Assignment operator
/// \param[in] _other Other key event
/// \return this
public: KeyEvent &operator=(const KeyEvent &_other);

/// \brief Move assignment operator.
/// \param[in] _other Other key event
/// \return this
public: KeyEvent& operator=(KeyEvent&& other);

IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
/// \brief Private data pointer
private: std::unique_ptr<KeyEventPrivate> dataPtr;
Expand Down
36 changes: 36 additions & 0 deletions events/src/KeyEvent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ KeyEvent::KeyEvent()
{
}

/////////////////////////////////////////////////
KeyEvent::KeyEvent(const KeyEvent &_other)
: dataPtr(new KeyEventPrivate)
{
*dataPtr = *_other.dataPtr;
}

/////////////////////////////////////////////////
KeyEvent::KeyEvent(KeyEvent &&_other)
{
this->dataPtr = std::move(_other.dataPtr);
_other.dataPtr = nullptr;
}

/////////////////////////////////////////////////
KeyEvent::~KeyEvent()
{
Expand Down Expand Up @@ -122,3 +136,25 @@ void KeyEvent::SetAlt(const bool _alt)
{
this->dataPtr->alt = _alt;
}

/////////////////////////////////////////////////
KeyEvent &KeyEvent::operator=(const KeyEvent &_other)
{
if (this == &_other)
return *this;

*this->dataPtr = *_other.dataPtr;

return *this;
}

/////////////////////////////////////////////////
KeyEvent &KeyEvent::operator=(KeyEvent&& other)
{
if (this->dataPtr)
{
this->dataPtr.reset();
}
this->dataPtr = std::move(other.dataPtr);
return *this;
}
128 changes: 106 additions & 22 deletions events/src/KeyEvent_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,111 @@ using namespace ignition;
using namespace common;

/////////////////////////////////////////////////
TEST(KeyEventTEST, URIPath)
TEST(KeyEventTEST, Constructor)
{
KeyEvent evt;
EXPECT_EQ(evt.Type(), KeyEvent::NO_EVENT);
EXPECT_EQ(evt.Key(), 0);
EXPECT_TRUE(evt.Text().empty());
EXPECT_FALSE(evt.Control());
EXPECT_FALSE(evt.Shift());
EXPECT_FALSE(evt.Alt());

evt.SetType(KeyEvent::PRESS);
evt.SetKey(123);
evt.SetText("hello");
evt.SetControl(true);
evt.SetShift(true);
evt.SetAlt(true);

EXPECT_EQ(evt.Type(), KeyEvent::PRESS);
EXPECT_EQ(evt.Key(), 123);
EXPECT_EQ(evt.Text(), "hello");
EXPECT_TRUE(evt.Control());
EXPECT_TRUE(evt.Shift());
EXPECT_TRUE(evt.Alt());
// copy constructor
{
KeyEvent evt;
EXPECT_EQ(evt.Type(), KeyEvent::NO_EVENT);
EXPECT_EQ(evt.Key(), 0);
EXPECT_TRUE(evt.Text().empty());
EXPECT_FALSE(evt.Control());
EXPECT_FALSE(evt.Shift());
EXPECT_FALSE(evt.Alt());

evt.SetType(KeyEvent::PRESS);
evt.SetKey(123);
evt.SetText("hello");
evt.SetControl(true);
evt.SetShift(true);
evt.SetAlt(true);

KeyEvent otherEvent(evt);
EXPECT_EQ(otherEvent.Type(), KeyEvent::PRESS);
EXPECT_EQ(otherEvent.Key(), 123);
EXPECT_EQ(otherEvent.Text(), "hello");
EXPECT_TRUE(otherEvent.Control());
EXPECT_TRUE(otherEvent.Shift());
EXPECT_TRUE(otherEvent.Alt());
}

// assignment
{
KeyEvent evt;
EXPECT_EQ(evt.Type(), KeyEvent::NO_EVENT);
EXPECT_EQ(evt.Key(), 0);
EXPECT_TRUE(evt.Text().empty());
EXPECT_FALSE(evt.Control());
EXPECT_FALSE(evt.Shift());
EXPECT_FALSE(evt.Alt());

evt.SetType(KeyEvent::PRESS);
evt.SetKey(123);
evt.SetText("hello");
evt.SetControl(true);
evt.SetShift(true);
evt.SetAlt(true);

KeyEvent otherEvent;
otherEvent = evt;
EXPECT_EQ(otherEvent.Type(), KeyEvent::PRESS);
EXPECT_EQ(otherEvent.Key(), 123);
EXPECT_EQ(otherEvent.Text(), "hello");
EXPECT_TRUE(otherEvent.Control());
EXPECT_TRUE(otherEvent.Shift());
EXPECT_TRUE(otherEvent.Alt());
}

// move assignment
{
KeyEvent evt;
EXPECT_EQ(evt.Type(), KeyEvent::NO_EVENT);
EXPECT_EQ(evt.Key(), 0);
EXPECT_TRUE(evt.Text().empty());
EXPECT_FALSE(evt.Control());
EXPECT_FALSE(evt.Shift());
EXPECT_FALSE(evt.Alt());

evt.SetType(KeyEvent::PRESS);
evt.SetKey(123);
evt.SetText("hello");
evt.SetControl(true);
evt.SetShift(true);
evt.SetAlt(true);

KeyEvent otherEvent;
otherEvent = std::move(evt);
EXPECT_EQ(otherEvent.Type(), KeyEvent::PRESS);
EXPECT_EQ(otherEvent.Key(), 123);
EXPECT_EQ(otherEvent.Text(), "hello");
EXPECT_TRUE(otherEvent.Control());
EXPECT_TRUE(otherEvent.Shift());
EXPECT_TRUE(otherEvent.Alt());
}

// move
{
KeyEvent evt;
EXPECT_EQ(evt.Type(), KeyEvent::NO_EVENT);
EXPECT_EQ(evt.Key(), 0);
EXPECT_TRUE(evt.Text().empty());
EXPECT_FALSE(evt.Control());
EXPECT_FALSE(evt.Shift());
EXPECT_FALSE(evt.Alt());

evt.SetType(KeyEvent::PRESS);
evt.SetKey(123);
evt.SetText("hello");
evt.SetControl(true);
evt.SetShift(true);
evt.SetAlt(true);

KeyEvent otherEvent(std::move(evt));
EXPECT_EQ(otherEvent.Type(), KeyEvent::PRESS);
EXPECT_EQ(otherEvent.Key(), 123);
EXPECT_EQ(otherEvent.Text(), "hello");
EXPECT_TRUE(otherEvent.Control());
EXPECT_TRUE(otherEvent.Shift());
EXPECT_TRUE(otherEvent.Alt());
}
}

0 comments on commit c6bddd8

Please sign in to comment.