Force.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 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 
27 template<typename GetForce, typename GetSurface>
28 struct ForceROImpl : public Element
29 {
30  static constexpr auto type = Elements::Force;
31 
32  ForceROImpl(const std::string & name, const ForceConfig & config, GetForce get_force_fn, GetSurface get_surface_fn)
33  : Element(name), get_force_fn_(get_force_fn), get_surface_fn_(get_surface_fn), config_(config)
34  {
36  "Force element force callback must return an sva::ForceVecd");
38  "Force element surface callback must return an sva::PTransformd");
39  }
40 
41  static constexpr size_t write_size() { return Element::write_size() + 3 + ForceConfig::write_size(); }
42 
43  void write(mc_rtc::MessagePackBuilder & builder, bool ro = true)
44  {
45  Element::write(builder);
46  builder.write(get_force_fn_());
47  builder.write(get_surface_fn_());
48  builder.write(ro);
49  config_.write(builder);
50  }
51 
52 private:
53  GetForce get_force_fn_;
54  GetSurface get_surface_fn_;
55  ForceConfig config_;
56 };
57 
58 template<typename GetForce, typename GetSurface, typename SetForce>
59 struct ForceImpl : public ForceROImpl<GetForce, GetSurface>
60 {
61  static constexpr auto type = Elements::Force;
63 
64  ForceImpl(const std::string & name,
65  const ForceConfig & config,
66  GetForce get_force_fn,
67  SetForce set_force_fn,
68  GetSurface get_surface_fn)
69  : ForceRO(name, config, get_force_fn, get_surface_fn), set_force_fn_(set_force_fn)
70  {
71  }
72 
73  void write(mc_rtc::MessagePackBuilder & builder) { ForceRO::write(builder, false); }
74 
76  {
77  set_force_fn_(data);
78  return true;
79  }
80 
81 private:
82  SetForce set_force_fn_;
83 };
84 
85 } // namespace details
86  //
88 template<typename GetForce, typename GetSurface>
89 auto Force(const std::string & name, GetForce get_force_fn, GetSurface get_surface_fn)
90 {
91  return details::ForceROImpl(name, {}, get_force_fn, get_surface_fn);
92 }
93 
95 template<typename GetForce, typename GetSurface>
96 auto Force(const std::string & name, const ForceConfig & config, GetForce get_force_fn, GetSurface get_surface_fn)
97 {
98  return details::ForceROImpl(name, config, get_force_fn, get_surface_fn);
99 }
100 
102 template<typename GetForce, typename GetSurface, typename SetForce>
103 auto Force(const std::string & name, GetForce get_force_fn, SetForce set_force_fn, GetSurface get_surface_fn)
104 {
105  return details::ForceImpl(name, ForceConfig{}, get_force_fn, set_force_fn, get_surface_fn);
106 }
107 
109 template<typename GetForce, typename GetSurface, typename SetForce>
110 auto Force(const std::string & name,
111  const ForceConfig & config,
112  GetForce get_force_fn,
113  SetForce set_force_fn,
114  GetSurface get_surface_fn)
115 {
116  return details::ForceImpl(name, config, get_force_fn, set_force_fn, get_surface_fn);
117 }
118 
119 } // namespace mc_rtc::gui
mc_rtc::gui::Element
Definition: elements.h:58
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::details::ForceROImpl
Definition: Force.h:28
types.h
mc_rtc::gui::details::ForceImpl
Definition: Force.h:59
mc_rtc::gui::Element::write
void write(mc_rtc::MessagePackBuilder &)
Definition: elements.h:83
elements.h
mc_rtc::gui::details::ForceROImpl::type
static constexpr auto type
Definition: Force.h:30
mc_rtc::gui::ForceConfig::write
void write(mc_rtc::MessagePackBuilder &out) const
Definition: types.h:313
mc_rtc::gui::details::ForceROImpl::write_size
static constexpr size_t write_size()
Definition: Force.h:41
mc_rtc::MessagePackBuilder::write
void write()
mc_rtc::gui::details::ForceImpl::type
static constexpr auto type
Definition: Force.h:61
mc_rtc::gui::Elements::Force
@ Force
mc_rtc::gui::ForceConfig::write_size
static constexpr size_t write_size()
Definition: types.h:311
mc_rtc::gui::Element::write_size
static constexpr size_t write_size()
Definition: elements.h:76
mc_rtc::gui::details::ForceROImpl::ForceROImpl
ForceROImpl(const std::string &name, const ForceConfig &config, GetForce get_force_fn, GetSurface get_surface_fn)
Definition: Force.h:32
mc_rtc::gui::details::ForceROImpl::write
void write(mc_rtc::MessagePackBuilder &builder, bool ro=true)
Definition: Force.h:43
mc_rtc::gui::ForceConfig
Definition: types.h:286
mc_rtc::gui::details::ForceImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Force.h:73
mc_rtc::gui::details::ForceImpl::handleRequest
bool handleRequest(const mc_rtc::Configuration &data)
Definition: Force.h:75
traits.h
mc_rtc::gui::details::CheckReturnType
Definition: traits.h:86
mc_rtc::gui::details::ForceImpl::ForceImpl
ForceImpl(const std::string &name, const ForceConfig &config, GetForce get_force_fn, SetForce set_force_fn, GetSurface get_surface_fn)
Definition: Force.h:64
mc_rtc::gui::Force
auto Force(const std::string &name, GetForce get_force_fn, GetSurface get_surface_fn)
Definition: Force.h:89
mc_rtc::gui
Definition: Observer.h:15
mc_rtc::gui::Element::name
const std::string & name() const
Definition: elements.h:61