From 98fcba8ef3a8657dfa9019543ccdf6f543c169da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 26 Feb 2020 16:20:44 +0100 Subject: [PATCH] [yul-phaser] Phaser: Reimplement << and >> operators using serializeChoice() and deserializeChoice() --- tools/yulPhaser/Phaser.cpp | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/tools/yulPhaser/Phaser.cpp b/tools/yulPhaser/Phaser.cpp index 45d9672d1..44fb9d130 100644 --- a/tools/yulPhaser/Phaser.cpp +++ b/tools/yulPhaser/Phaser.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -27,6 +28,7 @@ #include #include +#include #include #include @@ -41,32 +43,20 @@ using namespace solidity::phaser; namespace po = boost::program_options; -istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) +namespace { - string value; - _inputStream >> value; - if (value == "random") - _algorithm = Algorithm::Random; - else if (value == "GEWEP") - _algorithm = Algorithm::GEWEP; - else - _inputStream.setstate(ios_base::failbit); +map const AlgorithmToStringMap = +{ + {Algorithm::Random, "random"}, + {Algorithm::GEWEP, "GEWEP"}, +}; +map const StringToAlgorithmMap = invertMap(AlgorithmToStringMap); - return _inputStream; } -ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) -{ - if (_algorithm == Algorithm::Random) - _outputStream << "random"; - else if (_algorithm == Algorithm::GEWEP) - _outputStream << "GEWEP"; - else - _outputStream.setstate(ios_base::failbit); - - return _outputStream; -} +istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) { return deserializeChoice(_inputStream, _algorithm, StringToAlgorithmMap); } +ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) { return serializeChoice(_outputStream, _algorithm, AlgorithmToStringMap); } GeneticAlgorithmFactory::Options GeneticAlgorithmFactory::Options::fromCommandLine(po::variables_map const& _arguments) {