aboutsummaryrefslogtreecommitdiff
path: root/odb/section.hxx
blob: 990dd321eacb42abd4599603139dadbb5f250a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// file      : odb/section.hxx
// copyright : Copyright (c) 2009-2015 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