Xfce Foundation Classes
Main Page | Index | Namespace List | Alphabetical List | Class List | File List

# Xfc::G::StaticPrivate< T > Struct Template Reference

A compile-time GStaticPrivate C++ wrapper interface. More...

`#include <xfc/glib/thread.hh>`

Inheritance diagram for Xfc::G::StaticPrivate< T >:

List of all members.

## Public Member Functions

Accessors

- GStaticPrivate * g_static_private () const
    Get a pointer to the GStaticPrivate object.
- operator GStaticPrivate * () const
    Conversion operator; Safely converts a G::StaticPrivate object into a GStaticPrivate pointer.

Methods

- T * get () const
    Gets the pointer keyed to the private key for the current thread.
- void set (T *data, GDestroyNotify notify=&delete_data)
    Sets the pointer keyed to the private key for the current thread, and the destroy notify function to be called with that pointer (null or non-null), whenever the pointer is set again or whenever the current thread ends.

## Static Public Member Functions

- void delete_data (void *data)
    Default callback function called to delete the data pointer passed to set().

## Detailed Description

### template<typename T>
struct Xfc::G::StaticPrivate< T >

A compile-time GStaticPrivate C++ wrapper interface.

A StaticPrivate works almost like a Private, but it has one significant advantage. It doesn't need to be created at run-time like a Private, but can be defined at compile-time. This is similar to the difference between G::Mutex and G::StaticMutex. Now look at our give_me_next_number() example with StaticPrivate:

Example: Using StaticPrivate for per-thread data.

        int give_me_next_number ()
        {
            G::StaticPrivate current_number_key = XFC_STATIC_PRIVATE_INIT;
            int *current_number = current_number_key.get();

            if (!current_number)
            {
                    current_number = new int;
                    *current_number = 0;
                    current_number_key->set(current_number);
            }
            *current_number = calc_next_number(*current_number);
            return *current_number;
        }

## Member Function Documentation

template<typename T>
void Xfc::G::StaticPrivate< T >::delete_data | ( | void * | data | ) | `[inline, static]`

Default callback function called to delete the data pointer passed to set().

Parameters:

  | data | The data pointer passed to set().

The data pointer must be allocated by operator new.

template<typename T>
T* Xfc::G::StaticPrivate< T >::get | ( |  | ) | const`[inline]`

Gets the pointer keyed to the private key for the current thread.

Returns:
  The corresponding data pointer.

This pointer is null when set() hasn't been called for the current private key and thread yet. This method can also be used if G::Thread::init() has not yet been called, and will return the value of the private key cast to a 'T' pointer then.

template<typename T>
void Xfc::G::StaticPrivate< T >::set | ( | T * | data,
|  | GDestroyNotify | notify = `&delete_data`
| ) | `[inline]`

Sets the pointer keyed to the private key for the current thread, and the destroy notify function to be called with that pointer (null or non-null), whenever the pointer is set again or whenever the current thread ends.

Parameters:

  | data | The new data pointer.
  | notify | A function to be called with data whenever the current thread ends or sets the pointer again.

The default destroy notify function delete_data simply calls operator delete on data, so data must have been allocated by operator new. This method also works if G::Thread::init() has not yet been called. If G::Thread::init() is called later, the data keyed to the private key will be inherited only by the main thread, that is, the one that called G::Thread::init().

The documentation for this struct was generated from the following file:

- thread.hh

Xfce Foundation Classes

Copyright © 2004-2005 The XFC Development Team | XFC 4.3