Skip to content

Commit 47d01a5

Browse files
Add method get_keys from map (#60)
* Add method get_keys from map Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> * Fix header dll export order Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> * Add docstring and test Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> --------- Signed-off-by: Juan López Fernández <juanlopez@eprosima.com>
1 parent 70a8845 commit 47d01a5

5 files changed

Lines changed: 59 additions & 1 deletion

File tree

cpp_utils/include/cpp_utils/impl/utils.ipp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,17 @@ void* copy_to_void_ptr(
165165
return new_ptr;
166166
}
167167

168+
template <typename Key, typename Value>
169+
std::set<Key> get_keys(
170+
const std::map<Key, Value>& map)
171+
{
172+
std::set<Key> ret;
173+
for (const auto& elem : map)
174+
{
175+
ret.insert(elem.first);
176+
}
177+
return ret;
178+
}
179+
168180
} /* namespace utils */
169181
} /* namespace eprosima */

cpp_utils/include/cpp_utils/utils.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#pragma once
2222

23+
#include <map>
2324
#include <memory>
2425
#include <set>
2526
#include <sstream>
@@ -273,6 +274,20 @@ std::vector<std::string> split_string(
273274
const std::string& source,
274275
const std::string& delimiter);
275276

277+
/**
278+
* @brief Get std::map keys.
279+
*
280+
* Obtain the set of keys relative to a std::map.
281+
*
282+
* @param map [in] map whose keys are returned
283+
*
284+
* @return map keys
285+
*/
286+
template <typename Key, typename Value>
287+
CPP_UTILS_DllAPI
288+
std::set<Key> get_keys(
289+
const std::map<Key, Value>& map);
290+
276291
} /* namespace utils */
277292
} /* namespace eprosima */
278293

cpp_utils/src/cpp/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bool is_file_accessible(
116116
if ((FileAccessMode::exec& access_mode) == FileAccessMode::exec)
117117
{
118118
logWarning(
119-
DDSROUTER_UTILS,
119+
UTILS_UTILS,
120120
"Windows does not allow to check execution permission for file.");
121121
// Take out the FileAccessMode::exec bit
122122
access_mode =

cpp_utils/test/unittest/utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(TEST_LIST
3535
split_string_one_delimiter
3636
split_strings_one_delimiter
3737
split_string_delimiters
38+
get_keys
3839
)
3940

4041
set(TEST_EXTRA_LIBRARIES

cpp_utils/test/unittest/utils/utilsTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,36 @@ TEST(utilsTest, split_string_delimiters)
579579
}
580580
}
581581

582+
/**
583+
* Test method get_keys
584+
*/
585+
TEST(utilsTest, get_keys)
586+
{
587+
// std::string keys
588+
{
589+
std::set<std::string> keys = {"a", "b", "c", "d"};
590+
std::map<std::string, std::string> map;
591+
for (auto key: keys)
592+
{
593+
map.emplace(key, "value");
594+
}
595+
596+
ASSERT_EQ(keys, get_keys(map));
597+
}
598+
599+
// int keys
600+
{
601+
std::set<int> keys = {1, 2, 3, 4};
602+
std::map<int, std::string> map;
603+
for (auto key: keys)
604+
{
605+
map.emplace(key, "value");
606+
}
607+
608+
ASSERT_EQ(keys, get_keys(map));
609+
}
610+
}
611+
582612
int main(
583613
int argc,
584614
char** argv)

0 commit comments

Comments
 (0)