-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTools.cpp
205 lines (153 loc) · 5.18 KB
/
Tools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
////////////////////////////////////////////////////////////////////////////////
//! \file Tools.cpp
//! \brief The Tools class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Tools.hpp"
#include <XML/XPathIterator.hpp>
#include <XML/TextNode.hpp>
#include <Core/ConfigurationException.hpp>
#include <set>
#include <Core/Algorithm.hpp>
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
Tools::Tools()
: m_modified(false)
, m_tools()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
Tools::~Tools()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Get the number of tools.
size_t Tools::size() const
{
return m_tools.size();
}
////////////////////////////////////////////////////////////////////////////////
//! Get the tool at a given position.
const ConstToolPtr& Tools::tool(size_t index) const
{
ASSERT(index < m_tools.size());
return m_tools[index];
}
////////////////////////////////////////////////////////////////////////////////
//! Has the collection of tools been modified?
bool Tools::isModified() const
{
return m_modified;
}
////////////////////////////////////////////////////////////////////////////////
//! Get the collection start iterator.
Tools::const_iterator Tools::begin() const
{
return m_tools.begin();
}
////////////////////////////////////////////////////////////////////////////////
//! Get the collection end iterator.
Tools::const_iterator Tools::end() const
{
return m_tools.end();
}
////////////////////////////////////////////////////////////////////////////////
//! Add a tool to the collection.
void Tools::append(ConstToolPtr tool)
{
m_tools.push_back(tool);
m_modified = true;
}
////////////////////////////////////////////////////////////////////////////////
//! Replace a tool in the collection.
void Tools::replace(size_t index, ConstToolPtr tool)
{
ASSERT(index < m_tools.size());
m_tools[index] = tool;
m_modified = true;
}
////////////////////////////////////////////////////////////////////////////////
//! Remove a tool from the collection.
void Tools::remove(size_t index)
{
ASSERT(index < m_tools.size());
m_tools.erase(m_tools.begin() + index);
m_modified = true;
}
////////////////////////////////////////////////////////////////////////////////
//! Swap two tools in the collection by index.
void Tools::swap(size_t first, size_t second)
{
ASSERT(first < m_tools.size());
ASSERT(second < m_tools.size());
ConstToolPtr temp = m_tools[first];
m_tools[first] = m_tools[second];
m_tools[second] = temp;
m_modified = true;
}
////////////////////////////////////////////////////////////////////////////////
//! Load the set of tools from the XML document.
void Tools::load(const XML::DocumentPtr config)
{
ASSERT(config->hasRootElement());
Collection tools;
std::set<tstring> names;
XML::XPathIterator it(TXT("/FarmMonitor/Tools/Tool"), config->getRootElement());
XML::XPathIterator end;
for(; it != end; ++it)
{
XML::ElementNodePtr node = Core::dynamic_ptr_cast<XML::ElementNode>(*it);
const tstring name = node->findFirstElement(TXT("Name"))->getTextValue();
if (name.empty())
throw Core::ConfigurationException(TXT("The name for a tool cannot be empty"));
if (Core::exists(names, name))
throw Core::ConfigurationException(TXT("The name for a tool cannot be duplicated"));
const tstring cmdLine = node->findFirstElement(TXT("CommandLine"))->getTextValue();
if (cmdLine.empty())
throw Core::ConfigurationException(TXT("The command line for a tool cannot be empty"));
tools.push_back(makeTool(name, cmdLine));
names.insert(name);
}
std::swap(m_tools, tools);
m_modified = false;
}
////////////////////////////////////////////////////////////////////////////////
//! Save the set of tools to the XML document.
void Tools::save(XML::DocumentPtr config)
{
XML::XPathIterator it(TXT("/FarmMonitor/Tools"), config->getRootElement());
XML::ElementNodePtr tools(Core::dynamic_ptr_cast<XML::ElementNode>(*it));
for (size_t i = 0; i != m_tools.size(); ++i)
{
XML::ElementNodePtr name = XML::makeElement(TXT("Name"), XML::makeText(m_tools[i]->m_name));
XML::ElementNodePtr cmdLine = XML::makeElement(TXT("CommandLine"), XML::makeText(m_tools[i]->m_commandLine));
XML::ElementNodePtr tool = XML::makeElement(TXT("Tool"));
tool->appendChild(name);
tool->appendChild(cmdLine);
tools->appendChild(tool);
}
m_modified = false;
}
////////////////////////////////////////////////////////////////////////////////
//! Create a deep copy of another collection.
void Tools::deepCopy(const Tools& rhs)
{
deepCopy(rhs, false);
}
////////////////////////////////////////////////////////////////////////////////
//! Replace the entire collection with another.
void Tools::replaceAll(const Tools& rhs)
{
deepCopy(rhs, true);
}
////////////////////////////////////////////////////////////////////////////////
//! Create a deep copy of another collection.
void Tools::deepCopy(const Tools& rhs, bool modified)
{
Collection tools;
for (const_iterator it = rhs.begin(); it != rhs.end(); ++it)
tools.push_back(makeTool(*(*it)));
std::swap(m_tools, tools);
m_modified = modified;
}