Ref T203, platform class (OS)

This commit is contained in:
Klaus Basan
2017-12-09 19:34:01 +01:00
parent 54e9d1c429
commit 6347c65a2f
5 changed files with 529 additions and 6 deletions

226
src/blackmisc/platform.cpp Normal file
View File

@@ -0,0 +1,226 @@
/* Copyright (C) 2017
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "platform.h"
#include "blackmisc/icons.h"
#include "blackmisc/stringutils.h"
#include "blackmisc/comparefunctions.h"
#include "blackconfig/buildconfig.h"
#include <QJsonValue>
#include <Qt>
#include <QtGlobal>
using namespace BlackConfig;
namespace BlackMisc
{
CPlatform::CPlatform(const QString &p) : m_platform(stringToPlatform(p))
{ }
CPlatform::CPlatform(Platform p) : m_platform(p)
{ }
CPlatform::PlatformFlag CPlatform::getPlatformFlag() const
{
if (this->isSinglePlatform()) { return static_cast<PlatformFlag>(m_platform); }
return UnknownOs;
}
bool CPlatform::matchesAny(const CPlatform &otherPlatform) const
{
return (this->m_platform & otherPlatform.m_platform) > 0;
}
int CPlatform::numberPlatforms() const
{
const Platform p = this->getPlatform();
int c = 0;
if (p.testFlag(Win32)) c++;
if (p.testFlag(Win64)) c++;
if (p.testFlag(MacOS)) c++;
if (p.testFlag(Linux)) c++;
return c;
}
bool CPlatform::isSinglePlatform() const
{
return this->numberPlatforms() == 1;
}
bool CPlatform::isAnyWindows() const
{
const Platform p = this->getPlatform();
return p.testFlag(Win32) || p.testFlag(Win64);
}
QString CPlatform::getPlatformName() const
{
return this->convertToQString(true);
}
CIcon CPlatform::toIcon() const
{
if (this->getPlatform() == All) { return CIcon::iconByIndex(CIcons::OSAll); }
switch (this->getPlatformFlag())
{
case Win32:
case Win64: return CIcon::iconByIndex(CIcons::OSWindows);
case Linux: return CIcon::iconByIndex(CIcons::OSLinux);
case MacOS: return CIcon::iconByIndex(CIcons::OSMacOs);
default: break;
}
return CIcon::iconByIndex(CIcons::StandardIconEmpty);
}
QString CPlatform::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
switch (m_platform)
{
case Win32: return QStringLiteral("Win32");
case Win64: return QStringLiteral("Win64");
case Linux: return QStringLiteral("Linux");
case MacOS: return QStringLiteral("MacOSX");
default: break;
}
return QStringLiteral("unknown");
}
CVariant CPlatform::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexPlatform: return CVariant::fromValue(m_platform);
default: return CValueObject::propertyByIndex(index);
}
}
void CPlatform::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CPlatform>(); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexPlatform: this->setPlatform(static_cast<Platform>(variant.toInt())); break;
default: CValueObject::setPropertyByIndex(index, variant); break;
}
}
int CPlatform::comparePropertyByIndex(const CPropertyIndex &index, const CPlatform &compareValue) const
{
if (index.isMyself()) { return Compare::compare(m_platform, compareValue.m_platform); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexPlatform: return Compare::compare(m_platform, compareValue.m_platform);
default: Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison possible");
}
return 0;
}
CPlatform::Platform CPlatform::stringToPlatform(const QString &str)
{
const QString s(str.trimmed().toLower());
if (s.contains("win"))
{
if (s.contains("32")) return Win32;
return Win64;
}
if (s.contains("linux")) return Linux;
if (s.contains("mac") || s.contains("osx")) return MacOS;
// special ones
if (s.contains("debian")) return Linux;
if (s.contains("darwin")) return MacOS;
if (s.contains("all")) return All;
if (s.contains("independent")) return Independent;
return UnknownOs;
}
const CPlatform &CPlatform::stringToPlatformObject(const QString &str)
{
switch (stringToPlatform(str))
{
case Win32: return CPlatform::win32();
case Win64: return CPlatform::win64();
case Linux: return CPlatform::linux();
case MacOS: return CPlatform::macOS();
default: break;
}
return unknownOs();
}
namespace Private
{
const CPlatform &currentPlatformImpl()
{
if (CBuildConfig::isRunningOnWindowsNtPlatform())
{
const int wordSize = CBuildConfig::buildWordSize();
return wordSize == 64 ? CPlatform::win64() : CPlatform::win32();
}
if (CBuildConfig::isRunningOnLinuxPlatform()) { return CPlatform::linux(); }
if (CBuildConfig::isRunningOnMacOSPlatform()) { return CPlatform::macOS(); }
return CPlatform::unknownOs();
}
}
const CPlatform &CPlatform::currentPlatform()
{
static const CPlatform p = Private::currentPlatformImpl();
return p;
}
const CPlatform &CPlatform::win32()
{
static const CPlatform p(Win32);
return p;
}
const CPlatform &CPlatform::win64()
{
static const CPlatform p(Win64);
return p;
}
const CPlatform &CPlatform::linux()
{
static const CPlatform p(Linux);
return p;
}
const CPlatform &CPlatform::macOS()
{
static const CPlatform p(MacOS);
return p;
}
const CPlatform &CPlatform::unknownOs()
{
static const CPlatform p(UnknownOs);
return p;
}
const CPlatform &CPlatform::allOs()
{
static const CPlatform p(All);
return p;
}
const CPlatform &CPlatform::independent()
{
static const CPlatform p(All);
return p;
}
} // namespace

158
src/blackmisc/platform.h Normal file
View File

@@ -0,0 +1,158 @@
/* Copyright (C) 2017
* Swift Project Community / Contributors
*
* This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_PLATFORM_H
#define BLACKMISC_PLATFORM_H
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/db/datastore.h"
#include "blackmisc/icon.h"
#include "blackmisc/metaclass.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/variant.h"
#include <QMetaEnum>
#include <QString>
namespace BlackMisc
{
/*!
* Platform (i.e. a platform swift supports)
*/
class BLACKMISC_EXPORT CPlatform : public CValueObject<CPlatform>
{
public:
//! Properties by index
enum ColumnIndex
{
IndexPlatform = CPropertyIndex::GlobalIndexCPlatform
};
//! OS
enum PlatformFlag
{
UnknownOs = 0,
Win32 = 1 << 0,
Win64 = 1 << 1,
Linux = 1 << 2,
MacOS = 1 << 3,
Independent = 1 << 4,
AllWindows = Win32 | Win64,
All = AllWindows | Linux | MacOS,
All32 = Win32,
All64 = Win64 | Linux | MacOS
};
Q_DECLARE_FLAGS(Platform, PlatformFlag)
//! Constructor
CPlatform() {}
//! Constructor
CPlatform(const QString &p);
//! Constructor
CPlatform(Platform p);
//! Platform
Platform getPlatform() const { return static_cast<Platform>(m_platform); }
//! Platform flag
PlatformFlag getPlatformFlag() const;
//! Matches any other platform
bool matchesAny(const CPlatform &otherPlatform) const;
//! Number of supported platforms
int numberPlatforms() const;
//! Single platform?
bool isSinglePlatform() const;
//! Any Windows
bool isAnyWindows() const;
//! Name of platform
QString getPlatformName() const;
//! Set platform
void setPlatform(Platform p) { m_platform = p; }
//! Unknown?
bool isUnknown() const { return m_platform == static_cast<int>(UnknownOs); }
//! Set platform
void setPlatform(const QString &p) { setPlatform(stringToPlatform(p)); }
//! Representing icon
CIcon toIcon() const;
//! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const;
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant);
//! Compare for index
int comparePropertyByIndex(const CPropertyIndex &index, const CPlatform &compareValue) const;
//! Convert to QString
operator QString() { return this->toQString(); }
//! Convert to enum
static Platform stringToPlatform(const QString &str);
//! Convert to enum
const static CPlatform &stringToPlatformObject(const QString &str);
//! Current platform
static const CPlatform &currentPlatform();
//! Win32
static const CPlatform &win32();
//! Win64
static const CPlatform &win64();
//! Linux
static const CPlatform &linux();
//! Mac OS
static const CPlatform &macOS();
//! Unknown OS
static const CPlatform &unknownOs();
//! All OS
static const CPlatform &allOs();
//! Independent OS
static const CPlatform &independent();
private:
int m_platform = static_cast<int>(UnknownOs); //!< platform
BLACK_METACLASS(
CPlatform,
BLACK_METAMEMBER(platform)
);
};
} // namespace
Q_DECLARE_METATYPE(BlackMisc::CPlatform)
Q_DECLARE_METATYPE(BlackMisc::CPlatform::Platform)
Q_DECLARE_METATYPE(BlackMisc::CPlatform::PlatformFlag)
Q_DECLARE_OPERATORS_FOR_FLAGS(BlackMisc::CPlatform::Platform)
#endif // guard

