fork download
  1.  
  2. /*!
  3.  * \brief ノードと位置の関係を表現するクラスです。
  4.  */
  5. class Point
  6. {
  7. public:
  8. enum : int { Open = -1, Close = -2, Null = -3, };
  9.  
  10. public:
  11. Point();
  12. Point(QDomNode const &node, int point = Open);
  13.  
  14. int position() const;
  15. void clear();
  16.  
  17. bool operator ==(Point const &rhs) const;
  18. QDomNode& operator *();
  19. QDomNode const& operator *() const;
  20. QDomNode* operator ->();
  21. QDomNode const* operator ->() const;
  22.  
  23. private:
  24. QDomNode m_node;
  25. int m_point;
  26. };
  27.  
  28. /*!
  29.  * \brief QDomNodeの木構造に対するイテレータです。
  30.  *
  31.  * 木構造を文書内の出現順に巡回します。
  32.  * 要素ノードは開始タグと終了タグの位置でマークします。
  33.  *
  34.  * 要素ノードの終わり(終了タグ)は、NULLノードを返します。
  35.  * TEXTノードの終わりは、NULLノードを返しません。
  36.  *
  37.  * 内容を持たない要素であっても、終了タグがあるかのようにふるまいます。
  38.  * 派生クラスでフィルター可能であること、終了マーカは深さ方向の移動時しか記録が必要ない
  39.  * のでコストが低いことから、この動作を採用しました。
  40.  */
  41. class NodeIterator
  42. {
  43. public:
  44. NodeIterator();
  45. NodeIterator(QDomNode const &root);
  46.  
  47. NodeIterator& operator ++();
  48. NodeIterator operator ++(int);
  49.  
  50. bool operator ==(NodeIterator const &rhs) const;
  51. bool operator !=(NodeIterator const &rhs) const;
  52.  
  53. QDomNode& operator *();
  54. QDomNode const& operator *() const;
  55. QDomNode* operator ->();
  56.  
  57. Point& point();
  58.  
  59. private:
  60. NodeIterator(Point const &point);
  61. Point const current() const;
  62.  
  63. private:
  64. QStack<Point> m_stack;
  65. QDomNode m_null;
  66. };
  67.  
  68. TM::DOM::Point::Point()
  69. : m_point(Null)
  70. {
  71.  
  72. }
  73.  
  74. TM::DOM::Point::Point(QDomNode const &node, int point)
  75. : m_node(node)
  76. , m_point(point)
  77. {
  78.  
  79. }
  80.  
  81. int TM::DOM::Point::position() const
  82. {
  83. return m_point;
  84. }
  85.  
  86. void TM::DOM::Point::clear()
  87. {
  88. m_point = Null;
  89. m_node.clear();
  90. }
  91.  
  92. bool TM::DOM::Point::operator ==(Point const &rhs) const
  93. {
  94. return m_node == rhs.m_node && m_point == rhs.m_point;
  95. }
  96.  
  97. QDomNode& TM::DOM::Point::operator *()
  98. {
  99. return m_node;
  100. }
  101.  
  102. QDomNode const& TM::DOM::Point::operator *() const
  103. {
  104. return m_node;
  105. }
  106.  
  107. QDomNode* TM::DOM::Point::operator ->()
  108. {
  109. return &m_node;
  110. }
  111.  
  112. QDomNode const* TM::DOM::Point::operator ->() const
  113. {
  114. return &m_node;
  115. }
  116.  
  117. // NodeIterator ---------------------------------------------------------------
  118.  
  119. /*!
  120.  * \brief 末尾を示すイテレータを作成します。
  121.  */
  122. TM::DOM::NodeIterator::NodeIterator()
  123. {
  124.  
  125. }
  126.  
  127. /*!
  128.  * \brief 引数として与えられるノードから始まるイテレータを作成します。
  129.  * \param root 最初のノード。
  130.  */
  131. TM::DOM::NodeIterator::NodeIterator(QDomNode const &root)
  132. {
  133. m_stack.push(Point(root, Point::Open));
  134. }
  135.  
  136. /*!
  137.  * \brief 前置インクリメントです。
  138.  * \return インクリメントした後、自身への参照を返します。
  139.  */
  140. TM::DOM::NodeIterator& TM::DOM::NodeIterator::operator ++()
  141. {
  142. Point current = m_stack.pop();
  143. Q_ASSERT(current.position() != Point::Null);
  144. if(current.position() == Point::Open)
  145. {
  146. if(current->isElement()) m_stack.push(Point(*current, Point::Close)); // 終了マーカ
  147. QDomNode node = current->lastChild();
  148. while(!node.isNull())
  149. {
  150. m_stack.push(Point(node, Point::Open));
  151. node = node.previousSibling();
  152. }
  153. }
  154.  
  155. return *this;
  156. }
  157.  
  158. /*!
  159.  * \brief 後置インクリメントです。
  160.  * \return インクリメントする前の自身のコピーを返します。
  161.  */
  162. TM::DOM::NodeIterator TM::DOM::NodeIterator::operator ++(int)
  163. {
  164. NodeIterator self(current());
  165. ++(*this);
  166. return self;
  167. }
  168.  
  169. bool TM::DOM::NodeIterator::operator ==(NodeIterator const &rhs) const
  170. {
  171. return current() == rhs.current();
  172. }
  173.  
  174. bool TM::DOM::NodeIterator::operator !=(NodeIterator const &rhs) const
  175. {
  176. return !(*this == rhs);
  177. }
  178.  
  179. QDomNode& TM::DOM::NodeIterator::operator *()
  180. {
  181. Q_ASSERT(!m_stack.isEmpty());
  182. Point &point = m_stack.top();
  183. return (point.position() == Point::Close) ? m_null : *point;
  184. }
  185.  
  186. QDomNode const& TM::DOM::NodeIterator::operator *() const
  187. {
  188. Q_ASSERT(!m_stack.isEmpty());
  189. Point const &point = m_stack.top();
  190. return (point.position() == Point::Close) ? m_null : *point;
  191. }
  192.  
  193. QDomNode* TM::DOM::NodeIterator::operator ->()
  194. {
  195. Q_ASSERT(!m_stack.isEmpty());
  196. return &(**this);
  197. }
  198.  
  199. TM::DOM::Point& TM::DOM::NodeIterator::point()
  200. {
  201. Q_ASSERT(!m_stack.isEmpty());
  202. return m_stack.top();
  203. }
  204.  
  205. TM::DOM::NodeIterator::NodeIterator(const Point &point)
  206. {
  207. m_stack.push(point);
  208. }
  209.  
  210. TM::DOM::Point const TM::DOM::NodeIterator::current() const
  211. {
  212. return m_stack.isEmpty() ? Point() : m_stack.top();
  213. }
  214.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:12:17: error: expected ')' before 'const'
  Point(QDomNode const &node, int point = Open);
                 ^
