HEBI C++ API  3.8.0
lookup.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hebi.h"
4 
5 #include <cstddef>
6 #include <iterator>
7 #include <memory> // For shared_ptr
8 #include <vector>
9 
10 #include "group.hpp"
11 #include "mac_address.hpp"
12 
13 namespace hebi {
14 
24 class Lookup final {
25 private:
26  static const int32_t DEFAULT_TIMEOUT = 500;
27 
31  HebiLookupPtr lookup_;
32 
36  float initial_group_feedback_frequency_{100.0f};
37 
41  int32_t initial_group_command_lifetime_{250};
42 
43 public:
54  Lookup();
55 
68  Lookup(const std::vector<std::string>& interfaces);
69 
74  ~Lookup() noexcept; /* annotating specified destructor as noexcept is best-practice */
75 
84  void reset(const std::vector<std::string>& interfaces);
85 
107  std::shared_ptr<Group> getGroupFromNames(const std::vector<std::string>& families,
108  const std::vector<std::string>& names, int32_t timeout_ms = DEFAULT_TIMEOUT);
109 
124  std::shared_ptr<Group> getGroupFromMacs(const std::vector<MacAddress>& addresses,
125  int32_t timeout_ms = DEFAULT_TIMEOUT);
126 
141  std::shared_ptr<Group> getGroupFromFamily(const std::string& family, int32_t timeout_ms = DEFAULT_TIMEOUT);
142 
161  std::shared_ptr<Group> getConnectedGroupFromName(const std::string& family, const std::string& name,
162  int32_t timeout_ms = DEFAULT_TIMEOUT);
163 
179  std::shared_ptr<Group> getConnectedGroupFromMac(const MacAddress& address, int32_t timeout_ms = DEFAULT_TIMEOUT);
180 
198  void setInitialGroupFeedbackFrequencyHz(float frequency);
199 
217  void setInitialGroupCommandLifetimeMs(int32_t ms);
218 
224  double getLookupFrequencyHz() const;
225 
234  bool setLookupFrequencyHz(double frequency);
235 
236  class EntryList final {
237  struct Entry final {
238  std::string name_;
239  std::string family_;
240  MacAddress mac_address_;
241  uint32_t ip_address_;
242  bool is_stale_;
243  };
244 
245  private:
249  HebiLookupEntryListPtr lookup_list_;
250 
255  class Iterator final {
256  public:
257  // Iterator traits (not from std::iterator to be C++17 compliant)
258  using value_type = Entry;
259  using difference_type = int;
260  using pointer = Entry*;
261  using reference = Entry;
262  using iterator_category = std::bidirectional_iterator_tag;
263 
264  // Default constructable
265  explicit Iterator(const EntryList& list, size_t current);
266 
267  // Dereferencable
268  reference operator*() const;
269 
270  // Pre- and post-incrementable/decrementable
271  Iterator& operator++();
272  Iterator operator++(int);
273  Iterator& operator--();
274  Iterator operator--(int);
275 
276  // Equality / inequality
277  bool operator==(const Iterator& rhs) const;
278  bool operator!=(const Iterator& rhs) const;
279 
280  private:
281  const EntryList& list_;
282  size_t current_{0};
283  };
284 
285  public:
289  EntryList(HebiLookupEntryListPtr lookup_list) : lookup_list_(lookup_list) {}
290 
291  ~EntryList() noexcept;
292 
293  Entry operator[](size_t index) const;
294 
295  size_t size() const;
296 
297  Iterator begin() const;
298  Iterator end() const;
299 
300  private:
305  };
306 
307  std::shared_ptr<EntryList> getEntryList();
308 
309 private:
314 };
315 
316 } // namespace hebi
EntryList(HebiLookupEntryListPtr lookup_list)
Definition: lookup.hpp:289
Lookup()
Creates a Lookup object which can create Module and Group references. Typically, only one Lookup obje...
Definition: lookup.cpp:7
double getLookupFrequencyHz() const
Gets the rate [Hz] at which "discovery" packets are broadcast.
Definition: lookup.cpp:92
Definition: arm.cpp:8
A simple wrapper class for internal C-API HebiMacAddress objects to allow interfacing with API calls ...
Definition: mac_address.hpp:13
Iterator begin() const
Definition: lookup.cpp:166
Definition: lookup.hpp:236
#define HEBI_DISABLE_COPY_MOVE(Class)
Definition: util.hpp:6
std::shared_ptr< EntryList > getEntryList()
Definition: lookup.cpp:170
std::shared_ptr< Group > getGroupFromFamily(const std::string &family, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from all known modules with the given family.
Definition: lookup.cpp:59
~EntryList() noexcept
Definition: lookup.cpp:130
~Lookup() noexcept
Destructor frees all resources created by Lookup object, and stops the background query thread.
Definition: lookup.cpp:17
std::shared_ptr< Group > getConnectedGroupFromMac(const MacAddress &address, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from all modules known to connect to a module with the given mac address.
Definition: lookup.cpp:76
void reset(const std::vector< std::string > &interfaces)
Resets the lookup object to its initial state without destructing and re-constructing the object.
Definition: lookup.cpp:19
std::shared_ptr< Group > getGroupFromMacs(const std::vector< MacAddress > &addresses, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from modules with the given mac addresses.
Definition: lookup.cpp:47
int32_t getInitialGroupCommandLifetimeMs()
Gets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:88
void setInitialGroupFeedbackFrequencyHz(float frequency)
Sets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:86
Iterator end() const
Definition: lookup.cpp:168
bool setLookupFrequencyHz(double frequency)
Sets the lookup rate [Hz].
Definition: lookup.cpp:94
void setInitialGroupCommandLifetimeMs(int32_t ms)
Sets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:90
std::shared_ptr< Group > getConnectedGroupFromName(const std::string &family, const std::string &name, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from all modules known to connect to a module with the given name and family.
Definition: lookup.cpp:67
Maintains a registry of network-connected modules and returns Group objects to the user.
Definition: lookup.hpp:24
float getInitialGroupFeedbackFrequencyHz()
Gets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:84
std::shared_ptr< Group > getGroupFromNames(const std::vector< std::string > &families, const std::vector< std::string > &names, int32_t timeout_ms=DEFAULT_TIMEOUT)
Get a group from modules with the given names and families.
Definition: lookup.cpp:27
size_t size() const
Definition: lookup.cpp:164