Wt: Wt::Dbo::weak_ptr< C > Class Template Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions
Wt::Dbo::weak_ptr< C > Class Template Reference
Database Objects (Wt::Dbo)

A weak pointer for a database object. More...

`#include <Wt/Dbo/weak_ptr>`

List of all members.

## Public Member Functions
| weak_ptr ()
| Creates a new weak pointer.
| weak_ptr (const weak_ptr< C > &other)
| Copy constructor.
void | reset (C *obj=0)
| Sets the value.
weak_ptr< C > & | operator= (const weak_ptr< C > &other)
| Assignment operator.
weak_ptr< C > & | operator= (const ptr< C > &other)
| Assignment operator.
const C * | operator-> () const
| Dereference operator.
C * | modify () const
| Dereference operator, for writing.
bool | operator== (const weak_ptr< C > &other) const
| Comparison operator.
bool | operator== (const ptr< C > &other) const
| Comparison operator.
bool | operator!= (const weak_ptr< C > &other) const
| Comparison operator.
bool | operator!= (const ptr< C > &other) const
| Comparison operator.
| operator bool () const
| Checks for null.
| operator ptr< C > () const
| Casting operator to a ptr.
ptr< C > | query () const
| Promotes to a ptr.
ptr< C > | lock () const
| Promotes to a ptr.
dbo_traits< C >::IdType | id () const
| Returns the object id.

## Detailed Description

### template<class C>
class Wt::Dbo::weak_ptr< C >

A weak pointer for a database object.

A weak_ptr has similar API and capabilities as a ptr, but does not actually store a reference to the object itself. Instead, it uses a query to retrieve the object.

It is used only to implement a OneToOne relation:

- class A hasOne class B object, in a weak_ptr
- class B belongsTo a class A object, in a ptr

This cannot be implemented using two plain ptr's because otherwise a cycle would be created which interferes with the count-based shared pointer implementation of ptr.

Only in a OneToOne relation, the required querying logic for the weak_ptr to function properly is provided by the hasOne() call, and thus a weak_ptr cannot be used outside of a OneToOne relation.

The weak_ptr has almost the same capabilities as a ptr, except that it does not provide a dereference operator (*).

It's important to realize that reading a weak_ptr results in a query, and thus you may want to first convert the weak_ptr into a plain ptr when needing multiple operations.

## Constructor & Destructor Documentation

template<class C >

Wt::Dbo::weak_ptr< C >::weak_ptr | ( |  | ) |

Creates a new weak pointer.

The ptr does not point to anything.

## Member Function Documentation

template<class C >

dbo_traits< C >::IdType Wt::Dbo::weak_ptr< C >::id | ( |  | ) | const

Returns the object id.

Since this needs to query the value, you should have an active transaction.

template<class C >

ptr<C> Wt::Dbo::weak_ptr< C >::lock | ( |  | ) | const

Promotes to a ptr.

Returns the value as a plain ptr.

Since this needs to query the value, you should have an active transaction.

This is equivalent to query() or the ptr<C> cast operator.

template<class C >

weak_ptr< C >::mutator Wt::Dbo::weak_ptr< C >::modify | ( |  | ) | const

Dereference operator, for writing.

Returns the underlying object (or, rather, a proxy for it) with the intention to modify it. The proxy object will mark the object as dirty from its destructor. An involved modification should therefore preferably be implemented as a separate method or function to make sure that the object is marked as dirty after the whole modification:

       weak_ptr<A> a = ...;
       a.modify()->doSomething();

Since this needs to query the value, you should have an active transaction.

template<class C >

Wt::Dbo::weak_ptr< C >::operator bool | ( |  | ) | const

Checks for null.

Returns true if the pointer is pointing to a non-null object.

Since this needs to query the value, you should have an active transaction.

template<class C >

Wt::Dbo::weak_ptr< C >::operator ptr< C > | ( |  | ) | const

Casting operator to a ptr.

Returns the value as a plain ptr.

Since this needs to query the value, you should have an active transaction.

template<class C >

bool Wt::Dbo::weak_ptr< C >::operator!= | ( | const weak_ptr< C > & | other | ) | const

Comparison operator.

Two pointers are equal if and only if they reference the same database object.

Since this needs to query the value, you should have an active transaction.

template<class C >

bool Wt::Dbo::weak_ptr< C >::operator!= | ( | const ptr< C > & | other | ) | const

Comparison operator.

Two pointers are equal if and only if they reference the same database object.

Since this needs to query the value, you should have an active transaction.

template<class C >

weak_ptr< C >::accessor Wt::Dbo::weak_ptr< C >::operator-> | ( |  | ) | const

Dereference operator.

Note that this operator returns a const copy of the referenced object. Use modify() to get a non-const reference.

Since this needs to query the value, you should have an active transaction.

template<class C >

weak_ptr< C > & Wt::Dbo::weak_ptr< C >::operator= | ( | const weak_ptr< C > & | other | ) |

Assignment operator.

Since this needs to query the previous value, you should have an active transaction.

template<class C >

weak_ptr< C > & Wt::Dbo::weak_ptr< C >::operator= | ( | const ptr< C > & | other | ) |

Assignment operator.

Since this needs to query the previous value, you should have an active transaction.

template<class C >

bool Wt::Dbo::weak_ptr< C >::operator== | ( | const weak_ptr< C > & | other | ) | const

Comparison operator.

Two pointers are equal if and only if they reference the same database object.

Since this needs to query the value, you should have an active transaction.

template<class C >

bool Wt::Dbo::weak_ptr< C >::operator== | ( | const ptr< C > & | other | ) | const

Comparison operator.

Two pointers are equal if and only if they reference the same database object.

Since this needs to query the value, you should have an active transaction.

template<class C >

ptr< C > Wt::Dbo::weak_ptr< C >::query | ( |  | ) | const

Promotes to a ptr.

Returns the value as a plain ptr.

Since this needs to query the value, you should have an active transaction.

This is equivalent to lock() or the ptr<C> cast operator.

template<class C >

void Wt::Dbo::weak_ptr< C >::reset | ( | C * | obj = `0` | ) |

Sets the value.

This is assigns a new value, and:

      p.reset(obj);

is equivalent to:

      p = Wt::Dbo::ptr<T>(obj);

Since this needs to query the previous value, you should have an active transaction.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator