1//
2// Attributes.cpp
3//
4// Library: CppParser
5// Package: Attributes
6// Module: Attributes
7//
8// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/CppParser/Attributes.h"
16#include "Poco/NumberParser.h"
17#include "Poco/Exception.h"
18#include <algorithm>
19
20
21using Poco::NumberParser;
22
23
24namespace Poco {
25namespace CppParser {
26
27
28Attributes::Attributes()
29{
30}
31
32
33Attributes::Attributes(const Attributes& attrs)
34{
35 _map = attrs._map;
36}
37
38
39Attributes::~Attributes()
40{
41}
42
43
44Attributes& Attributes::operator = (const Attributes& attrs)
45{
46 AttrMap map(attrs._map);
47 std::swap(_map, map);
48 return *this;
49}
50
51
52bool Attributes::has(const std::string& name) const
53{
54 return _map.find(name) != _map.end();
55}
56
57
58std::string Attributes::getString(const std::string& name) const
59{
60 AttrMap::const_iterator it = _map.find(name);
61 if (it != _map.end())
62 return it->second;
63 else
64 throw Poco::NotFoundException(name);
65}
66
67
68std::string Attributes::getString(const std::string& name, const std::string& defaultValue) const
69{
70 AttrMap::const_iterator it = _map.find(name);
71 if (it != _map.end())
72 return it->second;
73 else
74 return defaultValue;
75}
76
77
78int Attributes::getInt(const std::string& name) const
79{
80 AttrMap::const_iterator it = _map.find(name);
81 if (it != _map.end())
82 return NumberParser::parse(it->second);
83 else
84 throw Poco::NotFoundException(name);
85}
86
87
88int Attributes::getInt(const std::string& name, int defaultValue) const
89{
90 AttrMap::const_iterator it = _map.find(name);
91 if (it != _map.end())
92 return NumberParser::parse(it->second);
93 else
94 return defaultValue;
95}
96
97
98bool Attributes::getBool(const std::string& name) const
99{
100 AttrMap::const_iterator it = _map.find(name);
101 if (it != _map.end())
102 return it->second != "false";
103 else
104 throw Poco::NotFoundException(name);
105}
106
107
108bool Attributes::getBool(const std::string& name, bool defaultValue) const
109{
110 AttrMap::const_iterator it = _map.find(name);
111 if (it != _map.end())
112 return it->second != "false";
113 else
114 return defaultValue;
115}
116
117
118void Attributes::set(const std::string& name, const std::string& value)
119{
120 _map[name] = value;
121}
122
123
124void Attributes::remove(const std::string& name)
125{
126 AttrMap::iterator it = _map.find(name);
127 if (it != _map.end())
128 _map.erase(it);
129}
130
131
132const std::string& Attributes::operator [] (const std::string& name) const
133{
134 AttrMap::const_iterator it = _map.find(name);
135 if (it != _map.end())
136 return it->second;
137 else
138 throw Poco::NotFoundException(name);
139}
140
141
142std::string& Attributes::operator [] (const std::string& name)
143{
144 return _map[name];
145}
146
147
148void Attributes::clear()
149{
150 _map.clear();
151}
152
153
154} } // namespace Poco::CppParser
155