ManaPlus
Public Member Functions | Data Fields
Resource Class Reference

#include <resource.h>

Inheritance diagram for Resource:
MemoryCounter AtlasResource ImageSet SDLMusic Shader ShaderProgram SoundEffect SpriteDef WalkLayer XML::Document

Public Member Functions

 Resource ()
 
 ~Resource ()
 
virtual void incRef ()
 
virtual void decRef ()
 
int calcMemoryLocal () const
 
std::string getCounterName () const
 
- Public Member Functions inherited from MemoryCounter
 MemoryCounter ()
 
virtual ~MemoryCounter ()
 
int calcMemory (const int level) const
 
virtual int calcMemoryChilds (const int level) const
 

Data Fields

time_t mTimeStamp
 
std::string mIdPath
 
std::string mSource
 
unsigned int mRefCount
 
bool mProtected
 
bool mNotCount
 

Detailed Description

A generic reference counted resource object.

Definition at line 34 of file resource.h.

Constructor & Destructor Documentation

◆ Resource()

Resource::Resource ( )
inline

Constructor

Definition at line 40 of file resource.h.

40  :
41  MemoryCounter(),
42  mTimeStamp(0),
43  mIdPath(),
44  mSource(),
45  mRefCount(0),
46  mProtected(false),
47 #ifdef DEBUG_DUMP_LEAKS
48  mNotCount(false),
49  mDumped(false)
50 #else // DEBUG_DUMP_LEAKS
51  mNotCount(false)
52 #endif // DEBUG_DUMP_LEAKS
53  {
54  }
bool mProtected
Definition: resource.h:88
std::string mIdPath
Definition: resource.h:84
std::string mSource
Definition: resource.h:85
unsigned int mRefCount
Definition: resource.h:87
time_t mTimeStamp
Definition: resource.h:82
bool mNotCount
Definition: resource.h:89

◆ ~Resource()

Resource::~Resource ( )

Destructor.

Definition at line 34 of file resource.cpp.

35 {
36 }

Member Function Documentation

◆ calcMemoryLocal()

int Resource::calcMemoryLocal ( ) const
virtual

Reimplemented from MemoryCounter.

Reimplemented in SpriteDef, SoundEffect, and SDLMusic.

Definition at line 76 of file resource.cpp.

77 {
78  return CAST_S32(sizeof(Resource)) +
79  CAST_S32(mIdPath.size()) +
80  CAST_S32(mSource.size());
81 }
#define CAST_S32
Definition: cast.h:30

References CAST_S32, mIdPath, and mSource.

Referenced by AtlasResource::calcMemoryLocal(), ImageSet::calcMemoryLocal(), WalkLayer::calcMemoryLocal(), SDLMusic::calcMemoryLocal(), SoundEffect::calcMemoryLocal(), and SpriteDef::calcMemoryLocal().

◆ decRef()

void Resource::decRef ( )
virtual

Decrements the reference count and deletes the object if no references are left.

Returns
true if the object was deleted false otherwise.

Reimplemented in AtlasResource.

Definition at line 50 of file resource.cpp.

51 {
52 #ifdef DEBUG_IMAGES
53  logger->log("before decRef for: %p", static_cast<void*>(this));
54 #endif // DEBUG_IMAGES
55 
56  // Reference may not already have reached zero
57  if (mRefCount == 0)
58  {
59  logger->log("Warning: mRefCount already zero for %s", mIdPath.c_str());
60  return;
61  }
62 
63  mRefCount--;
64 
65 #ifdef DEBUG_IMAGES
66  logger->log("after decRef: %p, %u", static_cast<void*>(this), mRefCount);
67 #endif // DEBUG_IMAGES
68 
69  if (mRefCount == 0 && !mNotCount)
70  {
71  // Warn the manager that this resource is no longer used.
73  }
74 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
Logger * logger
Definition: logger.cpp:89
void release(Resource *const res)

References Logger::log(), logger, mIdPath, mNotCount, mRefCount, and ResourceManager::release().

Referenced by Particle::addEffect(), ParticleEngine::addEffect(), ResourceManager::cleanProtected(), ShadersManager::createProgram(), AtlasResource::decRef(), SubImage::decRef(), ResourceManager::decRefDelete(), EquipmentWindow::fillBoxes(), EquipmentWindow::fillDefault(), SoundManager::haltMusic(), SpriteDef::includeSprite(), SpriteDef::load(), Theme::loadColors(), Button::loadImage(), Button::loadImageSet(), Theme::loadInfo(), SoundManager::logic(), ResourceManager::moveToDeleted(), SoundManager::playGuiSfx(), SoundManager::playSfx(), ProgressIndicator::ProgressIndicator(), Theme::readSkin(), Button::setImage(), Gui::setUseCustomCursor(), MapReader::unloadEmptyAtlas(), AnimatedSprite::~AnimatedSprite(), BrowserBox::~BrowserBox(), Button::~Button(), EmotePage::~EmotePage(), EmoteWindow::~EmoteWindow(), EquipmentWindow::~EquipmentWindow(), Gui::~Gui(), Map::~Map(), ShaderProgram::~ShaderProgram(), SimpleAnimation::~SimpleAnimation(), StaticBrowserBox::~StaticBrowserBox(), and WindowMenu::~WindowMenu().

◆ getCounterName()

std::string Resource::getCounterName ( ) const
inlinevirtual

Reimplemented from MemoryCounter.

Reimplemented in SoundEffect, and SDLMusic.

Definition at line 79 of file resource.h.

80  { return mIdPath + "-" + mSource; }

References mIdPath, and mSource.

◆ incRef()

void Resource::incRef ( )
virtual

Increments the internal reference count.

Reimplemented in AtlasResource.

Definition at line 38 of file resource.cpp.

39 {
40 #ifdef DEBUG_IMAGES
41  logger->log("before incRef for: %p", static_cast<void*>(this));
42  mRefCount++;
43  logger->log("after incRef: %p, %u", static_cast<void*>(this), mRefCount);
44 #else // DEBUG_IMAGES
45 
46  mRefCount++;
47 #endif // DEBUG_IMAGES
48 }

References Logger::log(), logger, and mRefCount.

Referenced by ResourceManager::addResource(), AnimatedSprite::AnimatedSprite(), ResourceManager::get(), ResourceManager::getFromCache(), AtlasResource::incRef(), SpriteDef::load(), AtlasManager::loadEmptyImages(), and AtlasManager::loadImages().

Field Documentation

◆ mIdPath

std::string Resource::mIdPath

◆ mNotCount

bool Resource::mNotCount

Definition at line 89 of file resource.h.

Referenced by decRef().

◆ mProtected

bool Resource::mProtected

Definition at line 88 of file resource.h.

Referenced by ResourceManager::cleanProtected(), and SpriteDef::load().

◆ mRefCount

unsigned int Resource::mRefCount

◆ mSource

std::string Resource::mSource

◆ mTimeStamp

time_t Resource::mTimeStamp

Time at which the resource was orphaned.

Definition at line 82 of file resource.h.

Referenced by ResourceManager::cleanOrphans(), and ResourceManager::release().


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