]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/objectloader.h
Cosmetic changes
[libs/datafile.git] / source / objectloader.h
index bab1ce5378f5bce344fd931d80061b6a67af9698..978366f730e51cc197fe7dd20ab7f56f4b52e84e 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_DATAFILE_OBJECTLOADER_H_
 #define MSP_DATAFILE_OBJECTLOADER_H_
 
+#include "except.h"
 #include "loader.h"
 
 namespace Msp {
@@ -13,7 +14,7 @@ Provides the basic functionality of an object loader.  Deriving from this
 allows loading values directly into member variables of the objects.
 */
 template<typename O>
-class ObjectLoader: public Loader
+class ObjectLoader: virtual public Loader
 {
 public:
        typedef O Object;
@@ -28,6 +29,27 @@ 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;
+
+       template<typename... Args>
+       DerivedObjectLoader(O &o, Args &&... a): B(o, std::forward<Args>(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.
@@ -39,7 +61,7 @@ public:
        typedef C Collection;
 
 protected:
-       C *coll;
+       C *coll = nullptr;
 
        CollectionObjectLoader(O &o, C *c): ObjectLoader<O>(o), coll(c) { }
 
@@ -47,7 +69,7 @@ public:
        C &get_collection() const
        {
                if(!coll)
-                       throw InvalidState("No collection");
+                       throw no_collection(typeid(O));
                return *coll;
        }
 };