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
17 changes: 14 additions & 3 deletions src/spider/worker/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -106,11 +107,21 @@ auto FunctionManager::get_instance() -> FunctionManager& {
return instance;
}

auto FunctionManager::get(std::string_view name) const -> FunctionMap::const_iterator {
for (auto it = m_function_map.cbegin(); it != m_function_map.cend(); ++it) {
if (it->first == name) {
return it;
}
}
return m_function_map.cend();
}

auto FunctionManager::get_function(std::string const& name) const -> Function const* {
if (auto const func_iter = m_function_map.find(name); func_iter != m_function_map.end()) {
return &(func_iter->second);
auto const it = get(name);
if (it == m_function_map.cend()) {
return nullptr;
}
return nullptr;
return &it->second;
}
} // namespace spider::core

Expand Down
33 changes: 19 additions & 14 deletions src/spider/worker/FunctionManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>

#include <absl/container/flat_hash_map.h>
#include <boost/uuid/uuid.hpp>
#include <fmt/format.h>

Expand Down Expand Up @@ -46,7 +46,7 @@ using ResultBuffer = msgpack::sbuffer;

using Function = std::function<ResultBuffer(TaskContext& context, ArgsBuffer const&)>;

using FunctionMap = absl::flat_hash_map<std::string, Function>;
using FunctionMap = std::vector<std::pair<std::string, Function>>;

template <class T>
struct TemplateParameter;
Expand Down Expand Up @@ -362,31 +362,36 @@ class FunctionManager {

template <class F>
auto register_function(std::string const& name, F f) -> bool {
if (m_function_map.contains(name)) {
if (m_function_map.cend() != get(name)) {
return false;
}
return m_function_map
.emplace(
name,
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
m_function_map.emplace_back(
name,
std::bind(
&FunctionInvoker<F>::apply,
std::move(f),
std::placeholders::_1,
std::placeholders::_2
)
.second;
);
return true;
}

auto register_function_invoker(std::string const& name, Function f) -> bool {
return m_function_map.emplace(name, f).second;
if (m_function_map.cend() != get(name)) {
return false;
}
m_function_map.emplace_back(name, std::move(f));
return true;
}

[[nodiscard]] auto get_function(std::string const& name) const -> Function const*;

[[nodiscard]] auto get_function_map() const -> FunctionMap const& { return m_function_map; }

private:
[[nodiscard]] auto get(std::string_view name) const -> FunctionMap::const_iterator;

FunctionManager() = default;

~FunctionManager() = default;
Expand Down
15 changes: 14 additions & 1 deletion src/spider/worker/FunctionNameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <optional>
#include <string>
#include <utility>
#include <vector>

#include <boost/dll/alias.hpp>

Expand All @@ -11,9 +13,20 @@ auto FunctionNameManager::get_instance() -> FunctionNameManager& {
return instance;
}

auto FunctionNameManager::get(TaskFunctionPointer const ptr) const
-> FunctionNameMap::const_iterator {
for (auto it = m_name_map.cbegin(); it != m_name_map.cend(); ++it) {
if (it->first == ptr) {
return it;
}
}
return m_name_map.cend();
}

auto FunctionNameManager::get_function_name(TaskFunctionPointer const ptr) const
-> std::optional<std::string> {
if (auto const& it = m_name_map.find(ptr); it != m_name_map.end()) {
auto const it = get(ptr);
if (it != m_name_map.cend()) {
return it->second;
}
return std::nullopt;
Expand Down
16 changes: 11 additions & 5 deletions src/spider/worker/FunctionNameManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <optional>
#include <string>

#include <absl/container/flat_hash_map.h>
#include <utility>
#include <vector>

// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#define NAME_CONCAT_DIRECT(s1, s2) s1##s2
Expand All @@ -19,7 +19,7 @@
namespace spider::core {
using TaskFunctionPointer = void (*)();

using FunctionNameMap = absl::flat_hash_map<TaskFunctionPointer, std::string>;
using FunctionNameMap = std::vector<std::pair<TaskFunctionPointer, std::string>>;

class FunctionNameManager {
public:
Expand All @@ -36,8 +36,12 @@ class FunctionNameManager {
template <typename F>
auto register_function(std::string const& name, F function_pointer) -> bool {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return m_name_map.emplace(reinterpret_cast<TaskFunctionPointer>(function_pointer), name)
.second;
if (m_name_map.cend() != get(reinterpret_cast<TaskFunctionPointer>(function_pointer))) {
return false;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
m_name_map.emplace_back(reinterpret_cast<TaskFunctionPointer>(function_pointer), name);
return true;
}

[[nodiscard]] auto get_function_name(TaskFunctionPointer ptr) const
Expand All @@ -48,6 +52,8 @@ class FunctionNameManager {
}

private:
[[nodiscard]] auto get(TaskFunctionPointer) const -> FunctionNameMap::const_iterator;

FunctionNameManager() = default;

~FunctionNameManager() = default;
Expand Down