Lodestar
An integrated real-time control package in C++
ProtoMessageBuilder.hpp
1 //
2 // Created by Hamza El-Kebir on 2/10/22.
3 //
4 
5 #ifndef LODESTAR_PROTOMESSAGEBUILDER_HPP
6 #define LODESTAR_PROTOMESSAGEBUILDER_HPP
7 
8 #include <Lodestar/blocks/Signal.hpp>
9 #include <Lodestar/aux/AlwaysFalse.hpp>
10 #include <Lodestar/io/proto/ProtoMessageType.hpp>
11 
12 #include <sstream>
13 #include <ersatz/Codegen.hpp>
14 #include <Eigen/Dense>
15 #include <cstdint>
16 
17 namespace ls {
18  namespace io {
20  ProtoMessageType type;
21  };
22 
23  template<typename TType>
25  static_assert(always_false<TType>::value, "Not proto message builder defined for this type.");
26  };
27 
28  template<typename TType>
30  };
31 
32  template<>
33  class ProtoMessageBuilder<bool> {
34  public:
35  static ProtoMessageHeraldInfo makeHeraldInfo()
36  {
37  return {ProtoMessageType::bool_t};
38  }
39 
40  static void defineMessage(::std::stringstream &ss)
41  {
42  ss << fmt::format("message {} {{\n", "bool_m");
43  ss << " required bool value = 1;\n";
44  ss << "}\n";
45  }
46 
47  static void defineOptions(::std::stringstream &ss)
48  {}
49 
50  static void defineHeader(::std::stringstream &ss)
51  {
52  ss << "syntax = \"proto2\";\n\n";
53  ss << "package ls.proto;\n\n";
54  }
55  };
56 
57  template<typename TScalar, int NRows, int NCols>
58  class ProtoMessageBuilder<Eigen::Matrix<TScalar, NRows, NCols>> {
59  public:
60  static_assert((NRows >= 0) && (NCols >= 0), "Matrix may not be dynamically sized.");
61 
62  static ProtoMessageHeraldInfo makeHeraldInfo()
63  {
64  return {ProtoMessageType::bool_t};
65  }
66 
67  inline static ::std::string getFileName()
68  {
69  return fmt::format("ls.proto.{}", getName());
70  }
71 
72  inline static ::std::string getProtoFileName()
73  {
74  return fmt::format("{}.proto", getFileName());
75  }
76 
77  inline static ::std::string getOptionsFileName()
78  {
79  return fmt::format("{}.options", getFileName());
80  }
81 
82  static ::std::string getScalarName()
83  {
84  if (::std::is_same<TScalar, double>::value)
85  return "double";
86  if (::std::is_same<TScalar, float>::value)
87  return "float";
88  if (::std::is_same<TScalar, int32_t>::value)
89  return "int32";
90  if (::std::is_same<TScalar, uint32_t>::value)
91  return "uint32";
92  }
93 
94  static ::std::string getShortScalarName()
95  {
96  if (::std::is_same<TScalar, double>::value)
97  return "d";
98  if (::std::is_same<TScalar, float>::value)
99  return "f";
100  if (::std::is_same<TScalar, int32_t>::value)
101  return "i";
102  if (::std::is_same<TScalar, uint32_t>::value)
103  return "u";
104  }
105 
106  inline static ::std::string getName()
107  {
108  return fmt::format("Matrix{}{}x{}", getShortScalarName(), NRows, NCols);
109  }
110 
111  inline static ::std::string getMessageName()
112  {
113  return fmt::format("{}_m", getName());
114  }
115 
116  inline static ::std::string getMessageTypeName()
117  {
118  return fmt::format("{}_t", getName());
119  }
120 
121  static void defineMessage(::std::stringstream &ss)
122  {
123  ss << fmt::format("message {} {{\n", getMessageName());
124  ss << fmt::format(" repeated {} coeff = 1;\n", getScalarName());
125  ss << "}\n";
126  }
127 
128  static void defineOptions(::std::stringstream &ss)
129  {
130  ss << fmt::format("{}.coeff max_size:{}", getMessageName(), NRows*NCols);
131  }
132 
133  static void defineHeader(::std::stringstream &ss)
134  {
135  ss << "syntax = \"proto2\";\n\n";
136  ss << "package ls.proto;\n\n";
137  }
138  };
139 
140  }
141 }
142 
143 
144 #endif //LODESTAR_PROTOMESSAGEBUILDER_HPP
always_false
Definition: AlwaysFalse.hpp:11
ls::io::ProtoMessageBuilder
Definition: ProtoMessageBuilder.hpp:24
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::io::ProtoMessageHeraldInfo
Definition: ProtoMessageBuilder.hpp:19
ls::blocks::Signal
Definition: Signal.hpp:22