tagparser/positioninset.h

144 lines
4.4 KiB
C
Raw Normal View History

#ifndef TAG_PARSER_POSITIONINSET_H
#define TAG_PARSER_POSITIONINSET_H
2015-04-22 19:22:01 +02:00
2016-08-29 15:43:05 +02:00
#include "./global.h"
2015-04-22 19:22:01 +02:00
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/misc/traits.h>
2015-04-22 19:22:01 +02:00
#include <string>
namespace TagParser {
2015-04-22 19:22:01 +02:00
/*!
* \class TagParser::PositionInSet
2015-04-22 19:22:01 +02:00
* \brief The PositionInSet class describes the position of an element in a
* set which consists of a certain number of elements.
*
* This class is used to parse and store values like "9/11" which are used
* by some tag formats to denote track positions.
*/
2018-03-07 01:17:50 +01:00
class TAG_PARSER_EXPORT PositionInSet {
2015-04-22 19:22:01 +02:00
public:
constexpr explicit PositionInSet(std::int32_t position = 0, std::int32_t total = 0);
2019-06-12 20:40:45 +02:00
template <typename StringType = std::string,
CppUtilities::Traits::EnableIfAny<CppUtilities::Traits::IsSpecializationOf<StringType, std::basic_string>> * = nullptr>
2015-04-22 19:22:01 +02:00
PositionInSet(const StringType &numericString);
2019-03-13 19:06:42 +01:00
constexpr std::int32_t position() const;
void setPosition(std::int32_t position);
2019-03-13 19:06:42 +01:00
constexpr std::int32_t total() const;
void setTotal(std::int32_t total);
2015-04-22 19:22:01 +02:00
constexpr bool isNull() const;
constexpr bool operator==(const PositionInSet &other) const;
2015-04-22 19:22:01 +02:00
2019-06-12 20:40:45 +02:00
template <typename StringType = std::string,
CppUtilities::Traits::EnableIfAny<CppUtilities::Traits::IsSpecializationOf<StringType, std::basic_string>> * = nullptr>
2015-04-22 19:22:01 +02:00
StringType toString() const;
private:
2019-03-13 19:06:42 +01:00
std::int32_t m_position;
std::int32_t m_total;
2015-04-22 19:22:01 +02:00
};
/*!
* \brief Constructs a new Position in set from the specified numeric string.
* \tparam StringType The type of the string (should be an instantiation of the basic_string class template).
* \param numericString Specifies the string containing the position and possibly
* the total element count (separated by "/").
*/
2019-06-10 22:49:11 +02:00
template <typename StringType, CppUtilities::Traits::EnableIfAny<CppUtilities::Traits::IsSpecializationOf<StringType, std::basic_string>> *>
2018-03-07 01:17:50 +01:00
PositionInSet::PositionInSet(const StringType &numericString)
: m_position(0)
, m_total(0)
2015-04-22 19:22:01 +02:00
{
const auto separator = numericString.find('/');
2018-03-07 01:17:50 +01:00
if (separator == StringType::npos || separator == numericString.length() - 1) {
2019-06-10 22:49:11 +02:00
m_position = CppUtilities::stringToNumber<std::int32_t, StringType>(numericString);
2018-03-07 01:17:50 +01:00
} else if (separator == 0) {
2019-06-10 22:49:11 +02:00
m_total = CppUtilities::stringToNumber<std::int32_t, StringType>(numericString.substr(1));
2015-04-22 19:22:01 +02:00
} else {
2019-06-10 22:49:11 +02:00
m_position = CppUtilities::stringToNumber<std::int32_t, StringType>(numericString.substr(0, separator));
m_total = CppUtilities::stringToNumber<std::int32_t, StringType>(numericString.substr(separator + 1));
2015-04-22 19:22:01 +02:00
}
}
/*!
* \brief Constructs a new Position in set of the specified element \a position and \a total element count.
* \param position
* \param total
*/
2019-03-13 19:06:42 +01:00
constexpr inline PositionInSet::PositionInSet(std::int32_t position, std::int32_t total)
2018-03-07 01:17:50 +01:00
: m_position(position)
, m_total(total)
{
}
2015-04-22 19:22:01 +02:00
/*!
* \brief Returns the element position of the current instance.
*/
2019-03-13 19:06:42 +01:00
constexpr inline std::int32_t PositionInSet::position() const
2015-04-22 19:22:01 +02:00
{
return m_position;
}
/*!
* \brief Sets the element position of the current instance.
*/
inline void PositionInSet::setPosition(int32_t position)
{
m_position = position;
}
2015-04-22 19:22:01 +02:00
/*!
* \brief Returns the total element count of the current instance.
*/
2019-03-13 19:06:42 +01:00
constexpr inline std::int32_t PositionInSet::total() const
2015-04-22 19:22:01 +02:00
{
return m_total;
}
/*!
* \brief Sets the total element count of the current instance.
*/
inline void PositionInSet::setTotal(int32_t total)
{
m_total = total;
}
2015-04-22 19:22:01 +02:00
/*!
* \brief Returns an indication whether both the element position and total element count is 0.
*/
constexpr inline bool PositionInSet::isNull() const
{
return m_position == 0 && m_total == 0;
}
/*!
* \brief Returns whether this instance equals \a other.
*/
constexpr inline bool PositionInSet::operator==(const PositionInSet &other) const
{
return m_position == other.m_position && m_total == other.m_total;
}
2015-04-22 19:22:01 +02:00
/*!
* \brief Returns the string representation of the current PositionInSet.
*/
2019-06-10 22:49:11 +02:00
template <typename StringType, CppUtilities::Traits::EnableIfAny<CppUtilities::Traits::IsSpecializationOf<StringType, std::basic_string>> *>
2015-04-22 19:22:01 +02:00
StringType PositionInSet::toString() const
{
std::basic_stringstream<typename StringType::value_type> ss;
2018-03-07 01:17:50 +01:00
if (m_position) {
2015-04-22 19:22:01 +02:00
ss << m_position;
}
2018-03-07 01:17:50 +01:00
if (m_total) {
2016-08-05 01:46:31 +02:00
ss << '/' << m_total;
2015-04-22 19:22:01 +02:00
}
return ss.str();
}
2018-03-07 01:17:50 +01:00
} // namespace TagParser
2015-04-22 19:22:01 +02:00
#endif // TAG_PARSER_POSITIONINSET_H