]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/objectloader.h
Add a two-argument constructor to DerivedObjectLoader
[libs/datafile.git] / source / objectloader.h
index 515ea6b8b81c6d51338735a704f960a0f27b802a..a2a8720e840e6393f3e185950741fc6ba0df5729 100644 (file)
@@ -1,18 +1,18 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2010  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_DATAFILE_OBJECTLOADER_H_
 #define MSP_DATAFILE_OBJECTLOADER_H_
 
+#include <typeinfo>
 #include "loader.h"
 
 namespace Msp {
 namespace DataFile {
 
+class no_collection: public std::runtime_error
+{
+public:
+       no_collection(const std::type_info &);
+};
+
 class Collection;
 
 /**
@@ -35,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<typename O, typename B>
+class DerivedObjectLoader: public B
+{
+public:
+       typedef O Object;
+
+protected:
+       O &obj;
+
+       DerivedObjectLoader(O &o): B(o), obj(o) { }
+
+       template<typename T>
+       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.
@@ -54,7 +77,7 @@ public:
        C &get_collection() const
        {
                if(!coll)
-                       throw InvalidState("No collection");
+                       throw no_collection(typeid(O));
                return *coll;
        }
 };