view libcommon/malikania/resources_locator.hpp @ 42:a47a4477f347

Misc: new style, closes #578
author David Demelier <markand@malikania.fr>
date Tue, 29 Nov 2016 21:21:36 +0100
parents
children fabbe1759cec
line wrap: on
line source

/*
 * resources_locator.hpp -- file and stream loader
 *
 * Copyright (c) 2013-2016 Malikania Authors
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef MALIKANIA_RESOURCES_LOCATOR_HPP
#define MALIKANIA_RESOURCES_LOCATOR_HPP

#include <string>
#include <memory>
#include <istream>

namespace malikania {

/**
 * \brief Abstract assets locator.
 */
class resources_locator {
public:
    /**
     * Default destructor.
     */
    virtual ~resources_locator() = default;

    /**
     * Read a whole resource as a string.
     *
     * \param id the resource id
     * \return the string
     * \throw std::runtime_error on any errors
     */
    virtual std::string read(const std::string& id) = 0;

    /**
     * Open a resource as a stream.
     *
     * \param id the resource id
     * \return the stream
     * \throw std::runtime_error on any errors
     */
    virtual std::unique_ptr<std::istream> open(const std::string& id) = 0;
};

/**
 * \brief Load a game from a directory.
 */
class directory_resources_locator : public resources_locator {
private:
    std::string m_path;

public:
    /**
     * Load the game from the directory.
     *
     * \param path the base directory
     */
    directory_resources_locator(std::string path) noexcept;

    /**
     * \copydoc resources_locator::read
     */
    std::string read(const std::string& id) override;

    /**
     * \copydoc resources_locator::open
     */
    std::unique_ptr<std::istream> open(const std::string& id) override;
};

} // !malikania

#endif // !MALIKANIA_RESOURCES_LOCATOR_HPP