67 lines
1.9 KiB
C++
67 lines
1.9 KiB
C++
#pragma once
|
|
#include <vector>
|
|
#include <stdio.h>
|
|
#include <fstream>
|
|
|
|
#include "../../core/Defines.h"
|
|
|
|
#include "../../core/Serializer.h"
|
|
|
|
// Pool builder class that can be used to build a pool for a specific kind of tree. Constructor should indicate the tree type.
|
|
template<class T>
|
|
class BasePoolBuilder
|
|
{
|
|
public:
|
|
virtual ~BasePoolBuilder() {}
|
|
|
|
virtual size_t GetPoolSize(const T* tree) = 0;
|
|
virtual bool BuildPool(const T* tree, std::vector<unsigned8>& pool) = 0;
|
|
virtual bool VerifyPool(std::vector<unsigned8>& pool, const unsigned8& depth) const = 0;
|
|
virtual std::string GetFullFileName(const std::string& fileName) const = 0;
|
|
|
|
virtual bool ReadPool(const std::string& fileName, std::vector<unsigned8>& pool) const
|
|
{
|
|
std::string binFileName = GetFullFileName(fileName);
|
|
std::ifstream nodePoolInFile(binFileName, std::ios::binary);
|
|
|
|
if (nodePoolInFile.good()) {
|
|
|
|
// Destroy whole current node pool
|
|
Serializer<std::vector<unsigned8>, unsigned64>::Deserialize(pool, nodePoolInFile);
|
|
nodePoolInFile.close();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool WritePool(const std::string& fileName, const std::vector<unsigned8>& pool) const
|
|
{
|
|
std::string binFileName = GetFullFileName(fileName);
|
|
|
|
if (!pool.empty()) {
|
|
std::ofstream nodePoolOutFile(binFileName, std::ios::binary);
|
|
Serializer<std::vector<unsigned8>, unsigned64>::Serialize(pool, nodePoolOutFile);
|
|
nodePoolOutFile.close();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool BuildOrReadPool(const T* tree, const std::string& fileName, std::vector<unsigned8>& pool)
|
|
{
|
|
if (ReadPool(fileName, pool)) return true;
|
|
bool res = BuildPool(tree, pool);
|
|
if (res) WritePool(fileName, pool);
|
|
return res;
|
|
}
|
|
|
|
bool VerifyCachedPool(const std::string& fileName, const unsigned8& depth) const
|
|
{
|
|
std::vector<unsigned8> pool;
|
|
if (ReadPool(fileName, pool))
|
|
return VerifyPool(pool, depth);
|
|
return false;
|
|
}
|
|
};
|
|
|