Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
section.hxx
Go to the documentation of this file.
1 // file : odb/section.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_SECTION_HXX
6 #define ODB_SECTION_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <odb/transaction.hxx>
11 #include <odb/details/export.hxx>
12 
13 namespace odb
14 {
15  class LIBODB_EXPORT section
16  {
17  public:
18  // Load state.
19  //
20  bool
21  loaded () const {return state_.loaded;}
22 
23  // Mark a loaded section as not loaded. This, for example, can be
24  // useful if you don't want the section to be reloaded during the
25  // object reload.
26  //
27  void
28  unload ()
29  {
30  state_.loaded = 0;
31  state_.changed = 0;
32  state_.restore = 0;
33  }
34 
35  // Change state.
36  //
37  bool
38  changed () const {return state_.changed;}
39 
40  // Mark the section as changed.
41  //
42  void
43  change ()
44  {
45  state_.changed = 1;
46  state_.restore = 0;
47  }
48 
49  // User data. 4 bits of custom state.
50  //
51  unsigned char
52  user_data () const {return state_.user;}
53 
54  void
55  user_data (unsigned char u) {state_.user = u;}
56 
57  public:
59  {
60  state_.loaded = 0;
61  state_.changed = 0;
62  state_.armed = 0;
63  state_.restore = 0;
64  }
65 
67  {
68  if (state_.armed)
69  disarm ();
70  }
71 
72  // Implementation details.
73  //
74  public:
75  // Arm the callback and set the restore flag if transaction is not NULL.
76  //
77  void
78  reset (bool l = false, bool c = false, transaction* t = 0) const
79  {
80  state_.loaded = l;
81  state_.changed = c;
82 
83  if (t != 0 && !state_.armed)
84  {
85  t->callback_register (&transacion_callback,
86  const_cast<section*> (this));
87  state_.armed = 1;
88  }
89 
90  state_.restore = (t != 0);
91  }
92 
93  private:
94  void
95  disarm ();
96 
97  static void
98  transacion_callback (unsigned short, void* key, unsigned long long);
99 
100  private:
101  mutable struct
102  {
103  unsigned char loaded : 1;
104  unsigned char changed : 1;
105  unsigned char armed : 1; // Transaction callback is armed.
106  unsigned char restore: 1; // Restore changed flag on rollback.
107  unsigned char user : 4; // User data.
108  } state_;
109  };
110 }
111 
112 #include <odb/post.hxx>
113 
114 #endif // ODB_SECTION_HXX
section()
Definition: section.hxx:58
Definition: transaction.hxx:22
~section()
Definition: section.hxx:66
void change()
Definition: section.hxx:43
unsigned char user_data() const
Definition: section.hxx:52
bool loaded() const
Definition: section.hxx:21
void reset(bool l=false, bool c=false, transaction *t=0) const
Definition: section.hxx:78
bool changed() const
Definition: section.hxx:38
void user_data(unsigned char u)
Definition: section.hxx:55
Definition: section.hxx:15
void unload()
Definition: section.hxx:28