]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/collection.h
Add find functions to Collection
[libs/datafile.git] / source / collection.h
index d0e8a1a8bb9b25c636ab23a8c1e4bbdcfa5c04a7..87af3a2c2c606cde35d00abd8a20bf092744e19d 100644 (file)
@@ -135,8 +135,27 @@ public:
                return extract<NCT>(get_var(name, get_type<NCT>(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<typename T>
+       T *find(const std::string &name) const
+       {
+               ItemMap::const_iterator i = items.find(name);
+               return (i!=items.end() ? extract<typename RemoveConst<T>::Type>(i->second) : 0);
+       }
+
+       template<typename T>
+       T *find(const std::string &name)
+       {
+               typedef typename RemoveConst<T>::Type NCT;
+               const Variant *var = find_var(name, get_type<NCT>(name));
+               return (var ? &extract<NCT>(*var) : 0);
+       }
+
 private:
        const Variant &get_var(const std::string &, const CollectionItemTypeBase *);
+       const Variant *find_var(const std::string &, const CollectionItemTypeBase *);
 
        template<typename T>
        T &extract(const Variant &var) const;