aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hybrid/sequence.txx
blob: 6d834fc6d5b29342015124fa9d8c6b17dd89f0eb (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// file      : xsde/cxx/hybrid/sequence.txx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <new> // placement new

namespace xsde
{
  namespace cxx
  {
    namespace hybrid
    {
      //
      // fix_sequence
      //

      template <typename T>
      void fix_sequence<T>::
      clear ()
      {
        for (size_t i = 0;  i < size_; ++i)
          static_cast<T*> (data_)[i].~T ();

        size_ = 0;
      }

#ifdef XSDE_EXCEPTIONS
      template <typename T>
      void fix_sequence<T>::
      assign (const T* p, size_t n)
      {
        if (size_ != 0)
          clear ();

        reserve (n);

        for (; size_ < n; ++size_)
          new (static_cast<T*> (data_) + size_) T (p[size_]);
      }
#else
      template <typename T>
      sequence_base::error fix_sequence<T>::
      assign (const T* p, size_t n)
      {
        if (size_ != 0)
          clear ();

        if (error r = reserve (n))
          return r;

        for (; size_ < n; ++size_)
          new (static_cast<T*> (data_) + size_) T (p[size_]);

        return error_none;
      }
#endif

#ifdef XSDE_EXCEPTIONS
      template <typename T>
      void fix_sequence<T>::
      move_ (void* dst, void* src, size_t n)
      {
        T* d = static_cast<T*> (dst);
        T* s = static_cast<T*> (src);

        // The copy c-tor can throw in which case we need to destroy
        // whatever objects we already copied into d.
        //
        size_t i = 0;
        guard g (d, i);

        for (; i < n; i++)
          new (d + i) T (s[i]);

        g.release ();

        for (size_t j = 0; j < n; j++)
          s[j].~T ();
      }
#else
      template <typename T>
      void fix_sequence<T>::
      move_ (void* dst, void* src, size_t n)
      {
        T* d = static_cast<T*> (dst);
        T* s = static_cast<T*> (src);

        for (size_t i = 0; i < n; i++)
        {
          new (d + i) T (s[i]);
          s[i].~T ();
        }
      }
#endif

      template <typename T>
      void fix_sequence<T>::
      move_forward_ (void* p, size_t n)
      {
        // We are moving a sequence of elements one position to the left.
        // The tricky part is to make sure we are in at least destructable
        // state if things turn bad. We assume that there is a valid
        // element at position p.
        //
        T* d = static_cast<T*> (p);

        for (size_t i = 0; i < n; i++)
          d[i] = d[i + 1];

        d[n].~T ();
      }

#ifdef XSDE_EXCEPTIONS
      template <typename T>
      void fix_sequence<T>::
      move_backward_ (void* p, size_t n, size_t& size)
      {
        // We are moving a sequence of elements one position to the right.
        // The tricky part is to make sure we are in at least destructable
        // state if things turn bad.
        //
        T* d = static_cast<T*> (p);
        T* e = d + n;

        new (e) T;
        size++;

        for (size_t i = n; i > 0; i--)
          d[i] = d[i - 1];
      }
#else
      template <typename T>
      void fix_sequence<T>::
      move_backward_ (void* p, size_t n)
      {
        // We are moving a sequence of elements one position to the right.
        //
        T* d = static_cast<T*> (p);
        T* e = d + n;

        new (e) T;

        for (size_t i = n; i > 0; i--)
          d[i] = d[i - 1];
      }
#endif

      //
      // var_sequence
      //

      template <typename T>
      void var_sequence<T>::
      clear ()
      {
        for (size_t i = 0;  i < size_; ++i)
        {
          T* x = static_cast<T**> (data_)[i];

#ifndef XSDE_CUSTOM_ALLOCATOR
          delete x;
#else
          if (x)
            x->~T ();
          cxx::free (x);
#endif
        }

        size_ = 0;
      }

#ifdef XSDE_EXCEPTIONS
      template <typename T>
      void var_sequence<T>::
      copy (var_sequence& c) const
      {
        if (c.size_ != 0)
          c.clear ();

        c.reserve (size_);

        for (; c.size_ < size_; ++c.size_)
        {
          static_cast<T**> (c.data_)[c.size_] =
            static_cast<T**> (data_)[c.size_]->_clone ();
        }
      }
#else
      template <typename T>
      sequence_base::error var_sequence<T>::
      copy (var_sequence& c) const
      {
        if (c.size_ != 0)
          c.clear ();

        if (error r = c.reserve (size_))
          return r;

        for (; c.size_ < size_; ++c.size_)
        {
          T* x = static_cast<T**> (data_)[c.size_]->_clone ();

          if (x == 0)
            return error_no_memory;

          static_cast<T**> (c.data_)[c.size_] = x;
        }

        return error_none;
      }
#endif
    }
  }
}