lookup.cpp
Go to the documentation of this file.
1 #include "lookup.hpp"
2 #include <algorithm> // For std::transform
3 
4 namespace hebi {
5 
7 {
9 }
10 
11 Lookup::~Lookup() noexcept
12 {
14 }
15 
16 std::shared_ptr<Group> Lookup::getGroupFromNames(const std::vector<std::string>& families, const std::vector<std::string>& names, int32_t timeout_ms)
17 {
18  std::shared_ptr<Group> ptr;
19  std::vector<const char *> names_cstrs;
20  std::vector<const char *> families_cstrs;
21  names_cstrs.reserve(names.size());
22  families_cstrs.reserve(families.size());
23 
24  std::transform(std::begin(names), std::end(names),
25  std::back_inserter(names_cstrs), [] (const std::string& name) { return name.c_str(); });
26  std::transform(std::begin(families), std::end(families),
27  std::back_inserter(families_cstrs), [] (const std::string& family) { return family.c_str(); });
28 
29  HebiGroupPtr group = hebiGroupCreateFromNames(lookup_, families_cstrs.data(), families_cstrs.size(), names_cstrs.data(), names_cstrs.size(), timeout_ms);
30  if (group != nullptr)
31  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
32  return ptr;
33 }
34 
35 std::shared_ptr<Group> Lookup::getGroupFromMacs(const std::vector<MacAddress>& addresses, int32_t timeout_ms)
36 {
37  std::shared_ptr<Group> ptr;
38  std::vector<const HebiMacAddress*> addresses_c;
39  addresses_c.reserve(addresses.size());
40  std::transform(std::begin(addresses), std::end(addresses),
41  std::back_inserter(addresses_c), [] (const MacAddress& addr) { return &addr.internal_; });
42  HebiGroupPtr group = hebiGroupCreateFromMacs(lookup_, addresses_c.data(), addresses.size(), timeout_ms);
43  if (group != nullptr)
44  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
45  return ptr;
46 }
47 
48 std::shared_ptr<Group> Lookup::getGroupFromFamily(const std::string& family, int32_t timeout_ms)
49 {
50  std::shared_ptr<Group> ptr;
51  HebiGroupPtr group = hebiGroupCreateFromFamily(lookup_, family.c_str(), timeout_ms);
52  if (group != nullptr)
53  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
54  return ptr;
55 }
56 
57 std::shared_ptr<Group> Lookup::getConnectedGroupFromName(const std::string& family_name, const std::string& name, int32_t timeout_ms)
58 {
59  std::shared_ptr<Group> ptr;
60  HebiGroupPtr group = hebiGroupCreateConnectedFromName(lookup_, family_name.c_str(), name.c_str(), timeout_ms);
61  if (group != nullptr)
62  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
63  return ptr;
64 }
65 
66 std::shared_ptr<Group> Lookup::getConnectedGroupFromMac(const MacAddress& address, int32_t timeout_ms)
67 {
68  std::shared_ptr<Group> ptr;
69  HebiGroupPtr group = hebiGroupCreateConnectedFromMac(lookup_, &(address.internal_), timeout_ms);
70  if (group != nullptr)
71  return std::make_shared<Group>(group, initial_group_feedback_frequency_, initial_group_command_lifetime_);
72  return ptr;
73 }
74 
76 {
78 }
79 
81 {
83 }
84 
86 {
88 }
89 
91 {
93 }
94 
96  : list_(list), current_(current)
97 { }
98 
100 {
101  return list_[current_];
102 }
103 
105 {
106  ++current_;
107  return *this;
108 }
109 
111 {
112  Lookup::EntryList::Iterator tmp = *this;
113  ++current_;
114  return tmp;
115 }
116 
118 {
119  --current_;
120  return *this;
121 }
122 
124 {
125  Lookup::EntryList::Iterator tmp = *this;
126  --current_;
127  return tmp;
128 }
129 
131 {
132  return this->current_ == rhs.current_;
133 }
134 
136 {
137  return !(*this == rhs);
138 }
139 
141 {
143  lookup_list_ = nullptr;
144 }
145 
147 {
148  size_t required_size;
149  hebiLookupEntryListGetName(lookup_list_, index, nullptr, &required_size);
150  auto buffer = new char [required_size];
151  hebiLookupEntryListGetName(lookup_list_, index, buffer, &required_size);
152  std::string name(buffer, required_size-1);
153  delete[] buffer;
154 
155  hebiLookupEntryListGetFamily(lookup_list_, index, nullptr, &required_size);
156  buffer = new char [required_size];
157  hebiLookupEntryListGetFamily(lookup_list_, index, buffer, &required_size);
158  std::string family(buffer, required_size-1);
159  delete[] buffer;
160 
161  HebiMacAddress mac_int;
163  MacAddress mac;
164  mac.internal_ = mac_int;
165 
166  Entry e = { name, family, mac };
167  return e;
168 }
169 
171 {
173 }
174 
176 {
177  return Lookup::EntryList::Iterator(*this, 0);
178 }
179 
181 {
182  return Lookup::EntryList::Iterator(*this, size());
183 }
184 
185 std::shared_ptr<Lookup::EntryList> Lookup::getEntryList()
186 {
187  std::shared_ptr<Lookup::EntryList> ptr;
188  auto entry_list = hebiCreateLookupEntryList(lookup_);
189  if (entry_list != nullptr)
190  return std::make_shared<Lookup::EntryList>(entry_list);
191  return ptr;
192 }
193 
194 } // namespace hebi
float initial_group_feedback_frequency_
Definition: lookup.hpp:47
HebiGroupPtr hebiGroupCreateConnectedFromMac(HebiLookupPtr lookup, const HebiMacAddress *address, int32_t timeout_ms)
Create a group with all modules connected to module with the given MAC address.
Iterator begin() const
Definition: lookup.cpp:175
struct _HebiGroup * HebiGroupPtr
The C-style&#39;s API representation of a group.
Definition: hebi.h:330
Lookup()
Creates a Lookup object which can create Module and Group references. Typically, only one Lookup obje...
Definition: lookup.cpp:6
Definition: color.hpp:5
A simple wrapper class for internal C-API HebiMacAddress objects to allow interfacing with API calls ...
Definition: mac_address.hpp:12
HebiStatusCode hebiLookupEntryListGetName(HebiLookupEntryListPtr lookup_list, size_t index, char *buffer, size_t *length)
size_t size() const
Definition: lookup.cpp:170
bool operator==(const Iterator &rhs) const
Definition: lookup.cpp:130
std::shared_ptr< EntryList > getEntryList()
Definition: lookup.cpp:185
HebiGroupPtr hebiGroupCreateConnectedFromName(HebiLookupPtr lookup, const char *family, const char *name, int32_t timeout_ms)
Create a group with all modules connected to module with the given name and family.
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:48
size_t hebiLookupEntryListGetSize(HebiLookupEntryListPtr lookup_list)
~EntryList() noexcept
Definition: lookup.cpp:140
HebiGroupPtr hebiGroupCreateFromNames(HebiLookupPtr lookup, const char *const *families, size_t num_families, const char *const *names, size_t num_names, int32_t timeout_ms)
Create a group with modules matching the given names and families.
~Lookup() noexcept
Destructor frees all resources created by Lookup object, and stops the background query thread...
Definition: lookup.cpp:11
HebiLookupEntryListPtr hebiCreateLookupEntryList(HebiLookupPtr lookup)
Return a snapshot of the contents of the module registry – i.e., which modules have been found by th...
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:66
reference operator*() const
Definition: lookup.cpp:99
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:35
HebiStatusCode hebiLookupEntryListGetFamily(HebiLookupEntryListPtr lookup_list, size_t index, char *buffer, size_t *length)
int32_t getInitialGroupCommandLifetimeMs()
Gets the default command lifetime value for groups created from this lookup.
Definition: lookup.cpp:85
HebiLookupEntryListPtr lookup_list_
Definition: lookup.hpp:214
HebiGroupPtr hebiGroupCreateFromFamily(HebiLookupPtr lookup, const char *family, int32_t timeout_ms)
Create a group with all modules known to the lookup with the given family.
HebiMacAddress internal_
Definition: mac_address.hpp:21
bool operator!=(const Iterator &rhs) const
Definition: lookup.cpp:135
HebiLookupPtr hebiLookupCreate(void)
Create a Lookup instance.
void setInitialGroupFeedbackFrequencyHz(float frequency)
Sets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:80
HebiLookupPtr lookup_
Definition: lookup.hpp:42
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:57
Entry operator[](size_t index) const
Definition: lookup.cpp:146
HebiStatusCode hebiLookupEntryListGetMacAddress(HebiLookupEntryListPtr lookup_list, size_t index, HebiMacAddress *mac_address)
HebiGroupPtr hebiGroupCreateFromMacs(HebiLookupPtr lookup, const HebiMacAddress *const *addresses, size_t num_addresses, int32_t timeout_ms)
Create a group of modules with the given MAC addresses.
Iterator end() const
Definition: lookup.cpp:180
int32_t initial_group_command_lifetime_
Definition: lookup.hpp:52
float getInitialGroupFeedbackFrequencyHz()
Gets the default feedback frequency value for groups created from this lookup.
Definition: lookup.cpp:75
void hebiLookupEntryListRelease(HebiLookupEntryListPtr lookup_list)
Release resources for a given lookup entry list; list should not be used after this call...
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:16
void hebiLookupRelease(HebiLookupPtr lookup)
Frees resources created by the lookup object.


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:21