From 91830e3bd38a05c73d03a5dfb88997799d44274b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 6 May 2013 12:05:39 +0200 Subject: Add support for object sections Sections are an optimization mechanism that allows the partitioning of data members of a persistent class into groups that can be separately loaded and/or updated. --- odb/section.hxx | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 odb/section.hxx (limited to 'odb/section.hxx') diff --git a/odb/section.hxx b/odb/section.hxx new file mode 100644 index 0000000..e32996c --- /dev/null +++ b/odb/section.hxx @@ -0,0 +1,114 @@ +// file : odb/section.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SECTION_HXX +#define ODB_SECTION_HXX + +#include + +#include +#include + +namespace odb +{ + class LIBODB_EXPORT section + { + public: + // Load state. + // + bool + loaded () const {return state_.loaded;} + + // Mark a loaded section as not loaded. This, for example, can be + // useful if you don't want the section to be reloaded during the + // object reload. + // + void + unload () + { + state_.loaded = 0; + state_.changed = 0; + state_.restore = 0; + } + + // Change state. + // + bool + changed () const {return state_.changed;} + + // Mark the section as changed. + // + void + change () + { + state_.changed = 1; + state_.restore = 0; + } + + // User data. 4 bits of custom state. + // + unsigned char + user_data () const {return state_.user;} + + void + user_data (unsigned char u) {state_.user = u;} + + public: + section () + { + state_.loaded = 0; + state_.changed = 0; + state_.armed = 0; + state_.restore = 0; + } + + ~section () + { + if (state_.armed) + disarm (); + } + + // Implementation details. + // + public: + // Arm the callback and set the restore flag if transaction is not NULL. + // + void + reset (bool l = false, bool c = false, transaction* t = 0) const + { + state_.loaded = l; + state_.changed = c; + + if (t != 0 && !state_.armed) + { + t->callback_register (&transacion_callback, + const_cast (this)); + state_.armed = 1; + } + + state_.restore = (t != 0); + } + + private: + void + disarm (); + + static void + transacion_callback (unsigned short, void* key, unsigned long long); + + private: + mutable struct + { + unsigned char loaded : 1; + unsigned char changed : 1; + unsigned char armed : 1; // Transaction callback is armed. + unsigned char restore: 1; // Restore changed flag on rollback. + unsigned char user : 4; // User data. + } state_; + }; +} + +#include + +#endif // ODB_SECTION_HXX -- cgit v1.1