/** \file FormatParserStorage.hpp * * date: Jun, 22 2010 * author: heber * */ #ifndef FORMATPARSERSTORAGE_HPP_ #define FORMATPARSERSTORAGE_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/Singleton.hpp" #include #include #include #include #include #include "CodePatterns/Assert.hpp" #include "CodePatterns/Log.hpp" #include "FormatParser.hpp" #include "ParserTypes.hpp" class atom; class FormatParserInterface; class FormatParserStorage : public Singleton { friend class Singleton; public: bool add(std::string type); bool add(ParserTypes type); bool load(boost::filesystem::path filename); bool load(std::string &filename); bool load(std::istream &input, std::string &suffix); bool save(std::ostream &output, std::string suffix, const std::vector &atoms); bool saveSelectedAtoms(std::ostream &output, std::string suffix); bool saveSelectedMolecules(std::ostream &output, std::string suffix); bool saveWorld(std::ostream &output, std::string suffix); void setOutputFormat(ParserTypes type); void setOutputFormat(std::string type); FormatParserInterface &get(ParserTypes _type); ParserTypes getTypeFromName(std::string type); ParserTypes getTypeFromSuffix(std::string type); const std::string &getSuffixFromType(ParserTypes type); const std::string &getNameFromType(ParserTypes type); void SetOutputPrefixForAll(std::string &_prefix); bool isAbleToSave(); void SaveAll(); private: // private constructors as this is a singleton FormatParserStorage(); ~FormatParserStorage(); // list of allocated parsers std::vector ParserList; // list of allocated strams std::vector ParserStream; // which parser is already present std::vector ParserPresent; // which parser is already present std::vector ParserDesiredOutputFormat; // default suffix of each parser type std::map ParserSuffixes; std::map ParserLookupSuffixes; // function pointers to each add...() std::map< ParserTypes, void (FormatParserStorage::*)() > ParserAddFunction; // type name of each parser type and reverse lookup std::map ParserNames; std::map ParserLookupNames; // prefix of the filenames to use on save std::string prefix; static const std::string unknownTypeString; public: template void addParser() { if (!ParserPresent[Ptype]) { ParserList[Ptype] = new FormatParser(); ParserPresent[Ptype] = true; } else { ASSERT(ParserNames.find(Ptype) != ParserNames.end(), "FormatParserStorage::addParser() - ParserNames unknown for type"+toString((size_t)Ptype)+"."); ASSERT(ParserSuffixes.find(Ptype) != ParserSuffixes.end(), "FormatParserStorage::addParser() - ParserSuffixes unknown for type"+toString((size_t)Ptype)+"."); LOG(2, "INFO: Parser " << ParserNames[Ptype] << " is already present." << std::endl << "Note that you don't need to add '-o " << ParserSuffixes[Ptype] << "' if the input file is non-empty and of type " << ParserSuffixes[Ptype] << "." << std::endl); } } template FormatParser &getParser() { if(!ParserPresent[Ptype]) addParser< Ptype >(); return dynamic_cast &>(*ParserList[Ptype]); } }; #endif // FORMATPARSERSTORAGE_HPP_