add CommandMap class

This commit is contained in:
Josh Holtrop 2017-02-04 16:16:48 -05:00
parent ced2d2856b
commit c071c43c5c
2 changed files with 105 additions and 0 deletions

65
src/core/CommandMap.cc Normal file
View File

@ -0,0 +1,65 @@
#include "CommandMap.h"
#include "Command.h"
CommandMap::CommandMap()
{
m_commands[0] = m_commands[1] = Command::NOP;
}
std::shared_ptr<CommandMap> CommandMap::access(uint32_t entry, bool insert)
{
auto it = m_children.find(entry);
if (it == m_children.end())
{
if (insert)
{
std::shared_ptr<CommandMap> new_cm = std::make_shared<CommandMap>();
m_children[entry] = new_cm;
return new_cm;
}
else
{
return nullptr;
}
}
return it->second;
}
std::shared_ptr<CommandMap> CommandMap::access(const std::vector<uint32_t> entries, bool insert)
{
if (entries.size() == 0u)
{
return nullptr;
}
std::shared_ptr<CommandMap> 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);
}

40
src/core/CommandMap.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef COMMANDMAP_H
#define COMMANDMAP_H
#include <unordered_map>
#include <vector>
#include <memory>
class CommandMap
{
public:
CommandMap();
std::shared_ptr<CommandMap> get(uint32_t entry)
{
return access(entry, false);
}
std::shared_ptr<CommandMap> get(const std::vector<uint32_t> entries)
{
return access(entries, false);
}
std::shared_ptr<CommandMap> put(uint32_t entry)
{
return access(entry, true);
}
std::shared_ptr<CommandMap> put(const std::vector<uint32_t> 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<uint32_t, std::shared_ptr<CommandMap>> m_children;
std::shared_ptr<CommandMap> access(uint32_t entry, bool insert);
std::shared_ptr<CommandMap> access(const std::vector<uint32_t> entries, bool insert);
};
#endif