<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://stlab.adobe.com/wiki/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://stlab.adobe.com/wiki/index.php?title=Copy_On_Write&amp;feed=atom&amp;action=history</id>
		<title>Copy On Write - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://stlab.adobe.com/wiki/index.php?title=Copy_On_Write&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://stlab.adobe.com/wiki/index.php?title=Copy_On_Write&amp;action=history"/>
		<updated>2013-05-22T11:33:44Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.19.0</generator>

	<entry>
		<id>http://stlab.adobe.com/wiki/index.php?title=Copy_On_Write&amp;diff=1452&amp;oldid=prev</id>
		<title>FosterBrereton: initial population</title>
		<link rel="alternate" type="text/html" href="http://stlab.adobe.com/wiki/index.php?title=Copy_On_Write&amp;diff=1452&amp;oldid=prev"/>
				<updated>2006-06-22T17:19:35Z</updated>
		
		<summary type="html">&lt;p&gt;initial population&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;===Reference Links===&lt;br /&gt;
&lt;br /&gt;
* [http://opensource.adobe.com/classadobe_1_1copy__on__write.html Current ASL Documentation]&lt;br /&gt;
* [http://opensource.adobe.com/group__concept__regular__type.html Regular Type Documentation]&lt;br /&gt;
&lt;br /&gt;
===Additional Documentation===&lt;br /&gt;
&lt;br /&gt;
Any type that models a !RegularType can be wrapped to use copy_on_write. To use copy_on_write you specify your variable as such:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// to be used later in the example&lt;br /&gt;
void stream_out(const int&amp;amp; x)&lt;br /&gt;
{&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; ' ';&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
typedef adobe::copy_on_write&amp;lt;std::vector&amp;lt;int&amp;gt; &amp;gt; cow_vec_t;&lt;br /&gt;
&lt;br /&gt;
cow_vec_t vec1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have your type established, you reference the wrapped type either using &amp;lt;code&amp;gt;operator -&amp;gt;&amp;lt;/code&amp;gt; (which returns a &amp;lt;code&amp;gt;const reference&amp;lt;/code&amp;gt; to your type) or via the &amp;lt;code&amp;gt;.write()&amp;lt;/code&amp;gt; mechanism, which returns a writeable reference. It is at the time when you call &amp;lt;code&amp;gt;write()&amp;lt;/code&amp;gt; that the copy on write wrapper spins off a copy of the encapsulated element if there are more than one references to the shared element under the hood&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vec1.write().push_back(42);&lt;br /&gt;
vec1.write().push_back(100);&lt;br /&gt;
vec1.write().push_back(1978);&lt;br /&gt;
&lt;br /&gt;
std::vector&amp;lt;int&amp;gt;::const_iterator first(vec1-&amp;gt;begin());&lt;br /&gt;
std::vector&amp;lt;int&amp;gt;::const_iterator last(vec1-&amp;gt;end());&lt;br /&gt;
&lt;br /&gt;
// ... do something with first, last ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assignment doesn't spin off a new copy of the thing, it just increments the reference count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cow_vec_t vec2;&lt;br /&gt;
&lt;br /&gt;
vec2 = vec1; // does not spin off a copy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, using &amp;lt;code&amp;gt;write()&amp;lt;/code&amp;gt; a copy is spun off if need be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vec2.write() /*now the copy is spun off*/ .push_back(84);&lt;br /&gt;
&lt;br /&gt;
adobe::for_each(vec1.get(), stream_out); // prints &amp;quot;42 100 1978 &amp;quot;&lt;br /&gt;
adobe::for_each(vec2.get(), stream_out); // prints &amp;quot;42 100 1978 84 &amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I say &amp;quot;if need be&amp;quot; because if there is only one reference to the current object then the write operation returns the current element itself. You can check if the reference you are currently holding is the only reference via &amp;lt;code&amp;gt;unique&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cow_vec_t vec3;&lt;br /&gt;
&lt;br /&gt;
vec3.write().push_back(42);&lt;br /&gt;
&lt;br /&gt;
assert(vec3.unique_instance());   // ... only one instance refers to the shared part &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also check to see if two copy_on_write instances are pointing to the same shared reference:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cow_vec_t vec4;&lt;br /&gt;
&lt;br /&gt;
vec4.write().push_back(42);&lt;br /&gt;
&lt;br /&gt;
cow_vec_t vec4(vec5);&lt;br /&gt;
&lt;br /&gt;
assert(vec4.identity(vec5));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>FosterBrereton</name></author>	</entry>

	</feed>