2
/// This file is part of Rheolef.
4
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
6
/// Rheolef is free software; you can redistribute it and/or modify
7
/// it under the terms of the GNU General Public License as published by
8
/// the Free Software Foundation; either version 2 of the License, or
9
/// (at your option) any later version.
11
/// Rheolef is distributed in the hope that it will be useful,
12
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
/// GNU General Public License for more details.
16
/// You should have received a copy of the GNU General Public License
17
/// along with Rheolef; if not, write to the Free Software
18
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
/// =========================================================================
21
#include "rheolef/smart_pointer.h"
22
using namespace rheolef;
25
// data representation (could be file "container_data.h")
28
class container_data {
34
// the copy constructor
36
// performs a complete copy
38
container_data(const container_data& x )
39
: values(new T[x.n]), n(x.n)
40
{ for (int i=0; i<n;i++) values[i]=x.values[i];}
42
// a customized constructor
43
explicit container_data(int n1)
44
: values(new T[n1]), n(n1) {}
46
~container_data() { delete [] values; }
48
// read and write accessors are separated
49
const T& operator[](int i) const
55
// an interface to data via the Objet class
56
// that count occurence (could be "container.h")
58
class container : private smart_pointer<container_data> {
60
// the customized cstor
61
explicit container(int n = 0);
63
// read/write accessors
64
const T& operator[](int i) const;
68
// here is the implementation of the interface
69
// (could be "container.c")
71
container::container (int n)
72
: smart_pointer<container_data> (new container_data(n))
76
container::operator[] (int i) const
78
// use read access data()
79
return data().operator[] (i);
82
container::operator[] (int i)
84
// use write access data() that check occurence count
85
return data().operator [] (i);
88
// main test program ("main.c")
97
std::cerr << "fatal: It is not a true copy semantic." << std::endl;
100
std::cerr << "It seems to be a true copy semantic." << std::endl;