Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 16 additions & 9 deletions score/launch_manager/src/daemon/src/common/identifier_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,33 @@ std::size_t Fnv1aHash(std::string_view data) noexcept

} // namespace

IdentifierHash::IdentifierHash(const std::string& id)
IdentifierHash::IdentifierHash(std::string_view id)
{
hash_id_ = Fnv1aHash(id);
const std::lock_guard<std::mutex> lock(get_registry_mutex());
get_registry()[hash_id_] = id;
}

IdentifierHash::IdentifierHash(std::string_view id)
IdentifierHash::IdentifierHash(std::size_t hash_id)
{
hash_id_ = Fnv1aHash(id);
const std::lock_guard<std::mutex> lock(get_registry_mutex());
get_registry()[hash_id_] = id;
hash_id_ = hash_id;
}

IdentifierHash::IdentifierHash(const char* id)
std::optional<IdentifierHash> IdentifierHash::if_exists(std::string_view id)
Comment thread
danth marked this conversation as resolved.
{
const std::string_view sv = (id != nullptr) ? std::string_view(id) : std::string_view("");
hash_id_ = Fnv1aHash(sv);
const std::size_t hash_id = Fnv1aHash(id);

const std::lock_guard<std::mutex> lock(get_registry_mutex());
get_registry()[hash_id_] = sv;
const std::unordered_map<std::size_t, std::string>& registry = get_registry();

if (registry.find(hash_id) == registry.end())
{
return std::nullopt;
}
else
{
return IdentifierHash(hash_id);
}
}

bool IdentifierHash::operator==(const IdentifierHash& other) const
Expand Down
18 changes: 10 additions & 8 deletions score/launch_manager/src/daemon/src/common/identifier_hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstddef>
#include <mutex>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
Expand All @@ -41,16 +42,13 @@ class IdentifierHash final
{
public:
/// @brief Constructs an IdentifierHash object from the given ID.
/// @param id A const reference to std::string representing an ID.
explicit IdentifierHash(const std::string& id);

/// @brief Constructs an IdentifierHash object from the given ID.
/// @param id A std::string_view representing an ID.
/// @param id A string representing an ID.
explicit IdentifierHash(std::string_view id);

/// @brief Constructs an IdentifierHash object with the given ID.
/// @param A C-string representing an ID.
explicit IdentifierHash(const char* id);
/// @brief Constructs an IdentifierHash object from the given ID,
/// if that ID is already in the registry.
/// @param id A string representing an ID.
static std::optional<IdentifierHash> if_exists(std::string_view id);

// This class is trivially copyable / movable
// For this reason we are applying the rule of zero
Expand Down Expand Up @@ -135,6 +133,10 @@ class IdentifierHash final
static std::mutex& get_registry_mutex();

private:
/// @brief Constructs an IdentifierHash object with the given ID.
/// @param A raw ID.
explicit IdentifierHash(std::size_t hash_id);

/// internal representation of the ID, that was passed in constructor
std::size_t hash_id_ = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,9 @@ TEST_F(IdentifierHashTest, IdentifierHash_HashValueIsStableAcrossCompilersAndPro
TEST_F(IdentifierHashTest, IdentifierHash_ConstructorOverloadsAgreeOnTheSameContent)
{
RecordProperty("Description", "Verify all constructors of IdentifierHash have the same hash.");
const std::string as_string = "ProcessGroup1/Startup";
const std::string_view as_string_view = "ProcessGroup1/Startup";
const char* as_c_string = "ProcessGroup1/Startup";

ASSERT_EQ(IdentifierHash(as_string).data(), IdentifierHash(as_string_view).data());
ASSERT_EQ(IdentifierHash(as_string).data(), IdentifierHash(as_c_string).data());
ASSERT_EQ(IdentifierHash(as_string_view).data(), IdentifierHash(as_c_string).data());
ASSERT_EQ(IdentifierHash().data(), IdentifierHash("").data());
ASSERT_EQ(IdentifierHash().data(), IdentifierHash::if_exists("").value().data());
}

TEST_F(IdentifierHashTest, IdentifierHash_LessThanOperator)
Expand Down Expand Up @@ -183,3 +179,19 @@ TEST_F(IdentifierHashTest, IdentifierHash_LessThanOperator)
ASSERT_FALSE(hash2 < hash1);
}
}

TEST_F(IdentifierHashTest, IdentifierHash_IfExists_Existing_CString)
{
RecordProperty("Description", "Verify that IdentifierHash::if_exists returns the hash when it exists.");

IdentifierHash("Hello");
EXPECT_TRUE(IdentifierHash::if_exists("Hello").has_value());
}

TEST_F(IdentifierHashTest, IdentifierHash_IfExists_NotExisting_CString)
{
RecordProperty(
"Description", "Verify that IdentifierHash::if_exists returns std::nullopt when the hash does not exist.");

EXPECT_FALSE(IdentifierHash::if_exists("Hello C-string").has_value());
}
Loading