Core: Switch Hashable struct to HashAppend function

Former-commit-id: 2a20eca0e75bf4067d390f4f5e446de78f26799c
This commit is contained in:
Lynix 2015-11-18 18:28:56 +01:00
parent 00423c4211
commit 167f3e4a27
9 changed files with 45 additions and 60 deletions

View File

@ -24,10 +24,10 @@ namespace Nz
template<typename T> void HashCombine(std::size_t& seed, const T& v); template<typename T> void HashCombine(std::size_t& seed, const T& v);
template<typename T> template<typename T>
struct Hashable; struct TypeTag {};
template<typename T> template<typename T>
struct TypeTag {};
} }
#include <Nazara/Core/Algorithm.inl> #include <Nazara/Core/Algorithm.inl>

View File

@ -53,7 +53,9 @@ namespace Nz
ByteArray ComputeHash(AbstractHash* hash, const T& v) ByteArray ComputeHash(AbstractHash* hash, const T& v)
{ {
hash->Begin(); hash->Begin();
Hashable<T>()(v, hash);
HashAppend(hash, v);
return hash->End(); return hash->End();
} }

View File

@ -128,8 +128,7 @@ namespace Nz
Container m_array; Container m_array;
}; };
template<> inline bool HashAppend(AbstractHash* hash, const ByteArray& byteArray);
struct Hashable<ByteArray>;
} }
namespace std namespace std

View File

@ -335,15 +335,11 @@ namespace Nz
return m_array >= rhs.m_array; return m_array >= rhs.m_array;
} }
template<> inline bool HashAppend(AbstractHash* hash, const ByteArray& byteArray)
struct Hashable<ByteArray>
{ {
bool operator()(const ByteArray& byteArray, AbstractHash* hash) const hash->Append(byteArray.GetConstBuffer(), byteArray.GetSize());
{ return true;
hash->Append(byteArray.GetConstBuffer(), byteArray.GetSize()); }
return true;
}
};
} }
namespace std namespace std

View File

@ -104,8 +104,7 @@ namespace Nz
FileImpl* m_impl; FileImpl* m_impl;
}; };
template<> NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile);
struct Hashable<File>;
} }
#include <Nazara/Core/File.inl> #include <Nazara/Core/File.inl>

View File

@ -3,8 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/AbstractHash.hpp> #include <Nazara/Core/AbstractHash.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
namespace Nz namespace Nz
@ -18,39 +17,6 @@ namespace Nz
{ {
return Nz::ComputeHash(hash, File(filePath)); return Nz::ComputeHash(hash, File(filePath));
} }
template<>
struct Hashable<File>
{
bool operator()(const Nz::File& originalFile, AbstractHash* hash) const
{
File file(originalFile.GetPath());
if (!file.Open(OpenMode_ReadOnly))
{
NazaraError("Unable to open file");
return false;
}
UInt64 remainingSize = file.GetSize();
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
while (remainingSize > 0)
{
unsigned int size = static_cast<unsigned int>(std::min(remainingSize, static_cast<UInt64>(NAZARA_CORE_FILE_BUFFERSIZE)));
if (file.Read(&buffer[0], sizeof(char), size) != sizeof(char)*size)
{
NazaraError("Unable to read file");
return false;
}
remainingSize -= size;
hash->Append(reinterpret_cast<UInt8*>(&buffer[0]), size);
}
return true;
}
};
} }
#include <Nazara/Core/DebugOff.hpp> #include <Nazara/Core/DebugOff.hpp>

View File

@ -324,8 +324,7 @@ namespace Nz
}; };
}; };
template<> inline bool HashAppend(AbstractHash* hash, const String& string);
struct Hashable<String>;
} }
namespace std namespace std

View File

@ -39,15 +39,11 @@ namespace Nz
string[strSize] = '\0'; string[strSize] = '\0';
} }
template<> inline bool HashAppend(AbstractHash* hash, const String& string)
struct Hashable<String>
{ {
bool operator()(const String& str, AbstractHash* hash) const hash->Append(reinterpret_cast<const UInt8*>(string.GetConstBuffer()), string.GetSize());
{ return true;
hash->Append(reinterpret_cast<const UInt8*>(str.GetConstBuffer()), str.GetSize()); }
return true;
}
};
} }
namespace std namespace std

View File

@ -596,4 +596,32 @@ namespace Nz
return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath)); return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath));
} }
NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile)
{
File file(originalFile.GetPath());
if (!file.Open(OpenMode_ReadOnly))
{
NazaraError("Unable to open file");
return false;
}
UInt64 remainingSize = file.GetSize();
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
while (remainingSize > 0)
{
unsigned int size = static_cast<unsigned int>(std::min(remainingSize, static_cast<UInt64>(NAZARA_CORE_FILE_BUFFERSIZE)));
if (file.Read(&buffer[0], sizeof(char), size) != sizeof(char)*size)
{
NazaraError("Unable to read file");
return false;
}
remainingSize -= size;
hash->Append(reinterpret_cast<UInt8*>(&buffer[0]), size);
}
return true;
};
} }