File: CloningWrapper.h

package info (click to toggle)
rosegarden 1%3A1.4.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 23,188 kB
  • ctags: 15,707
  • sloc: cpp: 148,264; xml: 4,687; python: 1,946; makefile: 1,146; perl: 1,033; sh: 587; ansic: 337; ruby: 173
file content (147 lines) | stat: -rw-r--r-- 3,811 bytes parent folder | download | duplicates (2)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// -*- c-basic-offset: 4 -*-

// -*- c-file-style:  "bsd" -*-
#ifndef _CLONING_WRAPPER_H_
#define _CLONING_WRAPPER_H_

// This is basically just another smart pointer; it wrappers a
// pointer-type (or some other wacky reference, like a CORBA object
// ref) and deals with aliasing, so that if you copy one of these
// wrappers the object is shared, but if you modify one copy (or at
// least call a non-const accessor) it unshares the data so the other
// one remains unmodified.

#include <cassert>

/* DefaultCloner works for any object that has a clone() method.
   clone() could be virtual, so it works for e.g. lists of pointers
   to various object types with a common base */
   
template <class Tptr>
struct DefaultCloner
{
    Tptr null() const { return 0; }
    Tptr clone(const Tptr t) const { return t->clone(); }
    void release(Tptr t) const { delete t; }
};

/* For CORBA object references, the following cloner works nicely:

template <class T, class Tptr>
struct CorbaCloner
{
    Tptr null() const { return T::_nil(); }
    Tptr clone(const Tptr t) const { return T::_duplicate(t); }
    void release(Tptr t) const { CORBA::release(t); }
};
*/

template <class Tptr, class Cloner = DefaultCloner<Tptr> >
class CloningWrapper
{
public:
    CloningWrapper(Cloner cloner = Cloner()) :
	m_t(cloner.null()), m_count(0), m_cloner(cloner) { }

    CloningWrapper(const Tptr t, Cloner cloner = Cloner()) :
	m_t(m_cloner.clone(t)), m_count(new int(1)), m_cloner(cloner) { }

    CloningWrapper(const CloningWrapper<Tptr, Cloner> &c) :
	m_t(c.m_t), m_count(c.m_count), m_cloner(c.m_cloner)
    {
	if (m_count) ++*m_count;
    }

    virtual ~CloningWrapper()
    {
//	cout << "wrapper dtor on " << this << endl;
	if (m_count && !--*m_count) {
	    m_cloner.release(m_t);
	    delete m_count;
	}
    }

    CloningWrapper &operator=(const CloningWrapper<Tptr, Cloner> &c)
    {
	if (&c != this) {
	    if (m_count && !--*m_count) {
		m_cloner.release(m_t);
		delete m_count;
	    }
	    m_t = c.m_t;
	    m_count = c.m_count;

	    // no -- keep the same cloner; all cloners of the same
	    // type should behave the same anyway, and egcs complains
	    // if you assign an empty object (as cloners tend to be)
//	    m_cloner = c.m_cloner; 

	    if (m_count) ++*m_count;
	}
	return *this;
    }

    virtual bool operator==(const CloningWrapper<Tptr, Cloner> &c) const
    {
	assert(0); // not meaningful: this and c may point to different subclasses
	return false;
    }

    virtual bool operator!=(const CloningWrapper<Tptr, Cloner> &c) const
    {
	assert(0); // not meaningful: this and c may point to different subclasses
	return false;
    }

    virtual bool operator<(const CloningWrapper<Tptr, Cloner> &c) const
    {
	assert(0); // likewise
	return false;
    }

    virtual const Tptr data() const
    {
	return m_t;
    }

    virtual const Tptr operator->() const
    {
	return m_t;
    }
    
    virtual Tptr cloned_data() const	 // caller owns returned value
    {
	return m_cloner.clone(m_t);
    }
    
    // The non-const accessors unshare the data, so that we don't
    // modify all copies.  This isn't completely secure, though -- we
    // could retain the non-const pointer returned from the accessor
    // until after this object's data has become shared again for any
    // reason, and then write through it.  I don't think we can win
    // here without making write-access rather hard work (e.g. with
    // a lock/unlock pair surrounding each access in the client code).

    virtual Tptr data()
    {
	if (m_count && *m_count > 1) {
	    --*m_count;
	    m_t = m_cloner.clone(m_t);
	    m_count = new int(1);
	}
	return m_t;
    }

    virtual Tptr operator->()
    {
	return data();
    }

private:
    Tptr m_t;
    int *m_count;
    Cloner m_cloner;
};

#endif