4 template<unsigned int N>
7 ref_counter() : p_refcnt(new unsigned int(N)) {}
8 ref_counter(const ref_counter<N>& x) : p_refcnt(x.p_refcnt) {
11 ref_counter& operator=(const ref_counter<N>& rhs) {
14 p_refcnt = rhs.p_refcnt;
17 ~ref_counter() {decrement();}
19 bool unique() const {return *p_refcnt == N;}
22 unsigned int* p_refcnt;
24 if (unique()) delete p_refcnt;
29 template<class T, unsigned int N>
33 ref_pointer() : the_p(0) {}
34 ref_pointer(T* just_newed) : the_p(just_newed) {}
35 virtual ~ref_pointer() {if (unique()) delete the_p;}
37 ref_pointer(T* the_p_arg, ref_counter<N>& ref_count_arg)
38 : the_p(the_p_arg), ref_count(ref_count_arg) {}
42 ref_pointer& operator=(const ref_pointer&);
43 ref_pointer& operator=(T*);
44 operator const T*() const {return the_p;}
45 T* operator()() {return the_p;}
46 T* operator()() const {return the_p;}
47 T& operator*() const {return *the_p;}
48 friend bool operator==(const ref_pointer<T, N>& lhs,
49 const ref_pointer<T, N>& rhs) {
50 return lhs.the_p == rhs.the_p;
52 friend bool operator!=(const ref_pointer<T, N>& lhs,
53 const ref_pointer<T, N>& rhs) {
54 return lhs.the_p != rhs.the_p;
58 bool unique() const {return ref_count.unique();}
59 bool isNull() const {return the_p==0;}
62 ref_counter<N>& refCount() {return ref_count;}
66 ref_counter<N> ref_count;
70 template<class T, unsigned int N>
71 ref_pointer<T, N>& ref_pointer<T, N>::operator=(const ref_pointer<T, N>& rhs) {
72 if (the_p != rhs.the_p) {
73 if (unique()) delete the_p;
75 ref_count = rhs.ref_count;
81 template<class T, unsigned int N>
82 ref_pointer<T, N>& ref_pointer<T, N>::operator=(T* just_newed) {
83 if (unique()) delete the_p;
85 ref_count = ref_counter<N>();
92 class CountedObjPtr : public ref_pointer<T, 1> {
95 CountedObjPtr(T* just_newed) : ref_pointer<T, 1>(just_newed) {}
96 CountedObjPtr(T* the_p_arg, ref_counter<1>& ref_count_arg)
97 : ref_pointer<T, 1>(the_p_arg, ref_count_arg) {}
98 CountedObjPtr<T>& operator=(T* rhs) {
99 ref_pointer<T, 1>::operator=(rhs);
102 CountedObjPtr<T>& operator=(const CountedObjPtr<T>& rhs) {
103 ref_pointer<T, 1>::operator=(rhs);
106 T* operator->() const {return (*this)();}
121 enum TSType { NormalTS, UpYldCrvTS, DownYldCrvTS, ZeroVolTS };
123 virtual ~TSObserver() {}
125 virtual void update(TSObservable* theChangedObservable) = 0;
126 virtual TSType key() const { return myKey; }
127 virtual TSType& key() { return myKey; }
129 TSObserver(TSType myKeyArg) : myKey(myKeyArg) {}
135 //now try to instantiate
136 template class CountedObjPtr<TSObserver>;