From: Mikko Rasa Date: Sat, 3 Aug 2013 12:14:11 +0000 (+0300) Subject: Use a common StatementKey structure for Loader and BinaryParser/Writer X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=commitdiff_plain;h=2d289d20a9a4aeac7774976e4213a7c72f1dc75b Use a common StatementKey structure for Loader and BinaryParser/Writer --- diff --git a/source/binarydict.h b/source/binarydict.h deleted file mode 100644 index 923ce39..0000000 --- a/source/binarydict.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef MSP_DATAFILE_BINARYDICT_H_ -#define MSP_DATAFILE_BINARYDICT_H_ - -#include - -namespace Msp { -namespace DataFile { - -/** -Stores statement information for binary files. -*/ -struct DictEntry -{ - std::string keyword; - std::string args; - - DictEntry() { } - DictEntry(const std::string &k, const std::string &a): keyword(k), args(a) { } - - bool operator<(const DictEntry &o) const - { return keyword -#include "binarydict.h" #include "parsermode.h" #include "type.h" @@ -15,7 +14,7 @@ Parses data in binary format. class BinaryParser: public ParserMode { private: - typedef std::map Dictionary; + typedef std::map Dictionary; typedef std::map StringMap; Dictionary dict; diff --git a/source/binarywriter.cpp b/source/binarywriter.cpp index d143330..087e140 100644 --- a/source/binarywriter.cpp +++ b/source/binarywriter.cpp @@ -16,9 +16,9 @@ BinaryWriter::BinaryWriter(Output &o): next_str_id(1), float_precision(32) { - dict[DictEntry("__kwd", "iss")] = -1; - dict[DictEntry("__str", "is")] = -2; - dict[DictEntry("__flt", "i")] = -3; + dict[StatementKey("__kwd", "iss")] = -1; + dict[StatementKey("__str", "is")] = -2; + dict[StatementKey("__flt", "i")] = -3; } void BinaryWriter::set_float_precision(unsigned fp) @@ -40,7 +40,7 @@ void BinaryWriter::write(const Statement &st) void BinaryWriter::write_(const Statement &st) { - int id = get_item(dict, DictEntry(st.keyword, st.get_signature())); + int id = get_item(dict, StatementKey(st.keyword, st.get_signature())); write_int(id); for(Statement::Arguments::const_iterator j = st.args.begin(); j!=st.args.end(); ++j) @@ -60,18 +60,18 @@ void BinaryWriter::write_(const Statement &st) void BinaryWriter::collect_keywords(const Statement &st) { - DictEntry de(st.keyword, st.get_signature()); + StatementKey key(st.keyword, st.get_signature()); - if(!dict.count(de)) + if(!dict.count(key)) { Statement kst; kst.keyword = "__kwd"; kst.args.push_back(next_kwd_id); - kst.args.push_back(de.keyword); - kst.args.push_back(de.args); + kst.args.push_back(key.keyword); + kst.args.push_back(key.signature); write_(kst); - dict[de] = next_kwd_id++; + dict[key] = next_kwd_id++; } for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i) diff --git a/source/binarywriter.h b/source/binarywriter.h index 81b4a46..b10c5ab 100644 --- a/source/binarywriter.h +++ b/source/binarywriter.h @@ -2,7 +2,7 @@ #define MSP_DATAFILE_BINARYWRITER_H_ #include -#include "binarydict.h" +#include "statement.h" #include "type.h" #include "writermode.h" @@ -15,7 +15,7 @@ Writes data in binary format. class BinaryWriter: public WriterMode { private: - typedef std::map Dictionary; + typedef std::map Dictionary; typedef std::map StringMap; Dictionary dict; diff --git a/source/loader.cpp b/source/loader.cpp index bffa9fd..2c0fbd1 100644 --- a/source/loader.cpp +++ b/source/loader.cpp @@ -113,7 +113,7 @@ void Loader::load_statement(const Statement &st) try { - ActionKey key(st.keyword, st.get_signature()); + StatementKey key(st.keyword, st.get_signature()); if(!aux_loaders.empty() && !has_action(key)) { @@ -152,7 +152,7 @@ void Loader::load_sub_with(Loader &ldr) void Loader::add(const string &kwd, LoaderAction *act) { - ActionKey key(kwd, (act ? act->get_signature() : "*")); + StatementKey key(kwd, (act ? act->get_signature() : "*")); ActionMap::iterator i = actions.find(key); if(i!=actions.end()) { @@ -168,19 +168,19 @@ void Loader::add_auxiliary_loader(Loader &ldr) aux_loaders.push_back(&ldr); } -bool Loader::has_action(const ActionKey &key) const +bool Loader::has_action(const StatementKey &key) const { - ActionMap::const_iterator i = actions.lower_bound(ActionKey(key.keyword, string())); + ActionMap::const_iterator i = actions.lower_bound(StatementKey(key.keyword, string())); for(; (i!=actions.end() && i->first.keyword==key.keyword); ++i) if(signature_match(key.signature, i->first.signature)) return true; return false; } -LoaderAction *Loader::find_action(const ActionKey &key) const +LoaderAction *Loader::find_action(const StatementKey &key) const { - ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string())); - ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~")); + ActionMap::const_iterator begin = actions.lower_bound(StatementKey(key.keyword, string())); + ActionMap::const_iterator end = actions.upper_bound(StatementKey(key.keyword, "~")); if(begin==end) throw unknown_keyword(key.keyword); @@ -199,18 +199,5 @@ const string &Loader::get_source() const return cur_st->source; } - -Loader::ActionKey::ActionKey(const string &k, const string &s): - keyword(k), - signature(s) -{ } - -bool Loader::ActionKey::operator<(const ActionKey &other) const -{ - if(keyword!=other.keyword) - return keyword ActionMap; + typedef std::map ActionMap; ActionMap actions; const Statement *cur_st; @@ -136,8 +126,8 @@ protected: void add_auxiliary_loader(Loader &); private: - bool has_action(const ActionKey &) const; - LoaderAction *find_action(const ActionKey &) const; + bool has_action(const StatementKey &) const; + LoaderAction *find_action(const StatementKey &) const; protected: /** Returns the source of the statement being processed. This can be used diff --git a/source/statement.h b/source/statement.h index f9287a5..1df3438 100644 --- a/source/statement.h +++ b/source/statement.h @@ -36,6 +36,18 @@ public: { return append(v); } }; +struct StatementKey +{ + std::string keyword; + std::string signature; + + StatementKey() { } + StatementKey(const std::string &k, const std::string &s): keyword(k), signature(s) { } + + bool operator<(const StatementKey &o) const + { return keyword