1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmPropertyMap.h"
4 
5 #include <algorithm>
6 #include <utility>
7 
Clear()8 void cmPropertyMap::Clear()
9 {
10   this->Map_.clear();
11 }
12 
SetProperty(const std::string & name,const char * value)13 void cmPropertyMap::SetProperty(const std::string& name, const char* value)
14 {
15   if (!value) {
16     this->Map_.erase(name);
17     return;
18   }
19 
20   this->Map_[name] = value;
21 }
SetProperty(const std::string & name,cmValue value)22 void cmPropertyMap::SetProperty(const std::string& name, cmValue value)
23 {
24   if (!value) {
25     this->Map_.erase(name);
26     return;
27   }
28 
29   this->Map_[name] = *value;
30 }
31 
AppendProperty(const std::string & name,const std::string & value,bool asString)32 void cmPropertyMap::AppendProperty(const std::string& name,
33                                    const std::string& value, bool asString)
34 {
35   // Skip if nothing to append.
36   if (value.empty()) {
37     return;
38   }
39 
40   {
41     std::string& pVal = this->Map_[name];
42     if (!pVal.empty() && !asString) {
43       pVal += ';';
44     }
45     pVal += value;
46   }
47 }
48 
RemoveProperty(const std::string & name)49 void cmPropertyMap::RemoveProperty(const std::string& name)
50 {
51   this->Map_.erase(name);
52 }
53 
GetPropertyValue(const std::string & name) const54 cmValue cmPropertyMap::GetPropertyValue(const std::string& name) const
55 {
56   auto it = this->Map_.find(name);
57   if (it != this->Map_.end()) {
58     return cmValue(it->second);
59   }
60   return nullptr;
61 }
62 
GetKeys() const63 std::vector<std::string> cmPropertyMap::GetKeys() const
64 {
65   std::vector<std::string> keyList;
66   keyList.reserve(this->Map_.size());
67   for (auto const& item : this->Map_) {
68     keyList.push_back(item.first);
69   }
70   std::sort(keyList.begin(), keyList.end());
71   return keyList;
72 }
73 
GetList() const74 std::vector<std::pair<std::string, std::string>> cmPropertyMap::GetList() const
75 {
76   using StringPair = std::pair<std::string, std::string>;
77   std::vector<StringPair> kvList;
78   kvList.reserve(this->Map_.size());
79   for (auto const& item : this->Map_) {
80     kvList.emplace_back(item.first, item.second);
81   }
82   std::sort(kvList.begin(), kvList.end(),
83             [](StringPair const& a, StringPair const& b) {
84               return a.first < b.first;
85             });
86   return kvList;
87 }
88