Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document new feature for disks
[simgrid.git] / src / surf / PropertyHolder.cpp
1 /* Copyright (c) 2015-2019. 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 "PropertyHolder.hpp"
7
8 #include <map>
9
10 namespace simgrid {
11 namespace surf {
12
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
15 {
16   if (not properties_)
17     return nullptr;
18   auto prop = properties_->find(key);
19   return prop == properties_->end() ? nullptr : prop->second.c_str();
20 }
21
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)
24 {
25   if (not properties_)
26     properties_.reset(new std::unordered_map<std::string, std::string>);
27   (*properties_)[key] = value;
28 }
29
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()
32 {
33   if (not properties_)
34     properties_.reset(new std::unordered_map<std::string, std::string>);
35   return properties_.get();
36 }
37
38 /** @brief Change the value of the given keys in the property set */
39 template <class Assoc> void PropertyHolder::set_properties(const Assoc& properties)
40 {
41   if (not properties_)
42     properties_.reset(new std::unordered_map<std::string, std::string>);
43   std::unordered_map<std::string, std::string> props(properties.cbegin(), properties.cend());
44 #if __cplusplus >= 201703L
45   props.merge(properties_);
46 #else
47   props.insert(properties_->cbegin(), properties_->cend());
48 #endif
49   properties_->swap(props);
50 }
51
52 template void PropertyHolder::set_properties(const std::map<std::string, std::string>& properties);
53 template void PropertyHolder::set_properties(const std::unordered_map<std::string, std::string>& properties);
54
55 } /* namespace surf */
56 } /* namespace simgrid */