Files
pip/libs/main/units/piunits_value.h

97 lines
3.0 KiB
C++

//! \~english Unit value class
//! \~russian Класс значения единицы
/*
PIP - Platform Independent Primitives
Unit value
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_VALUE_H
#define PIUNITS_VALUE_H
#include "piunits_base.h"
namespace PIUnits {
//! \~english Unit value representation
//! \~russian Представление значения единицы
class PIP_EXPORT Value {
public:
//! \~english Constructor
//! \~russian Конструктор
//! \param v Value
//! \param t Unit type
Value(double v = 0., int t = Class::Invalid);
//! \~english Check if value is valid
//! \~russian Проверить значение на корректность
//! \return true if valid
bool isValid() const { return m_type >= 0 && m_class; }
//! \~english Check if value is not valid
//! \~russian Проверить значение на некорректность
//! \return true if not valid
bool isNotValid() const { return m_type < 0 || !m_class; }
//! \~english Get raw value
//! \~russian Получить сырое значение
double value() const { return m_value; }
//! \~english Convert to string
//! \~russian Преобразовать в строку
//! \param format Format specifier
//! \param prec Precision
//! \return String representation
PIString toString(char format = 'g', int prec = 5) const;
//! \~english Convert to different unit type
//! \~russian Преобразовать в другой тип единицы
//! \param type_to Target unit type
//! \return true if successful
bool convert(int type_to);
//! \~english Get converted value
//! \~russian Получить преобразованное значение
//! \param type_to Target unit type
//! \return Converted value
Value converted(int type_to);
private:
//! \~english Numeric value
//! \~russian Числовое значение
double m_value;
//! \~english Unit type
//! \~russian Тип единицы
int m_type;
//! \~english Class pointer
//! \~russian Указатель на класс
Class::Internal::ClassBase * m_class;
};
}; // namespace PIUnits
//! \~english Output stream operator
//! \~russian Оператор вывода в поток
inline PICout operator<<(PICout s, const PIUnits::Value & v) {
s << v.toString();
return s;
}
#endif