blocxx
COWReferenceBase.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38#ifndef BLOCXX_CBLOCXX_REFERENCE_BASE_HPP_INCLUDE_GUARD_
39#define BLOCXX_CBLOCXX_REFERENCE_BASE_HPP_INCLUDE_GUARD_
40#include "blocxx/BLOCXX_config.h"
41#include "blocxx/RefCount.hpp"
42
43// The classes and functions defined in this file are not meant for general
44// use, they are internal implementation details. They may change at any time.
45
46namespace BLOCXX_NAMESPACE
47{
48
50template<class T>
51inline void COWRefSwap(T& x, T&y)
52{
53 T t = x;
54 x = y;
55 y = t;
56}
57
59// This class contains the non-templated code for COWReference, to help
60// minimize code bloat.
61class BLOCXX_COMMON_API COWReferenceBase
62{
63protected:
65 : m_pRefCount(new RefCount) {}
67 : m_pRefCount(arg.m_pRefCount)
68 {
69 m_pRefCount->inc();
70 }
71 void incRef()
72 {
73 m_pRefCount->inc();
74 }
75
76 bool decRef()
77 {
78 if (m_pRefCount->decAndTest())
79 {
80 delete m_pRefCount;
81 return true;
82 }
83 return false;
84 }
85
87 {
88 return m_pRefCount->get() > 1;
89 }
90
91 // returns true if we're the only copy.
93 {
94 if (m_pRefCount->decAndTest())
95 {
96 // only copy--don't need to clone, also not a race condition
97 incRef();
98 return true;
99 }
100 else
101 {
102 // need to become unique
103 m_pRefCount = new RefCount;
104 return false;
105 }
106 }
107
109 {
110 COWRefSwap(m_pRefCount, arg.m_pRefCount);
111 }
112
114 {
115 decRef();
116 m_pRefCount = arg.m_pRefCount;
117 incRef();
118 }
119
120protected:
122};
123
124} // end namespace BLOCXX_NAMESPACE
125
126#endif
127
128
COWReferenceBase(const COWReferenceBase &arg)
void useRefCountOf(const COWReferenceBase &arg)
void swap(COWReferenceBase &arg)
Taken from RFC 1321.
void COWRefSwap(T &x, T &y)