Optimize parameter passing to avoid unnecessary copying
This commit is contained in:
+12
-12
@@ -11,59 +11,59 @@
|
||||
Assets::~Assets() {
|
||||
}
|
||||
|
||||
Texture* Assets::getTexture(std::string name) const {
|
||||
Texture* Assets::getTexture(const std::string& name) const {
|
||||
auto found = textures.find(name);
|
||||
if (found == textures.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Texture> texture, std::string name){
|
||||
void Assets::store(std::unique_ptr<Texture> texture, const std::string& name){
|
||||
textures.emplace(name, std::move(texture));
|
||||
}
|
||||
|
||||
|
||||
Shader* Assets::getShader(std::string name) const{
|
||||
Shader* Assets::getShader(const std::string& name) const{
|
||||
auto found = shaders.find(name);
|
||||
if (found == shaders.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Shader> shader, std::string name){
|
||||
void Assets::store(std::unique_ptr<Shader> shader, const std::string& name){
|
||||
shaders.emplace(name, std::move(shader));
|
||||
}
|
||||
|
||||
Font* Assets::getFont(std::string name) const {
|
||||
Font* Assets::getFont(const std::string& name) const {
|
||||
auto found = fonts.find(name);
|
||||
if (found == fonts.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Font> font, std::string name){
|
||||
void Assets::store(std::unique_ptr<Font> font, const std::string& name){
|
||||
fonts.emplace(name, std::move(font));
|
||||
}
|
||||
|
||||
Atlas* Assets::getAtlas(std::string name) const {
|
||||
Atlas* Assets::getAtlas(const std::string& name) const {
|
||||
auto found = atlases.find(name);
|
||||
if (found == atlases.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Atlas> atlas, std::string name){
|
||||
void Assets::store(std::unique_ptr<Atlas> atlas, const std::string& name){
|
||||
atlases.emplace(name, std::move(atlas));
|
||||
}
|
||||
|
||||
audio::Sound* Assets::getSound(std::string name) const {
|
||||
audio::Sound* Assets::getSound(const std::string& name) const {
|
||||
auto found = sounds.find(name);
|
||||
if (found == sounds.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<audio::Sound> sound, std::string name) {
|
||||
void Assets::store(std::unique_ptr<audio::Sound> sound, const std::string& name) {
|
||||
sounds.emplace(name, std::move(sound));
|
||||
}
|
||||
|
||||
@@ -75,13 +75,13 @@ void Assets::store(const TextureAnimation& animation) {
|
||||
animations.emplace_back(animation);
|
||||
}
|
||||
|
||||
UiDocument* Assets::getLayout(std::string name) const {
|
||||
UiDocument* Assets::getLayout(const std::string& name) const {
|
||||
auto found = layouts.find(name);
|
||||
if (found == layouts.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<UiDocument> layout, std::string name) {
|
||||
void Assets::store(std::unique_ptr<UiDocument> layout, const std::string& name) {
|
||||
layouts[name] = std::shared_ptr<UiDocument>(std::move(layout));
|
||||
}
|
||||
|
||||
+12
-12
@@ -38,26 +38,26 @@ public:
|
||||
Assets(const Assets&) = delete;
|
||||
~Assets();
|
||||
|
||||
Texture* getTexture(std::string name) const;
|
||||
void store(std::unique_ptr<Texture> texture, std::string name);
|
||||
Texture* getTexture(const std::string& name) const;
|
||||
void store(std::unique_ptr<Texture> texture, const std::string& name);
|
||||
|
||||
Shader* getShader(std::string name) const;
|
||||
void store(std::unique_ptr<Shader> shader, std::string name);
|
||||
Shader* getShader(const std::string& name) const;
|
||||
void store(std::unique_ptr<Shader> shader, const std::string& name);
|
||||
|
||||
Font* getFont(std::string name) const;
|
||||
void store(std::unique_ptr<Font> font, std::string name);
|
||||
Font* getFont(const std::string& name) const;
|
||||
void store(std::unique_ptr<Font> font, const std::string& name);
|
||||
|
||||
Atlas* getAtlas(std::string name) const;
|
||||
void store(std::unique_ptr<Atlas> atlas, std::string name);
|
||||
Atlas* getAtlas(const std::string& name) const;
|
||||
void store(std::unique_ptr<Atlas> atlas, const std::string& name);
|
||||
|
||||
audio::Sound* getSound(std::string name) const;
|
||||
void store(std::unique_ptr<audio::Sound> sound, std::string name);
|
||||
audio::Sound* getSound(const std::string& name) const;
|
||||
void store(std::unique_ptr<audio::Sound> sound, const std::string& name);
|
||||
|
||||
const std::vector<TextureAnimation>& getAnimations();
|
||||
void store(const TextureAnimation& animation);
|
||||
|
||||
UiDocument* getLayout(std::string name) const;
|
||||
void store(std::unique_ptr<UiDocument> layout, std::string name);
|
||||
UiDocument* getLayout(const std::string& name) const;
|
||||
void store(std::unique_ptr<UiDocument> layout, const std::string& name);
|
||||
};
|
||||
|
||||
#endif // ASSETS_ASSETS_HPP_
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
static debug::Logger logger("assets-loader");
|
||||
|
||||
@@ -32,11 +33,11 @@ AssetsLoader::AssetsLoader(Assets* assets, const ResPaths* paths)
|
||||
}
|
||||
|
||||
void AssetsLoader::addLoader(AssetType tag, aloader_func func) {
|
||||
loaders[tag] = func;
|
||||
loaders[tag] = std::move(func);
|
||||
}
|
||||
|
||||
void AssetsLoader::add(AssetType tag, const std::string filename, const std::string alias, std::shared_ptr<AssetCfg> settings) {
|
||||
entries.push(aloader_entry{tag, filename, alias, settings});
|
||||
void AssetsLoader::add(AssetType tag, const std::string& filename, const std::string& alias, std::shared_ptr<AssetCfg> settings) {
|
||||
entries.push(aloader_entry{tag, filename, alias, std::move(settings)});
|
||||
}
|
||||
|
||||
bool AssetsLoader::hasNext() const {
|
||||
@@ -69,12 +70,12 @@ bool AssetsLoader::loadNext() {
|
||||
}
|
||||
}
|
||||
|
||||
void addLayouts(scriptenv env, const std::string& prefix, const fs::path& folder, AssetsLoader& loader) {
|
||||
void addLayouts(const scriptenv& env, const std::string& prefix, const fs::path& folder, AssetsLoader& loader) {
|
||||
if (!fs::is_directory(folder)) {
|
||||
return;
|
||||
}
|
||||
for (auto& entry : fs::directory_iterator(folder)) {
|
||||
const fs::path file = entry.path();
|
||||
const fs::path& file = entry.path();
|
||||
if (file.extension().u8string() != ".xml")
|
||||
continue;
|
||||
std::string name = prefix+":"+file.stem().u8string();
|
||||
@@ -82,7 +83,7 @@ void addLayouts(scriptenv env, const std::string& prefix, const fs::path& folder
|
||||
}
|
||||
}
|
||||
|
||||
void AssetsLoader::tryAddSound(std::string name) {
|
||||
void AssetsLoader::tryAddSound(const std::string& name) {
|
||||
if (name.empty()) {
|
||||
return;
|
||||
}
|
||||
@@ -148,7 +149,7 @@ void AssetsLoader::processPreloadList(AssetType tag, dynamic::List* list) {
|
||||
}
|
||||
}
|
||||
|
||||
void AssetsLoader::processPreloadConfig(fs::path file) {
|
||||
void AssetsLoader::processPreloadConfig(const fs::path& file) {
|
||||
auto root = files::read_json(file);
|
||||
processPreloadList(AssetType::font, root->list("fonts"));
|
||||
processPreloadList(AssetType::shader, root->list("shaders"));
|
||||
@@ -209,7 +210,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
|
||||
bool AssetsLoader::loadExternalTexture(
|
||||
Assets* assets,
|
||||
const std::string& name,
|
||||
std::vector<std::filesystem::path> alternatives
|
||||
const std::vector<std::filesystem::path>& alternatives
|
||||
) {
|
||||
if (assets->getTexture(name) != nullptr) {
|
||||
return true;
|
||||
@@ -255,7 +256,7 @@ std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone) {
|
||||
func(assets);
|
||||
}
|
||||
);
|
||||
pool->setOnComplete(onDone);
|
||||
pool->setOnComplete(std::move(onDone));
|
||||
while (!entries.empty()) {
|
||||
const aloader_entry& entry = entries.front();
|
||||
auto ptr = std::make_shared<aloader_entry>(entry);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
|
||||
namespace dynamic {
|
||||
class Map;
|
||||
@@ -38,7 +39,7 @@ struct AssetCfg {
|
||||
struct LayoutCfg : AssetCfg {
|
||||
scriptenv env;
|
||||
|
||||
LayoutCfg(scriptenv env) : env(env) {}
|
||||
LayoutCfg(scriptenv env) : env(std::move(env)) {}
|
||||
};
|
||||
|
||||
struct SoundCfg : AssetCfg {
|
||||
@@ -68,11 +69,11 @@ class AssetsLoader {
|
||||
std::queue<aloader_entry> entries;
|
||||
const ResPaths* paths;
|
||||
|
||||
void tryAddSound(std::string name);
|
||||
void tryAddSound(const std::string& name);
|
||||
|
||||
void processPreload(AssetType tag, const std::string& name, dynamic::Map* map);
|
||||
void processPreloadList(AssetType tag, dynamic::List* list);
|
||||
void processPreloadConfig(std::filesystem::path file);
|
||||
void processPreloadConfig(const std::filesystem::path& file);
|
||||
void processPreloadConfigs(const Content* content);
|
||||
public:
|
||||
AssetsLoader(Assets* assets, const ResPaths* paths);
|
||||
@@ -85,8 +86,8 @@ public:
|
||||
/// @param settings asset loading settings (based on asset type)
|
||||
void add(
|
||||
AssetType tag,
|
||||
const std::string filename,
|
||||
const std::string alias,
|
||||
const std::string& filename,
|
||||
const std::string& alias,
|
||||
std::shared_ptr<AssetCfg> settings=nullptr
|
||||
);
|
||||
|
||||
@@ -106,7 +107,7 @@ public:
|
||||
static bool loadExternalTexture(
|
||||
Assets* assets,
|
||||
const std::string& name,
|
||||
std::vector<std::filesystem::path> alternatives
|
||||
const std::vector<std::filesystem::path>& alternatives
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -36,9 +36,9 @@ static bool animation(
|
||||
assetload::postfunc assetload::texture(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg>
|
||||
const std::string& filename,
|
||||
const std::string& name,
|
||||
const std::shared_ptr<AssetCfg>&
|
||||
) {
|
||||
std::shared_ptr<ImageData> image (
|
||||
imageio::read(paths->find(filename+".png").u8string()).release()
|
||||
@@ -51,9 +51,9 @@ assetload::postfunc assetload::texture(
|
||||
assetload::postfunc assetload::shader(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg>
|
||||
const std::string& filename,
|
||||
const std::string& name,
|
||||
const std::shared_ptr<AssetCfg>&
|
||||
) {
|
||||
fs::path vertexFile = paths->find(filename+".glslv");
|
||||
fs::path fragmentFile = paths->find(filename+".glslf");
|
||||
@@ -88,9 +88,9 @@ static bool append_atlas(AtlasBuilder& atlas, const fs::path& file) {
|
||||
assetload::postfunc assetload::atlas(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string directory,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg>
|
||||
const std::string& directory,
|
||||
const std::string& name,
|
||||
const std::shared_ptr<AssetCfg>&
|
||||
) {
|
||||
AtlasBuilder builder;
|
||||
for (const auto& file : paths->listdir(directory)) {
|
||||
@@ -113,9 +113,9 @@ assetload::postfunc assetload::atlas(
|
||||
assetload::postfunc assetload::font(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg>
|
||||
const std::string& filename,
|
||||
const std::string& name,
|
||||
const std::shared_ptr<AssetCfg>&
|
||||
) {
|
||||
auto pages = std::make_shared<std::vector<std::unique_ptr<ImageData>>>();
|
||||
for (size_t i = 0; i <= 4; i++) {
|
||||
@@ -136,9 +136,9 @@ assetload::postfunc assetload::font(
|
||||
assetload::postfunc assetload::layout(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> config
|
||||
const std::string& file,
|
||||
const std::string& name,
|
||||
const std::shared_ptr<AssetCfg>& config
|
||||
) {
|
||||
return [=](auto assets) {
|
||||
try {
|
||||
@@ -154,9 +154,9 @@ assetload::postfunc assetload::layout(
|
||||
assetload::postfunc assetload::sound(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> config
|
||||
const std::string& file,
|
||||
const std::string& name,
|
||||
const std::shared_ptr<AssetCfg>& config
|
||||
) {
|
||||
auto cfg = std::dynamic_pointer_cast<SoundCfg>(config);
|
||||
bool keepPCM = cfg ? cfg->keepPCM : false;
|
||||
|
||||
@@ -17,45 +17,45 @@ namespace assetload {
|
||||
postfunc texture(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
const std::string& filename,
|
||||
const std::string &name,
|
||||
const std::shared_ptr<AssetCfg>& settings
|
||||
);
|
||||
postfunc shader(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
const std::string& filename,
|
||||
const std::string &name,
|
||||
const std::shared_ptr<AssetCfg>& settings
|
||||
);
|
||||
postfunc atlas(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string directory,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
const std::string &directory,
|
||||
const std::string &name,
|
||||
const std::shared_ptr<AssetCfg>& settings
|
||||
);
|
||||
postfunc font(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string filename,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
const std::string& filename,
|
||||
const std::string &name,
|
||||
const std::shared_ptr<AssetCfg>& settings
|
||||
);
|
||||
postfunc layout(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
const std::string& file,
|
||||
const std::string &name,
|
||||
const std::shared_ptr<AssetCfg>& settings
|
||||
);
|
||||
|
||||
postfunc sound(
|
||||
AssetsLoader*,
|
||||
const ResPaths* paths,
|
||||
const std::string file,
|
||||
const std::string name,
|
||||
std::shared_ptr<AssetCfg> settings
|
||||
const std::string& file,
|
||||
const std::string &name,
|
||||
const std::shared_ptr<AssetCfg>& settings
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user