masters-thesis/op-finder/OperationStorage.cpp

65 lines
1.4 KiB
C++

//
// Created by erki on 28.02.21.
//
#include "OperationStorage.hpp"
#include <fstream>
#include <filesystem>
#include <llvm/Support/CommandLine.h>
#include <nlohmann/json.hpp>
void to_json(nlohmann::json& j, const OperationStorage::Log& l)
{
j = nlohmann::json{
{"operation", l.operation},
{"line", l.line},
{"operand_lhs", l.operand_lhs},
{"operand_rhs", l.operand_rhs},
{"operand_result", l.operand_result}
};
}
void from_json(const nlohmann::json& j, OperationStorage::Log& l)
{
j.at("operation").get_to(l.operation);
j.at("line").get_to(l.line);
j.at("operand_lhs").get_to(l.operand_lhs);
j.at("operand_rhs").get_to(l.operand_rhs);
j.at("operand_result").get_to(l.operand_result);
}
OperationStorage::OperationStorage(const std::string& output_filename)
: _output_filename(output_filename)
{ }
OperationStorage::~OperationStorage()
{
_dumpToFile();
}
void OperationStorage::pushOperation(const std::string& filename, const Log& op)
{
auto it = _operations.find(filename);
if (it == _operations.end())
it = _operations.insert({ filename, {} }).first;
it->second.push_back(op);
}
const std::unordered_map<std::string, std::vector<OperationStorage::Log>>& OperationStorage::getOperations() const
{
return _operations;
}
void OperationStorage::_dumpToFile()
{
nlohmann::json json = _operations;
std::ofstream file(_output_filename);
file << json;
}