qBittorrent
Utils::Version< T, N, Mandatory > Class Template Reference

#include <version.h>

Public Types

typedef T ComponentType
 
typedef Version< T, N, Mandatory > ThisType
 

Public Member Functions

constexpr Version ()=default
 
template<typename ... Other>
constexpr Version (Other ... components)
 
 Version (const QString &version)
 Creates version from string in format "x.y.z". More...
 
 Version (const QByteArray &version)
 Creates version from byte array in format "x.y.z". More...
 
constexpr ComponentType majorNumber () const
 
constexpr ComponentType minorNumber () const
 
constexpr ComponentType revisionNumber () const
 
constexpr ComponentType patchNumber () const
 
constexpr ComponentType operator[] (const std::size_t i) const
 
 operator QString () const
 
constexpr bool isValid () const
 

Static Public Member Functions

template<typename StringClassWithSplitMethod >
static Version tryParse (const StringClassWithSplitMethod &s, const Version &defaultVersion)
 

Private Types

using ComponentsArray = std::array< T, N >
 

Private Member Functions

template<typename StringsList >
 Version (const StringsList &versionParts)
 

Static Private Member Functions

template<typename StringsList >
static ComponentsArray parseList (const StringsList &versionParts)
 

Private Attributes

ComponentsArray m_components {{}}
 

Friends

bool operator== (const ThisType &left, const ThisType &right)
 
bool operator< (const ThisType &left, const ThisType &right)
 

Detailed Description

template<typename T, std::size_t N, std::size_t Mandatory = N>
class Utils::Version< T, N, Mandatory >

Definition at line 41 of file version.h.

Member Typedef Documentation

◆ ComponentsArray

template<typename T , std::size_t N, std::size_t Mandatory = N>
using Utils::Version< T, N, Mandatory >::ComponentsArray = std::array<T, N>
private

Definition at line 157 of file version.h.

◆ ComponentType

template<typename T , std::size_t N, std::size_t Mandatory = N>
typedef T Utils::Version< T, N, Mandatory >::ComponentType

Definition at line 48 of file version.h.

◆ ThisType

template<typename T , std::size_t N, std::size_t Mandatory = N>
typedef Version<T, N, Mandatory> Utils::Version< T, N, Mandatory >::ThisType

Definition at line 49 of file version.h.

Constructor & Destructor Documentation

◆ Version() [1/5]

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr Utils::Version< T, N, Mandatory >::Version ( )
constexprdefault

◆ Version() [2/5]

template<typename T , std::size_t N, std::size_t Mandatory = N>
template<typename ... Other>
constexpr Utils::Version< T, N, Mandatory >::Version ( Other ...  components)
inlineconstexpr

Definition at line 54 of file version.h.

55  : m_components {{static_cast<T>(components) ...}}
56  {
57  }
ComponentsArray m_components
Definition: version.h:185

◆ Version() [3/5]

template<typename T , std::size_t N, std::size_t Mandatory = N>
Utils::Version< T, N, Mandatory >::Version ( const QString &  version)
inline

Creates version from string in format "x.y.z".

Parameters
versionVersion string in format "x.y.z"
Exceptions
RuntimeErrorif parsing fails

Definition at line 65 of file version.h.

66  : Version {version.split(QLatin1Char('.'))}
67  {
68  }
constexpr Version()=default

◆ Version() [4/5]

template<typename T , std::size_t N, std::size_t Mandatory = N>
Utils::Version< T, N, Mandatory >::Version ( const QByteArray &  version)
inline

Creates version from byte array in format "x.y.z".

Parameters
versionVersion string in format "x.y.z"
Exceptions
RuntimeErrorif parsing fails

Definition at line 76 of file version.h.

77  : Version {version.split('.')}
78  {
79  }

◆ Version() [5/5]

template<typename T , std::size_t N, std::size_t Mandatory = N>
template<typename StringsList >
Utils::Version< T, N, Mandatory >::Version ( const StringsList &  versionParts)
inlineprivate

Definition at line 180 of file version.h.

181  : m_components (parseList(versionParts)) // GCC 4.8.4 has problems with the uniform initializer here
182  {
183  }
static ComponentsArray parseList(const StringsList &versionParts)
Definition: version.h:160

Member Function Documentation

◆ isValid()

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr bool Utils::Version< T, N, Mandatory >::isValid ( ) const
inlineconstexpr

Definition at line 126 of file version.h.

127  {
128  return (*this != ThisType {});
129  }
Version< T, N, Mandatory > ThisType
Definition: version.h:49

Referenced by SearchPluginManager::getPluginVersion(), and SearchPluginManager::parseVersionInfo().

Here is the caller graph for this function:

