Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
copy_on_write.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_UTIL_COPY_ON_WRITE_H_
20 #define PAGESPEED_KERNEL_UTIL_COPY_ON_WRITE_H_
21 
23 
24 namespace net_instaweb {
25 
33 template<class T>
34 class CopyOnWrite {
35  public:
39 
42  explicit CopyOnWrite(const T& obj) : reference_(obj) {}
43  ~CopyOnWrite() {}
44 
45  const T* get() const { return reference_.get(); }
46  const T* operator->() const { return reference_.get(); }
47  const T& operator*() const { return *reference_.get(); }
48 
54  if (!reference_.unique()) {
55  reference_.reset(*get());
56  }
57  return reference_.get();
58  }
59 
60  CopyOnWrite& operator=(const CopyOnWrite& src) {
61  if (&src != this) {
62  reference_ = src.reference_;
63  }
64  return *this;
65  }
66 
67  CopyOnWrite(const CopyOnWrite& src) : reference_(src.reference_) {}
68 
71  void MergeOrShare(const CopyOnWrite& src) {
73  if (!src->empty()) {
75  if ((*this)->empty()) {
76  *this = src;
77  } else {
78  MakeWriteable()->Merge(*src.get());
79  }
80  }
81  }
82 
83  private:
84  typedef RefCountedObj<T> Reference;
85 
86  Reference reference_;
87 
89 };
90 
91 }
92 
93 #endif
T * MakeWriteable()
Definition: copy_on_write.h:53
bool unique() const
Definition: ref_counted_ptr.h:170
void reset(const T &val)
Definition: ref_counted_ptr.h:182
void MergeOrShare(const CopyOnWrite &src)
Definition: copy_on_write.h:71
Definition: copy_on_write.h:34
CopyOnWrite()
Definition: copy_on_write.h:38
CopyOnWrite(const T &obj)
Definition: copy_on_write.h:42
Definition: ref_counted_ptr.h:162