aboutsummaryrefslogtreecommitdiff
path: root/odb/section.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-05-06 12:05:39 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-08-14 15:18:23 +0200
commit91830e3bd38a05c73d03a5dfb88997799d44274b (patch)
tree399f1fc61ce959ac63b8b7ebaf5e3c58f80d8a7d /odb/section.hxx
parent72b262ee4f375089256f6438152bd323df1ff5a3 (diff)
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.
Diffstat (limited to 'odb/section.hxx')
-rw-r--r--odb/section.hxx114
1 files changed, 114 insertions, 0 deletions
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 <odb/pre.hxx>
+
+#include <odb/transaction.hxx>
+#include <odb/details/export.hxx>
+
+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<section*> (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 <odb/post.hxx>
+
+#endif // ODB_SECTION_HXX