1 /* Copyright (c) 2015-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include <xbt/PropertyHolder.hpp>
11 namespace simgrid::xbt {
13 /** @brief Return the property associated to the provided key (or nullptr if not existing) */
14 const char* PropertyHolder::get_property(const std::string& key) const
18 auto prop = properties_->find(key);
19 return prop == properties_->end() ? nullptr : prop->second.c_str();
22 /** @brief Change the value of a given key in the property set */
23 void PropertyHolder::set_property(const std::string& key, const std::string& value)
26 properties_ = std::make_unique<std::unordered_map<std::string, std::string>>();
27 (*properties_)[key] = value;
30 /** @brief Return the whole set of properties. Don't mess with it, dude! */
31 const std::unordered_map<std::string, std::string>* PropertyHolder::get_properties()
34 properties_ = std::make_unique<std::unordered_map<std::string, std::string>>();
35 return properties_.get();
38 /** @brief Change the value of the given keys in the property set */
39 template <class Assoc> void PropertyHolder::set_properties(const Assoc& properties)
42 properties_ = std::make_unique<std::unordered_map<std::string, std::string>>();
43 std::unordered_map<std::string, std::string> props(std::cbegin(properties), std::cend(properties));
44 #if __cplusplus >= 201703L
45 props.merge(*properties_);
47 props.insert(properties_->cbegin(), properties_->cend());
49 properties_->swap(props);
52 template void PropertyHolder::set_properties(const std::map<std::string, std::string, std::less<>>& properties);
53 template void PropertyHolder::set_properties(const std::unordered_map<std::string, std::string>& properties);
55 } // namespace simgrid::xbt