Silicon
A realtime platform for creating interactive media.
Asset.hpp
Go to the documentation of this file.
1// BSD 2-Clause License
2//
3// Copyright (c) 2023, Matthew McCall
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27//
28// Created by Matthew McCall on 1/6/23.
29//
30
31#ifndef SILICON_ASSET_HPP
32#define SILICON_ASSET_HPP
33
34#include <cstdint>
35#include <memory>
36#include <string>
37
38#include "Silicon/Async.hpp"
39#include "Silicon/Log.hpp"
40#include "Silicon/Types.hpp"
41
42namespace Si {
43
44class Asset
45{
46public:
47 template <typename T>
48 static std::shared_ptr<T> GetNow(const std::string &asset_path)
49 {
50 if (auto asset = s_loadedAssets.find(asset_path); asset != s_loadedAssets.end())
51 {
52 if (auto asset_ptr = asset->second.lock())
53 {
54 return std::static_pointer_cast<T>(asset_ptr);
55 }
56 else
57 {
58 Engine::Trace("Cache miss for asset: {}", asset_path);
59 }
60 }
61
62 auto asset = std::make_shared<T>(asset_path);
63 s_loadedAssets[asset_path] = asset;
64 return asset;
65 }
66
67 template <typename T>
68 static Future<std::shared_ptr<T>> Get(const std::string &asset_path)
69 {
70 return Async<std::shared_ptr<T>>([asset_path]() {
71 return GetNow<T>(asset_path);
72 });
73 }
74
75 [[nodiscard]] const std::string &GetPath() const;
77
78 virtual ~Asset();
79
80protected:
81 explicit Asset(std::string path);
82
84
85private:
86 std::string m_path;
87
89
90};
91
92class PlainTextAsset : public Asset
93{
94public:
95 explicit PlainTextAsset(std::string path);
96
97 [[nodiscard]] const std::string &GetText() const;
98
99private:
100 std::string m_text;
101
102};
103
104}
105
106#endif // SILICON_ASSET_HPP
const std::string & GetPath() const
Definition: Asset.cpp:39
Vector< std::uint8_t > m_data
Definition: Asset.hpp:83
Vector< std::uint8_t > & GetBytes()
Definition: Asset.cpp:44
virtual ~Asset()
Definition: Asset.cpp:83
static Future< std::shared_ptr< T > > Get(const std::string &asset_path)
Definition: Asset.hpp:68
Asset(std::string path)
Definition: Asset.cpp:49
static std::shared_ptr< T > GetNow(const std::string &asset_path)
Definition: Asset.hpp:48
PlainTextAsset(std::string path)
Definition: Asset.cpp:88
const std::string & GetText() const
Definition: Asset.cpp:95
void Trace(Args &&... args)
Definition: Log.hpp:79
Definition: Allocator.hpp:36
tf::Future< T > Future
Definition: Async.hpp:39