Extract parse_enum<T> for parsing protobuf enum to optional<T>

This commit is contained in:
Eric Harding committed 2019-08-21 18:40:26 -04:00
1 parent 8e3a83d314
commit 57bf60e6df
2 files changed
+35 -8

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include <string>
#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 <class T, class T2>
std::optional<T> 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;
}
}
}
+8 -8
View File
@@ -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 <boost/filesystem.hpp>
@@ -169,16 +170,14 @@ string& uppercase(string& s) {
return s;
}
template <class T, class T2>
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<T>(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<string>& 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)) ||