-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor hooks to use a dedicated handler in client
This will also enable calling a hook without available configuration, closing #39
- Loading branch information
1 parent
0817a3c
commit e67b207
Showing
9 changed files
with
100 additions
and
97 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
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,41 @@ | ||
#include "HookHandler.hpp" | ||
|
||
namespace Litr { | ||
|
||
HookHandler::HookHandler(const Ref<CLI::Instruction>& instruction) : m_Instruction(instruction) {} | ||
|
||
void HookHandler::Add(Code code, const std::vector<Value>& values, | ||
const HookHandler::HookCallback& callback) { | ||
LITR_PROFILE_FUNCTION(); | ||
|
||
for (auto&& value : values) { | ||
m_Hooks.emplace_back(code, value, callback); | ||
} | ||
} | ||
|
||
bool HookHandler::Execute() const { | ||
LITR_PROFILE_FUNCTION(); | ||
|
||
size_t offset{0}; | ||
|
||
while (offset < m_Instruction->Count()) { | ||
const auto code{static_cast<Code>(m_Instruction->Read(offset++))}; | ||
|
||
for (auto&& hook : m_Hooks) { | ||
if (code != hook.Code) continue; | ||
auto value{m_Instruction->ReadConstant(m_Instruction->Read(offset))}; | ||
if (value == hook.Value) { | ||
hook.Callback(m_Instruction); | ||
return true; | ||
} | ||
} | ||
|
||
if (code != Code::CLEAR) { | ||
offset++; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} // namespace Litr |
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,31 @@ | ||
#pragma once | ||
|
||
#include "Core.hpp" | ||
|
||
namespace Litr { | ||
|
||
class HookHandler { | ||
using HookCallback = std::function<void(const Ref<CLI::Instruction>& instruction)>; | ||
using Code = CLI::Instruction::Code; | ||
using Value = CLI::Instruction::Value; | ||
|
||
struct Hook { | ||
CLI::Instruction::Code Code; | ||
CLI::Instruction::Value Value; | ||
HookCallback Callback; | ||
|
||
Hook(CLI::Instruction::Code code, CLI::Instruction::Value value, HookCallback callback) | ||
: Code(code), Value(std::move(value)), Callback(std::move(callback)) {} | ||
}; | ||
|
||
public: | ||
explicit HookHandler(const Ref<CLI::Instruction>& instruction); | ||
void Add(Code code, const std::vector<Value>& values, const HookCallback& callback); | ||
[[nodiscard]] bool Execute() const; | ||
|
||
private: | ||
const Ref<CLI::Instruction>& m_Instruction; | ||
std::vector<Hook> m_Hooks{}; | ||
}; | ||
|
||
} // namespace Litr |
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
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