refactor: Improve utils code style (#841)

* Improve utils code style

* Fix indendation

Co-authored-by: brindosch <edeltraud70@gmx.de>
This commit is contained in:
Murat Seker 2020-06-28 23:12:22 +02:00 committed by GitHub
parent bfb50b8d91
commit 458113f8f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 71 additions and 98 deletions

View File

@ -10,8 +10,8 @@
namespace FileUtils { namespace FileUtils {
QString getBaseName( QString sourceFile); QString getBaseName(QString sourceFile);
QString getDirName( QString sourceFile); QString getDirName(QString sourceFile);
/// ///
/// @brief remove directory recursive given by path /// @brief remove directory recursive given by path

View File

@ -15,15 +15,13 @@ namespace NetUtils {
{ {
const quint16 prevPort = port; const quint16 prevPort = port;
QTcpServer server; QTcpServer server;
bool corrected = false;
while (!server.listen(QHostAddress::Any, port)) while (!server.listen(QHostAddress::Any, port))
{ {
corrected = true;
Warning(log,"Port '%d' is already in use, will increment", port); Warning(log,"Port '%d' is already in use, will increment", port);
port ++; port ++;
} }
server.close(); server.close();
if(corrected) if(port != prevPort)
{ {
Warning(log, "The requested Port '%d' was already in use, will use Port '%d' instead", prevPort, port); Warning(log, "The requested Port '%d' was already in use, will use Port '%d' instead", prevPort, port);
return false; return false;

View File

@ -84,7 +84,7 @@ public:
/// ///
/// @note The values are updated in place. /// @note The values are updated in place.
/// ///
void getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t & w ); void getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t & w) const;
/// ///
/// Apply the transform the the given RGB values. /// Apply the transform the the given RGB values.

View File

@ -58,68 +58,45 @@ namespace hyperion {
return stringToColorOrder(deviceConfig["colorOrder"].toString("rgb")); return stringToColorOrder(deviceConfig["colorOrder"].toString("rgb"));
} }
RgbTransform* createRgbTransform(const QJsonObject& colorConfig) RgbTransform createRgbTransform(const QJsonObject& colorConfig)
{ {
const double backlightThreshold = colorConfig["backlightThreshold"].toDouble(0.0); const double backlightThreshold = colorConfig["backlightThreshold"].toDouble(0.0);
const bool backlightColored = colorConfig["backlightColored"].toBool(false); const bool backlightColored = colorConfig["backlightColored"].toBool(false);
const double brightness = colorConfig["brightness"].toInt(100); const double brightness = colorConfig["brightness"].toInt(100);
const double brightnessComp= colorConfig["brightnessCompensation"].toInt(100); const double brightnessComp = colorConfig["brightnessCompensation"].toInt(100);
const double gammaR = colorConfig["gammaRed"].toDouble(1.0); const double gammaR = colorConfig["gammaRed"].toDouble(1.0);
const double gammaG = colorConfig["gammaGreen"].toDouble(1.0); const double gammaG = colorConfig["gammaGreen"].toDouble(1.0);
const double gammaB = colorConfig["gammaBlue"].toDouble(1.0); const double gammaB = colorConfig["gammaBlue"].toDouble(1.0);
RgbTransform* transform = new RgbTransform(gammaR, gammaG, gammaB, backlightThreshold, backlightColored, brightness, brightnessComp); return RgbTransform(gammaR, gammaG, gammaB, backlightThreshold, backlightColored, brightness, brightnessComp);
return transform;
} }
RgbChannelAdjustment* createRgbChannelAdjustment(const QJsonObject& colorConfig, const QString channelName, const int defaultR, const int defaultG, const int defaultB) RgbChannelAdjustment createRgbChannelAdjustment(const QJsonObject& colorConfig, const QString& channelName, const int defaultR, const int defaultG, const int defaultB)
{ {
const QJsonArray& channelConfig = colorConfig[channelName].toArray(); const QJsonArray& channelConfig = colorConfig[channelName].toArray();
RgbChannelAdjustment* adjustment = new RgbChannelAdjustment( return RgbChannelAdjustment(
channelConfig[0].toInt(defaultR), channelConfig[0].toInt(defaultR),
channelConfig[1].toInt(defaultG), channelConfig[1].toInt(defaultG),
channelConfig[2].toInt(defaultB), channelConfig[2].toInt(defaultB),
"ChannelAdjust_"+channelName.toUpper() "ChannelAdjust_" + channelName.toUpper()
); );
return adjustment;
} }
ColorAdjustment * createColorAdjustment(const QJsonObject & adjustmentConfig) ColorAdjustment* createColorAdjustment(const QJsonObject & adjustmentConfig)
{ {
const QString id = adjustmentConfig["id"].toString("default"); const QString id = adjustmentConfig["id"].toString("default");
RgbChannelAdjustment * blackAdjustment = createRgbChannelAdjustment(adjustmentConfig, "black" , 0, 0, 0);
RgbChannelAdjustment * whiteAdjustment = createRgbChannelAdjustment(adjustmentConfig, "white" , 255,255,255);
RgbChannelAdjustment * redAdjustment = createRgbChannelAdjustment(adjustmentConfig, "red" , 255, 0, 0);
RgbChannelAdjustment * greenAdjustment = createRgbChannelAdjustment(adjustmentConfig, "green" , 0,255, 0);
RgbChannelAdjustment * blueAdjustment = createRgbChannelAdjustment(adjustmentConfig, "blue" , 0, 0,255);
RgbChannelAdjustment * cyanAdjustment = createRgbChannelAdjustment(adjustmentConfig, "cyan" , 0,255,255);
RgbChannelAdjustment * magentaAdjustment = createRgbChannelAdjustment(adjustmentConfig, "magenta", 255, 0,255);
RgbChannelAdjustment * yellowAdjustment = createRgbChannelAdjustment(adjustmentConfig, "yellow" , 255,255, 0);
RgbTransform * rgbTransform = createRgbTransform(adjustmentConfig);
ColorAdjustment * adjustment = new ColorAdjustment(); ColorAdjustment * adjustment = new ColorAdjustment();
adjustment->_id = id; adjustment->_id = id;
adjustment->_rgbBlackAdjustment = *blackAdjustment; adjustment->_rgbBlackAdjustment = createRgbChannelAdjustment(adjustmentConfig, "black" , 0, 0, 0);
adjustment->_rgbWhiteAdjustment = *whiteAdjustment; adjustment->_rgbWhiteAdjustment = createRgbChannelAdjustment(adjustmentConfig, "white" , 255,255,255);
adjustment->_rgbRedAdjustment = *redAdjustment; adjustment->_rgbRedAdjustment = createRgbChannelAdjustment(adjustmentConfig, "red" , 255, 0, 0);
adjustment->_rgbGreenAdjustment = *greenAdjustment; adjustment->_rgbGreenAdjustment = createRgbChannelAdjustment(adjustmentConfig, "green" , 0,255, 0);
adjustment->_rgbBlueAdjustment = *blueAdjustment; adjustment->_rgbBlueAdjustment = createRgbChannelAdjustment(adjustmentConfig, "blue" , 0, 0,255);
adjustment->_rgbCyanAdjustment = *cyanAdjustment; adjustment->_rgbCyanAdjustment = createRgbChannelAdjustment(adjustmentConfig, "cyan" , 0,255,255);
adjustment->_rgbMagentaAdjustment = *magentaAdjustment; adjustment->_rgbMagentaAdjustment = createRgbChannelAdjustment(adjustmentConfig, "magenta", 255, 0,255);
adjustment->_rgbYellowAdjustment = *yellowAdjustment; adjustment->_rgbYellowAdjustment = createRgbChannelAdjustment(adjustmentConfig, "yellow" , 255,255, 0);
adjustment->_rgbTransform = *rgbTransform; adjustment->_rgbTransform = createRgbTransform(adjustmentConfig);
// Cleanup the allocated individual adjustments
delete blackAdjustment;
delete whiteAdjustment;
delete redAdjustment;
delete greenAdjustment;
delete blueAdjustment;
delete cyanAdjustment;
delete magentaAdjustment;
delete yellowAdjustment;
delete rgbTransform;
return adjustment; return adjustment;
} }

View File

@ -1,6 +1,7 @@
#include <utils/ColorSys.h> #include <utils/ColorSys.h>
#include <QColor> #include <QColor>
void ColorSys::rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance) void ColorSys::rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance)
{ {
QColor color(red,green,blue); QColor color(red,green,blue);

View File

@ -10,21 +10,20 @@
namespace FileUtils { namespace FileUtils {
QString getBaseName( QString sourceFile) QString getBaseName(QString sourceFile)
{ {
QFileInfo fi( sourceFile ); QFileInfo fi(sourceFile);
return fi.fileName(); return fi.fileName();
} }
QString getDirName( QString sourceFile) QString getDirName(QString sourceFile)
{ {
QFileInfo fi( sourceFile ); QFileInfo fi(sourceFile);
return fi.path(); return fi.path();
} }
bool removeDir(const QString& path, Logger* log) bool removeDir(const QString& path, Logger* log)
{ {
//QDir dir(path);
if(!QDir(path).removeRecursively()) if(!QDir(path).removeRecursively())
{ {
Error(log, "Failed to remove directory: %s", QSTRING_CSTR(path)); Error(log, "Failed to remove directory: %s", QSTRING_CSTR(path));
@ -35,8 +34,7 @@ namespace FileUtils {
bool fileExists(const QString& path, Logger* log, bool ignError) bool fileExists(const QString& path, Logger* log, bool ignError)
{ {
QFile file(path); if(!QFile::exists(path))
if(!file.exists())
{ {
ErrorIf((!ignError), log,"File does not exist: %s",QSTRING_CSTR(path)); ErrorIf((!ignError), log,"File does not exist: %s",QSTRING_CSTR(path));
return false; return false;

View File

@ -37,7 +37,7 @@ Logger* Logger::getInstance(QString name, Logger::LogLevel minLevel)
LoggerMap = new std::map<QString,Logger*>; LoggerMap = new std::map<QString,Logger*>;
} }
if ( LoggerMap->find(name) == LoggerMap->end() ) if (LoggerMap->find(name) == LoggerMap->end())
{ {
log = new Logger(name,minLevel); log = new Logger(name,minLevel);
LoggerMap->insert(std::pair<QString,Logger*>(name,log)); // compat version, replace it with following line if we have 100% c++11 LoggerMap->insert(std::pair<QString,Logger*>(name,log)); // compat version, replace it with following line if we have 100% c++11
@ -57,10 +57,10 @@ void Logger::deleteInstance(QString name)
if (LoggerMap == nullptr) if (LoggerMap == nullptr)
return; return;
if ( name.isEmpty() ) if (name.isEmpty())
{ {
std::map<QString,Logger*>::iterator it; std::map<QString,Logger*>::iterator it;
for ( it=LoggerMap->begin(); it != LoggerMap->end(); it++) for (it = LoggerMap->begin(); it != LoggerMap->end(); it++)
{ {
delete it->second; delete it->second;
} }
@ -76,7 +76,7 @@ void Logger::deleteInstance(QString name)
void Logger::setLogLevel(LogLevel level,QString name) void Logger::setLogLevel(LogLevel level,QString name)
{ {
if ( name.isEmpty() ) if (name.isEmpty())
{ {
GLOBAL_MIN_LOG_LEVEL = level; GLOBAL_MIN_LOG_LEVEL = level;
} }
@ -89,7 +89,7 @@ void Logger::setLogLevel(LogLevel level,QString name)
Logger::LogLevel Logger::getLogLevel(QString name) Logger::LogLevel Logger::getLogLevel(QString name)
{ {
if ( name.isEmpty() ) if (name.isEmpty())
{ {
return GLOBAL_MIN_LOG_LEVEL; return GLOBAL_MIN_LOG_LEVEL;
} }
@ -98,7 +98,7 @@ Logger::LogLevel Logger::getLogLevel(QString name)
return log->getMinLevel(); return log->getMinLevel();
} }
Logger::Logger ( QString name, LogLevel minLevel ) Logger::Logger (QString name, LogLevel minLevel)
: QObject() : QObject()
, _name(name) , _name(name)
, _minLevel(minLevel) , _minLevel(minLevel)
@ -125,7 +125,7 @@ Logger::Logger ( QString name, LogLevel minLevel )
loggerCount++; loggerCount++;
if (_syslogEnabled && loggerCount == 1 ) if (_syslogEnabled && loggerCount == 1)
{ {
#ifndef _WIN32 #ifndef _WIN32
openlog (_appname_char, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); openlog (_appname_char, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
@ -138,7 +138,7 @@ Logger::~Logger()
//Debug(this, "logger '%s' destroyed", QSTRING_CSTR(_name) ); //Debug(this, "logger '%s' destroyed", QSTRING_CSTR(_name) );
loggerCount--; loggerCount--;
#ifndef _WIN32 #ifndef _WIN32
if ( loggerCount == 0 ) if (loggerCount == 0)
closelog(); closelog();
#endif #endif
} }
@ -237,7 +237,7 @@ void LoggerManager::handleNewLogMessage(const Logger::T_LOG_MESSAGE &msg)
LoggerManager* LoggerManager::getInstance() LoggerManager* LoggerManager::getInstance()
{ {
if ( _instance == nullptr ) if (_instance == nullptr)
_instance = new LoggerManager(); _instance = new LoggerManager();
return _instance; return _instance;
} }

View File

@ -6,7 +6,7 @@ namespace Process {
void restartHyperion(bool asNewProcess){} void restartHyperion(bool asNewProcess){}
QByteArray command_exec(QString cmd, QByteArray data) QByteArray command_exec(QString /*cmd*/, QByteArray /*data*/)
{ {
return QSTRING_CSTR(QString()); return QSTRING_CSTR(QString());
} }
@ -45,7 +45,7 @@ void restartHyperion(bool asNewProcess)
{ {
int str_size = qargs[i].toLocal8Bit().size(); int str_size = qargs[i].toLocal8Bit().size();
args[i] = new char[str_size+1]; args[i] = new char[str_size+1];
strncpy(args[i], qargs[i].toLocal8Bit().constData(),str_size ); strncpy(args[i], qargs[i].toLocal8Bit().constData(),str_size);
args[i][str_size] = '\0'; args[i][str_size] = '\0';
} }
@ -56,7 +56,7 @@ void restartHyperion(bool asNewProcess)
QByteArray command_exec(QString cmd, QByteArray data) QByteArray command_exec(QString cmd, QByteArray data)
{ {
char buffer[128]; char buffer[128];
QString result = ""; QString result;
std::shared_ptr<FILE> pipe(popen(cmd.toLocal8Bit().constData(), "r"), pclose); std::shared_ptr<FILE> pipe(popen(cmd.toLocal8Bit().constData(), "r"), pclose);
if (pipe) if (pipe)

View File

@ -18,7 +18,7 @@ Logger* Profiler::_logger = nullptr;
double getClockDelta(clock_t start) double getClockDelta(clock_t start)
{ {
return ((double)(clock() - start) / CLOCKS_PER_SEC) ; return ((double)(clock() - start) / CLOCKS_PER_SEC);
} }
Profiler::Profiler(const char* sourceFile, const char* func, unsigned int line) Profiler::Profiler(const char* sourceFile, const char* func, unsigned int line)
@ -35,12 +35,12 @@ Profiler::Profiler(const char* sourceFile, const char* func, unsigned int line)
Profiler::~Profiler() Profiler::~Profiler()
{ {
_logger->Message( Logger::DEBUG, _file,_func, _line, "<<< exit block %d, executed for %f s", _blockId, getClockDelta(_startTime) ); _logger->Message( Logger::DEBUG, _file,_func, _line, "<<< exit block %d, executed for %f s", _blockId, getClockDelta(_startTime));
} }
void Profiler::initLogger() void Profiler::initLogger()
{ {
if (_logger == nullptr ) if (_logger == nullptr)
_logger = Logger::getInstance("PROFILER", Logger::DEBUG); _logger = Logger::getInstance("PROFILER", Logger::DEBUG);
} }
@ -52,22 +52,22 @@ void Profiler::TimerStart(const QString timerName, const char* sourceFile, const
StopWatchItem item = {sourceFile, func, line}; StopWatchItem item = {sourceFile, func, line};
ret = GlobalProfilerMap.emplace(timerName, item); ret = GlobalProfilerMap.emplace(timerName, item);
if ( ! ret.second ) if (!ret.second)
{ {
if ( ret.first->second.sourceFile == sourceFile && ret.first->second.func == func && ret.first->second.line == line ) if (ret.first->second.sourceFile == sourceFile && ret.first->second.func == func && ret.first->second.line == line)
{ {
_logger->Message(Logger::DEBUG, sourceFile, func, line, "restart timer '%s'", QSTRING_CSTR(timerName) ); _logger->Message(Logger::DEBUG, sourceFile, func, line, "restart timer '%s'", QSTRING_CSTR(timerName));
ret.first->second.startTime = clock(); ret.first->second.startTime = clock();
} }
else else
{ {
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' started in multiple locations. First occurence %s:%d:%s()", _logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' started in multiple locations. First occurence %s:%d:%s()",
QSTRING_CSTR(timerName), FileUtils::getBaseName(ret.first->second.sourceFile).toLocal8Bit().constData(), ret.first->second.line, ret.first->second.func ); QSTRING_CSTR(timerName), FileUtils::getBaseName(ret.first->second.sourceFile).toLocal8Bit().constData(), ret.first->second.line, ret.first->second.func);
} }
} }
else else
{ {
_logger->Message(Logger::DEBUG, sourceFile, func, line, "start timer '%s'", QSTRING_CSTR(timerName) ); _logger->Message(Logger::DEBUG, sourceFile, func, line, "start timer '%s'", QSTRING_CSTR(timerName));
} }
} }
@ -79,11 +79,11 @@ void Profiler::TimerGetTime(const QString timerName, const char* sourceFile, con
if (ret != GlobalProfilerMap.end()) if (ret != GlobalProfilerMap.end())
{ {
_logger->Message(Logger::DEBUG, sourceFile, func, line, "timer '%s' started at %s:%d:%s() took %f s execution time until here", QSTRING_CSTR(timerName), _logger->Message(Logger::DEBUG, sourceFile, func, line, "timer '%s' started at %s:%d:%s() took %f s execution time until here", QSTRING_CSTR(timerName),
FileUtils::getBaseName(ret->second.sourceFile).toLocal8Bit().constData(), ret->second.line, ret->second.func, getClockDelta(ret->second.startTime) ); FileUtils::getBaseName(ret->second.sourceFile).toLocal8Bit().constData(), ret->second.line, ret->second.func, getClockDelta(ret->second.startTime));
} }
else else
{ {
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' not started", QSTRING_CSTR(timerName) ); _logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' not started", QSTRING_CSTR(timerName));
} }
} }

View File

@ -26,7 +26,6 @@ void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, const WhiteAlgorithm algori
output->blue = input.blue - output->white; output->blue = input.blue - output->white;
break; break;
} }
case WhiteAlgorithm::SUB_MIN_WARM_ADJUST: case WhiteAlgorithm::SUB_MIN_WARM_ADJUST:
{ {
// http://forum.garagecube.com/viewtopic.php?t=10178 // http://forum.garagecube.com/viewtopic.php?t=10178

View File

@ -133,7 +133,7 @@ void RgbTransform::updateBrightnessComponents()
} }
} }
void RgbTransform::getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t & w ) void RgbTransform::getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t & w) const
{ {
rgb = _brightness_rgb; rgb = _brightness_rgb;
cmy = _brightness_cmy; cmy = _brightness_cmy;

View File

@ -25,7 +25,7 @@ SysInfo::~SysInfo()
SysInfo::HyperionSysInfo SysInfo::get() SysInfo::HyperionSysInfo SysInfo::get()
{ {
if ( SysInfo::_instance == nullptr ) if (SysInfo::_instance == nullptr)
SysInfo::_instance = new SysInfo(); SysInfo::_instance = new SysInfo();
return SysInfo::_instance->_sysinfo; return SysInfo::_instance->_sysinfo;