[deploy] fix: std::map insert => operator[]

This commit is contained in:
Nomango 2020-10-17 17:37:47 +08:00
parent 5e9937e0f0
commit 364a96b320
10 changed files with 72 additions and 56 deletions

View File

@ -35,78 +35,77 @@ SoundPlayer::~SoundPlayer()
ClearCache(); ClearCache();
} }
size_t SoundPlayer::GetId(const String& file_path) const
{
return std::hash<String>()(file_path);
}
size_t SoundPlayer::GetId(const Resource& res) const
{
return static_cast<size_t>(res.GetId());
}
size_t SoundPlayer::Load(const String& file_path) size_t SoundPlayer::Load(const String& file_path)
{ {
size_t hash = std::hash<String>()(file_path); size_t id = GetId(file_path);
if (sound_cache_.end() != sound_cache_.find(hash)) if (sound_cache_.end() != sound_cache_.find(id))
return hash; return id;
SoundPtr sound = MakePtr<Sound>(); SoundPtr sound = MakePtr<Sound>();
if (sound) if (sound && sound->Load(file_path))
{ {
if (sound->Load(file_path)) sound->SetVolume(volume_);
{ sound_cache_.insert(std::make_pair(id, sound));
sound->SetVolume(volume_); return id;
sound_cache_.insert(std::make_pair(hash, sound));
return hash;
}
} }
return 0; return 0;
} }
size_t SoundPlayer::Load(const Resource& res) size_t SoundPlayer::Load(const Resource& res)
{ {
size_t hash_code = static_cast<size_t>(res.GetId()); size_t id = GetId(res);
if (sound_cache_.end() != sound_cache_.find(hash_code)) if (sound_cache_.end() != sound_cache_.find(id))
return hash_code; return id;
SoundPtr sound = MakePtr<Sound>(); SoundPtr sound = MakePtr<Sound>();
if (sound) if (sound && sound->Load(res))
{ {
if (sound->Load(res)) sound->SetVolume(volume_);
{ sound_cache_.insert(std::make_pair(id, sound));
sound->SetVolume(volume_); return id;
sound_cache_.insert(std::make_pair(hash_code, sound));
return hash_code;
}
} }
return 0; return 0;
} }
void SoundPlayer::Play(size_t id, int loop_count) void SoundPlayer::Play(size_t id, int loop_count)
{ {
auto iter = sound_cache_.find(id); if (auto sound = GetSound(id))
if (sound_cache_.end() != iter) sound->Play(loop_count);
iter->second->Play(loop_count);
} }
void SoundPlayer::Pause(size_t id) void SoundPlayer::Pause(size_t id)
{ {
auto iter = sound_cache_.find(id); if (auto sound = GetSound(id))
if (sound_cache_.end() != iter) sound->Pause();
iter->second->Pause();
} }
void SoundPlayer::Resume(size_t id) void SoundPlayer::Resume(size_t id)
{ {
auto iter = sound_cache_.find(id); if (auto sound = GetSound(id))
if (sound_cache_.end() != iter) sound->Resume();
iter->second->Resume();
} }
void SoundPlayer::Stop(size_t id) void SoundPlayer::Stop(size_t id)
{ {
auto iter = sound_cache_.find(id); if (auto sound = GetSound(id))
if (sound_cache_.end() != iter) sound->Stop();
iter->second->Stop();
} }
bool SoundPlayer::IsPlaying(size_t id) bool SoundPlayer::IsPlaying(size_t id)
{ {
auto iter = sound_cache_.find(id); if (auto sound = GetSound(id))
if (sound_cache_.end() != iter) return sound->IsPlaying();
return iter->second->IsPlaying();
return false; return false;
} }
@ -124,6 +123,14 @@ void SoundPlayer::SetVolume(float volume)
} }
} }
SoundPtr SoundPlayer::GetSound(size_t id) const
{
auto iter = sound_cache_.find(id);
if (iter != sound_cache_.end())
return iter->second;
return SoundPtr();
}
void SoundPlayer::PauseAll() void SoundPlayer::PauseAll()
{ {
for (auto& pair : sound_cache_) for (auto& pair : sound_cache_)

View File

@ -91,6 +91,23 @@ public:
/// @param volume 音量大小1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量 /// @param volume 音量大小1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量
void SetVolume(float volume); void SetVolume(float volume);
/// \~chinese
/// @brief 获取本地音频文件id
/// @param file_path 本地音频文件路径
/// @return 音频标识符
size_t GetId(const String& file_path) const;
/// \~chinese
/// @brief 获取音频资源id
/// @param res 音频资源
/// @return 音频标识符
size_t GetId(const Resource& res) const;
/// \~chinese
/// @brief 获取音乐对象
/// @param id 音频标识符
SoundPtr GetSound(size_t id) const;
/// \~chinese /// \~chinese
/// @brief 暂停所有音频 /// @brief 暂停所有音频
void PauseAll(); void PauseAll();

View File

@ -53,7 +53,7 @@ bool ImGuiLayer::CheckVisibility(RenderContext& ctx) const
void ImGuiLayer::AddItem(const String& name, const ImGuiPipeline& item) void ImGuiLayer::AddItem(const String& name, const ImGuiPipeline& item)
{ {
pipelines_.insert(std::make_pair(name, item)); pipelines_[name] = item;
} }
void ImGuiLayer::RemoveItem(const String& name) void ImGuiLayer::RemoveItem(const String& name)

View File

@ -49,7 +49,7 @@ Component* ComponentManager::AddComponent(size_t index, ComponentPtr component)
{ {
component->InitComponent(target_); component->InitComponent(target_);
components_.insert(std::make_pair(index, component)); components_[index] = component;
} }
return component.Get(); return component.Get();
} }

