[yul-phaser] Add serializeChoice() and deserializeChoice()

This commit is contained in:
Kamil Śliwak
2020-03-13 11:21:23 +01:00
parent 0c3de9ef99
commit deaf1d0c6f
2 changed files with 141 additions and 0 deletions
+42
View File
@@ -20,7 +20,49 @@
#pragma once
#include <iostream>
#include <map>
namespace solidity::phaser
{
/// Reads a token from the input stream and translates it to a string using a map.
/// Sets the failbit in the stream if there's no matching value in the map.
template <typename C>
std::istream& deserializeChoice(
std::istream& _inputStream,
C& _choice,
std::map<std::string, C> const& _stringToValueMap
)
{
std::string deserializedValue;
_inputStream >> deserializedValue;
auto const& pair = _stringToValueMap.find(deserializedValue);
if (pair != _stringToValueMap.end())
_choice = pair->second;
else
_inputStream.setstate(std::ios_base::failbit);
return _inputStream;
}
/// Translates a value to a string using a map and prints it to the output stream.
/// Sets the failbit if the value is not in the map.
template <typename C>
std::ostream& serializeChoice(
std::ostream& _outputStream,
C const& _choice,
std::map<C, std::string> const& _valueToStringMap
)
{
auto const& pair = _valueToStringMap.find(_choice);
if (pair != _valueToStringMap.end())
_outputStream << pair->second;
else
_outputStream.setstate(std::ios_base::failbit);
return _outputStream;
}
}