Skip to content

Commit

Permalink
working on storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor-Haefner committed Apr 12, 2016
1 parent 5c7b2d7 commit ea23ae5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
22 changes: 18 additions & 4 deletions src/core/scripting/VRPyProjectManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "VRPyProjectManager.h"
#include "VRPyObject.h"
#include "VRPyTypeCaster.h"
#include "VRPyBaseT.h"

using namespace OSG;
Expand All @@ -12,23 +13,36 @@ PyMethodDef VRPyStorage::methods[] = {
};

PyMethodDef VRPyProjectManager::methods[] = {
{"addItem", (PyCFunction)VRPyProjectManager::addItem, METH_VARARGS, "Add a storable item - addItem( i )" },
{"addItem", (PyCFunction)VRPyProjectManager::addItem, METH_VARARGS, "Add a storable item - addItem( i, str mode )\n\tmode can be 'RELOAD' or 'REBUILD', reload will only reload the attributes of the object" },
{"getItems", (PyCFunction)VRPyProjectManager::getItems, METH_NOARGS, "Get all items - [items] getItems()" },
{"save", (PyCFunction)VRPyProjectManager::save, METH_VARARGS, "Save to file - save( str path )" },
{"load", (PyCFunction)VRPyProjectManager::load, METH_VARARGS, "Load from file - load( str path )" },
{NULL} /* Sentinel */
};

PyObject* VRPyProjectManager::getItems(VRPyProjectManager* self) {
if (!self->valid()) return NULL;
auto objs = self->objPtr->getItems();
PyObject* li = PyList_New(objs.size());
for (uint i=0; i<objs.size(); i++) {
VRObjectPtr o = static_pointer_cast<VRObject>(objs[i]);
if (o) PyList_SetItem(li, i, VRPyTypeCaster::cast( o ));
}
return li;
}

PyObject* VRPyProjectManager::addItem(VRPyProjectManager* self, PyObject* args) {
if (!self->valid()) return NULL;
PyObject* o;
if (! PyArg_ParseTuple(args, "O", &o)) return NULL;
PyObject* o; const char* m = 0;
if (! PyArg_ParseTuple(args, "Os", &o, (char*)&m)) return NULL;
if (!m) return NULL;

VRStoragePtr s;
if (VRPyObject::check(o)) s = dynamic_pointer_cast<VRStorage>(((VRPyObject*)o)->objPtr);
else if (VRPyStorage::check(o)) s = ((VRPyStorage*)o)->objPtr;
else { PyErr_SetString(err, "Not a storable item!"); return NULL; }

self->objPtr->addItem(s);
self->objPtr->addItem(s, m);
Py_RETURN_TRUE;
}

Expand Down
1 change: 1 addition & 0 deletions src/core/scripting/VRPyProjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct VRPyProjectManager : public VRPyBaseT<OSG::VRProjectManager> {
static PyMethodDef methods[];

static PyObject* addItem(VRPyProjectManager* self, PyObject* args);
static PyObject* getItems(VRPyProjectManager* self);
static PyObject* save(VRPyProjectManager* self, PyObject* args);
static PyObject* load(VRPyProjectManager* self, PyObject* args);
};
Expand Down
50 changes: 25 additions & 25 deletions src/core/tools/VRProjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,33 @@ VRProjectManager::VRProjectManager() : VRObject("ProjectManager") {}

VRProjectManagerPtr VRProjectManager::create() { return shared_ptr<VRProjectManager>(new VRProjectManager()); }

void VRProjectManager::setPersistencies(int reload, int rebuild) { reload_persistency = reload; rebuild_persistency = rebuild; }
void VRProjectManager::addItem(VRStoragePtr s) { if (s) vault.push_back(s); }
void VRProjectManager::addItem(VRStoragePtr s, string mode) {
if (!s) return;
if (mode == "RELOAD") vault_reload.push_back(s);
if (mode == "REBUILD") vault_rebuild.push_back(s);
}

vector<VRStoragePtr> VRProjectManager::getItems() { return vault_rebuild; }

void VRProjectManager::save(string path) {
if (fs::exists(path)) path = fs::canonical(path).string();
xmlpp::Document doc;
xmlpp::Element* root = doc.create_root_node("Project", "", "VRP"); // name, ns_uri, ns_prefix
for (auto v : vault) {
if (auto vs = v.lock()) {
vs->saveUnder(root); // objects with a persistency above this value will be saved
}

for (auto v : vault_reload) {
int p = v->getPersistency();
v->setPersistency(1);
v->saveUnder(root);
v->setPersistency(p);
}

for (auto v : vault_rebuild) {
int p = v->getPersistency();
v->setPersistency(2);
v->saveUnder(root);
v->setPersistency(p);
}

doc.write_to_file_formatted(path);
}

Expand All @@ -36,33 +51,18 @@ void VRProjectManager::load(string path) {
parser.parse_file(path.c_str());
xmlpp::Element* root = dynamic_cast<xmlpp::Element*>(parser.get_document()->get_root_node());

vector<VRStorageWeakPtr> new_vault;

vault_rebuild.clear();
int i=0;
for (auto n : root->get_children()) {
xmlpp::Element* e = dynamic_cast<xmlpp::Element*>(n);
if (!e) continue;

VRStoragePtr s;
int p = VRStorage::getPersistency(e);
cout << "VRProjectManager::load element persistency " << p << endl;

if (p == rebuild_persistency) {
cout << "VRProjectManager::load rebuild" << endl;
s = VRStorage::createFromStore(e);
}

if (p == reload_persistency) {
cout << "VRProjectManager::load reload" << endl;
if (i < vault.size()) s = vault[i].lock();
}


if (p == 1) { if (i < vault_reload.size()) s = vault_reload[i]; i++; }
if (p == 2) { s = VRStorage::createFromStore(e); vault_rebuild.push_back(s); }
if (!s) { cout << "VRProjectManager::load Warning! element unhandled"; continue; }

s->load(e);
new_vault.push_back(s);
i++;
}

vault = new_vault;
}
10 changes: 4 additions & 6 deletions src/core/tools/VRProjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ OSG_BEGIN_NAMESPACE;

class VRProjectManager : public VRObject {
private:
vector<VRStorageWeakPtr> vault;

int reload_persistency = 1;
int rebuild_persistency = 2;
vector<VRStoragePtr> vault_reload;
vector<VRStoragePtr> vault_rebuild;

public:
VRProjectManager();

static VRProjectManagerPtr create();

void setPersistencies(int reload, int rebuild);
void addItem(VRStoragePtr s);
void addItem(VRStoragePtr s, string mode);
vector<VRStoragePtr> getItems();

void save(string path);
void load(string path);
Expand Down

0 comments on commit ea23ae5

Please sign in to comment.