View File

@@ -0,0 +1,80 @@
/* Copyright (C) 2017
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "platformset.h"
#include "blackmisc/containerbase.h"
#include "blackmisc/dbus.h"
#include "blackmisc/variant.h"
#include <QDBusMetaType>
#include <QJsonObject>
#include <QString>
namespace BlackMisc
{
CPlatformSet::CPlatformSet() { }
CPlatformSet::CPlatformSet(const CCollection<CPlatform> &other) :
CCollection<CPlatform>(other)
{ }
QStringList CPlatformSet::getPlatformNames() const
{
QStringList names;
for (const CPlatform &p : *this)
{
names.append(p.getPlatformName());
}
return names;
}
CPlatformSet CPlatformSet::matchesAny(CPlatform::Platform platform) const
{
CPlatformSet set;
for (const CPlatform &p : *this)
{
if (!p.matchesAny(platform)) continue;
set.insert(p);
}
return set;
}
CPlatformSet CPlatformSet::exactMatch(CPlatform::Platform platform) const
{
CPlatformSet set;
for (const CPlatform &p : *this)
{
if (p.getPlatform() != platform) continue;
set.insert(p);
}
return set;
}
bool CPlatformSet::containsName(const QString &platformName) const
{
return this->contains(&CPlatform::getPlatformName, platformName);
}
const CPlatformSet &CPlatformSet::allPlatforms()
{
static const CPlatformSet platforms({ CPlatform::win32(), CPlatform::win64(), CPlatform::linux(), CPlatform::macOS() });
return platforms;
}
void CPlatformSet::registerMetadata()
{
qRegisterMetaType<BlackMisc::CSequence<CPlatform>>();
qDBusRegisterMetaType<BlackMisc::CSequence<CPlatform>>();
qRegisterMetaType<BlackMisc::CCollection<CPlatform>>();
qDBusRegisterMetaType<BlackMisc::CCollection<CPlatform>>();
qRegisterMetaType<CPlatformSet>();
qDBusRegisterMetaType<CPlatformSet>();
registerMetaValueType<CPlatformSet>();
}
} // namespace

View File

@@ -0,0 +1,62 @@
/* Copyright (C) 2017
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_PLATFORMSET_H
#define BLACKMISC_PLATFORMSET_H
#include "platform.h"
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/collection.h"
#include "blackmisc/sequence.h"
#include <QMetaType>
#include <tuple>
namespace BlackMisc
{
//! Value object for a set of platforms.
class BLACKMISC_EXPORT CPlatformSet : public CCollection<CPlatform>
{
public:
//! Default constructor.
CPlatformSet();
//! Initializer list constructor.
CPlatformSet(std::initializer_list<CPlatform> il) : CCollection<CPlatform>(il) {}
//! Construct from a base class object.
CPlatformSet(const CCollection<CPlatform> &other);
//! All platform names
QStringList getPlatformNames() const;
//! Matches any given platform
CPlatformSet matchesAny(CPlatform::Platform platform) const;
//! Matches given platform
CPlatformSet exactMatch(CPlatform::Platform platform) const;
//! Contains name?
bool containsName(const QString &platformName) const;
//! All platforms
static const CPlatformSet &allPlatforms();
//! Register metadata
static void registerMetadata();
};
} //namespace
Q_DECLARE_METATYPE(BlackMisc::CPlatformSet)
Q_DECLARE_METATYPE(BlackMisc::CCollection<BlackMisc::CPlatform>)
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::CPlatform>)
#endif //guard

View File

@@ -8,18 +8,14 @@
*/
#include "registermetadata.h"
#include "blackmisc/applicationinfo.h"
#include "blackmisc/applicationinfolist.h"
#include "blackmisc/audio/registermetadataaudio.h"
#include "blackmisc/aviation/registermetadataaviation.h"
#include "blackmisc/country.h"
#include "blackmisc/countrylist.h"
#include "blackmisc/dbus.h"
#include "blackmisc/db/registermetadatadb.h"
#include "blackmisc/geo/registermetadatageo.h"
#include "blackmisc/icon.h"
#include "blackmisc/iconlist.h"
#include "blackmisc/identifier.h"
#include "blackmisc/identifierlist.h"
#include "blackmisc/input/registermetadatainput.h"
#include "blackmisc/logcategory.h"
@@ -29,6 +25,7 @@
#include "blackmisc/namevariantpairlist.h"
#include "blackmisc/network/registermetadatanetwork.h"
#include "blackmisc/pixmap.h"
#include "blackmisc/platformset.h"
#include "blackmisc/pq/registermetadatapq.h"
#include "blackmisc/processinfo.h"
#include "blackmisc/propertyindex.h"
@@ -37,11 +34,9 @@
#include "blackmisc/rgbcolor.h"
#include "blackmisc/sequence.h"
#include "blackmisc/simulation/registermetadatasimulation.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/valuecache.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/variant.h"
#include "blackmisc/variantlist.h"
#include "blackmisc/variantmap.h"
#include "blackmisc/weather/registermetadataweather.h"
@@ -76,6 +71,8 @@ namespace BlackMisc
CNameVariantPair::registerMetadata();
CNameVariantPairList::registerMetadata();
CPixmap::registerMetadata();
CPlatform::registerMetadata();
CPlatformSet::registerMetadata();
CProcessInfo::registerMetadata();
CPropertyIndex::registerMetadata();
CPropertyIndex::registerMetadata();