profile_dictionary.cpp
Go to the documentation of this file.
1 
28 #include <boost/serialization/nvp.hpp>
29 #include <boost/serialization/shared_ptr.hpp>
30 #include <boost/serialization/unordered_map.hpp>
31 
32 #include <mutex>
33 
34 namespace tesseract_common
35 {
36 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
38 {
39  std::unique_lock lhs_lock(mutex_, std::defer_lock);
40  std::shared_lock rhs_lock(other.mutex_, std::defer_lock);
41  std::scoped_lock lock{ lhs_lock, rhs_lock };
42 
43  profiles_ = other.profiles_; // NOLINT(cppcoreguidelines-prefer-member-initializer)
44 }
45 
46 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
48 {
49  std::unique_lock lhs_lock(mutex_, std::defer_lock);
50  std::shared_lock rhs_lock(other.mutex_, std::defer_lock);
51  std::scoped_lock lock{ lhs_lock, rhs_lock };
52 
53  profiles_ = other.profiles_; // NOLINT(cppcoreguidelines-prefer-member-initializer)
54  return *this;
55 }
56 
57 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
59 {
60  std::unique_lock lhs_lock(mutex_, std::defer_lock);
61  std::unique_lock rhs_lock(other.mutex_, std::defer_lock);
62  std::scoped_lock lock{ lhs_lock, rhs_lock };
63 
64  profiles_ = std::move(other.profiles_); // NOLINT(cppcoreguidelines-prefer-member-initializer)
65 }
66 
67 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
69 {
70  std::unique_lock lhs_lock(mutex_, std::defer_lock);
71  std::unique_lock rhs_lock(other.mutex_, std::defer_lock);
72  std::scoped_lock lock{ lhs_lock, rhs_lock };
73 
74  profiles_ = std::move(other.profiles_); // NOLINT(cppcoreguidelines-prefer-member-initializer)
75  return *this;
76 }
77 
78 bool ProfileDictionary::hasProfileEntry(std::size_t key, const std::string& ns) const
79 {
80  const std::shared_lock lock(mutex_);
81  auto it = profiles_.find(ns);
82  if (it == profiles_.end())
83  return false;
84 
85  return (it->second.find(key) != it->second.end());
86 }
87 
88 void ProfileDictionary::removeProfileEntry(std::size_t key, const std::string& ns)
89 {
90  const std::unique_lock lock(mutex_);
91 
92  auto it = profiles_.find(ns);
93  if (it == profiles_.end())
94  return;
95 
96  it->second.erase(key);
97  if (it->second.empty())
98  profiles_.erase(it);
99 }
100 
101 std::unordered_map<std::string, Profile::ConstPtr> ProfileDictionary::getProfileEntry(std::size_t key,
102  const std::string& ns) const
103 {
104  const std::shared_lock lock(mutex_);
105  auto it = profiles_.find(ns);
106  if (it == profiles_.end())
107  throw std::runtime_error("Profile namespace does not exist for '" + ns + "'!");
108 
109  auto it2 = it->second.find(key);
110  if (it2 != it->second.end())
111  return it2->second;
112 
113  throw std::runtime_error("Profile entry does not exist for type name '" + std::to_string(key) + "' in namespace '" +
114  ns + "'!");
115 }
116 
117 std::unordered_map<std::string, std::unordered_map<std::size_t, std::unordered_map<std::string, Profile::ConstPtr>>>
119 {
120  const std::shared_lock lock(mutex_);
121  return profiles_;
122 }
123 
124 void ProfileDictionary::addProfile(const std::string& ns,
125  const std::string& profile_name,
126  const Profile::ConstPtr& profile)
127 {
128  if (ns.empty())
129  throw std::runtime_error("Adding profile with an empty namespace!");
130 
131  if (profile_name.empty())
132  throw std::runtime_error("Adding profile with an empty string as the key!");
133 
134  if (profile == nullptr)
135  throw std::runtime_error("Adding profile that is a nullptr");
136 
137  const std::unique_lock lock(mutex_);
138  auto it = profiles_.find(ns);
139  if (it == profiles_.end())
140  {
141  std::unordered_map<std::string, Profile::ConstPtr> new_entry;
142  new_entry[profile_name] = profile;
143  profiles_[ns][profile->getKey()] = new_entry;
144  }
145  else
146  {
147  auto it2 = it->second.find(profile->getKey());
148  if (it2 != it->second.end())
149  {
150  it2->second[profile_name] = profile;
151  }
152  else
153  {
154  std::unordered_map<std::string, Profile::ConstPtr> new_entry;
155  new_entry[profile_name] = profile;
156  it->second[profile->getKey()] = new_entry;
157  }
158  }
159 }
160 
161 void ProfileDictionary::addProfile(const std::string& ns,
162  const std::vector<std::string>& profile_names,
163  const Profile::ConstPtr& profile)
164 {
165  if (ns.empty())
166  throw std::runtime_error("Adding profile with an empty namespace!");
167 
168  if (profile_names.empty())
169  throw std::runtime_error("Adding profile with an empty vector of keys!");
170 
171  if (profile == nullptr)
172  throw std::runtime_error("Adding profile that is a nullptr");
173 
174  const std::unique_lock lock(mutex_);
175  auto it = profiles_.find(ns);
176  if (it == profiles_.end())
177  {
178  std::unordered_map<std::string, Profile::ConstPtr> new_entry;
179  for (const auto& profile_name : profile_names)
180  {
181  if (profile_name.empty())
182  throw std::runtime_error("Adding profile with an empty string as the key!");
183 
184  new_entry[profile_name] = profile;
185  }
186  profiles_[ns][profile->getKey()] = new_entry;
187  }
188  else
189  {
190  auto it2 = it->second.find(profile->getKey());
191  if (it2 != it->second.end())
192  {
193  for (const auto& profile_name : profile_names)
194  {
195  if (profile_name.empty())
196  throw std::runtime_error("Adding profile with an empty string as the key!");
197 
198  it2->second[profile_name] = profile;
199  }
200  }
201  else
202  {
203  std::unordered_map<std::string, Profile::ConstPtr> new_entry;
204  for (const auto& profile_name : profile_names)
205  {
206  if (profile_name.empty())
207  throw std::runtime_error("Adding profile with an empty string as the key!");
208 
209  new_entry[profile_name] = profile;
210  }
211  it->second[profile->getKey()] = new_entry;
212  }
213  }
214 }
215 
216 bool ProfileDictionary::hasProfile(std::size_t key, const std::string& ns, const std::string& profile_name) const
217 {
218  const std::shared_lock lock(mutex_);
219  auto it = profiles_.find(ns);
220  if (it == profiles_.end())
221  return false;
222 
223  auto it2 = it->second.find(key);
224  if (it2 != it->second.end())
225  {
226  auto it3 = it2->second.find(profile_name);
227  if (it3 != it2->second.end())
228  return true;
229  }
230  return false;
231 }
232 
234  const std::string& ns,
235  const std::string& profile_name) const
236 {
237  const std::shared_lock lock(mutex_);
238  return profiles_.at(ns).at(key).at(profile_name);
239 }
240 
241 void ProfileDictionary::removeProfile(std::size_t key, const std::string& ns, const std::string& profile_name)
242 {
243  const std::unique_lock lock(mutex_);
244  auto it = profiles_.find(ns);
245  if (it == profiles_.end())
246  return;
247 
248  auto it2 = it->second.find(key);
249  if (it2 != it->second.end())
250  {
251  it2->second.erase(profile_name);
252  if (it2->second.empty())
253  {
254  it->second.erase(it2);
255  if (it->second.empty())
256  profiles_.erase(it);
257  }
258  }
259 }
260 
262 {
263  const std::unique_lock lock(mutex_);
264  profiles_.clear();
265 }
266 
267 template <class Archive>
268 void ProfileDictionary::serialize(Archive& ar, const unsigned int /*version*/)
269 {
270  const std::shared_lock lock(mutex_);
271  ar& boost::serialization::make_nvp("profiles", profiles_);
272 }
273 
274 } // namespace tesseract_common
275 
278 BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_common::ProfileDictionary)
279 
281 BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_common::ProfileDictionaryPtrAnyPoly)
tesseract_common::ProfileDictionary::getProfile
Profile::ConstPtr getProfile(std::size_t key, const std::string &ns, const std::string &profile_name) const
Get a profile by name.
Definition: profile_dictionary.cpp:233
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::ProfileDictionary::mutex_
std::shared_mutex mutex_
Definition: profile_dictionary.h:151
TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE
#define TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(Type)
Definition: serialization.h:49
tesseract_common::AnyWrapper
Definition: any_poly.h:80
tesseract_common::ProfileDictionary::removeProfile
void removeProfile(std::size_t key, const std::string &ns, const std::string &profile_name)
Remove a profile.
Definition: profile_dictionary.cpp:241
tesseract_common::ProfileDictionary::removeProfileEntry
void removeProfileEntry(std::size_t key, const std::string &ns)
Remove a profile entry.
Definition: profile_dictionary.cpp:88
tesseract_common::ProfileDictionary::getAllProfileEntries
std::unordered_map< std::string, std::unordered_map< std::size_t, std::unordered_map< std::string, Profile::ConstPtr > > > getAllProfileEntries() const
Get all profile entries.
Definition: profile_dictionary.cpp:118
tesseract_common::ProfileDictionary::ProfileDictionary
ProfileDictionary()=default
tesseract_common::Profile::ConstPtr
std::shared_ptr< const Profile > ConstPtr
Definition: profile.h:43
tesseract_common::ProfileDictionary::hasProfileEntry
bool hasProfileEntry(std::size_t key, const std::string &ns) const
Check if a profile entry exists.
Definition: profile_dictionary.cpp:78
tesseract_common::ProfileDictionary::profiles_
std::unordered_map< std::string, std::unordered_map< std::size_t, std::unordered_map< std::string, Profile::ConstPtr > > > profiles_
Definition: profile_dictionary.h:153
tesseract_common::ProfileDictionary::clear
void clear()
Clear the dictionary.
Definition: profile_dictionary.cpp:261
tesseract_common::ProfileDictionary
This class is used to store profiles used by various tasks.
Definition: profile_dictionary.h:46
tesseract_common::ProfileDictionary::operator=
ProfileDictionary & operator=(const ProfileDictionary &)
Definition: profile_dictionary.cpp:47
tesseract_common::ProfileDictionary::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: profile_dictionary.cpp:268
tesseract_common::ProfileDictionary::hasProfile
bool hasProfile(std::size_t key, const std::string &ns, const std::string &profile_name) const
Check if a profile exists.
Definition: profile_dictionary.cpp:216
serialization.h
Additional Boost serialization wrappers.
profile_dictionary.h
This is a profile dictionary for storing all profiles.
tesseract_common::ProfileDictionary::getProfileEntry
std::unordered_map< std::string, Profile::ConstPtr > getProfileEntry(std::size_t key, const std::string &ns) const
Get a profile entry.
Definition: profile_dictionary.cpp:101
tesseract_common::ProfileDictionary::addProfile
void addProfile(const std::string &ns, const std::string &profile_name, const Profile::ConstPtr &profile)
Add a profile.
Definition: profile_dictionary.cpp:124


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40