view libcommon/malikania/locator.hpp @ 110:119bcc5a727e

Misc: replace copyrights, closes #655
author David Demelier <markand@malikania.fr>
date Tue, 05 Sep 2017 14:06:31 +0200
parents de77ef21a329
children 4b292c20124c
line wrap: on
line source

/*
 * locator.hpp -- file and stream loader
 *
 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
 *
 * 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_LOCATOR_HPP
#define MALIKANIA_LOCATOR_HPP

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

namespace mlk {

/**
 * \brief Abstract assets locator.
 */
class locator {
public:
    /**
     * Default destructor.
     */
    virtual ~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_locator : public locator {
private:
    std::string m_path;

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

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

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

} // !mlk

#endif // !MALIKANIA_LOCATOR_HPP