From c071c43c5c050813bf964ca6465cb94bd2bd697b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 4 Feb 2017 16:16:48 -0500 Subject: [PATCH] add CommandMap class --- src/core/CommandMap.cc | 65 ++++++++++++++++++++++++++++++++++++++++++ src/core/CommandMap.h | 40 ++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/core/CommandMap.cc create mode 100644 src/core/CommandMap.h diff --git a/src/core/CommandMap.cc b/src/core/CommandMap.cc new file mode 100644 index 0000000..706a382 --- /dev/null +++ b/src/core/CommandMap.cc @@ -0,0 +1,65 @@ +#include "CommandMap.h" +#include "Command.h" + +CommandMap::CommandMap() +{ + m_commands[0] = m_commands[1] = Command::NOP; +} + +std::shared_ptr CommandMap::access(uint32_t entry, bool insert) +{ + auto it = m_children.find(entry); + if (it == m_children.end()) + { + if (insert) + { + std::shared_ptr new_cm = std::make_shared(); + m_children[entry] = new_cm; + return new_cm; + } + else + { + return nullptr; + } + } + return it->second; +} + +std::shared_ptr CommandMap::access(const std::vector entries, bool insert) +{ + if (entries.size() == 0u) + { + return nullptr; + } + std::shared_ptr cm = access(entries[0], insert); + for (size_t i = 1u; i < entries.size(); i++) + { + if (cm == nullptr) + { + break; + } + cm = cm->access(entries[i], insert); + } + return cm; +} + +uint32_t CommandMap::command() const +{ + if (m_commands[1] != Command::NOP) + { + return m_commands[1]; + } + return m_commands[0]; +} + +void CommandMap::set_command(uint32_t command, bool user_mapping) +{ + m_commands[user_mapping ? 1 : 0] = command; +} + +bool CommandMap::empty() const +{ + return (m_commands[0] == Command::NOP) && + (m_commands[1] == Command::NOP) && + (m_children.size() == 0u); +} diff --git a/src/core/CommandMap.h b/src/core/CommandMap.h new file mode 100644 index 0000000..9f3900d --- /dev/null +++ b/src/core/CommandMap.h @@ -0,0 +1,40 @@ +#ifndef COMMANDMAP_H +#define COMMANDMAP_H + +#include +#include +#include + +class CommandMap +{ +public: + CommandMap(); + std::shared_ptr get(uint32_t entry) + { + return access(entry, false); + } + std::shared_ptr get(const std::vector entries) + { + return access(entries, false); + } + std::shared_ptr put(uint32_t entry) + { + return access(entry, true); + } + std::shared_ptr put(const std::vector entries) + { + return access(entries, true); + } + uint32_t command() const; + void set_command(uint32_t command, bool user_mapping); + bool empty() const; + +protected: + uint32_t m_commands[2]; + std::unordered_map> m_children; + + std::shared_ptr access(uint32_t entry, bool insert); + std::shared_ptr access(const std::vector entries, bool insert); +}; + +#endif