Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / xbt / PropertyHolder.cpp
1 /* Copyright (c) 2015-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <xbt/PropertyHolder.hpp>
7
8 #include <map>
9 #include <memory>
10
11 namespace simgrid {
12 namespace xbt {
13
14 /** @brief Return the property associated to the provided key (or nullptr if not existing) */
15 const char* PropertyHolder::get_property(const std::string& key) const
16 {
17   if (not properties_)
18     return nullptr;
19   auto prop = properties_->find(key);
20   return prop == properties_->end() ? nullptr : prop->second.c_str();
21 }
22
23 /** @brief Change the value of a given key in the property set */
24 void PropertyHolder::set_property(const std::string& key, const std::string& value)
25 {
26   if (not properties_)
27     properties_ = std::make_unique<std::unordered_map<std::string, std::string>>();
28   (*properties_)[key] = value;
29 }
30
31 /** @brief Return the whole set of properties. Don't mess with it, dude! */
32 const std::unordered_map<std::string, std::string>* PropertyHolder::get_properties()
33 {
34   if (not properties_)
35     properties_ = std::make_unique<std::unordered_map<std::string, std::string>>();
36   return properties_.get();
37 }
38
39 /** @brief Change the value of the given keys in the property set */
40 template <class Assoc> void PropertyHolder::set_properties(const Assoc& properties)
41 {
42   if (not properties_)
43     properties_ = std::make_unique<std::unordered_map<std::string, std::string>>();
44   std::unordered_map<std::string, std::string> props(properties.cbegin(), properties.cend());
45 #if __cplusplus >= 201703L
46   props.merge(properties_);
47 #else
48   props.insert(properties_->cbegin(), properties_->cend());
49 #endif
50   properties_->swap(props);
51 }
52
53 template void PropertyHolder::set_properties(const std::map<std::string, std::string, std::less<>>& properties);
54 template void PropertyHolder::set_properties(const std::unordered_map<std::string, std::string>& properties);
55
56 } // namespace xbt
57 } // namespace simgrid