TimeTableCatalogFactory.hh 1.88 KB
/*
 * TimeTableCatalogFactory.hh
 *
 *  Created on: 8 août 2013
 *      Author: CS
 */

#ifndef TIMETABLECATALOGFACTORY_HH_
#define TIMETABLECATALOGFACTORY_HH_

#include "AbstractReader.hh"
#include "AbstractWriter.hh"

#include <string>
#include <map>
#include <memory>
#include <mutex>

namespace TimeTableCatalog {

class TimeTableCatalogFactory {
public:
	virtual ~TimeTableCatalogFactory();

	/**
	 * Gets singleton instance.
	 */
	static TimeTableCatalogFactory& getInstance() {
		std::call_once(_onceFlag, [] {
				_instance.reset(new TimeTableCatalogFactory);
		});
		return *_instance.get();
	}

	/**
	 * Adds a possible tt reader.
	 */
	static void registerReader(const std::string& pKey,
			AbstractReader* pReader);

	/**
	 * Adds a possible tt writer.
	 */
	static void registerWriter(const std::string& pKey,
			AbstractWriter* pWriter);

	/**
	 * Automatically detects a tt type.
	 */
	 std::string getReaderType(const std::string& pPath);

	/**
	 * Checks a reader exists for this type.
	 */
	bool canRead(const std::string& pType) const;

	/**
	 * Creates a reader of a given type.
	 */
	std::unique_ptr<AbstractReader> createReader(const std::string& pKey,
			const std::string& pPath) const;

	/**
	 * Creates a writer of a given type.
	 */
	std::unique_ptr<AbstractWriter> createWriter(const std::string& pKey,
			const std::string& pPath, const std::string& pName = "") const;

	/**
	 * Map of registered tt readers.
	 */
	static std::map<const std::string, AbstractReader*> _readers;

	/**
	 * Map of registered tt writers.
	 */
	static std::map<const std::string, AbstractWriter*> _writers;

private:

	TimeTableCatalogFactory();

	/**
	 * Singleton unique instance
	 */
	static std::unique_ptr<TimeTableCatalogFactory> _instance;

	/**
	 * Flag to guarantee singleton unicity.
	 */
	static std::once_flag _onceFlag;


};

} /* namespace TimeTableCatalog */
#endif /* TIMETABLECATALOGFACTORY_HH_ */