Hy guys,
I'm studying C++ with my personal project and I'm using the Pattern Singleton but I don't have need to pointer and I like more this reference in this post, in particular, this citation
Why does everybody want to return a singleton as a pointer? Return it as a reference seems much more logical!
I implement a Singleton with a reference but I have a problem with an initialize element when the Singleton will do created, I have initialized the element in the construction but my solution not work and I can not find the problem, now I have a question How to initialize the component inside the Singleton when it will do created?
This is my code
Singleton.h
#ifndef CONFIGURATORSINGLETON_H
#define CONFIGURATORSINGLETON_H
#include <string>
#include "Properties.h"
using namespace std;
using namespace cppproperties;
namespace spyCBlock {
class ConfiguratorSingleton
{
public:
static ConfiguratorSingleton& getInstance()
{
static ConfiguratorSingleton SINGLETON;
return SINGLETON;
}
string getPathBlockDat() const;
string getPathBlockDecode() const;
string getFormatFileDecode() const;
private:
const string PATH_FILE = "/conf.properties";
const string PATH_BLOCK_DAT = "PATH_BLOCK_DAT";
const string PATH_BLOCK_DECODE = "PATH_BLOCK_DECODE";
const string FORMAT_BLOCK_DECODE = "FORMAT_BLOCK_DECODE";
ConfiguratorSingleton();
Properties configuration;
string getRootPath();
void obligatoryVariable();
//Variable proprieties obligatory
string pathBlockDat;
string pathBlockDecode;
string formatFileDecode;
};
}
#endif // CONFIGURATORSINGLETON_H
Singleton.cpp
#include <experimental/filesystem>
#include <glog/logging.h>
#include "ConfiguratorSingleton.h"
#include "PropertiesParser.h"
using namespace spyCBlock;
ConfiguratorSingleton::ConfiguratorSingleton()
{
string pathConfiguration = getRootPath() + PATH_FILE;
LOG(INFO) << "Path congiration file is: " << pathConfiguration;
configuration = PropertiesParser::Read(pathConfiguration);
}
string ConfiguratorSingleton::getRootPath()
{
return experimental::filesystem::current_path();
}
void ConfiguratorSingleton::obligatoryVariable()
{
LOG(WARNING) << "Init setting variable default SpyCBlock";
pathBlockDat = configuration.GetProperty(PATH_BLOCK_DAT);
LOG(WARNING) << "The path of the block bitcoin core is: " << pathBlockDat;
pathBlockDecode = configuration.GetProperty(PATH_BLOCK_DECODE);
LOG(WARNING) << "The path of the directory for save the block decode is: " << pathBlockDecode;
formatFileDecode = configuration.GetProperty(FORMAT_BLOCK_DECODE);
LOG(WARNING) << "The format of the decode output is: " << formatFileDecode;
}
string ConfiguratorSingleton::getFormatFileDecode() const
{
return formatFileDecode;
}
string ConfiguratorSingleton::getPathBlockDecode() const
{
return pathBlockDecode;
}
string ConfiguratorSingleton::getPathBlockDat() const
{
return pathBlockDat;
}
Aucun commentaire:
Enregistrer un commentaire