aboutsummaryrefslogtreecommitdiff
path: root/odb/vector-traits.hxx
blob: eae2c5b2936adf19b1bde6559e16dd31882ccd36 (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
// file      : odb/vector-traits.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_VECTOR_TRAITS_HXX
#define ODB_VECTOR_TRAITS_HXX

#include <odb/pre.hxx>

#include <odb/vector.hxx>
#include <odb/vector-impl.hxx>
#include <odb/container-traits.hxx>
#include <odb/transaction.hxx>

namespace odb
{
  template <typename V, typename A LIBODB_VECTOR_ARG_DECL>
  class access::container_traits<vector<V, A LIBODB_VECTOR_ARG_USE> >
  {
  public:
    static const container_kind kind = ck_ordered;
    static const bool smart = true;

    typedef vector<V, A> container_type;

    typedef V value_type;
    typedef typename container_type::size_type index_type;

    typedef smart_ordered_functions<index_type, value_type> functions;
    typedef ordered_functions<index_type, value_type> dumb_functions;

  public:
    static void
    persist (const container_type& c, const functions& f)
    {
      for (index_type i (0), n (c.size ()); i < n; ++i)
        f.insert (i, c[i]);

      // Now that this container is persistent, start tracking changes.
      //
      c._start ();
    }

    static void
    load (container_type& c, bool more, const functions& f)
    {
      // Stop tracking changes.
      //
      c._stop ();

      // Load.
      //
      c.clear ();
      while (more)
      {
        index_type dummy;
        c.push_back (value_type ());
        more = f.select (dummy, c.modify_back ());
      }

      // Start tracking changes.
      //
      c._start ();
    }

    static bool
    changed (const container_type&);

    static void
    update (const container_type&, const functions&);

    static void
    erase (const container_type* c, const functions& f)
    {
      f.delete_ (0);

      // Stop tracking changes.
      //
      if (c != 0)
        c->_stop ();
    }

    // Version of load() for dumb functions. Used to support
    // inverse members of the container type. The implementation
    // is identical to the smart one except we don't turn off/on
    // change tracking.
    //
    static void
    load (container_type& c, bool more, const dumb_functions& f)
    {
      c.clear ();

      while (more)
      {
        index_type dummy;
        c.push_back (value_type ());
        more = f.select (dummy, c.modify_back ());
      }
    }
  };
}

#include <odb/vector-traits.txx>

#include <odb/post.hxx>

#endif // ODB_VECTOR_TRAITS_HXX