]> git.tdb.fi Git - libs/datafile.git/commitdiff
Use the functions from maputils.h in various places
authorMikko Rasa <tdb@tdb.fi>
Mon, 25 Jul 2011 14:02:34 +0000 (17:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 25 Jul 2011 14:02:34 +0000 (17:02 +0300)
source/binaryparser.cpp
source/binaryparser.h
source/binarywriter.cpp
source/collection.h

index e8487223cf24d1290e07ed302774b3fb7ee00d28..d2fca32ef3a2155eb78eec3034e67649336c3c0f 100644 (file)
@@ -1,4 +1,5 @@
 #include <sys/param.h>
+#include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #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
index f6736faa0a0fb701cbfd4867245993e8855d21a5..2cdfbbeaba296e919ff68abb92015eb2647fbf38 100644 (file)
@@ -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
index 9651609164ce4d5a5679928ba7e4f1b1b2947337..2ff96a457524c7a1e28a13c6e098de535bf0e006 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #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
index 69d631fa9dfa59b78362d69e2c830783a86ff725..2d346d9a4557a72325a18ae6f89c232e5f629681 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_DATAFILE_COLLECTION_H_
 
 #include <msp/core/meta.h>
+#include <msp/core/maputils.h>
 #include <msp/core/refptr.h>
 #include "loader.h"
 
@@ -178,10 +179,11 @@ public:
        template<typename T>
        void add(const std::string &name, T *d)
        {
-               if(items.count(name))
-                       throw KeyError("Duplicate key in collection", name);
+               typedef typename RemoveConst<T>::Type NCT;
 
-               items[name]=new Item<typename RemoveConst<T>::Type>(d);
+               RefPtr<Item<NCT> > i=new Item<NCT>(d);
+               insert_unique(items, i.get());
+               i.release();
        }
 
        /**
@@ -192,11 +194,9 @@ public:
        {
                typedef typename RemoveConst<T>::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<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
+               const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i);
                if(!item)
                        throw TypeError("Type mismatch on item '"+name+"'");
 
@@ -213,8 +213,7 @@ public:
        {
                typedef typename RemoveConst<T>::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<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
+               ItemBase *i=get_item(items, name);
+
+               const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i);
                if(!item)
                        throw TypeError("Type mismatch on item '"+name+"'");