#include <sys/param.h>
+#include <msp/core/maputils.h>
#include <msp/strings/format.h>
#include "binaryparser.h"
#include "input.h"
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;
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
std::string parse_string();
bool parse_bool();
std::string parse_enum();
- const std::string &lookup_string(unsigned) const;
};
} // namespace DataFile
+#include <msp/core/maputils.h>
#include "binarywriter.h"
#include "statement.h"
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())
{
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
#define MSP_DATAFILE_COLLECTION_H_
#include <msp/core/meta.h>
+#include <msp/core/maputils.h>
#include <msp/core/refptr.h>
#include "loader.h"
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();
}
/**
{
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+"'");
{
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)
{
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+"'");