# PoolItem< T, Tid, Tpool > Struct Template Reference

Generalization for all pool items that are saved in the savegame. More...

`#include <oldpool.h>`

Inheritance diagram for PoolItem< T, Tid, Tpool >:

## Public Member Functions

virtual | ~PoolItem ()
| We like to have the correct class destructed.
| PoolItem ()
| Constructor of given class.
void * | operator new (size_t size)
| An overriden version of new that allocates memory on the pool.
void | operator delete (void *p)
| 'Free' the memory allocated by the overriden new.
void * | operator new (size_t size, int index)
| An overriden version of new, so you can directly allocate a new object with the correct index when one is loading the savegame.
void | operator delete (void *p, int index)
| 'Free' the memory allocated by the overriden new.
void * | operator new (size_t size, T *pn)
| An overriden version of new, so you can use the vehicle instance instead of a newly allocated piece of memory.
void | operator delete (void *p, T *pn)
| 'Free' the memory allocated by the overriden new.

## Static Public Member Functions

static bool | CanAllocateItem (uint count=1)
| Check whether we can allocate an item in this pool.

## Data Fields

Tid | index
| The pool-wide index of this object.

## Static Protected Member Functions

static T * | AllocateRaw ()
| Allocate a pool item; possibly allocate a new block in the pool.
static T * | AllocateRaw (uint &first)
| Allocate a pool item; possibly allocate a new block in the pool.
static bool | CleaningPool ()
| Are we cleaning this pool?

## Static Private Member Functions

static T * | AllocateSafeRaw (uint &first)
| Allocate a pool item; possibly allocate a new block in the pool.

## Detailed Description

### template<typename T, typename Tid, OldMemoryPool< T > * Tpool>
struct PoolItem< T, Tid, Tpool >

Generalization for all pool items that are saved in the savegame.

It specifies all the mechanics to access the pool easily.

Definition at line 181 of file oldpool.h.

## Constructor & Destructor Documentation

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

virtual PoolItem< T, Tid, Tpool >::~PoolItem | ( |  | ) | `[inline, virtual]`

We like to have the correct class destructed.

Warning:
  It is called even for object allocated on stack, so it is not present in the TPool! Then, index is undefined, not associated with TPool in any way.

Note:
  The idea is to free up allocated memory etc.

Definition at line 194 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

PoolItem< T, Tid, Tpool >::PoolItem | ( |  | ) | `[inline]`

Constructor of given class.

Warning:
  It is called even for object allocated on stack, so it may not be present in TPool! Then, index is undefined, not associated with TPool in any way.

Note:
  The idea is to initialize variables (except index)

Definition at line 206 of file oldpool.h.

## Member Function Documentation

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

void* PoolItem< T, Tid, Tpool >::operator new | ( | size_t | size | ) | `[inline]`

An overriden version of new that allocates memory on the pool.

Parameters:

  | size | the size of the variable (unused)

Precondition:
  CanAllocateItem()

Returns:
  the memory that is 'allocated'

Definition at line 217 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

void PoolItem< T, Tid, Tpool >::operator delete | ( | void * | p | ) | `[inline]`

'Free' the memory allocated by the overriden new.

Parameters:

  | p | the memory to 'free'

Note:
  we only update Tpool->first_free_index

Definition at line 227 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

void* PoolItem< T, Tid, Tpool >::operator new | ( | size_t | size,
|  | int | index |
| ) |  |  | `[inline]`

An overriden version of new, so you can directly allocate a new object with the correct index when one is loading the savegame.

Parameters:

  | size | the size of the variable (unused)
  | index | the index of the object

Returns:
  the memory that is 'allocated'

Definition at line 239 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

void PoolItem< T, Tid, Tpool >::operator delete | ( | void * | p,
|  | int | index |
| ) |  |  | `[inline]`

'Free' the memory allocated by the overriden new.

Parameters:

  | p | the memory to 'free'
  | index | the original parameter given to create the memory

Note:
  we only update Tpool->first_free_index

Definition at line 252 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

void* PoolItem< T, Tid, Tpool >::operator new | ( | size_t | size,
|  | T * | pn |
| ) |  |  | `[inline]`

An overriden version of new, so you can use the vehicle instance instead of a newly allocated piece of memory.

Parameters:

  | size | the size of the variable (unused)
  | pn | the already existing object to use as 'storage' backend

Returns:
  the memory that is 'allocated'

Definition at line 264 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

void PoolItem< T, Tid, Tpool >::operator delete | ( | void * | p,
|  | T * | pn |
| ) |  |  | `[inline]`

'Free' the memory allocated by the overriden new.

Parameters:

  | p | the memory to 'free'
  | pn | the pointer that was given to 'new' on creation.

Note:
  we only update Tpool->first_free_index

Definition at line 275 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

T * PoolItem< T, Tid, Tpool >::AllocateSafeRaw | ( | uint & | first | ) | `[inline, static, private]`

Allocate a pool item; possibly allocate a new block in the pool.

Parameters:

  | first | the first pool item to start searching

Precondition:
  first <= Tpool->GetSize()

  CanAllocateItem()

Returns:
  the allocated pool item

Definition at line 17 of file oldpool_func.h.

References PoolItem< T, Tid, Tpool >::AllocateRaw(), and PoolItem< T, Tid, Tpool >::index.

Referenced by PoolItem< Sign, uint16,&_Sign_pool >::AllocateRaw().

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

static T* PoolItem< T, Tid, Tpool >::AllocateRaw | ( |  | ) | `[inline, static, protected]`

Allocate a pool item; possibly allocate a new block in the pool.

Precondition:
  CanAllocateItem()

Returns:
  the allocated pool item.

Definition at line 289 of file oldpool.h.

Referenced by PoolItem< T, Tid, Tpool >::AllocateSafeRaw(), and PoolItem< Sign, uint16,&_Sign_pool >::operator new().

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

static T* PoolItem< T, Tid, Tpool >::AllocateRaw | ( | uint & | first | ) | `[inline, static, protected]`

Allocate a pool item; possibly allocate a new block in the pool.

Parameters:

  | first | the first pool item to start searching

Precondition:
  CanAllocateItem()

Returns:
  the allocated pool item.

Definition at line 300 of file oldpool.h.

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

static bool PoolItem< T, Tid, Tpool >::CleaningPool | ( |  | ) | `[inline, static, protected]`

Are we cleaning this pool?

Returns:
  true if we are

Definition at line 311 of file oldpool.h.

Referenced by RoadStop::~RoadStop(), and Station::~Station().

template<typename T, typename Tid, OldMemoryPool< T > * Tpool>

bool PoolItem< T, Tid, Tpool >::CanAllocateItem | ( | uint | count = `1` | ) | `[inline, static]`

Check whether we can allocate an item in this pool.

This to prevent the need to actually construct the object and then destructing it again, which could be *very* costly.

Parameters:

  | count | the number of items to create

Returns:
  true if and only if at least count items can be allocated.

Definition at line 46 of file oldpool_func.h.

Referenced by AddEngineReplacement(), CmdBuildAirport(), CmdBuildBuoy(), CmdBuildDock(), CmdBuildRailroadStation(), CmdBuildRoadStop(), CmdCloneOrder(), CmdInsertOrder(), and CreateNewIndustryHelper().

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

- src/oldpool.h
- src/oldpool_func.h