View File

@ -427,7 +427,7 @@ inline Deserializer& operator>>(Deserializer& deserializer, Map<_KTy, _Ty>& map)
_KTy key; _KTy key;
_Ty value; _Ty value;
deserializer >> key >> value; deserializer >> key >> value;
map.insert(std::make_pair(key, value)); map[key] = value;
} }
return deserializer; return deserializer;
} }

View File

@ -119,13 +119,13 @@ FontCache::~FontCache() {}
void FontCache::AddFont(size_t key, FontPtr font) void FontCache::AddFont(size_t key, FontPtr font)
{ {
font_cache_.insert(std::make_pair(key, font)); font_cache_[key] = font;
} }
void FontCache::AddFontByFamily(const String& font_family, FontPtr font) void FontCache::AddFontByFamily(const String& font_family, FontPtr font)
{ {
String family = TransformFamily(font_family); String family = TransformFamily(font_family);
font_family_cache_.insert(std::make_pair(family, font)); font_family_cache_[family] = font;
} }
FontPtr FontCache::GetFont(size_t key) const FontPtr FontCache::GetFont(size_t key) const

View File

@ -32,12 +32,12 @@ TextureCache::~TextureCache()
void TextureCache::AddTexture(size_t key, TexturePtr texture) void TextureCache::AddTexture(size_t key, TexturePtr texture)
{ {
texture_cache_.insert(std::make_pair(key, texture)); texture_cache_[key] = texture;
} }
void TextureCache::AddGifImage(size_t key, GifImagePtr gif) void TextureCache::AddGifImage(size_t key, GifImagePtr gif)
{ {
gif_texture_cache_.insert(std::make_pair(key, gif)); gif_texture_cache_[key] = gif;
} }
TexturePtr TextureCache::GetTexture(size_t key) const TexturePtr TextureCache::GetTexture(size_t key) const

View File

@ -311,13 +311,13 @@ void ConfigIni::SetSectionMap(const SectionMap& sections)
void ConfigIni::SetSection(const String& section, const ValueMap& values) void ConfigIni::SetSection(const String& section, const ValueMap& values)
{ {
sections_.insert(std::make_pair(section, values)); sections_[section] = values;
} }
void ConfigIni::SetString(const String& section, const String& key, const String& value) void ConfigIni::SetString(const String& section, const String& key, const String& value)
{ {
if (HasSection(section)) if (HasSection(section))
sections_[section].insert(std::make_pair(key, value)); sections_[section][key] = value;
else else
SetSection(section, ValueMap{ { key, value } }); SetSection(section, ValueMap{ { key, value } });
} }
@ -363,19 +363,11 @@ void ConfigIni::DeleteKey(const String& section, const String& key)
ConfigIni::ValueMap& ConfigIni::operator[](const String& section) ConfigIni::ValueMap& ConfigIni::operator[](const String& section)
{ {
if (!HasSection(section))
{
sections_.insert(std::make_pair(section, ValueMap()));
}
return sections_[section]; return sections_[section];
} }
const ConfigIni::ValueMap& ConfigIni::operator[](const String& section) const const ConfigIni::ValueMap& ConfigIni::operator[](const String& section) const
{ {
if (!HasSection(section))
{
const_cast<SectionMap&>(sections_).insert(std::make_pair(section, ValueMap()));
}
return sections_.at(section); return sections_.at(section);
} }

View File

@ -47,7 +47,7 @@ bool ResourceCache::LoadFromXmlFile(const String& file_path)
void ResourceCache::AddObject(const String& id, ObjectBasePtr obj) void ResourceCache::AddObject(const String& id, ObjectBasePtr obj)
{ {
object_cache_.insert(std::make_pair(id, obj)); object_cache_[id] = obj;
} }
void ResourceCache::Remove(const String& id) void ResourceCache::Remove(const String& id)

View File

@ -34,19 +34,19 @@ Any UserData::Get(const String& key, const Any& default_data) const
void UserData::Set(const String& key, const Any& data) void UserData::Set(const String& key, const Any& data)
{ {
data_.insert(std::make_pair(key, data)); data_[key] = data;
} }
void UserData::Set(const DataPair& pair) void UserData::Set(const DataPair& pair)
{ {
data_.insert(pair); data_[pair.first] = pair.second;
} }
void UserData::Set(const std::initializer_list<DataPair>& list) void UserData::Set(const std::initializer_list<DataPair>& list)
{ {
for (const auto& pair : list) for (const auto& pair : list)
{ {
data_.insert(pair); data_[pair.first] = pair.second;
} }
} }