2002-09-05 11:31:30 +00:00
|
|
|
/**
|
2002-02-27 09:59:52 +00:00
|
|
|
* \file GraphicsCache.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-09-05 11:31:30 +00:00
|
|
|
* \author Baruch Even
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-02-27 09:59:52 +00:00
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#include "GraphicsCache.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "GraphicsCacheItem.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "GraphicsImage.h"
|
2002-06-25 15:59:10 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2004-11-20 09:19:52 +00:00
|
|
|
#include <map>
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
2003-09-05 03:10:30 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
|
|
|
using support::FileName;
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace graphics {
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
/** The cache contains one item per file, so use a map to find the
|
|
|
|
* cache item quickly by filename.
|
|
|
|
*/
|
2006-11-26 21:30:39 +00:00
|
|
|
typedef std::map<FileName, Cache::ItemPtr> CacheType;
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class Cache::Impl {
|
|
|
|
public:
|
2002-06-28 11:22:56 +00:00
|
|
|
///
|
|
|
|
CacheType cache;
|
|
|
|
};
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache & Cache::get()
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
|
|
|
// Now return the cache
|
2002-06-28 11:22:56 +00:00
|
|
|
static Cache singleton;
|
2000-09-14 17:53:12 +00:00
|
|
|
return singleton;
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::Cache()
|
2003-09-21 16:02:54 +00:00
|
|
|
: pimpl_(new Impl)
|
2002-06-28 11:22:56 +00:00
|
|
|
{}
|
2000-07-31 16:39:50 +00:00
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::~Cache()
|
|
|
|
{}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
std::vector<string> Cache::loadableFormats() const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
return Image::loadableFormats();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Cache::add(FileName const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
// Is the file in the cache already?
|
|
|
|
if (inCache(file)) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "Cache::add(" << file << "):\n"
|
2002-06-25 15:59:10 +00:00
|
|
|
<< "The file is already in the cache."
|
|
|
|
<< std::endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
2002-06-25 15:59:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->cache[file] = ItemPtr(new CacheItem(file));
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Cache::remove(FileName const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
CacheType::iterator it = pimpl_->cache.find(file);
|
|
|
|
if (it == pimpl_->cache.end())
|
2002-06-25 15:59:10 +00:00
|
|
|
return;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
ItemPtr & item = it->second;
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
if (item.use_count() == 1) {
|
|
|
|
// The graphics file is in the cache, but nothing else
|
|
|
|
// references it.
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->cache.erase(it);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool Cache::inCache(FileName const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
return pimpl_->cache.find(file) != pimpl_->cache.end();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
Cache::ItemPtr const Cache::item(FileName const & file) const
|
2002-04-11 17:40:44 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
CacheType::const_iterator it = pimpl_->cache.find(file);
|
|
|
|
if (it == pimpl_->cache.end())
|
|
|
|
return ItemPtr();
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
return it->second;
|
2002-04-11 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // namespace lyx
|