1 | /** \file FormatParserStorage.hpp
|
---|
2 | *
|
---|
3 | * date: Jun, 22 2010
|
---|
4 | * author: heber
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef FORMATPARSERSTORAGE_HPP_
|
---|
9 | #define FORMATPARSERSTORAGE_HPP_
|
---|
10 |
|
---|
11 | // include config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include "CodePatterns/Singleton.hpp"
|
---|
17 |
|
---|
18 | #include <string>
|
---|
19 | #include <map>
|
---|
20 | #include <vector>
|
---|
21 |
|
---|
22 | #include "Parser/FormatParser.hpp"
|
---|
23 | #include "Parser/MpqcParser.hpp"
|
---|
24 | #include "Parser/PcpParser.hpp"
|
---|
25 | #include "Parser/PdbParser.hpp"
|
---|
26 | #include "Parser/TremoloParser.hpp"
|
---|
27 | #include "Parser/XyzParser.hpp"
|
---|
28 |
|
---|
29 | class atom;
|
---|
30 |
|
---|
31 | // enum has to be outside of class for operator++ to be possible
|
---|
32 | enum ParserTypes { mpqc, pcp, pdb, tremolo, xyz, ParserTypes_end, ParserTypes_begin = mpqc };
|
---|
33 | typedef enum ParserTypes ParserTypes;
|
---|
34 |
|
---|
35 | ParserTypes &operator++(ParserTypes &type);
|
---|
36 |
|
---|
37 | template <class T> enum ParserTypes getPType();
|
---|
38 | template <> enum ParserTypes getPType<MpqcParser>();
|
---|
39 | template <> enum ParserTypes getPType<PcpParser>();
|
---|
40 | template <> enum ParserTypes getPType<PdbParser>();
|
---|
41 | template <> enum ParserTypes getPType<TremoloParser>();
|
---|
42 | template <> enum ParserTypes getPType<XyzParser>();
|
---|
43 |
|
---|
44 |
|
---|
45 | class FormatParserStorage : public Singleton<FormatParserStorage> {
|
---|
46 | friend class Singleton<FormatParserStorage>;
|
---|
47 | public:
|
---|
48 |
|
---|
49 | void addMpqc();
|
---|
50 | void addPcp();
|
---|
51 | void addPdb();
|
---|
52 | void addTremolo();
|
---|
53 | void addXyz();
|
---|
54 | bool add(std::string type);
|
---|
55 | bool add(ParserTypes type);
|
---|
56 |
|
---|
57 | bool load(std::istream &input, std::string suffix);
|
---|
58 | bool save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms);
|
---|
59 | bool saveSelectedAtoms(std::ostream &output, std::string suffix);
|
---|
60 | bool saveSelectedMolecules(std::ostream &output, std::string suffix);
|
---|
61 | bool saveWorld(std::ostream &output, std::string suffix);
|
---|
62 | MpqcParser &getMpqc();
|
---|
63 | PcpParser &getPcp();
|
---|
64 | PdbParser &getPdb();
|
---|
65 | TremoloParser &getTremolo();
|
---|
66 | XyzParser &getXyz();
|
---|
67 | FormatParser &get(enum ParserTypes _type);
|
---|
68 |
|
---|
69 | ParserTypes getTypeFromName(std::string type);
|
---|
70 | ParserTypes getTypeFromSuffix(std::string type);
|
---|
71 |
|
---|
72 | void SetOutputPrefixForAll(std::string &_prefix);
|
---|
73 | void SaveAll();
|
---|
74 |
|
---|
75 | private:
|
---|
76 | // private constructors as this is a singleton
|
---|
77 | FormatParserStorage();
|
---|
78 | ~FormatParserStorage();
|
---|
79 |
|
---|
80 | // list of allocated parsers
|
---|
81 | std::vector<FormatParser *> ParserList;
|
---|
82 |
|
---|
83 | // list of allocated strams
|
---|
84 | std::vector<std::ofstream *> ParserStream;
|
---|
85 |
|
---|
86 | // which parser is already present
|
---|
87 | std::vector<bool> ParserPresent;
|
---|
88 |
|
---|
89 | // default suffix of each parser type
|
---|
90 | std::map<ParserTypes, std::string> ParserSuffixes;
|
---|
91 | std::map<std::string, ParserTypes> ParserLookupSuffixes;
|
---|
92 |
|
---|
93 | // function pointers to each add...()
|
---|
94 | std::map< ParserTypes, void (FormatParserStorage::*)() > ParserAddFunction;
|
---|
95 |
|
---|
96 | // type name of each parser type and reverse lookup
|
---|
97 | std::map<ParserTypes, std::string> ParserNames;
|
---|
98 | std::map<std::string, ParserTypes> ParserLookupNames;
|
---|
99 |
|
---|
100 |
|
---|
101 | // prefix of the filenames to use on save
|
---|
102 | std::string prefix;
|
---|
103 |
|
---|
104 | public:
|
---|
105 |
|
---|
106 | template<class ParserT> void addParser()
|
---|
107 | {
|
---|
108 | enum ParserTypes Ptype = getPType<ParserT>();
|
---|
109 | if (!ParserPresent[Ptype]) {
|
---|
110 | ParserList[Ptype] = new ParserT();
|
---|
111 | ParserPresent[Ptype] = true;
|
---|
112 | } else {
|
---|
113 | ASSERT(ParserNames.find(Ptype) != ParserNames.end(),
|
---|
114 | "FormatParserStorage::addParser() - ParserNames unknown for type"+toString((size_t)Ptype)+".");
|
---|
115 | ASSERT(ParserSuffixes.find(Ptype) != ParserSuffixes.end(),
|
---|
116 | "FormatParserStorage::addParser() - ParserSuffixes unknown for type"+toString((size_t)Ptype)+".");
|
---|
117 | LOG(2, "INFO: Parser " << ParserNames[Ptype] << " is already present." << std::endl
|
---|
118 | << "Note that you don't need to add '-o "
|
---|
119 | << ParserSuffixes[Ptype] << "' if the input file is non-empty and of type "
|
---|
120 | << ParserSuffixes[Ptype] << "." << std::endl);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | template<class ParserT> ParserT &getParser()
|
---|
125 | {
|
---|
126 | enum ParserTypes Ptype = getPType<ParserT>();
|
---|
127 | if(!ParserPresent[Ptype])
|
---|
128 | addParser<ParserT>();
|
---|
129 | return dynamic_cast<ParserT &>(*ParserList[Ptype]);
|
---|
130 | }
|
---|
131 |
|
---|
132 | };
|
---|
133 |
|
---|
134 | #endif // FORMATPARSERSTORAGE_HPP_
|
---|