aboutsummaryrefslogtreecommitdiff
path: root/odb/qt/containers/list-traits.txx
blob: 8af5c8583153984281f7ab624d5a2b2f6bf7f5b3 (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
// file      : odb/qt/containers/list-traits.txx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

namespace odb
{
  template <typename V>
  bool access::container_traits<QOdbList<V> >::
  changed (const container_type& c)
  {
    // Because modifications can cancel each other (e.g., push and pop),
    // it is tricky to keep track of whether there are any changes in
    // the container. Instead, we are just going to examine each element
    // just like update().
    //

    // We should either be tracking or summarily changed.
    //
    if (c._tracking ())
    {
      const vector_impl& impl (c._impl ());

      for (std::size_t i (0), n (impl.size ()); i < n; ++i)
      {
        if (impl.state (i) != vector_impl::state_unchanged)
          return true;
      }
    }
    else
      return true;

    return false;
  }

  template <typename V>
  void access::container_traits<QOdbList<V> >::
  update (const container_type& c, const functions& f)
  {
    bool u (false); // Updated flag.

    if (c._tracking ())
    {
      const vector_impl& impl (c._impl ());

      for (std::size_t i (0), n (impl.size ()); i < n; ++i)
      {
        vector_impl::element_state_type s (impl.state (i));
        index_type ii (static_cast<index_type> (i));

        switch (s)
        {
        case vector_impl::state_unchanged:
          {
            break;
          }
        case vector_impl::state_inserted:
          {
            f.insert (ii, c[ii]);
            u = u || true;
            break;
          }
        case vector_impl::state_updated:
          {
            f.update (ii, c[ii]);
            u = u || true;
            break;
          }
        case vector_impl::state_erased:
          {
            f.delete_ (ii); // Delete from i onwards.
            u = u || true;
            break;
          }
        }

        // We delete all trailing elements in one go.
        //
        if (s == vector_impl::state_erased)
          break;
      }
    }
    else
    {
      // Fall back to delete all/insert all.
      //
      f.delete_ (0);

      for (index_type i (0), n (c.size ()); i < n; ++i)
        f.insert (i, c[i]);

      u = true;
    }

    // Arm the rollback callback and (re)start change tracking.
    //
    if (u)
    {
      c._arm (transaction::current ());
      c._start ();
    }
  }
}