From 873231fe1e8203204f4dbf22e1e2c8283ebc7a12 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 25 Jan 2009 14:19:27 +0000 Subject: [PATCH] 2009-01-25 Tatsuhiro Tsujikawa Rewritten PStringVisitor and its implementation. * src/PStringBuildVisitor.cc * src/PStringBuildVisitor.h * src/PStringDatum.h * src/PStringNumLoop.h * src/PStringSegment.cc * src/PStringSegment.h * src/PStringSelect.h * src/PStringVisitor.h * src/download_helper.cc * test/PStringBuildVisitorTest.cc * test/ParameterizedStringParserTest.cc --- ChangeLog | 15 ++++++++ src/PStringBuildVisitor.cc | 49 +++++++++++++++++++++++---- src/PStringBuildVisitor.h | 26 +++++++------- src/PStringDatum.h | 4 +-- src/PStringNumLoop.h | 12 ++++--- src/PStringSegment.cc | 12 ++----- src/PStringSegment.h | 15 +------- src/PStringSelect.h | 9 ++--- src/PStringVisitor.h | 13 +++++-- src/download_helper.cc | 2 +- test/PStringBuildVisitorTest.cc | 10 +++--- test/ParameterizedStringParserTest.cc | 8 +++-- 12 files changed, 107 insertions(+), 68 deletions(-) diff --git a/ChangeLog b/ChangeLog index b5f6721f..ee9afffd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2009-01-25 Tatsuhiro Tsujikawa + + Rewritten PStringVisitor and its implementation. + * src/PStringBuildVisitor.cc + * src/PStringBuildVisitor.h + * src/PStringDatum.h + * src/PStringNumLoop.h + * src/PStringSegment.cc + * src/PStringSegment.h + * src/PStringSelect.h + * src/PStringVisitor.h + * src/download_helper.cc + * test/PStringBuildVisitorTest.cc + * test/ParameterizedStringParserTest.cc + 2009-01-25 Tatsuhiro Tsujikawa Persist connection between the built-in HTTP server and a client diff --git a/src/PStringBuildVisitor.cc b/src/PStringBuildVisitor.cc index 585f77d6..493e1a17 100644 --- a/src/PStringBuildVisitor.cc +++ b/src/PStringBuildVisitor.cc @@ -33,26 +33,61 @@ */ /* copyright --> */ #include "PStringBuildVisitor.h" +#include "PStringSegment.h" +#include "PStringNumLoop.h" +#include "PStringSelect.h" namespace aria2 { -void PStringBuildVisitor::hello(PStringSegment* segment) +void PStringBuildVisitor::visit(PStringSegment& segment) { std::string uri; if(_buildQueue.empty()) { - uri += segment->getValue(); + uri += segment.getValue(); } else { - uri = _buildQueue.front()+segment->getValue(); + uri = _buildQueue.front()+segment.getValue(); } _buildQueue.push_front(uri); - if(!segment->hasNext()) { + if(!segment.hasNext()) { _uris.push_back(uri); } -} -void PStringBuildVisitor::goodbye(PStringSegment* segment) -{ + if(!segment.getNext().isNull()) { + segment.getNext()->accept(*this); + } + _buildQueue.pop_front(); } +void PStringBuildVisitor::visit(PStringNumLoop& s) +{ + unsigned int start = s.getStartValue(); + unsigned int end = s.getEndValue(); + unsigned int step = s.getStep(); + for(unsigned int i = start; i <= end; i += step) { + PStringSegment(s.getNumberDecorator()->decorate(i), + s.getNext()).accept(*this); + } +} + +void PStringBuildVisitor::visit(PStringSelect& s) +{ + const std::deque& values = s.getValues(); + for(std::deque::const_iterator i = values.begin(); + i != values.end(); ++i) { + PStringSegment(*i, s.getNext()).accept(*this); + } +} + +const std::deque& PStringBuildVisitor::getURIs() const +{ + return _uris; +} + +void PStringBuildVisitor::reset() +{ + _buildQueue.clear(); + _uris.clear(); +} + } // namespace aria2 diff --git a/src/PStringBuildVisitor.h b/src/PStringBuildVisitor.h index 2e8d484f..49ef4a3b 100644 --- a/src/PStringBuildVisitor.h +++ b/src/PStringBuildVisitor.h @@ -36,34 +36,32 @@ #define _D_P_STRING_BUILD_VISITOR_H_ #include "PStringVisitor.h" -#include "PStringSegment.h" + +#include +#include + +#include "SharedHandle.h" namespace aria2 { -class PStringBuildVisitor : public PStringVisitor, public PStringSegmentVisitor +class PStringBuildVisitor : public PStringVisitor { private: std::deque _buildQueue; std::deque _uris; - public: - virtual void hello(PStringSegment* segment); + virtual void visit(PStringSegment& s); - virtual void goodbye(PStringSegment* segment); + virtual void visit(PStringNumLoop& s); - const std::deque& getURIs() const - { - return _uris; - } + virtual void visit(PStringSelect& s); - void reset() - { - _buildQueue.clear(); - _uris.clear(); - } + const std::deque& getURIs() const; + + void reset(); }; typedef SharedHandle PStringBuildVisitorHandle; diff --git a/src/PStringDatum.h b/src/PStringDatum.h index 6050ba36..130f3766 100644 --- a/src/PStringDatum.h +++ b/src/PStringDatum.h @@ -37,7 +37,6 @@ #include "common.h" #include "SharedHandle.h" -#include namespace aria2 { @@ -47,11 +46,10 @@ class PStringDatum { public: virtual ~PStringDatum() {} - virtual void accept(PStringVisitor* visitor) = 0; + virtual void accept(PStringVisitor& visitor) = 0; }; typedef SharedHandle PStringDatumHandle; -typedef std::deque PStringData; } // namespace aria2 diff --git a/src/PStringNumLoop.h b/src/PStringNumLoop.h index 56148fcf..6e0fdbed 100644 --- a/src/PStringNumLoop.h +++ b/src/PStringNumLoop.h @@ -38,6 +38,7 @@ #include "PStringDatum.h" #include "PStringSegment.h" #include "NumberDecorator.h" +#include "PStringVisitor.h" namespace aria2 { @@ -76,11 +77,14 @@ public: virtual ~PStringNumLoop() {} - virtual void accept(PStringVisitor* visitor) + virtual void accept(PStringVisitor& visitor) { - for(unsigned int i = _startValue; i <= _endValue; i += _step) { - PStringSegment(_numberDecorator->decorate(i), _next).accept(visitor); - } + visitor.visit(*this); + } + + const NumberDecoratorHandle& getNumberDecorator() const + { + return _numberDecorator; } PStringDatumHandle getNext() const diff --git a/src/PStringSegment.cc b/src/PStringSegment.cc index 3a650f3c..b2c9206a 100644 --- a/src/PStringSegment.cc +++ b/src/PStringSegment.cc @@ -47,17 +47,9 @@ PStringSegment::PStringSegment(const std::string& value): PStringSegment::~PStringSegment() {} -void PStringSegment::accept(PStringVisitor* visitor) +void PStringSegment::accept(PStringVisitor& visitor) { - PStringSegmentVisitor* v = dynamic_cast(visitor); - if(!v) { - throw FatalException("Class cast exception"); - } - v->hello(this); - if(!_next.isNull()) { - _next->accept(visitor); - } - v->goodbye(this); + visitor.visit(*this); } const std::string& PStringSegment::getValue() const diff --git a/src/PStringSegment.h b/src/PStringSegment.h index 9b6036d3..e98a847c 100644 --- a/src/PStringSegment.h +++ b/src/PStringSegment.h @@ -40,8 +40,6 @@ namespace aria2 { -class PStringSegmentVisitor; - class PStringSegment : public PStringDatum { private: @@ -58,7 +56,7 @@ public: virtual ~PStringSegment(); - virtual void accept(PStringVisitor* visitor); + virtual void accept(PStringVisitor& visitor); const std::string& getValue() const; @@ -69,17 +67,6 @@ public: typedef SharedHandle PStringSegmentHandle; -class PStringSegmentVisitor { -public: - virtual ~PStringSegmentVisitor() {} - - virtual void hello(PStringSegment* s) = 0; - - virtual void goodbye(PStringSegment* s) = 0; -}; - -typedef SharedHandle PStringSegmentVisitorHandle; - } // namespace aria2 #endif // _D_P_STRING_SEGMENT_H_ diff --git a/src/PStringSelect.h b/src/PStringSelect.h index 2a629f43..9c9ccab1 100644 --- a/src/PStringSelect.h +++ b/src/PStringSelect.h @@ -37,11 +37,10 @@ #include "PStringDatum.h" #include "PStringSegment.h" +#include "PStringVisitor.h" namespace aria2 { -class PStringVisitor; - class PStringSelect : public PStringDatum { private: @@ -61,11 +60,9 @@ public: virtual ~PStringSelect() {} - virtual void accept(PStringVisitor* visitor) + virtual void accept(PStringVisitor& visitor) { - for(std::deque::iterator itr = _values.begin(); itr != _values.end(); ++itr) { - PStringSegment(*itr, _next).accept(visitor); - } + visitor.visit(*this); } const std::deque& getValues() const diff --git a/src/PStringVisitor.h b/src/PStringVisitor.h index 56dc6c48..d5298a2f 100644 --- a/src/PStringVisitor.h +++ b/src/PStringVisitor.h @@ -36,16 +36,23 @@ #define _D_P_STRING_VISITOR_H_ #include "common.h" -#include "SharedHandle.h" namespace aria2 { +class PStringSegment; +class PStringNumLoop; +class PStringSelect; + class PStringVisitor { public: virtual ~PStringVisitor() {} -}; -typedef SharedHandle PStringVisitorHandle; + virtual void visit(PStringSegment& s) = 0; + + virtual void visit(PStringNumLoop& s) = 0; + + virtual void visit(PStringSelect& s) = 0; +}; } // namespace aria2 diff --git a/src/download_helper.cc b/src/download_helper.cc index 9f9e6b46..5a4e2b14 100644 --- a/src/download_helper.cc +++ b/src/download_helper.cc @@ -66,7 +66,7 @@ static void unfoldURI for(std::deque::const_iterator itr = args.begin(); itr != args.end(); ++itr) { v.reset(); - p.parse(*itr)->accept(&v); + p.parse(*itr)->accept(v); result.insert(result.end(), v.getURIs().begin(), v.getURIs().end()); } } diff --git a/test/PStringBuildVisitorTest.cc b/test/PStringBuildVisitorTest.cc index c2601f26..7aab8b94 100644 --- a/test/PStringBuildVisitorTest.cc +++ b/test/PStringBuildVisitorTest.cc @@ -1,9 +1,11 @@ #include "PStringBuildVisitor.h" + +#include + #include "PStringSegment.h" #include "PStringNumLoop.h" #include "PStringSelect.h" #include "FixedWidthNumberDecorator.h" -#include namespace aria2 { @@ -39,7 +41,7 @@ void PStringBuildVisitorTest::testVisit_select() PStringBuildVisitor v; - select1->accept(&v); + select1->accept(v); CPPUNIT_ASSERT_EQUAL((size_t)3, v.getURIs().size()); CPPUNIT_ASSERT_EQUAL(std::string("alpha/tango"), v.getURIs()[0]); @@ -57,7 +59,7 @@ void PStringBuildVisitorTest::testVisit_numLoop() PStringBuildVisitor v; - loop1->accept(&v); + loop1->accept(v); CPPUNIT_ASSERT_EQUAL((size_t)3, v.getURIs().size()); CPPUNIT_ASSERT_EQUAL(std::string("00/tango"), v.getURIs()[0]); @@ -80,7 +82,7 @@ void PStringBuildVisitorTest::testVisit_select_numLoop() PStringBuildVisitor v; - loop1->accept(&v); + loop1->accept(v); CPPUNIT_ASSERT_EQUAL((size_t)9, v.getURIs().size()); CPPUNIT_ASSERT_EQUAL(std::string("00alpha/tango"), v.getURIs()[0]); diff --git a/test/ParameterizedStringParserTest.cc b/test/ParameterizedStringParserTest.cc index 4b835ed6..88690d31 100644 --- a/test/ParameterizedStringParserTest.cc +++ b/test/ParameterizedStringParserTest.cc @@ -1,10 +1,14 @@ #include "ParameterizedStringParser.h" + +#include +#include + +#include + #include "PStringSelect.h" #include "PStringSegment.h" #include "PStringNumLoop.h" #include "FatalException.h" -#include -#include namespace aria2 {