Commit aa035961b38b6f01090e6f6a2ee4c9b9fb5041e8

Authored by Jay Berkenbilt
1 parent 047bcfca

add * and -> operators

git-svn-id: svn+q:///qpdf/trunk@1029 71b93d88-0707-0410-a8cf-f5a4172ac649
include/qpdf/PointerHolder.hh
@@ -124,6 +124,24 @@ class PointerHolder @@ -124,6 +124,24 @@ class PointerHolder
124 return this->data->refcount; 124 return this->data->refcount;
125 } 125 }
126 126
  127 + T const& operator*() const
  128 + {
  129 + return *this->data->pointer;
  130 + }
  131 + T& operator*()
  132 + {
  133 + return *this->data->pointer;
  134 + }
  135 +
  136 + T const* operator->() const
  137 + {
  138 + return this->data->pointer;
  139 + }
  140 + T* operator->()
  141 + {
  142 + return this->data->pointer;
  143 + }
  144 +
127 private: 145 private:
128 void init(Data* data) 146 void init(Data* data)
129 { 147 {
libtests/pointer_holder.cc
@@ -12,6 +12,7 @@ class Object @@ -12,6 +12,7 @@ class Object
12 Object(); 12 Object();
13 ~Object(); 13 ~Object();
14 void hello(); 14 void hello();
  15 + void hello() const;
15 16
16 private: 17 private:
17 static int next_id; 18 static int next_id;
@@ -38,8 +39,21 @@ Object::hello() @@ -38,8 +39,21 @@ Object::hello()
38 std::cout << "calling Object::hello for " << this->id << std::endl; 39 std::cout << "calling Object::hello for " << this->id << std::endl;
39 } 40 }
40 41
  42 +void
  43 +Object::hello() const
  44 +{
  45 + std::cout << "calling Object::hello const for " << this->id << std::endl;
  46 +}
  47 +
41 typedef PointerHolder<Object> ObjectHolder; 48 typedef PointerHolder<Object> ObjectHolder;
42 49
  50 +void callHello(ObjectHolder const& oh)
  51 +{
  52 + oh.getPointer()->hello();
  53 + oh->hello();
  54 + (*oh).hello();
  55 +}
  56 +
43 int main(int argc, char* argv[]) 57 int main(int argc, char* argv[])
44 { 58 {
45 std::list<ObjectHolder> ol1; 59 std::list<ObjectHolder> ol1;
@@ -74,6 +88,9 @@ int main(int argc, char* argv[]) @@ -74,6 +88,9 @@ int main(int argc, char* argv[])
74 } 88 }
75 89
76 ol1.front().getPointer()->hello(); 90 ol1.front().getPointer()->hello();
  91 + ol1.front()->hello();
  92 + (*ol1.front()).hello();
  93 + callHello(ol1.front());
77 ol1.pop_front(); 94 ol1.pop_front();
78 std::cout << "goodbye" << std::endl; 95 std::cout << "goodbye" << std::endl;
79 return 0; 96 return 0;
libtests/qtest/ph/ph.out
@@ -7,6 +7,11 @@ equal okay @@ -7,6 +7,11 @@ equal okay
7 less than okay 7 less than okay
8 created Object, id 3 8 created Object, id 3
9 calling Object::hello for 1 9 calling Object::hello for 1
  10 +calling Object::hello for 1
  11 +calling Object::hello for 1
  12 +calling Object::hello const for 1
  13 +calling Object::hello const for 1
  14 +calling Object::hello const for 1
10 goodbye 15 goodbye
11 destroyed Object, id 3 16 destroyed Object, id 3
12 destroyed Object, id 1 17 destroyed Object, id 1