1 | /*! |
---|
2 | @file IdentifierList.cc |
---|
3 | @brief Implementation of the IdentifierList class. |
---|
4 | */ |
---|
5 | |
---|
6 | #include "IdentifierList.h" |
---|
7 | #include "Identifier.h" |
---|
8 | |
---|
9 | namespace orxonox |
---|
10 | { |
---|
11 | // ############################### |
---|
12 | // ### IdentifierList ### |
---|
13 | // ############################### |
---|
14 | /** |
---|
15 | @brief Constructor: Sets first_ to zero. |
---|
16 | */ |
---|
17 | IdentifierList::IdentifierList() |
---|
18 | { |
---|
19 | this->first_ = 0; |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | @brief Destructor: Deletes all elements in the list, but NOT THE IDENTIFIERS. |
---|
24 | */ |
---|
25 | IdentifierList::~IdentifierList() |
---|
26 | { |
---|
27 | IdentifierListElement* temp; |
---|
28 | while (this->first_) |
---|
29 | { |
---|
30 | temp = this->first_->next_; |
---|
31 | delete this->first_; |
---|
32 | this->first_ = temp; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | @brief Adds an Identifier to the list. |
---|
38 | @param identifier The Identifier to add |
---|
39 | */ |
---|
40 | void IdentifierList::add(const Identifier* identifier) |
---|
41 | { |
---|
42 | IdentifierListElement* temp = this->first_; |
---|
43 | this->first_ = new IdentifierListElement(identifier); |
---|
44 | this->first_->next_ = temp; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | @brief Removes an Identifier from the list. |
---|
49 | @param identifier The Identifier to remove |
---|
50 | */ |
---|
51 | void IdentifierList::remove(const Identifier* identifier) |
---|
52 | { |
---|
53 | if (!identifier) |
---|
54 | return; |
---|
55 | |
---|
56 | // Check if we have to delete the first element |
---|
57 | if (this->first_->identifier_ == identifier) |
---|
58 | { |
---|
59 | IdentifierListElement* temp = this->first_->next_; |
---|
60 | delete this->first_; |
---|
61 | this->first_ = temp; |
---|
62 | |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | // Iterate through the list |
---|
67 | IdentifierListElement* temp = this->first_; |
---|
68 | while (temp->next_) |
---|
69 | { |
---|
70 | if (temp->next_->identifier_ == identifier) |
---|
71 | { |
---|
72 | IdentifierListElement* temp2 = temp->next_->next_; |
---|
73 | delete temp->next_; |
---|
74 | temp->next_ = temp2; |
---|
75 | |
---|
76 | return; |
---|
77 | } |
---|
78 | |
---|
79 | temp = temp->next_; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | @brief Checks if a given Identifier is in the list and returns true if yes. |
---|
85 | @param identifier The Identifier to check |
---|
86 | @return True if the Identifier is in the list |
---|
87 | */ |
---|
88 | bool IdentifierList::isInList(const Identifier* identifier) const |
---|
89 | { |
---|
90 | IdentifierListElement* temp = this->first_; |
---|
91 | while (temp) |
---|
92 | { |
---|
93 | if (temp->identifier_ == identifier) |
---|
94 | return true; |
---|
95 | |
---|
96 | temp = temp->next_; |
---|
97 | } |
---|
98 | |
---|
99 | return false; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | @returns a string, containing the names of all Identifiers in the list. |
---|
104 | */ |
---|
105 | std::string IdentifierList::toString() const |
---|
106 | { |
---|
107 | IdentifierListElement* temp = this->first_; |
---|
108 | std::string output = ""; |
---|
109 | |
---|
110 | while (temp) |
---|
111 | { |
---|
112 | output += temp->identifier_->getName(); |
---|
113 | output += " "; |
---|
114 | |
---|
115 | temp = temp->next_; |
---|
116 | } |
---|
117 | |
---|
118 | return output; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | // ############################### |
---|
123 | // ### IdentifierListElement ### |
---|
124 | // ############################### |
---|
125 | /** |
---|
126 | @brief Constructor: Creates the list-element with a given identifier. |
---|
127 | @param identifier The Identifier to store |
---|
128 | */ |
---|
129 | IdentifierListElement::IdentifierListElement(const Identifier* identifier) |
---|
130 | { |
---|
131 | this->identifier_ = identifier; |
---|
132 | this->next_ = 0; |
---|
133 | } |
---|
134 | } |
---|