Revisão | beaef01972d35e24e8f0d7f56d6ddadc30a86f09 (tree) |
---|---|
Hora | 2020-11-22 20:52:21 |
Autor | dhrname <dhrname@user...> |
Commiter | dhrname |
Add the throwNULLArgument
@@ -23,6 +23,8 @@ | ||
23 | 23 | |
24 | 24 | #include <iostream> |
25 | 25 | #include <string> |
26 | +#include <typeinfo> | |
27 | +#include <stdexcept> | |
26 | 28 | |
27 | 29 | /*Nodeの抽象クラス |
28 | 30 | * 5方向リンクのリスト |
@@ -31,13 +33,24 @@ class Node | ||
31 | 33 | { |
32 | 34 | public: |
33 | 35 | virtual Node* getParent() = 0; |
36 | + virtual void setParent(Node*) = 0; | |
37 | + | |
34 | 38 | virtual Node* getNext() = 0; |
39 | + virtual void setNext(Node*) = 0; | |
40 | + | |
35 | 41 | virtual Node* getPrev() = 0; |
42 | + virtual void setPrev(Node*) = 0; | |
43 | + | |
36 | 44 | virtual Node* getFirstChild() = 0; |
45 | + virtual void setFirstChild(Node*) = 0; | |
46 | + | |
37 | 47 | virtual Node* getLastChild() = 0; |
38 | - virtual Node* removeChild(Node*) = 0; | |
39 | - virtual Node* insertBefore(Node*) = 0; | |
40 | - virtual Node* appendChild(Node*) = 0; | |
48 | + virtual void setLastChild(Node*) = 0; | |
49 | + | |
50 | + virtual Node* removeChild(Node* const) = 0; | |
51 | + virtual Node* insertBefore(Node* const, Node* const) = 0; | |
52 | + virtual Node* appendChild(Node* const) = 0; | |
53 | + | |
41 | 54 | virtual bool isNode() = 0; |
42 | 55 | }; |
43 | 56 |
@@ -45,13 +58,18 @@ public: | ||
45 | 58 | class EmptyNode: public Node |
46 | 59 | { |
47 | 60 | public: |
48 | - Node* getParent(){return this;}; | |
49 | - Node* getNext(){return this;}; | |
50 | - Node* getPrev(){return this;}; | |
51 | - Node* getFirstChild(){return this;}; | |
52 | - Node* getLastChild(){return this;}; | |
61 | + Node* getParent() {return this;}; | |
62 | + Node* getNext() {return this;}; | |
63 | + Node* getPrev() {return this;}; | |
64 | + Node* getFirstChild() {return this;}; | |
65 | + Node* getLastChild() {return this;}; | |
66 | + void setParent(Node* node){}; | |
67 | + void setNext(Node* node){}; | |
68 | + void setPrev(Node* node){}; | |
69 | + void setFirstChild(Node* node){}; | |
70 | + void setLastChild(Node* node){}; | |
53 | 71 | Node* removeChild(Node*){return this;}; |
54 | - Node* insertBefore(Node*){return this;}; | |
72 | + Node* insertBefore(Node*, Node*){return this;}; | |
55 | 73 | Node* appendChild(Node*){return this;}; |
56 | 74 | bool isNode() |
57 | 75 | { |
@@ -59,13 +77,13 @@ public: | ||
59 | 77 | } |
60 | 78 | }; |
61 | 79 | |
62 | -Node* const emptynode = new EmptyNode(); | |
80 | +EmptyNode* const emptynode = new EmptyNode(); | |
63 | 81 | |
64 | 82 | /*BaseNodeの基底クラス |
65 | 83 | * Node抽象クラスに対する内部実装*/ |
66 | 84 | class BaseNode: public EmptyNode |
67 | 85 | { |
68 | -private: | |
86 | +protected: | |
69 | 87 | Node* parentNode; |
70 | 88 | Node* nextSibling; |
71 | 89 | Node* previousSibling; |
@@ -83,39 +101,81 @@ public: | ||
83 | 101 | |
84 | 102 | virtual ~BaseNode(){} |
85 | 103 | |
86 | - virtual void setParent(Node* node); | |
87 | - virtual void setNext(Node* node); | |
88 | - virtual void setPrev(Node* node); | |
89 | - virtual void setFirstChild(Node* node); | |
90 | - virtual void setLastChild(Node* node); | |
104 | + virtual void throwNULLArgumentError(Node* const, const std::string&); | |
105 | + virtual void throwArgumentError(Node* const, const std::string&); | |
106 | + | |
107 | + virtual void setParent(Node*); | |
108 | + virtual void setNext(Node*); | |
109 | + virtual void setPrev(Node*); | |
110 | + virtual void setFirstChild(Node*); | |
111 | + virtual void setLastChild(Node*); | |
112 | + | |
91 | 113 | virtual Node* getParent(); |
92 | 114 | virtual Node* getNext(); |
93 | 115 | virtual Node* getPrev(); |
94 | 116 | virtual Node* getFirstChild(); |
95 | 117 | virtual Node* getLastChild(); |
118 | + | |
96 | 119 | virtual Node* removeChild(Node*); |
97 | - virtual Node* insertBefore(Node*); | |
120 | + virtual Node* insertBefore(Node*, Node*); | |
98 | 121 | virtual Node* appendChild(Node*); |
99 | - virtual bool isNode() | |
122 | + | |
123 | + virtual bool isNode() const | |
100 | 124 | { |
101 | 125 | return true; |
102 | 126 | } |
103 | 127 | }; |
104 | 128 | |
129 | +/*throwNULLArgumentError 関数 | |
130 | + * 引数のNULLチェックをして、nullptrだったら例外を投げる | |
131 | + * @param node 木構造の節 | |
132 | + * @param name 引数の例外が発生したメンバ関数名*/ | |
133 | +inline void BaseNode::throwNULLArgumentError(Node* const node, const std::string& name) | |
134 | +{ | |
135 | + if (nullptr == node) | |
136 | + { | |
137 | + /*バグの放置を防ぐため、例外を投げる前に、エラーを出力して報告*/ | |
138 | + std::cerr << "NULL Argument Error on the member " << name << std::endl; | |
139 | + throw std::invalid_argument("error!"); | |
140 | + } | |
141 | +} | |
105 | 142 | |
143 | +/*throwArgumentError 関数 | |
144 | + * 引数のチェックをして、nullptrか、emptynode(空ノード)だったら例外を投げる | |
145 | + * @param node 木構造の節 | |
146 | + * @param str 引数の例外が発生したメンバ関数名*/ | |
147 | +inline void BaseNode::throwArgumentError(Node* const node, const std::string& name) | |
148 | +{ | |
149 | + this->throwNULLArgumentError(node, name); | |
150 | + if ( node->isNode() ) | |
151 | + { | |
152 | + /*バグの放置を防ぐため、例外を投げる前に、エラーを出力して報告*/ | |
153 | + std::cerr << "Argument Error on the member " << name << std::endl; | |
154 | + throw std::invalid_argument("error!"); | |
155 | + } | |
156 | +} | |
106 | 157 | |
107 | -void BaseNode::setParent(Node* node){ | |
158 | +void BaseNode::setParent(Node* node) | |
159 | +{ | |
160 | + this->throwNULLArgumentError(node, "setParent"); | |
108 | 161 | this->parentNode = node; |
109 | 162 | } |
110 | -void BaseNode::setNext(Node* node){ | |
163 | + | |
164 | +void BaseNode::setNext(Node* node) | |
165 | +{ | |
111 | 166 | this->nextSibling = node; |
112 | 167 | } |
113 | -void BaseNode::setPrev(Node* node){ | |
168 | + | |
169 | +void BaseNode::setPrev(Node* node) | |
170 | +{ | |
114 | 171 | this->previousSibling = node; |
115 | 172 | } |
116 | -void BaseNode::setFirstChild(Node* node){ | |
173 | + | |
174 | +void BaseNode::setFirstChild(Node* node) | |
175 | +{ | |
117 | 176 | this->firstChild = node; |
118 | 177 | } |
178 | + | |
119 | 179 | void BaseNode::setLastChild(Node* node){ |
120 | 180 | this->lastChild = node; |
121 | 181 | } |
@@ -124,6 +184,7 @@ Node* BaseNode::getParent() | ||
124 | 184 | { |
125 | 185 | return this->parentNode; |
126 | 186 | } |
187 | + | |
127 | 188 | Node* BaseNode::getNext() |
128 | 189 | { |
129 | 190 | return this->nextSibling; |
@@ -140,11 +201,34 @@ Node* BaseNode::getLastChild() | ||
140 | 201 | { |
141 | 202 | return this->lastChild; |
142 | 203 | } |
143 | -Node* BaseNode::removeChild(Node* const node) | |
204 | +Node* BaseNode::removeChild(Node* const child) | |
144 | 205 | { |
145 | - return this->parentNode; | |
206 | + this->throwArgumentError(child, "removeChild"); | |
207 | + | |
208 | + child->setParent(emptynode); | |
209 | + | |
210 | + /*nodeが抜けた後、隣接ノードに関するメンバは書き換えておく*/ | |
211 | + if (child->getPrev()->isNode()) | |
212 | + { | |
213 | + child->getPrev()->setNext(child->getNext()); | |
214 | + } | |
215 | + if (child->getNext()->isNode()) | |
216 | + { | |
217 | + child->getNext()->setPrev(child->getPrev()); | |
218 | + } | |
219 | + | |
220 | + if (this->getLastChild() == child) | |
221 | + { | |
222 | + /*末尾ノードがchildである場合は自分のlastChildメンバを書きかえておく*/ | |
223 | + this->lastChild = child->getPrev(); | |
224 | + } | |
225 | + | |
226 | + child->setNext(emptynode); | |
227 | + child->setPrev(emptynode); | |
228 | + | |
229 | + return child; | |
146 | 230 | } |
147 | -Node* BaseNode::insertBefore(Node* const node) | |
231 | +Node* BaseNode::insertBefore(Node* const node, Node* const prev) | |
148 | 232 | { |
149 | 233 | return this->parentNode; |
150 | 234 | } |
@@ -155,7 +239,14 @@ Node* BaseNode::appendChild(Node* const node) | ||
155 | 239 | |
156 | 240 | int main(int argc, char **argv) |
157 | 241 | { |
158 | - std::string str1 = "ABCD"; | |
159 | - std::cout << str1 << std::endl; | |
242 | + std::string str1 = "ABC"; | |
243 | + std::cout << "A" << str1 << std::endl; | |
244 | + try | |
245 | + { | |
246 | + } | |
247 | + catch(std::invalid_argument e) | |
248 | + { | |
249 | + } | |
250 | + delete emptynode; | |
160 | 251 | return 0; |
161 | 252 | } |