| #ifndef PRODUCTION_SUSHID_SAFEPOWER_AGENT_STATE_FIELDS_H_ |
| #define PRODUCTION_SUSHID_SAFEPOWER_AGENT_STATE_FIELDS_H_ |
| |
| #include <string> |
| #include <tuple> |
| |
| #include "safepower_agent.pb.h" |
| #include "absl/strings/string_view.h" |
| |
| namespace safepower_agent { |
| |
| template <typename Proto, typename FieldT> |
| class FieldDef { |
| public: |
| explicit constexpr FieldDef(FieldT (Proto::*get)() const, |
| bool (Proto::*has)() const, |
| void (Proto::*set)(FieldT)) |
| : get_(get), has_(has), set_(set) {} |
| |
| FieldT get_from(const Proto& proto) const { return (proto.*get_)(); } |
| bool is_in(const Proto& proto) const { return (proto.*has_)(); } |
| void set_in(Proto& proto, FieldT value) const { (proto.*set_)(value); } |
| |
| private: |
| FieldT (Proto::*get_)() const; |
| bool (Proto::*has_)() const; |
| void (Proto::*set_)(FieldT); |
| }; |
| |
| template <typename Proto, typename FieldT> |
| FieldDef(FieldT (Proto::*get)() const, bool (Proto::*has)() const, |
| void (Proto::*set)(FieldT)) -> FieldDef<Proto, FieldT>; |
| |
| template <typename Proto> |
| class FieldDef<Proto, std::string> { |
| public: |
| explicit constexpr FieldDef(const std::string& (Proto::*get)() const, |
| bool (Proto::*has)() const, |
| void (Proto::*set)(const std::string&)) |
| : get_(get), has_(has), set_(set) {} |
| |
| const std::string& get_from(const Proto& proto) const { |
| return (proto.*get_)(); |
| } |
| bool is_in(const Proto& proto) const { return (proto.*has_)(); } |
| void set_in(Proto& proto, absl::string_view value) const { |
| (proto.*set_)(value); |
| } |
| |
| private: |
| const std::string& (Proto::*get_)() const; |
| bool (Proto::*has_)() const; |
| void (Proto::*set_)(const std::string&); |
| }; |
| |
| template <typename Proto> |
| struct ProtoFields { |
| // static constexpr std::tuple kFields; is not defined here so we have a |
| // compiler error if we forget to define it for some type. |
| }; |
| |
| #define FIELD_DEF(Proto, field_name) \ |
| FieldDef(&Proto::field_name, &Proto::has_##field_name, \ |
| &Proto::set_##field_name) |
| |
| // LINT.IfChange(ComponentStateFields) |
| template <> |
| struct ProtoFields<safepower_agent_proto::DaemonState> { |
| static constexpr std::tuple kFields = { |
| FIELD_DEF(safepower_agent_proto::DaemonState, healthy), |
| FIELD_DEF(safepower_agent_proto::DaemonState, restart_counter), |
| }; |
| }; |
| |
| template <> |
| struct ProtoFields<safepower_agent_proto::BootState> { |
| static constexpr std::tuple kFields = { |
| FIELD_DEF(safepower_agent_proto::BootState, boot_counter), |
| FIELD_DEF(safepower_agent_proto::BootState, uptime_ms), |
| FIELD_DEF(safepower_agent_proto::BootState, state), |
| }; |
| }; |
| |
| template <> |
| struct ProtoFields<safepower_agent_proto::ConnectionState> { |
| static constexpr std::tuple kFields = { |
| FIELD_DEF(safepower_agent_proto::ConnectionState, reconnect_counter), |
| }; |
| }; |
| |
| template <> |
| struct ProtoFields<safepower_agent_proto::PowerState> { |
| static constexpr std::tuple kFields = { |
| FIELD_DEF(safepower_agent_proto::PowerState, state), |
| FIELD_DEF(safepower_agent_proto::PowerState, power_cycle_counter), |
| }; |
| }; |
| // LINT.ThenChange(//depot/proto/safepower_agent.proto:ComponentState) |
| #undef FIELD_DEF |
| |
| } // namespace safepower_agent |
| #endif // PRODUCTION_SUSHID_SAFEPOWER_AGENT_STATE_FIELDS_H_ |