• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

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

programming language


Commit MetaInfo

Revision02be6c45261925e49c25633b4e080a8acab761a9 (tree)
Time2020-11-23 19:38:14
Authordhrname <dhrname@user...>
Commiterdhrname

Log Message

Add the appendChild method and new node class

Change Summary

Incremental Difference

--- a/main.cpp
+++ b/main.cpp
@@ -52,6 +52,11 @@ public:
5252 virtual Node* appendChild(Node* const) = 0;
5353
5454 virtual bool isNode() = 0;
55+
56+ virtual bool isCaseOf(type_info&) = 0;
57+
58+ /*getValueメンバ関数は、多重定義を想定しているので、必ずしもNode*となるとは限らない*/
59+ virtual Node* getValue() = 0;
5560 };
5661
5762 /*空のノードクラス*/
@@ -73,8 +78,14 @@ public:
7378 Node* appendChild(Node*){return this;};
7479 bool isNode()
7580 {
76- return false;
77- }
81+ return false;
82+ };
83+ virtual bool isCaseOf(type_info& id)
84+ {
85+ return false;
86+ };
87+
88+ virtual Node* getValue(){return this;};
7889 };
7990
8091 EmptyNode* const emptynode = new EmptyNode();
@@ -83,7 +94,7 @@ EmptyNode* const emptynode = new EmptyNode();
8394 * Node抽象クラスに対する内部実装*/
8495 class BaseNode: public EmptyNode
8596 {
86-protected:
97+private:
8798 Node* parentNode;
8899 Node* nextSibling;
89100 Node* previousSibling;
@@ -244,25 +255,38 @@ Node* BaseNode::removeChild(Node* const child)
244255
245256 return child;
246257 }
258+
259+/*insertBefore メンバ関数
260+ * 子ノードのprevの前隣りに、nodeを挿入する
261+ * 引数prevが空ノードであれば、親ノードに末子ノードとして登録される
262+ *@param node
263+ *@param prev
264+ *@return 挿入されたnode
265+ **/
247266 Node* BaseNode::insertBefore(Node* const node, Node* const prev)
248267 {
249- this.throwArgumentError(node, "insertBefore");
268+ this->throwArgumentError(node, "insertBefore");
250269
251- /*prevはemptynodeの可能性が含まれる*/
252- this.throwNULLArgumentError(prev, "insertBefore");
270+ /*prevはemptynodeの可能性が含まれるので、throwArgumentErrorは使わない*/
271+ this->throwNULLArgumentError(prev, "insertBefore");
272+
273+ if (prev->getParent() != this)
274+ {
275+ throw std::invalid_argument("NotFoundError: prev is not child on the insertBefore");
276+ }
253277
254- /*祖先ノードの中に、今挿入しつつあるnodeと一致するものがあれば、参照エラー
278+ /*祖先ノードの中に、今挿入しつつあるnodeと一致するものがあれば、階層の参照エラー
255279 * というのは、循環参照を引き起こすため*/
256280 Node* p = this;
257281
258- while (p.isNode())
282+ while (p->isNode())
259283 {
260284 if (p == node)
261285 {
262- throw std::invalid_argument("Reference error on the insertBefore");
286+ throw std::invalid_argument("HierarchyRequestError on the insertBefore");
263287 }
264288
265- p = p->getParent(p);
289+ p = p->getParent();
266290 }
267291
268292 Node* pnode = node->getParent();
@@ -312,9 +336,24 @@ Node* BaseNode::insertBefore(Node* const node, Node* const prev)
312336 }
313337 Node* BaseNode::appendChild(Node* const node)
314338 {
315- return this->parentNode;
339+ return this->insertBefore(node, emptynode);
316340 }
317341
342+template<typename T>
343+class node
344+{
345+private:
346+ T& nodeValue;
347+ type_info valueType;
348+
349+public:
350+ node(T& value)
351+ : nodeValue(value),
352+ valueType(typeid(T))
353+ {}
354+
355+};
356+
318357 int main(int argc, char **argv)
319358 {
320359 std::string str1 = "ABC";