HElib 1.0 Implementing Homomorphic Encryption

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Friends Pages
cloned_ptr.h
Go to the documentation of this file.
1 /* Copyright (C) 2012,2013 IBM Corp.
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 * See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16 #ifndef CLONED_PTR_H
17 #define CLONED_PTR_H
18
26 /*
27 * This seems like a very useful template class for smart
28 * pointers with "deep cloning" semantics and class heirarchies.
29 *
30 * To work, a class needs a "clone" method that creates a new copy.
31 *
32 * NOTES:
33 *
34 * this did not compile...had to change the definition of the class clone<X>
35 *
36 * added boolean operators
37 *
38 * added assign operation set_ptr(X* p) removed get(), introduced public
39 * method get_ptr()... these names chosen to make it easier to hunt down
40 * "loopholes"
41 *
42 * added noclone<X> template
43 *
44 * Made "const"-ness apply to both the pointer and the object pointed to.
45 * For this kind of "deep copy" semantics, this makes the most sense.
46 *
47 * Made a corresponding class copied_ptr, with "shallow" cloning...
48 * To bad this can't really be done with templates... C++11 defines
49 * the concept of "template aliases", which would do the trick...
50 * unfortunately, gcc does not yet implement this...
51 *
52 * Changed the name "clone" to avoid conflicts...
53 *****/
54
55 /* For ANSI-challenged compilers, you may want to
56 * #define NO_MEMBER_TEMPLATES or explicit */
57
63 template <class X> class deep_clone
64 {
65 public:
66 static X* apply(const X* x) {return x->clone();}
67 };
68
74 template <class X> class shallow_clone
75 {
76 public:
77 static X* apply(const X* x) {return new X(*x); }
78 };
79
80
81 #ifndef NO_MEMBER_TEMPLATES
82
83 #define CLONED_PTR_TEMPLATE_MEMBERS(CLONED_PTR_TYPE) \
84 \
85 template <class Y> CLONED_PTR_TYPE(const CLONED_PTR_TYPE<Y>& r) \
86 {copy(r.ptr);} \
87 template <class Y> CLONED_PTR_TYPE& operator=(const CLONED_PTR_TYPE<Y>& r) \
88 { \
89 if (this != &r) { \
90 delete ptr; \
91 copy(r.ptr); \
92 } \
93 return *this; \
94 } \
95
96 #else
97
98 #define CLONED_PTR_TEMPLATE_MEMBERS(CLONED_PTR_TYPE)
99
100 #endif
101
102
103
104 #define CLONED_PTR_DECLARE(CLONED_PTR_TYPE,CLONED_PTR_INIT) \
105 \
106 template <class X, class Cloner = CLONED_PTR_INIT<X> > class CLONED_PTR_TYPE \
107 { \
108 public: \
109 typedef X element_type; \
110 \
111 explicit CLONED_PTR_TYPE(X* p = 0) : ptr(p) {} \
112 ~CLONED_PTR_TYPE() {delete ptr;} \
113 CLONED_PTR_TYPE(const CLONED_PTR_TYPE& r) {copy(r.ptr);} \
114 \
115 CLONED_PTR_TYPE& operator=(const CLONED_PTR_TYPE& r) \
116 { \
117 if (this != &r) { \
118 delete ptr; \
119 copy(r.ptr); \
120 } \
121 return *this; \
122 } \
123 \
124 void set_ptr(X* p) \
125 { \
126 if (ptr != p) { \
127 delete ptr; \
128 ptr = p; \
129 } \
130 } \
131 \
132 CLONED_PTR_TEMPLATE_MEMBERS(CLONED_PTR_TYPE) \
133 \
134 const X& operator*() const {return *ptr;} \
135 X& operator*() {return *ptr;} \
136 \
137 const X* operator->() const {return ptr;} \
138 X* operator->() {return ptr;} \
139 \
140 bool null() const { return ptr == NULL; } \
141 \
142 const X* get_ptr() const { return ptr; } \
143 X* get_ptr() { return ptr; } \
144 \
145 private: \
146 X* ptr; \
147 \
148 void copy(X* p) {ptr = (p ? Cloner::apply(p) : 0);} \
149 }; \
150
151
152 // declare the template class cloned_ptr<X>
153 CLONED_PTR_DECLARE(cloned_ptr, deep_clone)
154
155 // declare the template class coppied_ptr<X>
156 CLONED_PTR_DECLARE(copied_ptr, shallow_clone)
157
158 // template <class X> using copied_ptr = cloned_ptr<X, noclone<X> >;
159
160
161
162 #endif // CLONED_PTR_H
163