// -*- C++ -*-
-/* This file is part of
- * =================================================
- *
- * LyX, The Document Processor
- * Copyright 1995 Matthias Ettrich.
- * Copyright 1995-2000 The LyX Team.
+/**
+ * \file GraphicsCache.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * This file Copyright 2000 Baruch Even
- * ================================================= */
+ * \author Baruch Even
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ *
+ * lyx::graphics::Cache is the manager of the image cache.
+ * It is responsible for creating the lyx::graphics::CacheItem's
+ * and maintaining them.
+ *
+ * lyx::graphics::Cache is a singleton class. It is possible to have only one
+ * instance of it at any moment.
+ */
#ifndef GRAPHICSCACHE_H
#define GRAPHICSCACHE_H
-#ifdef __GNUG__
-#pragma interface
-#endif
+#include <memory>
+#include <string>
+#include <vector>
+
-#include <map>
+namespace lyx {
-#include "LString.h"
-#include "GraphicsCacheItem.h"
-#include "support/utility.hpp"
+namespace support { class FileName; }
-/** GraphicsCache is the manager of the image cache, it is responsible to
- create the GraphicsCacheItem's and maintain them.
-
- GraphicsCache is a singleton class, there should be only one instance of
- it at any moment.
-*/
-class GraphicsCache : public noncopyable {
+namespace graphics {
+
+class CacheItem;
+
+class Cache {
public:
- /// Get the instance of the class.
- static GraphicsCache * getInstance();
- /// Add a file to the cache.
- GraphicsCacheItem * addFile(string const & filename);
+ /// This is a singleton class. Get the instance.
+ static Cache & get();
+
+ /** Which graphics formats can be loaded directly by the image loader.
+ * Other formats can be loaded if a converter to a loadable format
+ * can be defined.
+ */
+ std::vector<std::string> const & loadableFormats() const;
+
+ /// Add a graphics file to the cache.
+ void add(support::FileName const & file, support::FileName const & doc_file) const;
+
+ /// Remove a file from the cache.
+ void remove(support::FileName const & file) const;
+
+ /// Returns \c true if the file is in the cache.
+ bool inCache(support::FileName const & file) const;
+
+ /** Get the cache item associated with file.
+ * Returns an empty container if there is no such item.
+ *
+ * IMPORTANT: whatever uses an image must make a local copy of this
+ * ItemPtr. The shared_ptr<>::use_count() function is
+ * used to ascertain whether or not to remove the item from the cache
+ * when remove(file) is called.
+ *
+ * You have been warned!
+ */
+ typedef std::shared_ptr<CacheItem> ItemPtr;
+ ///
+ ItemPtr const item(support::FileName const & file) const;
private:
- /// Remove a cache item if it's count has gone to zero.
- void removeFile(string const & filename);
-
- /// Private c-tor so we can control how many objects are instantiated.
- GraphicsCache() {}
-
- /// Private d-tor so that no-one will destroy us.
- ~GraphicsCache();
-
- /// Holder of the single instance of the class.
- static GraphicsCache * singleton;
- ///
- typedef std::map<string, GraphicsCacheItem *> CacheType;
- ///
- CacheType cache;
-
- // We need this so that an Item can tell the cache that it should be
- // deleted. (to call removeFile).
- // It also helps removing a warning gcc emits.
- friend GraphicsCacheItem;
+ /// noncopyable
+ Cache(Cache const &);
+ void operator=(Cache const &);
+
+ /** Make the c-tor, d-tor private so we can control how many objects
+ * are instantiated.
+ */
+ Cache();
+ ///
+ ~Cache();
+
+ /// Use the Pimpl idiom to hide the internals.
+ class Impl;
+ /// The pointer never changes although *pimpl_'s contents may.
+ Impl * const pimpl_;
};
-#endif
+
+} // namespace graphics
+} // namespace lyx
+
+#endif // GRAPHICSCACHE_H