Back to index

unity  6.0.0
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
unity::TextureCache Class Reference

#include <TextureCache.h>

Inheritance diagram for unity::TextureCache:
Inheritance graph
Collaboration diagram for unity::TextureCache:
Collaboration graph

List of all members.

Public Types

typedef nux::ObjectPtr
< nux::BaseTexture > 
typedef sigc::slot
< nux::BaseTexture
*, std::string const &, int,
int > 

Public Member Functions

BaseTexturePtr FindTexture (std::string const &texture_id, int width, int height, CreateTextureCallback callback)
std::size_t Size () const

Static Public Member Functions

static TextureCacheGetDefault ()

Private Member Functions

 TextureCache ()
 ~TextureCache ()
std::string Hash (std::string const &, int width, int height)
void OnDestroyNotify (nux::Trackable *Object, std::string key)

Private Attributes

std::map< std::string,
nux::BaseTexture * > 

Detailed Description

Definition at line 40 of file TextureCache.h.

Member Typedef Documentation

typedef nux::ObjectPtr<nux::BaseTexture> unity::TextureCache::BaseTexturePtr

Definition at line 43 of file TextureCache.h.

typedef sigc::slot<nux::BaseTexture*, std::string const&, int, int> unity::TextureCache::CreateTextureCallback

Definition at line 46 of file TextureCache.h.

Constructor & Destructor Documentation

Definition at line 33 of file TextureCache.cpp.


Definition at line 37 of file TextureCache.cpp.


Member Function Documentation

TextureCache::BaseTexturePtr unity::TextureCache::FindTexture ( std::string const &  texture_id,
int  width,
int  height,
CreateTextureCallback  callback 

Definition at line 54 of file TextureCache.cpp.

  std::string key = Hash(texture_id, width, height);
  BaseTexturePtr texture(cache_[key]);

  if (!texture)
    texture = slot(texture_id, width, height);

    // Now here is the magic.
    // The slot function is required to return a new texture.  This has an
    // internal reference count of one.  The TextureCache wants to hold the
    // texture inside the internal map for as long as the object itself
    // exists, but it doesn't want any ownership of the texture.  How we
    // handle this is to always return a smart pointer for the texture.  These
    // smart pointers have the ownership of the texture.
    // We also hook into the objects OnDestroyed signal to remove it from the
    // map.  To avoid a reverse lookup map, we bind the key to the method
    // call.  By using a mem_fun rather than a lambda function, and through
    // the magic of sigc::trackable, we don't need to remember the connection
    // itself.  We get notified when the object is being destroyed, and if we
    // are destroyed first, then the sigc::trackable disconnects all methods
    // created using mem_fun.

    // Reduce the internal reference count of the texture, so the smart
    // pointer is the sole owner of the object.

    cache_[key] = texture.GetPointer();

    auto on_destroy = sigc::mem_fun(this, &TextureCache::OnDestroyNotify);
    texture->OnDestroyed.connect(sigc::bind(on_destroy, key));

  return texture;

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 41 of file TextureCache.cpp.

  static TextureCache instance;
  return instance;

Here is the caller graph for this function:

std::string unity::TextureCache::Hash ( std::string const &  id,
int  width,
int  height 
) [private]

Definition at line 47 of file TextureCache.cpp.

  std::ostringstream sout;
  sout << id << "-" << width << "x" << height;
  return sout.str();

Here is the caller graph for this function:

void unity::TextureCache::OnDestroyNotify ( nux::Trackable *  Object,
std::string  key 
) [private]

Definition at line 95 of file TextureCache.cpp.


Here is the caller graph for this function:

std::size_t unity::TextureCache::Size ( ) const

Definition at line 101 of file TextureCache.cpp.

  return cache_.size();

Member Data Documentation

std::map<std::string, nux::BaseTexture*> unity::TextureCache::cache_ [private]

Definition at line 64 of file TextureCache.h.

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