-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1812
- Loading branch information
Showing
5 changed files
with
255 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of clio: https://github.com/XRPLF/clio | ||
Copyright (c) 2025, the clio developers. | ||
Permission to use, copy, modify, and distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#pragma once | ||
|
||
#include <utility> | ||
|
||
namespace util { | ||
|
||
/** | ||
* @brief A base-class that can be used to check whether the current instance was moved from | ||
*/ | ||
class MoveTracker { | ||
bool wasMoved_ = false; | ||
|
||
protected: | ||
/** | ||
* @brief The function to be used by clients in order to check whether the instance was moved from | ||
* @return true if moved from; false otherwise | ||
*/ | ||
[[nodiscard]] bool | ||
wasMoved() const noexcept | ||
{ | ||
return wasMoved_; | ||
} | ||
|
||
MoveTracker() = default; // should only be used via inheritance | ||
|
||
public: | ||
virtual ~MoveTracker() = default; | ||
|
||
/** | ||
* @brief Move constructor sets the moved-from state on `other` and resets the state on `this` | ||
* @param other The moved-from object | ||
*/ | ||
MoveTracker(MoveTracker&& other) | ||
{ | ||
*this = std::move(other); | ||
} | ||
|
||
/** | ||
* @brief Move operator sets the moved-from state on `other` and resets the state on `this` | ||
* @param other The moved-from object | ||
* @return Reference to self | ||
*/ | ||
MoveTracker& | ||
operator=(MoveTracker&& other) | ||
{ | ||
if (this != &other) { | ||
other.wasMoved_ = true; | ||
wasMoved_ = false; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
MoveTracker(MoveTracker const&) = default; | ||
MoveTracker& | ||
operator=(MoveTracker const&) = default; | ||
}; | ||
|
||
} // namespace util |
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,68 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of clio: https://github.com/XRPLF/clio | ||
Copyright (c) 2025, the clio developers. | ||
Permission to use, copy, modify, and distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#include "util/MoveTracker.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <utility> | ||
|
||
namespace { | ||
struct MoveMe : util::MoveTracker { | ||
using MoveTracker::wasMoved; // expose as public for tests | ||
}; | ||
} // namespace | ||
|
||
TEST(MoveTrackerTests, SimpleChecks) | ||
{ | ||
auto moveMe = MoveMe(); | ||
EXPECT_FALSE(moveMe.wasMoved()); | ||
|
||
auto other = std::move(moveMe); | ||
EXPECT_TRUE(moveMe.wasMoved()); | ||
EXPECT_FALSE(other.wasMoved()); | ||
} | ||
|
||
TEST(MoveTrackerTests, SupportReuse) | ||
{ | ||
auto original = MoveMe(); | ||
auto other = std::move(original); | ||
|
||
original = std::move(other); | ||
EXPECT_FALSE(original.wasMoved()); | ||
EXPECT_TRUE(other.wasMoved()); | ||
} | ||
|
||
TEST(MoveTrackerTests, SelfMove) | ||
{ | ||
auto original = MoveMe(); | ||
[&](MoveMe& from) { original = std::move(from); }(original); // avoids the compiler catching self-move | ||
|
||
EXPECT_FALSE(original.wasMoved()); | ||
} | ||
|
||
TEST(MoveTrackerTests, SelfMoveAfterWasMoved) | ||
{ | ||
auto original = MoveMe(); | ||
[[maybe_unused]] auto fake = std::move(original); | ||
|
||
[&](MoveMe& from) { original = std::move(from); }(original); // avoids the compiler catching self-move | ||
|
||
EXPECT_TRUE(original.wasMoved()); | ||
} |
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