prog.cpp:18:2: error: 'QDomNode' does not name a type
  QDomNode& operator *();
  ^
prog.cpp:19:2: error: 'QDomNode' does not name a type
  QDomNode const& operator *() const;
  ^
prog.cpp:20:2: error: 'QDomNode' does not name a type
  QDomNode* operator ->();
  ^
prog.cpp:21:2: error: 'QDomNode' does not name a type
  QDomNode const* operator ->() const;
  ^
prog.cpp:24:2: error: 'QDomNode' does not name a type
  QDomNode m_node;
  ^
prog.cpp:45:24: error: expected ')' before 'const'
  NodeIterator(QDomNode const &root);
                        ^
prog.cpp:53:2: error: 'QDomNode' does not name a type
  QDomNode& operator *();
  ^
prog.cpp:54:2: error: 'QDomNode' does not name a type
  QDomNode const& operator *() const;
  ^
prog.cpp:55:2: error: 'QDomNode' does not name a type
  QDomNode* operator ->();
  ^
prog.cpp:64:2: error: 'QStack' does not name a type
  QStack<Point> m_stack;
  ^
prog.cpp:65:2: error: 'QDomNode' does not name a type
  QDomNode m_null;
  ^
prog.cpp:68:1: error: 'TM' does not name a type
 TM::DOM::Point::Point()
 ^
prog.cpp:74:1: error: 'TM' does not name a type
 TM::DOM::Point::Point(QDomNode const &node, int point)
 ^
prog.cpp:81:5: error: 'TM' has not been declared
 int TM::DOM::Point::position() const
     ^
