• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

コミットメタ情報

リビジョンbeaef01972d35e24e8f0d7f56d6ddadc30a86f09 (tree)
日時2020-11-22 20:52:21
作者dhrname <dhrname@user...>
コミッターdhrname

ログメッセージ

Add the throwNULLArgument

変更サマリ

差分

--- a/joestar/cpp/main.cpp
+++ b/joestar/cpp/main.cpp
@@ -23,6 +23,8 @@
2323
2424 #include <iostream>
2525 #include <string>
26+#include <typeinfo>
27+#include <stdexcept>
2628
2729 /*Nodeの抽象クラス
2830 * 5方向リンクのリスト
@@ -31,13 +33,24 @@ class Node
3133 {
3234 public:
3335 virtual Node* getParent() = 0;
36+ virtual void setParent(Node*) = 0;
37+
3438 virtual Node* getNext() = 0;
39+ virtual void setNext(Node*) = 0;
40+
3541 virtual Node* getPrev() = 0;
42+ virtual void setPrev(Node*) = 0;
43+
3644 virtual Node* getFirstChild() = 0;
45+ virtual void setFirstChild(Node*) = 0;
46+
3747 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+
4154 virtual bool isNode() = 0;
4255 };
4356
@@ -45,13 +58,18 @@ public:
4558 class EmptyNode: public Node
4659 {
4760 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){};
5371 Node* removeChild(Node*){return this;};
54- Node* insertBefore(Node*){return this;};
72+ Node* insertBefore(Node*, Node*){return this;};
5573 Node* appendChild(Node*){return this;};
5674 bool isNode()
5775 {
@@ -59,13 +77,13 @@ public:
5977 }
6078 };
6179
62-Node* const emptynode = new EmptyNode();
80+EmptyNode* const emptynode = new EmptyNode();
6381
6482 /*BaseNodeの基底クラス
6583 * Node抽象クラスに対する内部実装*/
6684 class BaseNode: public EmptyNode
6785 {
68-private:
86+protected:
6987 Node* parentNode;
7088 Node* nextSibling;
7189 Node* previousSibling;
@@ -83,39 +101,81 @@ public:
83101
84102 virtual ~BaseNode(){}
85103
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+
91113 virtual Node* getParent();
92114 virtual Node* getNext();
93115 virtual Node* getPrev();
94116 virtual Node* getFirstChild();
95117 virtual Node* getLastChild();
118+
96119 virtual Node* removeChild(Node*);
97- virtual Node* insertBefore(Node*);
120+ virtual Node* insertBefore(Node*, Node*);
98121 virtual Node* appendChild(Node*);
99- virtual bool isNode()
122+
123+ virtual bool isNode() const
100124 {
101125 return true;
102126 }
103127 };
104128
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+}
105142
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+}
106157
107-void BaseNode::setParent(Node* node){
158+void BaseNode::setParent(Node* node)
159+{
160+ this->throwNULLArgumentError(node, "setParent");
108161 this->parentNode = node;
109162 }
110-void BaseNode::setNext(Node* node){
163+
164+void BaseNode::setNext(Node* node)
165+{
111166 this->nextSibling = node;
112167 }
113-void BaseNode::setPrev(Node* node){
168+
169+void BaseNode::setPrev(Node* node)
170+{
114171 this->previousSibling = node;
115172 }
116-void BaseNode::setFirstChild(Node* node){
173+
174+void BaseNode::setFirstChild(Node* node)
175+{
117176 this->firstChild = node;
118177 }
178+
119179 void BaseNode::setLastChild(Node* node){
120180 this->lastChild = node;
121181 }
@@ -124,6 +184,7 @@ Node* BaseNode::getParent()
124184 {
125185 return this->parentNode;
126186 }
187+
127188 Node* BaseNode::getNext()
128189 {
129190 return this->nextSibling;
@@ -140,11 +201,34 @@ Node* BaseNode::getLastChild()
140201 {
141202 return this->lastChild;
142203 }
143-Node* BaseNode::removeChild(Node* const node)
204+Node* BaseNode::removeChild(Node* const child)
144205 {
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;
146230 }
147-Node* BaseNode::insertBefore(Node* const node)
231+Node* BaseNode::insertBefore(Node* const node, Node* const prev)
148232 {
149233 return this->parentNode;
150234 }
@@ -155,7 +239,14 @@ Node* BaseNode::appendChild(Node* const node)
155239
156240 int main(int argc, char **argv)
157241 {
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;
160251 return 0;
161252 }