◆ majorNumber()

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr ComponentType Utils::Version< T, N, Mandatory >::majorNumber ( ) const
inlineconstexpr

Definition at line 81 of file version.h.

82  {
83  static_assert(N >= 1, "The number of version components is too small");
84  return m_components[0];
85  }

◆ minorNumber()

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr ComponentType Utils::Version< T, N, Mandatory >::minorNumber ( ) const
inlineconstexpr

Definition at line 87 of file version.h.

88  {
89  static_assert(N >= 2, "The number of version components is too small");
90  return m_components[1];
91  }

◆ operator QString()

template<typename T , std::size_t N, std::size_t Mandatory = N>
Utils::Version< T, N, Mandatory >::operator QString ( ) const
inline

Definition at line 110 of file version.h.

111  {
112  // find the last one non-zero component
113  std::size_t lastSignificantIndex = N - 1;
114  while ((lastSignificantIndex > 0) && ((*this)[lastSignificantIndex] == 0))
115  --lastSignificantIndex;
116 
117  if (lastSignificantIndex + 1 < Mandatory) // lastSignificantIndex >= 0
118  lastSignificantIndex = Mandatory - 1; // and Mandatory >= 1
119 
120  QString res = QString::number((*this)[0]);
121  for (std::size_t i = 1; i <= lastSignificantIndex; ++i)
122  res += QLatin1Char('.') + QString::number((*this)[i]);
123  return res;
124  }

◆ operator[]()

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr ComponentType Utils::Version< T, N, Mandatory >::operator[] ( const std::size_t  i) const
inlineconstexpr

Definition at line 105 of file version.h.

106  {
107  return m_components.at(i);
108  }

◆ parseList()

template<typename T , std::size_t N, std::size_t Mandatory = N>
template<typename StringsList >
static ComponentsArray Utils::Version< T, N, Mandatory >::parseList ( const StringsList &  versionParts)
inlinestaticprivate

Definition at line 160 of file version.h.

161  {
162  if ((static_cast<std::size_t>(versionParts.size()) > N)
163  || (static_cast<std::size_t>(versionParts.size()) < Mandatory))
164  {
165  throw RuntimeError(QLatin1String("Incorrect number of version components"));
166  }
167 
168  bool ok = false;
169  ComponentsArray res {{}};
170  for (std::size_t i = 0; i < static_cast<std::size_t>(versionParts.size()); ++i)
171  {
172  res[i] = static_cast<T>(versionParts[static_cast<typename StringsList::size_type>(i)].toInt(&ok));
173  if (!ok)
174  throw RuntimeError(QLatin1String("Can not parse version component"));
175  }
176  return res;
177  }
std::array< T, N > ComponentsArray
Definition: version.h:157

◆ patchNumber()

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr ComponentType Utils::Version< T, N, Mandatory >::patchNumber ( ) const
inlineconstexpr

Definition at line 99 of file version.h.

100  {
101  static_assert(N >= 4, "The number of version components is too small");
102  return m_components[3];
103  }

◆ revisionNumber()

template<typename T , std::size_t N, std::size_t Mandatory = N>
constexpr ComponentType Utils::Version< T, N, Mandatory >::revisionNumber ( ) const
inlineconstexpr

Definition at line 93 of file version.h.

94  {
95  static_assert(N >= 3, "The number of version components is too small");
96  return m_components[2];
97  }

◆ tryParse()

template<typename T , std::size_t N, std::size_t Mandatory = N>
template<typename StringClassWithSplitMethod >
static Version Utils::Version< T, N, Mandatory >::tryParse ( const StringClassWithSplitMethod &  s,
const Version< T, N, Mandatory > &  defaultVersion 
)
inlinestatic

Definition at line 143 of file version.h.

144  {
145  try
146  {
147  return Version(s);
148  }
149  catch (const RuntimeError &error)
150  {
151  qDebug() << "Error parsing version:" << error.message();
152  return defaultVersion;
153  }
154  }
QString message() const noexcept
Definition: exceptions.cpp:36

References Exception::message().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<

template<typename T , std::size_t N, std::size_t Mandatory = N>
bool operator< ( const ThisType left,
const ThisType right 
)
friend

Definition at line 137 of file version.h.

138  {
139  return (left.m_components < right.m_components);
140  }

◆ operator==

template<typename T , std::size_t N, std::size_t Mandatory = N>
bool operator== ( const ThisType left,
const ThisType right 
)
friend

Definition at line 132 of file version.h.

133  {
134  return (left.m_components == right.m_components);
135  }

Member Data Documentation

◆ m_components

template<typename T , std::size_t N, std::size_t Mandatory = N>
ComponentsArray Utils::Version< T, N, Mandatory >::m_components {{}}
private

Definition at line 185 of file version.h.


The documentation for this class was generated from the following file: