aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hybrid/sequence.txx
blob: 12faa9992f93fe765ab08b4ce651f3a78dd8d029 (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
// file      : xsde/cxx/hybrid/sequence.txx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <new> // placement new

namespace xsde
{
  namespace cxx
  {
    namespace hybrid
    {
      //
      // fix_seq
      //

      template <typename T>
      void fix_seq<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_seq<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_seq<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_seq<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_seq<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_seq<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_seq
      //

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

        size_ = 0;
      }
    }
  }
}