summaryrefslogtreecommitdiff
path: root/TAO/tests/Sequence_Unit_Tests/allocation_traits.hpp
blob: 24727f943ef3d4de4d6e5991c31c0c04b9749390 (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
#ifndef guard_allocation_traits_hpp
#define guard_allocation_traits_hpp
/**
 * @file
 *
 * @brief Details can be found in the documentation for
 * TAO::details::generic_sequence 
 *
 * $Id$
 *
 * @author Carlos O'Ryan
 */

#include "tao/Basic_Types.h"

namespace TAO
{
namespace details
{

template<typename T, bool dummy>
struct unbounded_allocation_traits
{
  typedef T value_type;

  inline static CORBA::ULong default_maximum()
  {
    return 0;
  }

  inline static value_type * default_buffer_allocation()
  {
    return 0;
  }

  inline static value_type * allocbuf(CORBA::ULong maximum)
  {
    return new value_type[maximum];
  }

  inline static void freebuf(value_type * buffer)
  {
    delete[] buffer;
  }
};

template<typename T, CORBA::ULong MAX, bool dummy>
struct bounded_allocation_traits
{
  typedef T value_type;

  inline static CORBA::ULong default_maximum()
  {
    return MAX;
  }

  inline static value_type * default_buffer_allocation()
  {
    return allocbuf(MAX);
  }

  inline static value_type * allocbuf(CORBA::ULong /* maximum */)
  {
    return new value_type[MAX];
  }

  inline static void freebuf(value_type * buffer)
  {
    delete[] buffer;
  }

  inline static CORBA::ULong maximum()
  {
    return MAX;
  }
  /* static CORBA::ULong const MAXIMUM = MAX; */
};

template<typename value_type, class reference_traits, bool dummy>
struct unbounded_reference_allocation_traits
  : public unbounded_allocation_traits<value_type,dummy>
{
  typedef unbounded_allocation_traits<value_type,dummy> base_allocation_traits;

  inline static value_type * allocbuf(CORBA::ULong maximum)
  {
    value_type * buffer =
      base_allocation_traits::allocbuf(maximum + 1);
    reinterpret_cast<value_type*>(buffer[0]) = buffer + maximum + 1;

    reference_traits::zero_range(buffer + 1, buffer + maximum + 1);

    return buffer + 1;
  }

  inline static void freebuf(value_type * buffer)
  {
    if(buffer != 0)
    {
      value_type * begin = buffer - 1;
      value_type * end = reinterpret_cast<value_type*>(*begin);
      reference_traits::release_range(buffer, end);

      buffer = begin;
    }
    base_allocation_traits::freebuf(buffer);
  }
};

template<typename value_type, class reference_traits, CORBA::ULong MAX, bool dummy>
struct bounded_reference_allocation_traits
  : public bounded_allocation_traits<value_type,MAX,dummy>
{
  typedef bounded_allocation_traits<value_type,MAX,dummy> base_allocation_traits;

  inline static value_type * allocbuf(CORBA::ULong /* maximum */)
  {
    value_type * buffer = base_allocation_traits::allocbuf(MAX);
    reference_traits::zero_range(buffer, buffer + MAX);
  }

  inline static void freebuf(value_type * buffer)
  {
    reference_traits::release_range(buffer, buffer + MAX);
    base_allocation_traits::freebuf(buffer);
  }
};

} // namespace details
} // namespace TAO

#endif // guard_allocation_traits_hpp