Skip to content

Commit e6f3c6c

Browse files
guyingzhaonikias
authored andcommitted
C++: Array: Add const Node& variants to Append, Insert
1 parent ed8a733 commit e6f3c6c

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

include/plist/Array.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,18 @@ public :
5252
const_iterator End() const;
5353
const_iterator end() const;
5454
size_t size() const;
55-
void Append(Node* node);
56-
void Insert(Node* node, unsigned int pos);
55+
void Append(const Node& node);
56+
void Append(const Node* node);
57+
void Insert(const Node& node, unsigned int pos);
58+
void Insert(const Node* node, unsigned int pos);
5759
void Remove(Node* node);
5860
void Remove(unsigned int pos);
59-
unsigned int GetNodeIndex(Node* node) const;
60-
template <typename T>
61-
T* at(unsigned int index)
62-
{
61+
unsigned int GetNodeIndex(const Node& node) const;
62+
unsigned int GetNodeIndex(const Node* node) const;
63+
template <typename T> T* at(unsigned int index) {
6364
return (T*)(_array.at(index));
6465
}
65-
template <typename T>
66-
T* At(unsigned int index)
67-
{
66+
template <typename T> T* At(unsigned int index) {
6867
return (T*)(_array.at(index));
6968
}
7069

src/Array.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ size_t Array::size() const {
134134
return _array.size();
135135
}
136136

137-
void Array::Append(Node* node)
137+
void Array::Append(const Node* node)
138138
{
139139
if (node)
140140
{
@@ -145,7 +145,12 @@ void Array::Append(Node* node)
145145
}
146146
}
147147

148-
void Array::Insert(Node* node, unsigned int pos)
148+
void Array::Append(const Node& node)
149+
{
150+
Append(&node);
151+
}
152+
153+
void Array::Insert(const Node* node, unsigned int pos)
149154
{
150155
if (node)
151156
{
@@ -158,6 +163,11 @@ void Array::Insert(Node* node, unsigned int pos)
158163
}
159164
}
160165

166+
void Array::Insert(const Node &node, unsigned int pos)
167+
{
168+
Insert(&node, pos);
169+
}
170+
161171
void Array::Remove(Node* node)
162172
{
163173
if (node)
@@ -183,10 +193,15 @@ void Array::Remove(unsigned int pos)
183193
_array.erase(it);
184194
}
185195

186-
unsigned int Array::GetNodeIndex(Node* node) const
196+
unsigned int Array::GetNodeIndex(const Node* node) const
187197
{
188198
std::vector<Node*>::const_iterator it = std::find(_array.begin(), _array.end(), node);
189199
return std::distance (_array.begin(), it);
190200
}
191201

202+
unsigned int Array::GetNodeIndex(const Node& node) const
203+
{
204+
return GetNodeIndex(&node);
205+
}
206+
192207
} // namespace PList

0 commit comments

Comments
 (0)