From 57bf60e6dfbaa8722b323ffeb4154c92bb619ac6 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 21 Aug 2019 17:49:40 -0400 Subject: [PATCH] Extract parse_enum for parsing protobuf enum to optional --- src/lib/utils/parse_enum.h | 27 +++++++++++++++++++++++++++ src/server/sparkd/md/run_md.cpp | 16 ++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/lib/utils/parse_enum.h diff --git a/src/lib/utils/parse_enum.h b/src/lib/utils/parse_enum.h new file mode 100644 index 000000000..3b8447bae --- /dev/null +++ b/src/lib/utils/parse_enum.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include "lib/utils/optional.h" +namespace bts { + + inline std::string uppercase_copy(const std::string& cs) { + std::string s = cs; + std::transform(s.begin(), s.end(), s.begin(), ::toupper); + return s; + } + + template + std::optional parse_enum(const std::string& value, const std::string& prefix, T2 parse) { + auto temp = value; + T result; + if (temp.find(prefix) == std::string::npos) { + temp = prefix + "_" + temp; + } + if(parse(temp, &result)) { + return {result}; + } + else { + return std::nullopt; + } + } +} \ No newline at end of file diff --git a/src/server/sparkd/md/run_md.cpp b/src/server/sparkd/md/run_md.cpp index 42f4da101..35ded097d 100644 --- a/src/server/sparkd/md/run_md.cpp +++ b/src/server/sparkd/md/run_md.cpp @@ -16,6 +16,7 @@ #include "lib/md/SecurityDB.h" #include "server/sparkd/exchange_sim/ExchangeSim.h" #include "lib/utils/thread_manager.h" +#include "lib/utils/parse_enum.h" #include @@ -169,16 +170,14 @@ string& uppercase(string& s) { return s; } template -bool parse_enum(const string& value, const string& prefix, T2 parse, T default_value, T* presult) { - string temp = value; - if (temp.find(prefix) == string::npos) { - temp = prefix + "_" + temp; - } - if(parse(temp, presult)) { +bool parse_enum(const string& value, const string& prefix, T2 parse, T default_value, T* result) { + auto opt = ::bts::parse_enum(value, prefix, parse); + if (opt) { + *result = opt.value(); return true; } else { - *presult = default_value; + *result = default_value; return false; } } @@ -208,13 +207,14 @@ struct MDArgs { MDArgs parse_md_args(const vector& args) { using namespace api; using namespace co2; - auto venue = VENUE_UNSET; // Default to glbx. If you change this change below (used) also + auto venue = VENUE_UNSET; auto platform = PLATFORM_UNSET; auto type = SECURITYTYPE_UNSET; bool hot = false; long number = 0; std::string symbol; for (auto str : args) { + // Iterate through each argument and see if it parses to one of these enums uppercase(str); bool used = (venue == VENUE_UNSET && parse_enum(str, "VENUE", Venue_Parse, venue, &venue)) ||