2009-01-25 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

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
pull/1/head
Tatsuhiro Tsujikawa 2009-01-25 14:19:27 +00:00
parent 5def96906f
commit 873231fe1e
12 changed files with 107 additions and 68 deletions

View File

@ -1,3 +1,18 @@
2009-01-25 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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 <t-tujikawa@users.sourceforge.net>
Persist connection between the built-in HTTP server and a client

View File

@ -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);
}
if(!segment.getNext().isNull()) {
segment.getNext()->accept(*this);
}
void PStringBuildVisitor::goodbye(PStringSegment* segment)
{
_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<std::string>& values = s.getValues();
for(std::deque<std::string>::const_iterator i = values.begin();
i != values.end(); ++i) {
PStringSegment(*i, s.getNext()).accept(*this);
}
}
const std::deque<std::string>& PStringBuildVisitor::getURIs() const
{
return _uris;
}
void PStringBuildVisitor::reset()
{
_buildQueue.clear();
_uris.clear();
}
} // namespace aria2

View File

@ -36,34 +36,32 @@
#define _D_P_STRING_BUILD_VISITOR_H_
#include "PStringVisitor.h"
#include "PStringSegment.h"
#include <deque>
#include <string>
#include "SharedHandle.h"
namespace aria2 {
class PStringBuildVisitor : public PStringVisitor, public PStringSegmentVisitor
class PStringBuildVisitor : public PStringVisitor
{
private:
std::deque<std::string> _buildQueue;
std::deque<std::string> _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<std::string>& getURIs() const
{
return _uris;
}
virtual void visit(PStringSelect& s);
void reset()
{
_buildQueue.clear();
_uris.clear();
}
const std::deque<std::string>& getURIs() const;
void reset();
};
typedef SharedHandle<PStringBuildVisitor> PStringBuildVisitorHandle;

View File

@ -37,7 +37,6 @@
#include "common.h"
#include "SharedHandle.h"
#include <deque>
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<PStringDatum> PStringDatumHandle;
typedef std::deque<PStringDatumHandle> PStringData;
} // namespace aria2

View File

@ -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

View File

@ -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<PStringSegmentVisitor*>(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

View File

@ -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<PStringSegment> PStringSegmentHandle;
class PStringSegmentVisitor {
public:
virtual ~PStringSegmentVisitor() {}
virtual void hello(PStringSegment* s) = 0;
virtual void goodbye(PStringSegment* s) = 0;
};
typedef SharedHandle<PStringSegmentVisitor> PStringSegmentVisitorHandle;
} // namespace aria2
#endif // _D_P_STRING_SEGMENT_H_

View File

@ -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<std::string>::iterator itr = _values.begin(); itr != _values.end(); ++itr) {
PStringSegment(*itr, _next).accept(visitor);
}
visitor.visit(*this);
}
const std::deque<std::string>& getValues() const

View File

@ -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<PStringVisitor> PStringVisitorHandle;
virtual void visit(PStringSegment& s) = 0;
virtual void visit(PStringNumLoop& s) = 0;
virtual void visit(PStringSelect& s) = 0;
};
} // namespace aria2

View File

@ -66,7 +66,7 @@ static void unfoldURI
for(std::deque<std::string>::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());
}
}

View File

@ -1,9 +1,11 @@
#include "PStringBuildVisitor.h"
#include <cppunit/extensions/HelperMacros.h>
#include "PStringSegment.h"
#include "PStringNumLoop.h"
#include "PStringSelect.h"
#include "FixedWidthNumberDecorator.h"
#include <cppunit/extensions/HelperMacros.h>
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]);

View File

@ -1,10 +1,14 @@
#include "ParameterizedStringParser.h"
#include <iostream>
#include <deque>
#include <cppunit/extensions/HelperMacros.h>
#include "PStringSelect.h"
#include "PStringSegment.h"
#include "PStringNumLoop.h"
#include "FatalException.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {