#pragma once #include #include #include #include #include namespace bts::core { class ProtoReader { public: template static bool read_proto(const std::string &path, T &result); template static bool read_proto( const std::string &path, T &result, std::ostream &err); template static std::optional read_proto(const std::string &path); }; ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// template bool ProtoReader::read_proto( const std::string &path, T &result, std::ostream &err) { std::ifstream f(path); if (f.good()) { std::string str( (std::istreambuf_iterator(f)), (std::istreambuf_iterator())); if (google::protobuf::TextFormat::ParseFromString(str, &result)) { return true; } err << "Invalid config in " << path << "\n"; } else { err << "File not found in " << path << "\n"; } return false; } template bool ProtoReader::read_proto(const std::string &path, T &result) { struct NullBuffer : std::streambuf { int overflow(int c) override { return c; } }; NullBuffer null_buff; std::ostream no_op(&null_buff); return read_proto(path, result, no_op); } template std::optional ProtoReader::read_proto(const std::string &path) { T result; if (read_proto(path, result)) { return result; } return std::nullopt; } } // namespace spartan::core