mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add serializeChoice() and deserializeChoice()
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user