poco/JSON/include/Poco/JSON/TemplateCache.h
2012-04-29 20:09:55 +00:00

140 lines
3.3 KiB
C++

//
// TemplateCache.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: TemplateCache
//
// Definition of the TemplateCache class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_JSONTemplateCache_INCLUDED
#define JSON_JSONTemplateCache_INCLUDED
#include <vector>
#include <map>
#include "Poco/Path.h"
#include "Poco/SharedPtr.h"
#include "Poco/Logger.h"
#include "Poco/JSON/Template.h"
namespace Poco
{
namespace JSON
{
class JSON_API TemplateCache
/// Use to cache parsed templates. Templates are
/// stored in a map with the full path as key.
/// When a template file has changed, the cache
/// will remove the old template from the cache
/// and load a new one.
{
public:
TemplateCache();
/// Constructor. The cache must be created
/// and not destroyed as long as it is used.
virtual ~TemplateCache();
/// Destructor
void addPath(const Path& path);
/// Add a path for resolving template paths.
/// The order of check is FIFO.
Template::Ptr getTemplate(const Path& path);
/// Returns a template from the cache.
/// When the template file is not yet loaded
/// or when the file has changed, the template
/// will be (re)loaded and parsed. A shared pointer
/// is returned, so it is safe to use this template
/// even when the template isn't stored anymore in
/// the cache.
static TemplateCache* instance();
/// Returns the only instance of this cache
void setLogger(Logger& logger);
/// Sets the logger for the cache.
private:
static TemplateCache* _instance;
std::vector<Path> _includePaths;
std::map<std::string, Template::Ptr> _cache;
Logger* _logger;
void setup();
Path resolvePath(const Path& path) const;
};
inline void TemplateCache::addPath(const Path& path)
{
_includePaths.push_back(path);
}
inline TemplateCache* TemplateCache::instance()
{
return _instance;
}
inline void TemplateCache::setLogger(Logger& logger)
{
_logger = &logger;
}
}} // Namespace Poco::JSON
#endif // JSON_JSONTemplateCache_INCLUDED