Wt  4.10.4
Public Member Functions | List of all members
Wt::Dbo::weak_ptr< C > Class Template Reference

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

#include <Wt/Dbo/weak_ptr.h>

Public Member Functions

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

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:

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.

A Wt::Dbo::ptr<C> can be converted to a Wt::Dbo::ptr<const C> when extracting data from Dbo objects. There are overloads for the copy constructor, copy assignment, and comparison operators to make this work as expected.

Constructor & Destructor Documentation

◆ weak_ptr()

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

◆ id()

template<class C >
dbo_traits< C >::IdType Wt::Dbo::weak_ptr< C >::id

Returns the object id.

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

◆ lock()

template<class C >
ptr< C > Wt::Dbo::weak_ptr< C >::lock

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.

◆ modify()

template<class C >
weak_ptr< C >::mutator Wt::Dbo::weak_ptr< C >::modify

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.

◆ operator bool()

template<class C >
Wt::Dbo::weak_ptr< C >::operator bool
explicit

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.

◆ operator ptr< C >()

template<class C >
Wt::Dbo::weak_ptr< C >::operator ptr< C >

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.

◆ operator!=() [1/2]

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.

◆ operator!=() [2/2]

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.

◆ operator->()

template<class C >
weak_ptr< C >::accessor Wt::Dbo::weak_ptr< C >::operator->

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.

◆ operator=() [1/2]

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

Copy assignment operator.

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

◆ operator=() [2/2]

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

Copy assignment operator.

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

◆ operator==() [1/2]

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.

◆ operator==() [2/2]

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.

◆ query()

template<class C >
ptr< C > Wt::Dbo::weak_ptr< C >::query

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.

◆ reset()

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);
A smart pointer for a database object.
Definition: ptr.h:565

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