added loaded callbacks to DataSet
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 11 Nov 2017 04:57:12 +0000 (06:57 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 11 Nov 2017 04:57:12 +0000 (06:57 +0200)
src/dataset.h
src/dataset.inl

index a4736c4..5c4ca51 100644 (file)
 #ifndef DATASET_H_
 #define DATASET_H_
 
+#include <vector>
 #include <string>
 #include <map>
 #include <resman.h>
 
 template <typename T>
 class DataSet {
+public:
+       typedef void (*LoadedCallbackFunc)(T, void*);
+
 protected:
        mutable std::map<std::string, T> data;
        mutable struct resman *rman;
 
+       std::vector<std::pair<LoadedCallbackFunc, void*> > loaded_cb;
+
        T (*create)();
        bool (*load)(T, const char*);
        bool (*done)(T);
@@ -46,6 +52,9 @@ public:
        void update();
 
        T get(const char *name) const;
+
+       void add_loaded_callback(LoadedCallbackFunc func, void *cls = 0);
+       bool remove_loaded_callback(LoadedCallbackFunc func);
 };
 
 #include "dataset.inl"
index ca01ea2..954081e 100644 (file)
@@ -51,6 +51,28 @@ T DataSet<T>::get(const char *name) const
        return res;
 }
 
+template <typename T>
+void DataSet<T>::add_loaded_callback(LoadedCallbackFunc func, void *cls)
+{
+       std::pair<LoadedCallbackFunc, void*> cb;
+       cb.first = func;
+       cb.second = cls;
+       loaded_cb.push_back(cb);
+}
+
+template <typename T>
+bool DataSet<T>::remove_loaded_callback(LoadedCallbackFunc func)
+{
+       int numcb = (int)loaded_cb.size();
+       for(int i=0; i<numcb; i++) {
+               if(loaded_cb[i].first == func) {
+                       loaded_cb.erase(loaded_cb.begin() + i);
+                       return true;
+               }
+       }
+       return false;
+}
+
 
 // --- static functions to pass as callback to resman ---
 
@@ -84,6 +106,11 @@ int DataSet<T>::dataset_done_func(int id, void *cls)
        if(dset->done) {
                dset->done(data);
        }
+
+       int numcb = (int)dset->loaded_cb.size();
+       for(int i=0; i<numcb; i++) {
+               dset->loaded_cb[i].first(data, dset->loaded_cb[i].second);
+       }
        return 0;
 }