aboutsummaryrefslogtreecommitdiff
path: root/odb/container-traits.hxx
blob: f65197fd07ffc0131b225ba6b6a0339ae9d01eb0 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// file      : odb/container-traits.hxx
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_CONTAINER_TRAITS_HXX
#define ODB_CONTAINER_TRAITS_HXX

#include <odb/pre.hxx>

#include <odb/forward.hxx>

namespace odb
{
  // Keep this enum synchronized with the one in odb/odb/context.hxx.
  //
  enum container_kind
  {
    ck_ordered,
    ck_set,
    ck_multiset,
    ck_map,
    ck_multimap
  };

  //
  // Container API provided by the generated code.
  //

  // Ordered containers.
  //
  template <typename I, typename V>
  struct ordered_functions
  {
    typedef I index_type;
    typedef V value_type;

    // Return true if the order is preserved in the database. If the
    // order is not preserved, then the index argument in the functions
    // below is not used.
    //
    bool
    ordered () const
    {
      return ordered_;
    }

    void
    insert_one (I index, const V& value) const
    {
      insert_one_ (index, value, data_);
    }

    bool
    load_all (I& next_index, V& next_value) const
    {
      return load_all_ (next_index, next_value, data_);
    }

    void
    delete_all () const
    {
      delete_all_ (data_);
    }

    // Implementation details.
    //
  public:
    typedef void (*insert_one_type) (I, const V&, void*);
    typedef bool (*load_all_type) (I&, V&, void*);
    typedef void (*delete_all_type) (void*);

    ordered_functions (void* data,
                       insert_one_type io,
                       load_all_type la,
                       delete_all_type da)
        : data_ (data), insert_one_ (io), load_all_ (la), delete_all_ (da)
    {
    }

    void
    ordered (bool v)
    {
      ordered_ = v;
    }

  private:
    void* data_;
    bool ordered_;
    insert_one_type insert_one_;
    load_all_type load_all_;
    delete_all_type delete_all_;
  };

  // Set/multiset containers.
  //
  template <typename V>
  struct set_functions
  {
    typedef V value_type;

    void
    insert_one (const V& value) const
    {
      insert_one_ (value, data_);
    }

    bool
    load_all (V& next_value) const
    {
      return load_all_ (next_value, data_);
    }

    void
    delete_all () const
    {
      delete_all_ (data_);
    }

    // Implementation details.
    //
  public:
    typedef void (*insert_one_type) (const V&, void*);
    typedef bool (*load_all_type) (V&, void*);
    typedef void (*delete_all_type) (void*);

    set_functions (void* data,
                   insert_one_type io,
                   load_all_type la,
                   delete_all_type da)
        : data_ (data), insert_one_ (io), load_all_ (la), delete_all_ (da)
    {
    }

  private:
    void* data_;
    insert_one_type insert_one_;
    load_all_type load_all_;
    delete_all_type delete_all_;
  };

  // Map/multimap containers.
  //
  template <typename K, typename V>
  struct map_functions
  {
    typedef K key_type;
    typedef V value_type;

    void
    insert_one (const K& key, const V& value) const
    {
      insert_one_ (key, value, data_);
    }

    bool
    load_all (K& next_key, V& next_value) const
    {
      return load_all_ (next_key, next_value, data_);
    }

    void
    delete_all () const
    {
      delete_all_ (data_);
    }

    // Implementation details.
    //
  public:
    typedef void (*insert_one_type) (const K&, const V&, void*);
    typedef bool (*load_all_type) (K&, V&, void*);
    typedef void (*delete_all_type) (void*);

    map_functions (void* data,
                   insert_one_type io,
                   load_all_type la,
                   delete_all_type da)
        : data_ (data), insert_one_ (io), load_all_ (la), delete_all_ (da)
    {
    }

  private:
    void* data_;
    insert_one_type insert_one_;
    load_all_type load_all_;
    delete_all_type delete_all_;
  };
}

#include <odb/post.hxx>

#include <odb/std-map-traits.hxx>
#include <odb/std-set-traits.hxx>
#include <odb/std-list-traits.hxx>
#include <odb/std-vector-traits.hxx>

#endif // ODB_CONTAINER_TRAITS_HXX