sharedheap_def.h 5.43 KB
Newer Older
1
2
3
4
5
/*! \file sharedheap_def.h
 * \brief shared heap definitions (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id$
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
 * \author Thomas Forbriger
 * \since 08/12/2002
 * 
 * shared heap definitions (prototypes)
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * \note
 * Never include this header directly. Here you find code factored out from
 * the aff::SharedHeap class. This code may be compiled into a binary library.
 *
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 08/12/2002   V1.0   copied from libcontxx
 *  - 20/12/2002   V1.1   (thof)
 *                        - moved SHeap to namespace util
 *  - 28/12/2002   V1.2   (thof)
 *                        - changed base class from specialization to
 *                          independent class template
 *  - 31/12/2002   V1.3   (thof)
 *                        - NULL is deprecated as pointed out by Wolfgang
 *                        - removed constructor from Theadstruct
 *  - 19/12/2003   V1.4   (thof)
 *                        - allocate at least one element (default
 *                          constructor)
 * 
 * ============================================================================
 */

#ifndef AFF_SHAREDHEAP_H_VERSION 
#error "include this only through sharedheap.h"
#endif

// include guard
#ifndef AFF_SHAREDHEAP_DEF_H_VERSION

#define AFF_SHAREDHEAP_DEF_H_VERSION \
  "AFF_SHAREDHEAP_DEF_H   V1.4"
#define AFF_SHAREDHEAP_DEF_H_CVSID \
61
  "$Id$"
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

namespace aff {

#ifdef AFF_PREBUILT
namespace prebuilt {
#endif

#ifdef AFF_PREBUILT
#ifndef AFF_COMPILING_LIBRARY
#error "definition read in prebuilt mode and not during library compilation"
#endif
#endif

/*======================================================================*/

namespace util {

  //! create counted reference object
  template <typename T>
    inline
    SHeap<T>::SHeap(const Tsize& size)
    : Mheap(0), Msize(size), Mnref(1), Mextern(false)
    { 
      if (size>0) {
        try { Mheap=new T[size]; }
        catch (std::bad_alloc) {
          throw AllocException(size, sizeof(T));
        }
      } else { 
        Msize=0;
      }
    }

  /*----------------------------------------------------------------------*/

  //! create counted reference object from external memory
  template <typename T>
    inline
    SHeap<T>::SHeap(T* pointer, const Tsize& size)
    : Mheap(pointer), Msize(size), Mnref(1), Mextern(true) { }

  /*----------------------------------------------------------------------*/

  //! remove counted reference
  template <typename T>
    inline
    SHeap<T>::~SHeap()
    {
      if ((Mheap!=0) && (Msize>0) && (!Mextern))
      { 
        delete[] Mheap; 
      }
    }

} // namespace util

/*======================================================================*/

  //! create representation from nothing
  template <typename T>
    inline
    ConstSharedHeap<T>::ConstSharedHeap()
    : Mheapstruct(new Theapstruct(1)) { }

  /*----------------------------------------------------------------------*/

  //! create representation for given number of elements
  template <typename T>
    inline
    ConstSharedHeap<T>::ConstSharedHeap(const Tsize& size)
    : Mheapstruct(new Theapstruct(size)) { }

  /*----------------------------------------------------------------------*/

  //! create from externally managed memory
  template <typename T>
    inline
    ConstSharedHeap<T>::ConstSharedHeap(T* pointer, const Tsize& size)
    : Mheapstruct(new Theapstruct(pointer, size)) { }

  /*----------------------------------------------------------------------*/

  //! create representation from another representation
  template <typename T>
    inline
    ConstSharedHeap<T>::ConstSharedHeap(const Tcontainer& sharedheap)
    : Mheapstruct(sharedheap.Mheapstruct) 
    { ++(Mheapstruct->Mnref); }

  /*----------------------------------------------------------------------*/

  //! delete representation
  template <typename T>
    inline
    ConstSharedHeap<T>::~ConstSharedHeap()
    {
      if (--(Mheapstruct->Mnref) < 1)
      {
        delete Mheapstruct;
      }
    }

  /*----------------------------------------------------------------------*/

  //! copy representation by exchanging counted reference
  template <typename T>
    inline
    ConstSharedHeap<T>&
    ConstSharedHeap<T>::operator=(const ConstSharedHeap<T>& sharedheap)
    {
      if (Mheapstruct != sharedheap.Mheapstruct)
      {
        if (--(Mheapstruct->Mnref) < 1)
        {
          delete Mheapstruct;
        }
        Mheapstruct=sharedheap.Mheapstruct;
        ++(Mheapstruct->Mnref);
      }
      return(*this);
    }

#ifdef AFF_PREBUILT
} // namespace prebuilt
#endif

} // namespace aff

#endif // AFF_SHAREDHEAP_DEF_H_VERSION (includeguard)

/* ----- END OF sharedheap_def.h ----- */