StateBuilder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2021 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <mc_rtc/Configuration.h>
8 #include <mc_rtc/gui/elements.h>
9 #include <mc_rtc/gui/plot.h>
10 
11 #include <unordered_map>
12 
13 namespace mc_rtc
14 {
15 
16 namespace gui
17 {
18 
20 enum class ElementsStacking
21 {
22  Vertical = 0,
24 };
25 
28 {
39  static constexpr int8_t PROTOCOL_VERSION = 4;
40 
42  StateBuilder();
43 
52  template<typename T>
53  void addElement(const std::vector<std::string> & category, T element);
54 
67  template<typename SourceT, typename T>
68  void addElement(SourceT * source, const std::vector<std::string> & category, T element);
69 
78  template<typename T, typename... Args>
79  void addElement(const std::vector<std::string> & category, T element, Args... args);
80 
91  template<typename SourceT, typename T, typename... Args>
92  void addElement(SourceT * source, const std::vector<std::string> & category, T element, Args... args);
93 
104  template<typename T>
105  void addElement(const std::vector<std::string> & category, ElementsStacking stacking, T element);
106 
119  template<typename SourceT, typename T>
120  void addElement(SourceT * source, const std::vector<std::string> & category, ElementsStacking stacking, T element);
121 
132  template<typename T, typename... Args>
133  void addElement(const std::vector<std::string> & category, ElementsStacking stacking, T element, Args... args);
134 
147  template<typename SourceT, typename T, typename... Args>
148  void addElement(SourceT * source,
149  const std::vector<std::string> & category,
150  ElementsStacking stacking,
151  T element,
152  Args... args);
153 
161  bool hasElement(const std::vector<std::string> & category, const std::string & name);
162 
164  void reset();
165 
167  void removeCategory(const std::vector<std::string> & category);
168 
170  void removeElement(const std::vector<std::string> & category, const std::string & name);
171 
183  void removeElements(void * source);
184 
193  void removeElements(const std::vector<std::string> & category, void * source, bool recurse = false);
194 
208  template<typename... Args>
209  void addXYPlot(const std::string & name,
210  plot::AxisConfiguration xConfig,
211  plot::AxisConfiguration yLeftConfig,
212  plot::AxisConfiguration yRightConfig,
213  Args... args);
214 
226  template<typename... Args>
227  void addXYPlot(const std::string & name,
228  plot::AxisConfiguration xConfig,
229  plot::AxisConfiguration yLeftConfig,
230  Args... args);
231 
243  template<typename... Args>
244  void addXYPlot(const std::string & name, plot::AxisConfiguration xConfig, Args... args);
245 
257  template<typename... Args>
258  void addXYPlot(const std::string & name, Args... args);
259 
273  template<typename T, typename... Args>
274  void addPlot(const std::string & name,
275  T abscissa,
276  plot::AxisConfiguration yLeftConfig,
277  plot::AxisConfiguration yRightConfig,
278  Args... args);
279 
291  template<typename T, typename... Args>
292  void addPlot(const std::string & name, T abscissa, plot::AxisConfiguration yLeftConfig, Args... args);
293 
303  template<typename T, typename... Args>
304  void addPlot(const std::string & name, T abscissa, Args... args);
305 
321  template<typename T>
322  bool addPlotData(const std::string & name, T data);
323 
325  void removePlot(const std::string & name);
326 
334  size_t update(std::vector<char> & data);
335 
337  void update();
338 
340  bool handleRequest(const std::vector<std::string> & category,
341  const std::string & name,
342  const mc_rtc::Configuration & data);
343 
350  mc_rtc::Configuration data();
351 
353  inline size_t size() const { return elements_.size(); }
354 
355 private:
356  template<typename T>
357  void addElementImpl(void * source,
358  const std::vector<std::string> & category,
359  ElementsStacking stacking,
360  T element,
361  size_t rem = 0);
362 
364  mc_rtc::Configuration data_;
366  using plot_callback_function_t = std::function<void(mc_rtc::MessagePackBuilder &, const std::string &, bool)>;
367  struct PlotCallback
368  {
369  plot::Plot type;
370  size_t msg_size;
371  plot_callback_function_t callback;
372  };
377  uint64_t plot_id_ = 0;
379  std::unordered_map<std::string, PlotCallback> plots_;
381  bool update_data_ = true;
383  std::vector<char> data_buffer_;
385  size_t data_buffer_size_ = 0;
386  struct Category;
387  struct MC_RTC_GUI_DLLAPI ElementStore
388  {
389  const Element & operator()() const;
390  Element & operator()();
391  std::function<Element &()> element;
392  void (*write)(Element &, mc_rtc::MessagePackBuilder &);
393  bool (*handleRequest)(Element &, const mc_rtc::Configuration &);
394  void * source;
395 
396  template<typename T>
397  ElementStore(T self, const Category & category, ElementsStacking stacking, void * source);
398  };
399  struct Category
400  {
401  std::string name;
402  std::vector<ElementStore> elements;
403  std::vector<Category> sub;
406  std::vector<Category>::iterator find(const std::string & name);
408  int id = 0;
410  inline size_t size() const
411  {
412  size_t s = 0;
413  for(const auto & c : sub) { s += c.size(); }
414  return s + elements.size();
415  }
416  };
417  Category elements_;
418 
427  Category * getCategory(const std::vector<std::string> & category, size_t depth = std::numeric_limits<size_t>::max());
428 
430  Category & getOrCreateCategory(const std::vector<std::string> & category);
431 
433  void update(mc_rtc::MessagePackBuilder & builder, Category & category);
434 
436  void removeElements(Category & category, void * source);
437 
438  std::string cat2str(const std::vector<std::string> & category);
439 
440  void addPlotData(PlotCallback &) {}
441 
442  template<typename T, typename... Args>
443  void addPlotData(PlotCallback & callback, T plot, Args... args);
444 };
445 
446 } // namespace gui
447 
448 } // namespace mc_rtc
449 
450 #include "StateBuilder.hpp"
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::gui::plot::Plot
Plot
Definition: types.h:21
mc_rtc::gui::ElementsStacking::Vertical
@ Vertical
mc_rtc::gui::ElementsStacking::Horizontal
@ Horizontal
mc_rtc::gui::StateBuilder::size
size_t size() const
Definition: StateBuilder.h:353
mc_rtc::gui::ElementsStacking
ElementsStacking
Definition: StateBuilder.h:20
mc_rtc::gui::plot::AxisConfiguration
Definition: types.h:105
elements.h
mc_rtc::gui::details::write
auto write(T &value)
Definition: traits.h:224
mc_rtc::gui::StateBuilder
Definition: StateBuilder.h:27
Configuration.h
plot.h
MC_RTC_GUI_DLLAPI
#define MC_RTC_GUI_DLLAPI
Definition: api.h:50
mc_rtc
Definition: Contact.h:87