Blame view

src/TimeTableCatalog/TimeTableCatalogFactory.cc 4.48 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
/*
 * TimeTableCatalogFactory.cc
 *
 *  Created on: 8 août 2013
 *      Author: CS
 */

#include "TimeTableCatalogFactory.hh"
#include "AsciiReader.hh"
8a44e963   Hacene SI HADJ MOHAND   reader
10
#include "SpaceReader.hh"
fbe3c2bb   Benjamin Renard   First commit
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "InternalXMLReader.hh"
#include "VOTableReader.hh"
#include "AsciiWriter.hh"
#include "InternalXMLWriter.hh"
#include "VOTableWriter.hh"
#include <boost/algorithm/string.hpp>

namespace TimeTableCatalog {

std::unique_ptr<TimeTableCatalogFactory> TimeTableCatalogFactory::_instance =
		nullptr;
std::once_flag TimeTableCatalogFactory::_onceFlag;

std::map<const std::string, AbstractReader*> TimeTableCatalogFactory::_readers =
		std::map<const std::string, AbstractReader*>();
std::map<const std::string, AbstractWriter*> TimeTableCatalogFactory::_writers =
		std::map<const std::string, AbstractWriter*>();

TimeTableCatalogFactory::TimeTableCatalogFactory() {
	// register readers
	registerReader(AsciiReader::FORMAT,
			new AsciiReader(std::string()));
	registerReader(InternalXMLReader::FORMAT,
			new InternalXMLReader(std::string()));
	registerReader(VOTableReader::FORMAT,
			new VOTableReader(std::string()));
8a44e963   Hacene SI HADJ MOHAND   reader
37
38
                    registerReader(SpaceReader::FORMAT,
			new SpaceReader(std::string()));
fbe3c2bb   Benjamin Renard   First commit
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

	// register writers
	registerWriter(AsciiWriter::FORMAT,
			new AsciiWriter(std::string()));
	registerWriter(InternalXMLWriter::FORMAT,
			new InternalXMLWriter(std::string()));
	registerWriter(VOTableWriter::FORMAT,
			new VOTableWriter(std::string()));

}

TimeTableCatalogFactory::~TimeTableCatalogFactory() {
	std::map<const std::string, AbstractReader*>::const_iterator itr;
	for (itr = _readers.begin(); itr != _readers.end(); ++itr) {
		delete (*itr).second;
	}

	std::map<const std::string, AbstractWriter*>::const_iterator itw;
	for (itw = _writers.begin(); itw != _writers.end(); ++itw) {
		delete (*itw).second;
	}
}

void TimeTableCatalogFactory::registerReader(const std::string& pKey,
		AbstractReader* pReader) {
	// register if not already in
	if (_readers.find(pKey) == _readers.end()) {
		_readers[pKey] = pReader;
	}
}

void TimeTableCatalogFactory::registerWriter(const std::string& pKey,
		AbstractWriter* pWriter) {
	// register if not already in
	if (_writers.find(pKey) == _writers.end()) {
		_writers[pKey] = pWriter;
	}
}

std::string TimeTableCatalogFactory::getReaderType(
		const std::string& pPath) {
	for (std::map<const std::string, AbstractReader*>::const_iterator iterator =
			TimeTableCatalogFactory::_readers.begin();
			iterator != TimeTableCatalogFactory::_readers.end();
			++iterator) {
		if (iterator->second->canRead(pPath)) {
			return iterator->first;
		}
	}
	return std::string();
}

/**
 * Checks a reader exists for this type.
 */
bool TimeTableCatalogFactory::canRead(const std::string& pType) const {
	for (std::map<const std::string, AbstractReader*>::const_iterator iterator =
			TimeTableCatalogFactory::_readers.begin();
			iterator != TimeTableCatalogFactory::_readers.end();
			++iterator) {
		if (pType == iterator->first) {
			return true;
		}
	}
	return false;
}

std::unique_ptr<AbstractReader> TimeTableCatalogFactory::createReader(
		const std::string& pKey, const std::string& pPath) const {
	std::map<const std::string, AbstractReader*>::const_iterator it =
			_readers.find(pKey);

	if (it != _readers.end()) {
		return ((*it).second)->createInstance(pPath);
	}

	return 0;
}

std::unique_ptr<AbstractWriter> TimeTableCatalogFactory::createWriter(
		const std::string& pKey, const std::string& pPath,
		const std::string& pName) const {
	std::map<const std::string, AbstractWriter*>::const_iterator it =
			_writers.find(pKey);

	if (it != _writers.end()) {
		return ((*it).second)->createInstance(pPath, pName);
	}

	return 0;
}

/**
 * Reads a time table.
 */
void TimeTable::read(const std::string& ppath, const std::string& ptype) {
	std::unique_ptr<AbstractReader> reader =
			TimeTableCatalogFactory::getInstance().createReader(ptype,
					ppath);
	reader->read(*this);

}

/**
 * Writes a time table.
 */
std::string TimeTable::write(const std::string& ppath, const std::string& ptype,
		const std::string& pName) const {
	std::unique_ptr<AbstractWriter> writer =
			TimeTableCatalogFactory::getInstance().createWriter(ptype,
					ppath, pName);
	return writer->write(*this);
}

/**
 * Reads a time table and writes it into another format.
 */
void TimeTable::convert(const std::string& pInPath, const std::string& pInType,
		const std::string& pOutPath, const std::string& pName,
		const std::string& pOutType) {
	read(pInPath, pInType);
	write(pOutPath, pOutType, pName);
}

} /* namespace TimeTableCatalog */