Table.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
8 #include <mc_rtc/gui/elements.h>
9 #include <mc_rtc/gui/types.h>
10 
11 namespace mc_rtc::gui
12 {
13 
14 namespace details
15 {
16 
26 template<typename GetHeader, typename GetData>
27 struct TableImpl : public Element
28 {
29  static constexpr auto type = Elements::Table;
30 
31  TableImpl(const std::string & name, GetHeader get_header_fn, GetData get_data_fn)
32  : Element(name), get_header_fn_(get_header_fn), get_data_fn_(get_data_fn)
33  {
34  }
35 
36  static constexpr size_t write_size() { return Element::write_size() + 2; }
37 
39  {
40  Element::write(builder);
41  builder.write(get_header_fn_());
42  builder.write(get_data_fn_());
43  }
44 
45 private:
46  GetHeader get_header_fn_;
47  GetData get_data_fn_;
48 };
49 
61 template<typename GetHeader, typename GetFormat, typename GetData>
62 struct FormattedTableImpl : public TableImpl<GetHeader, GetData>
63 {
64  FormattedTableImpl(const std::string & name, GetHeader get_header_fn, GetFormat get_format_fn, GetData get_data_fn)
65  : TableImpl<GetHeader, GetData>(name, get_header_fn, get_data_fn), get_format_fn_(get_format_fn)
66  {
67  }
68 
69  static constexpr size_t write_size() { return TableImpl<GetHeader, GetData>::write_size() + 1; }
70 
72  {
74  builder.write(get_format_fn_());
75  }
76 
77 private:
78  GetFormat get_format_fn_;
79 };
80 
90 template<typename GetData>
91 struct StaticTableImpl : public Element
92 {
93  static constexpr auto type = Elements::Table;
94 
95  StaticTableImpl(const std::string & name,
96  std::vector<std::string> header,
97  std::vector<std::string> format,
98  GetData get_data_fn)
99  : Element(name), header_(std::move(header)), format_(std::move(format)), get_data_fn_(get_data_fn)
100  {
101  }
102 
103  static constexpr size_t write_size() { return Element::write_size() + 3; }
104 
106  {
107  Element::write(builder);
108  builder.write(header_);
109  builder.write(get_data_fn_());
110  builder.write(format_);
111  }
112 
113 private:
114  std::vector<std::string> header_;
115  std::vector<std::string> format_;
116  GetData get_data_fn_;
117 };
118 
119 } // namespace details
120 
122 template<typename GetData>
123 auto Table(const std::string & name, std::vector<std::string> header, GetData get_data_fn)
124 {
125  return details::StaticTableImpl(name, std::move(header), std::vector<std::string>(header.size(), "{}"), get_data_fn);
126 }
127 
129 template<typename GetData>
130 auto Table(const std::string & name,
131  std::vector<std::string> header,
132  std::vector<std::string> format,
133  GetData get_data_fn)
134 {
135  while(format.size() < header.size()) { format.push_back("{}"); }
136  return details::StaticTableImpl(name, std::move(header), std::move(format), get_data_fn);
137 }
138 
140 template<typename GetHeader, typename GetData>
141 auto Table(const std::string & name, GetHeader get_header_fn, GetData get_data_fn)
142 {
143  return details::TableImpl(name, get_header_fn, get_data_fn);
144 }
145 
147 template<typename GetHeader, typename GetFormat, typename GetData>
148 auto Table(const std::string & name, GetHeader get_header_fn, GetFormat get_format_fn, GetData get_data_fn)
149 {
150  return details::FormattedTableImpl(name, get_header_fn, get_format_fn, get_data_fn);
151 }
152 
153 } // namespace mc_rtc::gui
mc_rtc::gui::Element
Definition: elements.h:58
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::gui::details::StaticTableImpl::StaticTableImpl
StaticTableImpl(const std::string &name, std::vector< std::string > header, std::vector< std::string > format, GetData get_data_fn)
Definition: Table.h:95
mc_rtc::gui::details::TableImpl::type
static constexpr auto type
Definition: Table.h:29
types.h
mc_rtc::gui::Table
auto Table(const std::string &name, std::vector< std::string > header, GetData get_data_fn)
Definition: Table.h:123
mc_rtc::gui::Element::write
void write(mc_rtc::MessagePackBuilder &)
Definition: elements.h:83
mc_rtc::gui::details::StaticTableImpl
Definition: Table.h:91
mc_rtc::gui::details::FormattedTableImpl::FormattedTableImpl
FormattedTableImpl(const std::string &name, GetHeader get_header_fn, GetFormat get_format_fn, GetData get_data_fn)
Definition: Table.h:64
mc_rtc::gui::details::StaticTableImpl::type
static constexpr auto type
Definition: Table.h:93
elements.h
mc_rtc::gui::details::FormattedTableImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Table.h:71
mc_rtc::gui::details::FormattedTableImpl
Definition: Table.h:62
mc_rtc::MessagePackBuilder::write
void write()
mc_rtc::gui::Element::write_size
static constexpr size_t write_size()
Definition: elements.h:76
mc_rtc::gui::Elements::Table
@ Table
mc_rtc::gui::details::TableImpl::write_size
static constexpr size_t write_size()
Definition: Table.h:36
std
Definition: Contact.h:66
mc_rtc::gui::details::FormattedTableImpl::write_size
static constexpr size_t write_size()
Definition: Table.h:69
mc_rtc::gui::details::TableImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Table.h:38
mc_rtc::gui::details::StaticTableImpl::write_size
static constexpr size_t write_size()
Definition: Table.h:103
mc_rtc::gui::details::StaticTableImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Table.h:105
traits.h
mc_rtc::gui::details::TableImpl::TableImpl
TableImpl(const std::string &name, GetHeader get_header_fn, GetData get_data_fn)
Definition: Table.h:31
mc_rtc::gui::details::TableImpl
Definition: Table.h:27
mc_rtc::gui
Definition: Observer.h:15
mc_rtc::gui::Element::name
const std::string & name() const
Definition: elements.h:61