X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Fcollection.h;h=87af3a2c2c606cde35d00abd8a20bf092744e19d;hp=d0e8a1a8bb9b25c636ab23a8c1e4bbdcfa5c04a7;hb=b26fbba2bf5d53554325762b8022c501b3b8c8fb;hpb=0881c95bef812d94d8086225c2736b4f15b4c8dc diff --git a/source/collection.h b/source/collection.h index d0e8a1a..87af3a2 100644 --- a/source/collection.h +++ b/source/collection.h @@ -135,8 +135,27 @@ public: return extract(get_var(name, get_type(name))); } + /** Finds a typed object in the collection. Returns null if the name does + not exist. Throws if the name exists but the object is of an incorrect + type. */ + template + T *find(const std::string &name) const + { + ItemMap::const_iterator i = items.find(name); + return (i!=items.end() ? extract::Type>(i->second) : 0); + } + + template + T *find(const std::string &name) + { + typedef typename RemoveConst::Type NCT; + const Variant *var = find_var(name, get_type(name)); + return (var ? &extract(*var) : 0); + } + private: const Variant &get_var(const std::string &, const CollectionItemTypeBase *); + const Variant *find_var(const std::string &, const CollectionItemTypeBase *); template T &extract(const Variant &var) const;