Core: Switch Hashable struct to HashAppend function
Former-commit-id: 2a20eca0e75bf4067d390f4f5e446de78f26799c
This commit is contained in:
parent
00423c4211
commit
167f3e4a27
|
|
@ -24,10 +24,10 @@ namespace Nz
|
|||
template<typename T> void HashCombine(std::size_t& seed, const T& v);
|
||||
|
||||
template<typename T>
|
||||
struct Hashable;
|
||||
struct TypeTag {};
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct TypeTag {};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Algorithm.inl>
|
||||
|
|
|
|||
|
|
@ -53,7 +53,9 @@ namespace Nz
|
|||
ByteArray ComputeHash(AbstractHash* hash, const T& v)
|
||||
{
|
||||
hash->Begin();
|
||||
Hashable<T>()(v, hash);
|
||||
|
||||
HashAppend(hash, v);
|
||||
|
||||
return hash->End();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -128,8 +128,7 @@ namespace Nz
|
|||
Container m_array;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Hashable<ByteArray>;
|
||||
inline bool HashAppend(AbstractHash* hash, const ByteArray& byteArray);
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -335,15 +335,11 @@ namespace Nz
|
|||
return m_array >= rhs.m_array;
|
||||
}
|
||||
|
||||
template<>
|
||||
struct Hashable<ByteArray>
|
||||
{
|
||||
bool operator()(const ByteArray& byteArray, AbstractHash* hash) const
|
||||
inline bool HashAppend(AbstractHash* hash, const ByteArray& byteArray)
|
||||
{
|
||||
hash->Append(byteArray.GetConstBuffer(), byteArray.GetSize());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -104,8 +104,7 @@ namespace Nz
|
|||
FileImpl* m_impl;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Hashable<File>;
|
||||
NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/File.inl>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -18,39 +17,6 @@ namespace Nz
|
|||
{
|
||||
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>
|
||||
|
|
|
|||
|
|
@ -324,8 +324,7 @@ namespace Nz
|
|||
};
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Hashable<String>;
|
||||
inline bool HashAppend(AbstractHash* hash, const String& string);
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -39,15 +39,11 @@ namespace Nz
|
|||
string[strSize] = '\0';
|
||||
}
|
||||
|
||||
template<>
|
||||
struct Hashable<String>
|
||||
inline bool HashAppend(AbstractHash* hash, const String& string)
|
||||
{
|
||||
bool operator()(const String& str, AbstractHash* hash) const
|
||||
{
|
||||
hash->Append(reinterpret_cast<const UInt8*>(str.GetConstBuffer()), str.GetSize());
|
||||
hash->Append(reinterpret_cast<const UInt8*>(string.GetConstBuffer()), string.GetSize());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -596,4 +596,32 @@ namespace Nz
|
|||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue