From 4a4116da45acff8082a42f1b30b5cf92926c4dcd Mon Sep 17 00:00:00 2001 From: karutoh Date: Fri, 26 Jan 2024 09:00:31 -0800 Subject: [PATCH] Backup. --- CMakeLists.txt | 1 + include/ehs/io/Resource.h | 45 +++++++++++++++++ include/ehs/io/audio/Audio.h | 16 ++---- src/io/Resource.cpp | 95 ++++++++++++++++++++++++++++++++++++ src/io/audio/Audio.cpp | 54 ++++++-------------- 5 files changed, 161 insertions(+), 50 deletions(-) create mode 100644 include/ehs/io/Resource.h create mode 100644 src/io/Resource.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ca2692..baf6d44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ set(EHS_SOURCES src/json/JsonArray.cpp include/ehs/json/JsonArray.h src/json/JsonVar.cpp include/ehs/json/JsonVar.h + src/io/Resource.cpp include/ehs/io/Resource.h src/io/Console.cpp include/ehs/io/Console.h src/io/RIFF_Chunk.cpp include/ehs/io/RIFF_Chunk.h src/io/RIFF.cpp include/ehs/io/RIFF.h diff --git a/include/ehs/io/Resource.h b/include/ehs/io/Resource.h new file mode 100644 index 0000000..23e6136 --- /dev/null +++ b/include/ehs/io/Resource.h @@ -0,0 +1,45 @@ +#pragma once + +#include "ehs/Types.h" +#include "ehs/Str.h" +#include "ehs/Vec2.h" +#include "ehs/BaseObj.h" + +namespace ehs +{ + class Resource : public BaseObj + { + private: + ehs::UInt_64 hashId; + ehs::Str_8 id; + + public: + Resource(); + + Resource(ehs::Str_8 id); + + Resource(Resource&& rsrc) noexcept; + + Resource(const Resource& rsrc); + + Resource& operator=(Resource&& rsrc) noexcept; + + Resource& operator=(const Resource& rsrc); + + bool operator==(ehs::UInt_64 otherHashId) const; + + bool operator!=(ehs::UInt_64 otherHashId) const; + + virtual void Release(); + + void SetId(ehs::Str_8 newId); + + ehs::UInt_64 GetHashId() const; + + ehs::Str_8 GetId() const; + + virtual bool IsValid() const; + + Resource* Clone() const override; + }; +} \ No newline at end of file diff --git a/include/ehs/io/audio/Audio.h b/include/ehs/io/audio/Audio.h index 84ef387..556d7b1 100644 --- a/include/ehs/io/audio/Audio.h +++ b/include/ehs/io/audio/Audio.h @@ -1,22 +1,20 @@ #pragma once #include "ehs/Types.h" -#include "ehs/BaseObj.h" #include "ehs/DataType.h" #include "ehs/Str.h" #include "ehs/Serializer.h" #include "ehs/Vector.h" #include "ehs/Array.h" +#include "ehs/io/Resource.h" #include "AudioCodec.h" namespace ehs { - class Audio : public BaseObj + class Audio : public Resource { private: static Array codecs; - UInt_64 hashId; - Str_8 id; UInt_64 sampleRate; DataType dataType; UInt_8 byteDepth; @@ -37,7 +35,7 @@ namespace ehs static const AudioCodec* GetCodec(const Str_8& ext); - ~Audio(); + ~Audio() override; Audio(); @@ -65,13 +63,7 @@ namespace ehs operator Byte*(); - void Release(); - - UInt_64 GetHashId() const; - - void SetId(Str_8 newId); - - Str_8 GetId() const; + void Release() override; UInt_64 GetSampleRate() const; diff --git a/src/io/Resource.cpp b/src/io/Resource.cpp new file mode 100644 index 0000000..e4f1545 --- /dev/null +++ b/src/io/Resource.cpp @@ -0,0 +1,95 @@ +#include "ehs/io/Resource.h" + +namespace ehs +{ + Resource::Resource() + : hashId(0) + { + AddType("Resource"); + } + + Resource::Resource(ehs::Str_8 id) + : hashId(id.Hash_64()), id(std::move(id)) + { + AddType("Resource"); + } + + Resource::Resource(Resource&& rsrc) noexcept + : BaseObj((BaseObj&&)rsrc), hashId(rsrc.hashId), id(std::move(rsrc.id)) + { + rsrc.hashId = 0; + } + + Resource::Resource(const Resource& rsrc) + : BaseObj(rsrc), hashId(rsrc.hashId), id(rsrc.id) + { + } + + Resource& Resource::operator=(Resource&& rsrc) noexcept + { + if (this == &rsrc) + return *this; + + BaseObj::operator=((BaseObj&&)rsrc); + + hashId = rsrc.hashId; + id = std::move(rsrc.id); + + rsrc.hashId = 0; + + return *this; + } + + Resource& Resource::operator=(const Resource& rsrc) + { + if (this == &rsrc) + return *this; + + BaseObj::operator=(rsrc); + + hashId = rsrc.hashId; + id = rsrc.id; + + return *this; + } + + bool Resource::operator==(const ehs::UInt_64 otherHashId) const + { + return hashId == otherHashId; + } + + bool Resource::operator!=(const ehs::UInt_64 otherHashId) const + { + return hashId != otherHashId; + } + + void Resource::Release() + { + } + + void Resource::SetId(ehs::Str_8 newId) + { + hashId = newId.Hash_64(); + id = std::move(newId); + } + + ehs::UInt_64 Resource::GetHashId() const + { + return hashId; + } + + ehs::Str_8 Resource::GetId() const + { + return id; + } + + bool Resource::IsValid() const + { + return hashId; + } + + Resource* Resource::Clone() const + { + return new Resource(*this); + } +} \ No newline at end of file diff --git a/src/io/audio/Audio.cpp b/src/io/audio/Audio.cpp index 0efcb53..b743a3c 100644 --- a/src/io/audio/Audio.cpp +++ b/src/io/audio/Audio.cpp @@ -51,14 +51,14 @@ namespace ehs } Audio::Audio() - : hashId(0), sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0), frames(0), - length(0.0f), data(nullptr), peak(nullptr) + : sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0), frames(0), length(0.0f), data(nullptr), + peak(nullptr) { AddType("Audio"); } Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const UInt_64 frames, const Byte* const data) - : hashId(id.Hash_64()), id((Str_8&&)id), dataType(dataType), byteDepth(ToByteDepth(dataType)), sampleRate(sampleRate), + : Resource((Str_8&&)id), dataType(dataType), byteDepth(ToByteDepth(dataType)), sampleRate(sampleRate), channels(channels), frames(frames), length((float)frames / (float)sampleRate), data(new Byte[GetSize()]), peak(new Byte[byteDepth]) { @@ -68,7 +68,7 @@ namespace ehs } Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const Serializer& data) - : hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), + : Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), channels(channels), frames(data.Size() / channels / byteDepth), length((float)frames / (float)sampleRate), data(new Byte[data.Size()]), peak(new Byte[byteDepth]) { @@ -78,7 +78,7 @@ namespace ehs } Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const Vector& data) - : hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), + : Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), channels(channels), frames(data.Size() / channels / byteDepth), length((float)frames / (float)sampleRate), data(new Byte[data.Size()]), peak(new Byte[byteDepth]) { @@ -88,7 +88,7 @@ namespace ehs } Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const Array& data) - : hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), + : Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), channels(channels), frames(data.Size() / channels / byteDepth), length((float)frames / (float)sampleRate), data(new Byte[data.Size()]), peak(new Byte[byteDepth]) { @@ -98,7 +98,7 @@ namespace ehs } Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const UInt_64 frames) - : hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), + : Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)), channels(channels), frames(frames), length((float)frames / (float)sampleRate), data(new Byte[GetSize()]), peak(new Byte[byteDepth]) { @@ -106,18 +106,17 @@ namespace ehs } Audio::Audio(Str_8 id) - : hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0), + : Resource((Str_8&&)id), sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0), frames(0), length(0.0f), data(nullptr), peak(nullptr) { AddType("Audio"); } Audio::Audio(Audio&& audio) noexcept - : BaseObj((BaseObj&&)audio), hashId(audio.hashId), id((Str_8&&)id), sampleRate(audio.sampleRate), - dataType(audio.dataType), byteDepth(audio.byteDepth), channels(audio.channels), frames(audio.frames), - length(audio.length), data(audio.data), peak(audio.peak) + : Resource((Resource&&)audio), sampleRate(audio.sampleRate), dataType(audio.dataType), + byteDepth(audio.byteDepth), channels(audio.channels), frames(audio.frames), length(audio.length), + data(audio.data), peak(audio.peak) { - audio.hashId = 0; audio.sampleRate = 0; audio.dataType = DataType::FLOAT; audio.byteDepth = ToByteDepth(audio.dataType); @@ -129,9 +128,9 @@ namespace ehs } Audio::Audio(const Audio& audio) - : BaseObj(audio), hashId(audio.hashId), id(audio.id), sampleRate(audio.sampleRate), dataType(audio.dataType), - byteDepth(audio.byteDepth), channels(audio.channels), frames(audio.frames), length(audio.length), - data(new Byte[GetSize()]), peak(new Byte[audio.byteDepth]) + : Resource(audio), sampleRate(audio.sampleRate), dataType(audio.dataType), byteDepth(audio.byteDepth), + channels(audio.channels), frames(audio.frames), length(audio.length), data(new Byte[GetSize()]), + peak(new Byte[audio.byteDepth]) { Util::Copy(data, audio.data, GetSize()); } @@ -141,10 +140,8 @@ namespace ehs if (this == &audio) return *this; - BaseObj::operator=((BaseObj&&)audio); + Resource::operator=((Resource&&)audio); - hashId = audio.hashId; - id = (Str_8&&)audio.id; sampleRate = audio.sampleRate; dataType = audio.dataType; byteDepth = audio.byteDepth; @@ -156,7 +153,6 @@ namespace ehs delete[] peak; peak = audio.peak; - audio.hashId = 0; audio.sampleRate = 0; audio.dataType = DataType::FLOAT; audio.byteDepth = ToByteDepth(audio.dataType); @@ -174,10 +170,8 @@ namespace ehs if (this == &audio) return *this; - BaseObj::operator=(audio); + Resource::operator=(audio); - hashId = audio.hashId; - id = audio.id; sampleRate = audio.sampleRate; dataType = audio.dataType; byteDepth = audio.byteDepth; @@ -220,22 +214,6 @@ namespace ehs peak = nullptr; } - UInt_64 Audio::GetHashId() const - { - return hashId; - } - - void Audio::SetId(Str_8 newId) - { - hashId = newId.Hash_64(); - id = (Str_8&&)newId; - } - - Str_8 Audio::GetId() const - { - return id; - } - UInt_64 Audio::GetSampleRate() const { return sampleRate;