~ubuntu-branches/ubuntu/raring/rheolef/raring-proposed

« back to all changes in this revision

Viewing changes to util/tst/smart_pointer_tst.cc

  • Committer: Package Import Robot
  • Author(s): Pierre Saramito, Pierre Saramito, Sylvestre Ledru
  • Date: 2012-05-14 14:02:09 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120514140209-dzbdlidkotyflf9e
Tags: 6.1-1
[ Pierre Saramito ]
* New upstream release 6.1 (minor changes):
  - support arbitrarily polynomial order Pk
  - source code supports g++-4.7 (closes: #671996)

[ Sylvestre Ledru ]
* update of the watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
///
 
2
/// This file is part of Rheolef.
 
3
///
 
4
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
5
///
 
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.
 
10
///
 
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.
 
15
///
 
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
 
19
///
 
20
/// =========================================================================
 
21
#include "rheolef/smart_pointer.h"
 
22
using namespace rheolef;
 
23
using namespace std;
 
24
//
 
25
// data representation (could be file "container_data.h")
 
26
//
 
27
typedef int T;
 
28
class container_data {
 
29
    private:  
 
30
        T *values;
 
31
        int n;
 
32
    public: 
 
33
        //       IMPORTANT: 
 
34
        //  the copy constructor 
 
35
        //        **MAY** 
 
36
        //  performs a complete copy
 
37
        //
 
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];}
 
41
 
 
42
        // a customized constructor
 
43
        explicit container_data(int n1)
 
44
         : values(new T[n1]), n(n1) {}
 
45
 
 
46
        ~container_data() { delete [] values; }
 
47
 
 
48
        // read and write accessors are separated
 
49
        const T& operator[](int i) const 
 
50
                    { return values[i]; }
 
51
              T& operator[](int i)       
 
52
                    { return values[i]; }
 
53
};
 
54
//
 
55
// an interface to data via the Objet class
 
56
//        that count occurence (could be "container.h")
 
57
//
 
58
class container : private smart_pointer<container_data> {
 
59
public:
 
60
    // the customized cstor
 
61
    explicit container(int n = 0);
 
62
 
 
63
    // read/write accessors
 
64
    const T&  operator[](int i) const;
 
65
          T&  operator[](int i);
 
66
};
 
67
//
 
68
// here is the implementation of the interface
 
69
//  (could be "container.c")
 
70
//
 
71
container::container (int n)
 
72
: smart_pointer<container_data> (new container_data(n))
 
73
{
 
74
}
 
75
const T&
 
76
container::operator[] (int i) const
 
77
{
 
78
    // use read access data()
 
79
    return data().operator[] (i);
 
80
}
 
81
T&
 
82
container::operator[] (int i)
 
83
{
 
84
    // use write access data() that check occurence count
 
85
    return data().operator [] (i);
 
86
}
 
87
//
 
88
// main test program ("main.c")
 
89
//
 
90
int main()
 
91
{
 
92
    container A(10);
 
93
    A[1] = 1;
 
94
    container B = A;
 
95
    B[1] = 2;
 
96
    if (A[1] == B[1]) {
 
97
            std::cerr << "fatal: It is not a true copy semantic." << std::endl;
 
98
        exit(1);
 
99
    }
 
100
    std::cerr << "It seems to be a true copy semantic." << std::endl;
 
101
    return 0;
 
102
}