Skip to content

Commit cbd8395

Browse files
Move profile dictionary to tesseract_common
1 parent e234ff5 commit cbd8395

File tree

8 files changed

+757
-0
lines changed

8 files changed

+757
-0
lines changed

tesseract_common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ add_library(
9292
src/eigen_serialization.cpp
9393
src/utils.cpp
9494
src/plugin_info.cpp
95+
src/profile_dictionary.cpp
96+
src/profile.cpp
9597
src/resource_locator.cpp
9698
src/types.cpp
9799
src/stopwatch.cpp

tesseract_common/include/tesseract_common/fwd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ struct Serialization;
8282

8383
// timer.h
8484
class Timer;
85+
86+
// Profile Dictionary
87+
class Profile;
88+
class ProfileDictionary;
8589
} // namespace tesseract_common
8690

8791
#endif // TESSERACT_COMMON_TESSERACT_COMMON_FWD_H
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @file profile.h
3+
* @brief This is a profile base class
4+
*
5+
* @author Levi Armstrong
6+
* @date December 2, 2024
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @copyright Copyright (c) 2024, 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_H
27+
#define TESSERACT_COMMON_PROFILE_H
28+
29+
#include <memory>
30+
#include <boost/serialization/access.hpp>
31+
#include <boost/serialization/export.hpp>
32+
33+
namespace tesseract_common
34+
{
35+
/**
36+
* @brief The Profile class
37+
*/
38+
class Profile
39+
{
40+
public:
41+
using Ptr = std::shared_ptr<Profile>;
42+
using ConstPtr = std::shared_ptr<const Profile>;
43+
44+
Profile() = default;
45+
Profile(std::size_t key);
46+
virtual ~Profile() = default;
47+
48+
/**
49+
* @brief Get the hash code associated with the profile
50+
* @return The profile's hash code
51+
*/
52+
std::size_t getKey() const;
53+
54+
protected:
55+
Profile(const Profile&) = default;
56+
Profile& operator=(const Profile&) = default;
57+
Profile(Profile&&) = default;
58+
Profile& operator=(Profile&&) = default;
59+
60+
std::size_t key_{ 0 };
61+
friend class boost::serialization::access;
62+
template <class Archive>
63+
void serialize(Archive&, const unsigned int); // NOLINT
64+
};
65+
} // namespace tesseract_common
66+
67+
BOOST_CLASS_EXPORT_KEY(tesseract_common::Profile)
68+
69+
#endif // TESSERACT_COMMON_PROFILE_H
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

tesseract_common/src/profile.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file profile.cpp
3+
* @brief This is a profile base class
4+
*
5+
* @author Levi Armstrong
6+
* @date December 2, 2024
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @copyright Copyright (c) 2024, 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+
27+
#include <tesseract_common/profile.h>
28+
#include <tesseract_common/serialization.h>
29+
#include <boost/serialization/export.hpp>
30+
#include <boost/serialization/nvp.hpp>
31+
32+
namespace tesseract_common
33+
{
34+
Profile::Profile(std::size_t key) : key_(key) {}
35+
36+
std::size_t Profile::getKey() const { return key_; }
37+
38+
template <class Archive>
39+
void Profile::serialize(Archive& ar, const unsigned int /*version*/)
40+
{
41+
ar& boost::serialization::make_nvp("key", key_);
42+
}
43+
} // namespace tesseract_common
44+
45+
TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(tesseract_common::Profile)
46+
BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_common::Profile)

0 commit comments

Comments
 (0)