X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobjectloader.h;h=76fc412abed60db13c7b7e4b80837ffad0ca93df;hb=4edbe0eb078c4e480682862ccb68ebc8cb284045;hp=bab1ce5378f5bce344fd931d80061b6a67af9698;hpb=7df5e45c7f414f6a07681dc4ec2abb63b091a309;p=libs%2Fdatafile.git diff --git a/source/objectloader.h b/source/objectloader.h index bab1ce5..76fc412 100644 --- a/source/objectloader.h +++ b/source/objectloader.h @@ -1,11 +1,18 @@ #ifndef MSP_DATAFILE_OBJECTLOADER_H_ #define MSP_DATAFILE_OBJECTLOADER_H_ +#include #include "loader.h" namespace Msp { namespace DataFile { +class no_collection: public std::runtime_error +{ +public: + no_collection(const std::type_info &); +}; + class Collection; /** @@ -13,7 +20,7 @@ Provides the basic functionality of an object loader. Deriving from this allows loading values directly into member variables of the objects. */ template -class ObjectLoader: public Loader +class ObjectLoader: virtual public Loader { public: typedef O Object; @@ -28,6 +35,29 @@ public: }; +/** +Convenience class for loading derived objects. Inherits from the base class +loader and shadows its members with ones for the derived type. +*/ +template +class DerivedObjectLoader: public B +{ +public: + typedef O Object; + +protected: + O &obj; + + DerivedObjectLoader(O &o): B(o), obj(o) { } + + template + DerivedObjectLoader(O &o, T &a): B(o, a), obj(o) { } + +public: + O &get_object() const { return obj; } +}; + + /** Provides functionality for loading objects with a Collection. Deriving from this allows loading pointers to objects in the collection automatically. @@ -47,7 +77,7 @@ public: C &get_collection() const { if(!coll) - throw InvalidState("No collection"); + throw no_collection(typeid(O)); return *coll; } };