From: Mikko Rasa Date: Mon, 25 Jul 2011 14:02:34 +0000 (+0300) Subject: Use the functions from maputils.h in various places X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=commitdiff_plain;h=35d0da96bad97214376f18d15078e6d731e6f219 Use the functions from maputils.h in various places --- diff --git a/source/binaryparser.cpp b/source/binaryparser.cpp index e848722..d2fca32 100644 --- a/source/binaryparser.cpp +++ b/source/binaryparser.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "binaryparser.h" #include "input.h" @@ -54,10 +55,7 @@ Statement BinaryParser::parse_statement() if(!in) return Statement(); - Dictionary::const_iterator i = dict.find(id); - if(i==dict.end()) - throw_at(KeyError("Unknown statement ID", lexical_cast(id)), src); - const DictEntry &de = i->second; + const DictEntry &de = get_item(dict, id); Statement result; result.keyword = de.keyword; @@ -152,20 +150,12 @@ string BinaryParser::parse_string() return result; } else - return lookup_string(-len); + return get_item(strings, -len); } string BinaryParser::parse_enum() { - return lookup_string(parse_int()); -} - -const string &BinaryParser::lookup_string(unsigned id) const -{ - StringMap::const_iterator i = strings.find(id); - if(i==strings.end()) - throw_at(KeyError("Unknown string", lexical_cast(id)), src); - return i->second; + return get_item(strings, parse_int()); } } // namespace DataFile diff --git a/source/binaryparser.h b/source/binaryparser.h index f6736fa..2cdfbbe 100644 --- a/source/binaryparser.h +++ b/source/binaryparser.h @@ -32,7 +32,6 @@ private: std::string parse_string(); bool parse_bool(); std::string parse_enum(); - const std::string &lookup_string(unsigned) const; }; } // namespace DataFile diff --git a/source/binarywriter.cpp b/source/binarywriter.cpp index 9651609..2ff96a4 100644 --- a/source/binarywriter.cpp +++ b/source/binarywriter.cpp @@ -1,3 +1,4 @@ +#include #include "binarywriter.h" #include "statement.h" @@ -23,11 +24,9 @@ void BinaryWriter::write(const Statement &st) void BinaryWriter::write_(const Statement &st) { - Dictionary::iterator i = dict.find(DictEntry(st.keyword, st.get_signature())); - if(i==dict.end()) - throw InvalidParameterValue("Unknown statement"); + unsigned id = get_item(dict, DictEntry(st.keyword, st.get_signature())); - write_int(i->second); + write_int(id); for(Statement::Arguments::const_iterator j = st.args.begin(); j!=st.args.end(); ++j) switch(j->get_signature()) { @@ -135,10 +134,7 @@ void BinaryWriter::write_float(float f) void BinaryWriter::write_enum(const string &e) { - StringMap::const_iterator i = strings.find(e); - if(i==strings.end()) - throw InvalidParameterValue("Unknown enum"); - write_int(i->second); + write_int(get_item(strings, e)); } } // namespace DataFile diff --git a/source/collection.h b/source/collection.h index 69d631f..2d346d9 100644 --- a/source/collection.h +++ b/source/collection.h @@ -2,6 +2,7 @@ #define MSP_DATAFILE_COLLECTION_H_ #include +#include #include #include "loader.h" @@ -178,10 +179,11 @@ public: template void add(const std::string &name, T *d) { - if(items.count(name)) - throw KeyError("Duplicate key in collection", name); + typedef typename RemoveConst::Type NCT; - items[name]=new Item::Type>(d); + RefPtr > i=new Item(d); + insert_unique(items, i.get()); + i.release(); } /** @@ -192,11 +194,9 @@ public: { typedef typename RemoveConst::Type NCT; - ItemMap::const_iterator i=items.find(name); - if(i==items.end()) - throw KeyError("Item not found in collection", name); + ItemBase *i=get_item(items, name); - const Item *item=dynamic_cast *>(i->second); + const Item *item=dynamic_cast *>(i); if(!item) throw TypeError("Type mismatch on item '"+name+"'"); @@ -213,8 +213,7 @@ public: { typedef typename RemoveConst::Type NCT; - ItemMap::const_iterator i=items.find(name); - if(i==items.end()) + if(!items.count(name)) { for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j) { @@ -226,10 +225,11 @@ public: return d; } } - throw KeyError("Item not found in collection", name); } - const Item *item=dynamic_cast *>(i->second); + ItemBase *i=get_item(items, name); + + const Item *item=dynamic_cast *>(i); if(!item) throw TypeError("Type mismatch on item '"+name+"'");