|
| 1 | +/** |
| 2 | + * @file profile_dictionary.h |
| 3 | + * @brief This is a profile dictionary for storing all profiles |
| 4 | + * |
| 5 | + * @author Levi Armstrong |
| 6 | + * @date December 2, 2020 |
| 7 | + * @version TODO |
| 8 | + * @bug No known bugs |
| 9 | + * |
| 10 | + * @copyright Copyright (c) 2020, Southwest Research Institute |
| 11 | + * |
| 12 | + * @par License |
| 13 | + * Software License Agreement (Apache License) |
| 14 | + * @par |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 16 | + * you may not use this file except in compliance with the License. |
| 17 | + * You may obtain a copy of the License at |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * @par |
| 20 | + * Unless required by applicable law or agreed to in writing, software |
| 21 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 22 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | + * See the License for the specific language governing permissions and |
| 24 | + * limitations under the License. |
| 25 | + */ |
| 26 | +#ifndef TESSERACT_COMMON_PROFILE_DICTIONARY_H |
| 27 | +#define TESSERACT_COMMON_PROFILE_DICTIONARY_H |
| 28 | + |
| 29 | +#include <tesseract_common/macros.h> |
| 30 | +TESSERACT_COMMON_IGNORE_WARNINGS_PUSH |
| 31 | +#include <unordered_map> |
| 32 | +#include <memory> |
| 33 | +#include <shared_mutex> |
| 34 | +TESSERACT_COMMON_IGNORE_WARNINGS_POP |
| 35 | + |
| 36 | +#include <tesseract_common/profile.h> |
| 37 | +#include <tesseract_common/any_poly.h> |
| 38 | + |
| 39 | +namespace tesseract_common |
| 40 | +{ |
| 41 | +/** |
| 42 | + * @brief This class is used to store profiles used by various tasks |
| 43 | + * @details This is a thread safe class |
| 44 | + */ |
| 45 | +class ProfileDictionary |
| 46 | +{ |
| 47 | +public: |
| 48 | + using Ptr = std::shared_ptr<ProfileDictionary>; |
| 49 | + using ConstPtr = std::shared_ptr<const ProfileDictionary>; |
| 50 | + |
| 51 | + ProfileDictionary() = default; |
| 52 | + ~ProfileDictionary() = default; |
| 53 | + ProfileDictionary(const ProfileDictionary&); |
| 54 | + ProfileDictionary& operator=(const ProfileDictionary&); |
| 55 | + ProfileDictionary(ProfileDictionary&&) noexcept; |
| 56 | + ProfileDictionary& operator=(ProfileDictionary&&) noexcept; |
| 57 | + |
| 58 | + /** |
| 59 | + * @brief Add a profile |
| 60 | + * @details If the profile entry does not exist it will create one |
| 61 | + * @param ns The profile namespace |
| 62 | + * @param profile_name The profile name |
| 63 | + * @param profile The profile to add |
| 64 | + */ |
| 65 | + void addProfile(const std::string& ns, const std::string& profile_name, const Profile::ConstPtr& profile); |
| 66 | + void addProfile(const std::string& ns, |
| 67 | + const std::vector<std::string>& profile_names, |
| 68 | + const Profile::ConstPtr& profile); |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Check if a profile exists |
| 72 | + * @details If profile entry does not exist it also returns false |
| 73 | + * @param key The profile key |
| 74 | + * @param ns The profile namespace |
| 75 | + * @param profile_name The profile name |
| 76 | + * @return True if profile exists, otherwise false |
| 77 | + */ |
| 78 | + bool hasProfile(std::size_t key, const std::string& ns, const std::string& profile_name) const; |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief Get a profile by name |
| 82 | + * @details Check if the profile exist before calling this function, if missing an exception is thrown |
| 83 | + * @param key The profile key |
| 84 | + * @param ns The profile namespace |
| 85 | + * @param profile_name The profile name |
| 86 | + * @return The profile |
| 87 | + */ |
| 88 | + Profile::ConstPtr getProfile(std::size_t key, const std::string& ns, const std::string& profile_name) const; |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief Remove a profile |
| 92 | + * @param key The profile key |
| 93 | + * @param ns The profile namespace |
| 94 | + * @param profile_name The profile to be removed |
| 95 | + */ |
| 96 | + void removeProfile(std::size_t key, const std::string& ns, const std::string& profile_name); |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Check if a profile entry exists |
| 100 | + * @param key The profile key |
| 101 | + * @param ns The profile namespace |
| 102 | + * @return True if exists, otherwise false |
| 103 | + */ |
| 104 | + bool hasProfileEntry(std::size_t key, const std::string& ns) const; |
| 105 | + |
| 106 | + /** |
| 107 | + * @brief Remove a profile entry |
| 108 | + * @param key The profile key |
| 109 | + * @param ns The profile namespace |
| 110 | + */ |
| 111 | + void removeProfileEntry(std::size_t key, const std::string& ns); |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Get a profile entry |
| 115 | + * @param key The profile key |
| 116 | + * @param ns The profile namespace |
| 117 | + * @return The profile map associated with the profile entry |
| 118 | + */ |
| 119 | + std::unordered_map<std::string, Profile::ConstPtr> getProfileEntry(std::size_t key, const std::string& ns) const; |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Get all profile entries |
| 123 | + * @return All profile entries |
| 124 | + */ |
| 125 | + std::unordered_map<std::string, std::unordered_map<std::size_t, std::unordered_map<std::string, Profile::ConstPtr>>> |
| 126 | + getAllProfileEntries() const; |
| 127 | + |
| 128 | + /** @brief Clear the dictionary */ |
| 129 | + void clear(); |
| 130 | + |
| 131 | +protected: |
| 132 | + mutable std::shared_mutex mutex_; |
| 133 | + std::unordered_map<std::string, std::unordered_map<std::size_t, std::unordered_map<std::string, Profile::ConstPtr>>> |
| 134 | + profiles_; |
| 135 | + |
| 136 | + friend class boost::serialization::access; |
| 137 | + template <class Archive> |
| 138 | + void serialize(Archive& ar, const unsigned int version); // NOLINT |
| 139 | +}; |
| 140 | + |
| 141 | +using ProfileDictionaryPtrAnyPoly = tesseract_common::AnyWrapper<std::shared_ptr<ProfileDictionary>>; |
| 142 | +} // namespace tesseract_common |
| 143 | + |
| 144 | +BOOST_CLASS_EXPORT_KEY(tesseract_common::ProfileDictionary) |
| 145 | +BOOST_CLASS_EXPORT_KEY(tesseract_common::ProfileDictionaryPtrAnyPoly) |
| 146 | + |
| 147 | +#endif // TESSERACT_COMMON_PROFILE_DICTIONARY_H |
0 commit comments