prog.cpp:81:32: error: non-member function 'int position()' cannot have cv-qualifier
 int TM::DOM::Point::position() const
                                ^
prog.cpp: In function 'int position()':
prog.cpp:83:9: error: 'm_point' was not declared in this scope
  return m_point;
         ^
prog.cpp: At global scope:
prog.cpp:86:6: error: 'TM' has not been declared
 void TM::DOM::Point::clear()
      ^
prog.cpp: In function 'void clear()':
prog.cpp:88:2: error: 'm_point' was not declared in this scope
  m_point = Null;
  ^
prog.cpp:88:12: error: 'Null' was not declared in this scope
  m_point = Null;
            ^
prog.cpp:89:2: error: 'm_node' was not declared in this scope
  m_node.clear();
  ^
prog.cpp: At global scope:
prog.cpp:92:6: error: 'TM' has not been declared
 bool TM::DOM::Point::operator ==(Point const &rhs) const
      ^
prog.cpp:92:52: error: non-member function 'bool operator==(const Point&)' cannot have cv-qualifier
 bool TM::DOM::Point::operator ==(Point const &rhs) const
                                                    ^
prog.cpp:92:52: error: 'bool operator==(const Point&)' must take exactly two arguments
prog.cpp:97:1: error: 'QDomNode' does not name a type
 QDomNode& TM::DOM::Point::operator *()
 ^
prog.cpp:102:1: error: 'QDomNode' does not name a type
 QDomNode const& TM::DOM::Point::operator *() const
 ^
prog.cpp:107:1: error: 'QDomNode' does not name a type
 QDomNode* TM::DOM::Point::operator ->()
 ^
prog.cpp:112:1: error: 'QDomNode' does not name a type
 QDomNode const* TM::DOM::Point::operator ->() const
 ^
prog.cpp:122:1: error: 'TM' does not name a type
 TM::DOM::NodeIterator::NodeIterator()
 ^
prog.cpp:131:1: error: 'TM' does not name a type
 TM::DOM::NodeIterator::NodeIterator(QDomNode const &root)
 ^
prog.cpp:140:1: error: 'TM' does not name a type
 TM::DOM::NodeIterator& TM::DOM::NodeIterator::operator ++()
 ^
prog.cpp:162:1: error: 'TM' does not name a type
 TM::DOM::NodeIterator TM::DOM::NodeIterator::operator ++(int)
 ^
prog.cpp:169:6: error: 'TM' has not been declared
 bool TM::DOM::NodeIterator::operator ==(NodeIterator const &rhs) const
      ^
prog.cpp:169:66: error: non-member function 'bool operator==(const NodeIterator&)' cannot have cv-qualifier
 bool TM::DOM::NodeIterator::operator ==(NodeIterator const &rhs) const
                                                                  ^
prog.cpp:169:66: error: 'bool operator==(const NodeIterator&)' must take exactly two arguments
prog.cpp:174:6: error: 'TM' has not been declared
 bool TM::DOM::NodeIterator::operator !=(NodeIterator const &rhs) const
      ^
prog.cpp:174:66: error: non-member function 'bool operator!=(const NodeIterator&)' cannot have cv-qualifier
 bool TM::DOM::NodeIterator::operator !=(NodeIterator const &rhs) const
                                                                  ^
prog.cpp:174:66: error: 'bool operator!=(const NodeIterator&)' must take exactly two arguments
prog.cpp:179:1: error: 'QDomNode' does not name a type
 QDomNode& TM::DOM::NodeIterator::operator *()
 ^
prog.cpp:186:1: error: 'QDomNode' does not name a type
 QDomNode const& TM::DOM::NodeIterator::operator *() const
 ^
prog.cpp:193:1: error: 'QDomNode' does not name a type
 QDomNode* TM::DOM::NodeIterator::operator ->()
 ^
prog.cpp:199:1: error: 'TM' does not name a type
 TM::DOM::Point& TM::DOM::NodeIterator::point()
 ^
prog.cpp:205:1: error: 'TM' does not name a type
 TM::DOM::NodeIterator::NodeIterator(const Point &point)
 ^
prog.cpp:210:1: error: 'TM' does not name a type
 TM::DOM::Point const TM::DOM::NodeIterator::current() const
 ^
stdout
Standard output is empty