91 lines
2.6 KiB
C++
91 lines
2.6 KiB
C++
/**
|
|
This file is a part of our_dick
|
|
Copyright (C) 2022 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef OUR_DICK_GRAPHICS_RESOURCE_CONTAINER_HPP
|
|
#define OUR_DICK_GRAPHICS_RESOURCE_CONTAINER_HPP
|
|
|
|
#include <string>
|
|
#include <map>
|
|
#include <utility> //pair, forward
|
|
#include <tuple>
|
|
#include <memory> //shared_ptr
|
|
|
|
#include "util/deferred.hpp"
|
|
|
|
namespace gfx{
|
|
|
|
template<class T>
|
|
class resource_container
|
|
{
|
|
public:
|
|
using value_type = T;
|
|
using key_type = std::string;
|
|
using reference = T&;
|
|
using const_reference = const T&;
|
|
using pointer = T*;
|
|
using const_pointer = const T*;
|
|
using node = std::shared_ptr<value_type>;
|
|
using const_node = std::shared_ptr<value_type>;
|
|
|
|
private:
|
|
std::map<key_type,std::shared_ptr<value_type>> m_map;
|
|
|
|
public:
|
|
resource_container(void) = default;
|
|
resource_container(const resource_container&) = default;
|
|
resource_container(resource_container&&) = default;
|
|
|
|
~resource_container(void) = default;
|
|
|
|
resource_container& operator=(const resource_container&) = default;
|
|
resource_container& operator=(resource_container&&) = default;
|
|
|
|
bool has_value(const char* key)const;
|
|
template<class... Args>
|
|
std::pair<node,bool> emplace_value(const char* key, Args&&... args);
|
|
node get_value(const char* file);
|
|
bool erase_value(const char* key);
|
|
};
|
|
|
|
template<class T>
|
|
bool resource_container<T>::has_value(const char* key)const{
|
|
if(auto it = m_map.find(key);it != m_map.end())
|
|
return true;
|
|
return false;
|
|
}
|
|
template<class T>
|
|
template<class... Args>
|
|
auto resource_container<T>::emplace_value(const char* key, Args&&... args) -> std::pair<node,bool>{
|
|
auto p = m_map.try_emplace(key, util::deferred_function(std::make_shared<T,Args&&...>, std::forward<Args>(args)...));
|
|
return {(*p.first).second, p.second};
|
|
}
|
|
template<class T>
|
|
auto resource_container<T>::get_value(const char* key) -> node{
|
|
if(auto it = m_map.find(key);it != m_map.end())
|
|
return (*it).second;
|
|
return nullptr;
|
|
}
|
|
template<class T>
|
|
bool resource_container<T>::erase_value(const char* key){
|
|
return m_map.erase(key) == 1;
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|