Client.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 
6 #include "Layer.h"
7 #include "Interaction.h"
8 
9 
10 namespace armarx
11 {
12  class Component;
13  class ManagedIceObject;
14 
15 namespace viz
16 {
17  /**
18  * A staged commit prepares multiple layers to be committed.
19  *
20  * Add all relevant layer updates via .add(layer).
21  * Add layers for which you want interaction feedback via .requestInteraction(layer).
22  * Then, commit via client.apply(stagedCommit).
23  *
24  * A staged commit can be reused for subsequent commits.
25  * Remember to call .reset() to clear the internal data structures.
26  *
27  * As long as you keep the staged commits separate, this method is thread-safe.
28  * So you can have two threads calling .commit(A) and .commit(B)
29  * simultaneously without any locking mechanism.
30  */
31  struct StagedCommit
32  {
33  /**
34  * @brief Stage a layer to be committed later via client.apply(*this)
35  * @param layer The layer to be added to this staged commit.
36  */
37  void add(Layer const& layer)
38  {
39  data_.updates.push_back(layer.data_);
40  }
41 
42  void add(std::initializer_list<Layer> layers)
43  {
44  data_.updates.reserve(data_.updates.size() + layers.size());
45  for (Layer const& layer : layers)
46  {
47  data_.updates.push_back(layer.data_);
48  }
49  }
50 
51  /**
52  * @brief Request interaction feedback for a particular layer.
53  * @param layer The layer you want to get interaction feedback for.
54  */
55  void requestInteraction(Layer const& layer)
56  {
57  data_.interactionComponent = layer.data_.component;
58  data_.interactionLayers.push_back(layer.data_.name);
59  }
60 
61  /**
62  * @brief Reset all staged layers and interaction requests.
63  */
64  void reset()
65  {
66  data_.updates.clear();
67  data_.interactionComponent.clear();
68  data_.interactionLayers.clear();
69  }
70 
71  viz::data::CommitInput data_;
72  };
73 
74  struct CommitResult
75  {
76  long revision() const
77  {
78  return data_.revision;
79  }
80 
82  {
83  const InteractionFeedback* begin = reinterpret_cast<const InteractionFeedback*>(data_.interactions.data());
84  const InteractionFeedback* end = begin + data_.interactions.size();
85  return InteractionFeedbackRange{begin, end};
86  }
87 
88  data::CommitResult data_;
89  };
90 
92  {
93  bool isAvailable() const
94  {
95  return async && async->isCompleted();
96  }
97 
99  {
100  CommitResult result;
101  result.data_ = storage->end_commitAndReceiveInteractions(async);
102  return result;
103  }
104 
105  Ice::AsyncResultPtr async;
106  armarx::viz::StorageInterfacePrx storage;
107  };
108 
109  struct Client
110  {
111  Client() = default;
112  Client(const Client&) = default;
113 
115  std::string const& topicNameProperty = "ArVizTopicName",
116  std::string const& storageNameProperty = "ArVizStorageName");
117 
119  std::string const& topicName = "ArVizTopic",
120  std::string const& storageName = "ArVizStorage");
121 
122  static Client createFromTopic(std::string const& componentName,
123  armarx::viz::Topic::ProxyType const& topic);
124 
125  static Client createFromProxies(std::string const& componentName,
127  armarx::viz::StorageAndTopicInterfacePrx const& storage);
128 
130  std::string const& topicName = "ArVizTopic",
131  std::string const& storageName = "ArVizStorage");
132 
133  Layer layer(std::string const& name) const;
134 
136  {
137  return StagedCommit();
138  }
139 
141 
143 
144  void commit(Layer const& layer)
145  {
146  std::vector<Layer> layers;
147  layers.push_back(layer);
148  commit(layers);
149  }
150 
151  void commit(std::vector<Layer> const& layers);
152 
153  void commitLayerContaining(std::string const& name)
154  {
155  std::vector<viz::Layer> layers;
156  layers.push_back(this->layer(name));
157  commit(layers);
158  }
159 
160  template <typename ElementT>
161  void commitLayerContaining(std::string const& name, ElementT const& element)
162  {
163  std::vector<viz::Layer> layers;
164  viz::Layer& newLayer = layers.emplace_back(this->layer(name));
165  newLayer.add(element);
166  commit(layers);
167  }
168 
169  void commitDeleteLayer(std::string const& name)
170  {
171  std::vector<viz::Layer> layers;
172  viz::Layer& layerToDelete = layers.emplace_back(this->layer(name));
173  layerToDelete.markForDeletion();
174  commit(layers);
175  }
176 
177  private:
178  std::string componentName;
179  armarx::viz::StorageInterfacePrx storage;
181  };
182 
183 }
184 }
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::viz::StagedCommit::add
void add(Layer const &layer)
Stage a layer to be committed later via client.apply(*this)
Definition: Client.h:37
armarx::viz::Client::createFromProxies
static Client createFromProxies(std::string const &componentName, armarx::viz::Topic::ProxyType const &topic, armarx::viz::StorageAndTopicInterfacePrx const &storage)
Definition: Client.cpp:45
armarx::viz::Client::stage
StagedCommit stage()
Definition: Client.h:135
armarx::viz::Client::commitAsync
CommitResultAsync commitAsync(StagedCommit const &commit)
Definition: Client.cpp:89
armarx::viz::CommitResult::revision
long revision() const
Definition: Client.h:76
armarx::ProxyType::component
@ component
armarx::viz::Client::createFromTopic
static Client createFromTopic(std::string const &componentName, armarx::viz::Topic::ProxyType const &topic)
Definition: Client.cpp:36
armarx::viz::Client::commitDeleteLayer
void commitDeleteLayer(std::string const &name)
Definition: Client.h:169
armarx::viz::CommitResultAsync
Definition: Client.h:91
Layer.h
armarx::viz::CommitResultAsync::isAvailable
bool isAvailable() const
Definition: Client.h:93
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::viz::StagedCommit
A staged commit prepares multiple layers to be committed.
Definition: Client.h:31
Interaction.h
armarx::viz::CommitResultAsync::waitAndGet
CommitResult waitAndGet() const
Definition: Client.h:98
armarx::viz::Client::commitLayerContaining
void commitLayerContaining(std::string const &name, ElementT const &element)
Definition: Client.h:161
armarx::viz::Client::Client
Client()=default
armarx::viz::StagedCommit::reset
void reset()
Reset all staged layers and interaction requests.
Definition: Client.h:64
armarx::viz::Client::createForGuiPlugin
static Client createForGuiPlugin(armarx::Component &component, std::string const &topicName="ArVizTopic", std::string const &storageName="ArVizStorage")
Definition: Client.cpp:56
armarx::viz::Layer::data_
data::LayerUpdate data_
Definition: Layer.h:53
armarx::viz::CommitResult::interactions
InteractionFeedbackRange interactions() const
Definition: Client.h:81
armarx::viz::StagedCommit::requestInteraction
void requestInteraction(Layer const &layer)
Request interaction feedback for a particular layer.
Definition: Client.h:55
armarx::ProxyType::topic
@ topic
armarx::viz::CommitResultAsync::storage
armarx::viz::StorageInterfacePrx storage
Definition: Client.h:106
IceStorm::TopicPrx
::IceInternal::ProxyHandle< ::IceProxy::IceStorm::Topic > TopicPrx
Definition: IceManager.h:70
armarx::ProxyType
ProxyType
Definition: ProxyPropertyDefinition.h:41
armarx::viz::InteractionFeedbackRange
Definition: Interaction.h:135
armarx::viz::CommitResult
Definition: Client.h:74
armarx::viz::InteractionFeedback
Definition: Interaction.h:57
armarx::viz::Client::commit
void commit(Layer const &layer)
Definition: Client.h:144
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
armarx::viz::CommitResult::data_
data::CommitResult data_
Definition: Client.h:88
armarx::viz::StagedCommit::add
void add(std::initializer_list< Layer > layers)
Definition: Client.h:42
armarx::viz::StagedCommit::data_
viz::data::CommitInput data_
Definition: Client.h:71
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::viz::Client
Definition: Client.h:109
armarx::viz::Layer
Definition: Layer.h:12
armarx::viz::Layer::markForDeletion
void markForDeletion()
Definition: Layer.h:43
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:416
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::viz::Client::commitLayerContaining
void commitLayerContaining(std::string const &name)
Definition: Client.h:153
armarx::viz::CommitResultAsync::async
Ice::AsyncResultPtr async
Definition: Client.h:105