// { dg-do assemble }
|
// { dg-do assemble }
|
// prms-id: 13911
|
// prms-id: 13911
|
|
|
template
|
template
|
class ref_counter {
|
class ref_counter {
|
public:
|
public:
|
ref_counter() : p_refcnt(new unsigned int(N)) {}
|
ref_counter() : p_refcnt(new unsigned int(N)) {}
|
ref_counter(const ref_counter& x) : p_refcnt(x.p_refcnt) {
|
ref_counter(const ref_counter& x) : p_refcnt(x.p_refcnt) {
|
++*p_refcnt;
|
++*p_refcnt;
|
}
|
}
|
ref_counter& operator=(const ref_counter& rhs) {
|
ref_counter& operator=(const ref_counter& rhs) {
|
++*rhs.p_refcnt;
|
++*rhs.p_refcnt;
|
decrement();
|
decrement();
|
p_refcnt = rhs.p_refcnt;
|
p_refcnt = rhs.p_refcnt;
|
return *this;
|
return *this;
|
}
|
}
|
~ref_counter() {decrement();}
|
~ref_counter() {decrement();}
|
|
|
bool unique() const {return *p_refcnt == N;}
|
bool unique() const {return *p_refcnt == N;}
|
|
|
private:
|
private:
|
unsigned int* p_refcnt;
|
unsigned int* p_refcnt;
|
void decrement() {
|
void decrement() {
|
if (unique()) delete p_refcnt;
|
if (unique()) delete p_refcnt;
|
else --*p_refcnt;
|
else --*p_refcnt;
|
}
|
}
|
};
|
};
|
|
|
template
|
template
|
class ref_pointer {
|
class ref_pointer {
|
public:
|
public:
|
|
|
ref_pointer() : the_p(0) {}
|
ref_pointer() : the_p(0) {}
|
ref_pointer(T* just_newed) : the_p(just_newed) {}
|
ref_pointer(T* just_newed) : the_p(just_newed) {}
|
virtual ~ref_pointer() {if (unique()) delete the_p;}
|
virtual ~ref_pointer() {if (unique()) delete the_p;}
|
protected:
|
protected:
|
ref_pointer(T* the_p_arg, ref_counter& ref_count_arg)
|
ref_pointer(T* the_p_arg, ref_counter& ref_count_arg)
|
: the_p(the_p_arg), ref_count(ref_count_arg) {}
|
: the_p(the_p_arg), ref_count(ref_count_arg) {}
|
|
|
public:
|
public:
|
|
|
ref_pointer& operator=(const ref_pointer&);
|
ref_pointer& operator=(const ref_pointer&);
|
ref_pointer& operator=(T*);
|
ref_pointer& operator=(T*);
|
operator const T*() const {return the_p;}
|
operator const T*() const {return the_p;}
|
T* operator()() {return the_p;}
|
T* operator()() {return the_p;}
|
T* operator()() const {return the_p;}
|
T* operator()() const {return the_p;}
|
T& operator*() const {return *the_p;}
|
T& operator*() const {return *the_p;}
|
friend bool operator==(const ref_pointer& lhs,
|
friend bool operator==(const ref_pointer& lhs,
|
const ref_pointer& rhs) {
|
const ref_pointer& rhs) {
|
return lhs.the_p == rhs.the_p;
|
return lhs.the_p == rhs.the_p;
|
}
|
}
|
friend bool operator!=(const ref_pointer& lhs,
|
friend bool operator!=(const ref_pointer& lhs,
|
const ref_pointer& rhs) {
|
const ref_pointer& rhs) {
|
return lhs.the_p != rhs.the_p;
|
return lhs.the_p != rhs.the_p;
|
}
|
}
|
|
|
|
|
bool unique() const {return ref_count.unique();}
|
bool unique() const {return ref_count.unique();}
|
bool isNull() const {return the_p==0;}
|
bool isNull() const {return the_p==0;}
|
|
|
protected:
|
protected:
|
ref_counter& refCount() {return ref_count;}
|
ref_counter& refCount() {return ref_count;}
|
|
|
private:
|
private:
|
|
|
ref_counter ref_count;
|
ref_counter ref_count;
|
T* the_p;
|
T* the_p;
|
};
|
};
|
|
|
template
|
template
|
ref_pointer& ref_pointer::operator=(const ref_pointer& rhs) {
|
ref_pointer& ref_pointer::operator=(const ref_pointer& rhs) {
|
if (the_p != rhs.the_p) {
|
if (the_p != rhs.the_p) {
|
if (unique()) delete the_p;
|
if (unique()) delete the_p;
|
the_p = rhs.the_p;
|
the_p = rhs.the_p;
|
ref_count = rhs.ref_count;
|
ref_count = rhs.ref_count;
|
}
|
}
|
return *this;
|
return *this;
|
}
|
}
|
|
|
|
|
template
|
template
|
ref_pointer& ref_pointer::operator=(T* just_newed) {
|
ref_pointer& ref_pointer::operator=(T* just_newed) {
|
if (unique()) delete the_p;
|
if (unique()) delete the_p;
|
the_p = just_newed;
|
the_p = just_newed;
|
ref_count = ref_counter();
|
ref_count = ref_counter();
|
return *this;
|
return *this;
|
}
|
}
|
|
|
|
|
|
|
template
|
template
|
class CountedObjPtr : public ref_pointer {
|
class CountedObjPtr : public ref_pointer {
|
public:
|
public:
|
CountedObjPtr() {}
|
CountedObjPtr() {}
|
CountedObjPtr(T* just_newed) : ref_pointer(just_newed) {}
|
CountedObjPtr(T* just_newed) : ref_pointer(just_newed) {}
|
CountedObjPtr(T* the_p_arg, ref_counter<1>& ref_count_arg)
|
CountedObjPtr(T* the_p_arg, ref_counter<1>& ref_count_arg)
|
: ref_pointer(the_p_arg, ref_count_arg) {}
|
: ref_pointer(the_p_arg, ref_count_arg) {}
|
CountedObjPtr& operator=(T* rhs) {
|
CountedObjPtr& operator=(T* rhs) {
|
ref_pointer::operator=(rhs);
|
ref_pointer::operator=(rhs);
|
return *this;
|
return *this;
|
}
|
}
|
CountedObjPtr& operator=(const CountedObjPtr& rhs) {
|
CountedObjPtr& operator=(const CountedObjPtr& rhs) {
|
ref_pointer::operator=(rhs);
|
ref_pointer::operator=(rhs);
|
return *this;
|
return *this;
|
}
|
}
|
T* operator->() const {return (*this)();}
|
T* operator->() const {return (*this)();}
|
|
|
};
|
};
|
|
|
|
|
|
|
|
|
|
|
//instantiating type
|
//instantiating type
|
|
|
class TSObservable;
|
class TSObservable;
|
|
|
class TSObserver {
|
class TSObserver {
|
public:
|
public:
|
|
|
enum TSType { NormalTS, UpYldCrvTS, DownYldCrvTS, ZeroVolTS };
|
enum TSType { NormalTS, UpYldCrvTS, DownYldCrvTS, ZeroVolTS };
|
|
|
virtual ~TSObserver() {}
|
virtual ~TSObserver() {}
|
|
|
virtual void update(TSObservable* theChangedObservable) = 0;
|
virtual void update(TSObservable* theChangedObservable) = 0;
|
virtual TSType key() const { return myKey; }
|
virtual TSType key() const { return myKey; }
|
virtual TSType& key() { return myKey; }
|
virtual TSType& key() { return myKey; }
|
protected:
|
protected:
|
TSObserver(TSType myKeyArg) : myKey(myKeyArg) {}
|
TSObserver(TSType myKeyArg) : myKey(myKeyArg) {}
|
TSType myKey;
|
TSType myKey;
|
};
|
};
|
|
|
|
|
|
|
//now try to instantiate
|
//now try to instantiate
|
template class CountedObjPtr;
|
template class CountedObjPtr;
|
|
|