Loading...
Searching...
No Matches
basic_format_spec.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2019 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_BASIC_FORMAT_SPEC_INCLUDED
#define ETL_BASIC_FORMAT_SPEC_INCLUDED
#include "platform.h"
#include "type_traits.h"
#include "utility.h"
namespace etl
{
namespace private_basic_format_spec
{
//*******************************************************
// Structures returned by stream formatting manipulators.
//*******************************************************
struct base_spec
{
ETL_CONSTEXPR base_spec(uint_least8_t base_)
: base(base_)
{
}
const uint_least8_t base;
};
//*********************************
struct width_spec
{
ETL_CONSTEXPR width_spec(uint_least8_t width_)
: width(width_)
{
}
const uint_least8_t width;
};
//*********************************
template <typename TChar>
struct fill_spec
{
ETL_CONSTEXPR fill_spec(TChar fill_)
: fill(fill_)
{
}
const TChar fill;
};
//*********************************
struct precision_spec
{
ETL_CONSTEXPR precision_spec(uint_least8_t precision_)
: precision(precision_)
{
}
const uint_least8_t precision;
};
//*********************************
struct uppercase_spec
{
ETL_CONSTEXPR uppercase_spec(bool upper_case_)
: upper_case(upper_case_)
{
}
const bool upper_case;
};
//*********************************
struct boolalpha_spec
{
ETL_CONSTEXPR boolalpha_spec(bool boolalpha_)
: boolalpha(boolalpha_)
{
}
const bool boolalpha;
};
//*********************************
struct showbase_spec
{
ETL_CONSTEXPR showbase_spec(bool show_base_)
: show_base(show_base_)
{
}
const bool show_base;
};
//*********************************
struct left_spec
{
};
//*********************************
struct right_spec
{
};
} // namespace private_basic_format_spec
//***************************************************************************
// Stream formatting manipulators.
//***************************************************************************
static ETL_CONSTEXPR private_basic_format_spec::base_spec setbase(uint32_t base)
{
return private_basic_format_spec::base_spec(base);
}
//*********************************
static ETL_CONSTEXPR private_basic_format_spec::width_spec setw(uint32_t width)
{
return private_basic_format_spec::width_spec(width);
}
//*********************************
template <typename TChar>
static ETL_CONSTEXPR private_basic_format_spec::fill_spec<TChar> setfill(TChar fill)
{
return private_basic_format_spec::fill_spec<TChar>(fill);
}
//*********************************
static ETL_CONSTEXPR private_basic_format_spec::precision_spec setprecision(uint32_t precision)
{
return private_basic_format_spec::precision_spec(precision);
}
//*********************************
static ETL_CONSTANT private_basic_format_spec::base_spec bin(2U);
//*********************************
static ETL_CONSTANT private_basic_format_spec::base_spec oct(8U);
//*********************************
static ETL_CONSTANT private_basic_format_spec::base_spec dec(10U);
//*********************************
static ETL_CONSTANT private_basic_format_spec::base_spec hex(16U);
//*********************************
static ETL_CONSTANT private_basic_format_spec::left_spec left = private_basic_format_spec::left_spec();
//*********************************
static ETL_CONSTANT private_basic_format_spec::right_spec right = private_basic_format_spec::right_spec();
//*********************************
static ETL_CONSTANT private_basic_format_spec::boolalpha_spec boolalpha(true);
//*********************************
static ETL_CONSTANT private_basic_format_spec::boolalpha_spec noboolalpha(false);
//*********************************
static ETL_CONSTANT private_basic_format_spec::uppercase_spec uppercase(true);
//*********************************
static ETL_CONSTANT private_basic_format_spec::uppercase_spec nouppercase(false);
//*********************************
static ETL_CONSTANT private_basic_format_spec::showbase_spec showbase(true);
//*********************************
static ETL_CONSTANT private_basic_format_spec::showbase_spec noshowbase(false);
//***************************************************************************
//***************************************************************************
template <typename TString>
class basic_format_spec
{
public:
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR basic_format_spec() ETL_NOEXCEPT
: base_(10U)
, width_(0U)
, precision_(0U)
, upper_case_(false)
, left_justified_(false)
, boolalpha_(false)
, show_base_(false)
, fill_(typename TString::value_type(' '))
, scientific_(false)
{
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR basic_format_spec(uint_least8_t base__, uint_least8_t width__, uint_least8_t precision__, bool upper_case__, bool left_justified__,
bool boolalpha__, bool show_base__, typename TString::value_type fill__, bool scientific__ = false) ETL_NOEXCEPT
: base_(base__)
, width_(width__)
, precision_(precision__)
, upper_case_(upper_case__)
, left_justified_(left_justified__)
, boolalpha_(boolalpha__)
, show_base_(show_base__)
, fill_(fill__)
, scientific_(scientific__)
{
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 void clear() ETL_NOEXCEPT
{
base_ = 10U;
width_ = 0U;
precision_ = 0U;
upper_case_ = false;
left_justified_ = false;
boolalpha_ = false;
show_base_ = false;
scientific_ = false;
fill_ = typename TString::value_type(' ');
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& base(uint32_t b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
base_ = static_cast<uint_least8_t>(b);
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& base(uint32_t b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
base_ = static_cast<uint_least8_t>(b);
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& binary() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return base(2);
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& binary() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return etl::move(base(2));
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& octal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return base(8);
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& octal() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return etl::move(base(8));
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& decimal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return base(10);
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& decimal() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return etl::move(base(10));
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& hex() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return base(16);
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& hex() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
return etl::move(base(16));
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR uint32_t get_base() const ETL_NOEXCEPT
{
return base_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& show_base(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
show_base_ = b;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& show_base(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
show_base_ = b;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR bool is_show_base() const ETL_NOEXCEPT
{
return show_base_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& width(uint32_t w) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
width_ = static_cast<uint_least8_t>(w);
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& width(uint32_t w) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
width_ = static_cast<uint_least8_t>(w);
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT
{
return width_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& precision(uint32_t p) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
precision_ = static_cast<uint_least8_t>(p);
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& precision(uint32_t p) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
precision_ = static_cast<uint_least8_t>(p);
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR uint32_t get_precision() const ETL_NOEXCEPT
{
return precision_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& upper_case(bool u) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
upper_case_ = u;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& upper_case(bool u) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
upper_case_ = u;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR bool is_upper_case() const ETL_NOEXCEPT
{
return upper_case_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& fill(typename TString::value_type c) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
fill_ = c;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& fill(typename TString::value_type c) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
fill_ = c;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR typename TString::value_type get_fill() const ETL_NOEXCEPT
{
return fill_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& left() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
left_justified_ = true;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& left() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
left_justified_ = true;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR bool is_left() const ETL_NOEXCEPT
{
return left_justified_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& right() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
left_justified_ = false;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& right() ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
left_justified_ = false;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR bool is_right() const ETL_NOEXCEPT
{
return !left_justified_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& boolalpha(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
boolalpha_ = b;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& boolalpha(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
boolalpha_ = b;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR bool is_boolalpha() const ETL_NOEXCEPT
{
return boolalpha_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 basic_format_spec& scientific(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
scientific_ = b;
return *this;
}
#if ETL_USING_CPP11
ETL_CONSTEXPR14 basic_format_spec&& scientific(bool b) ETL_RVALUE_REF_QUALIFIER ETL_NOEXCEPT
{
scientific_ = b;
return etl::move(*this);
}
#endif
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR bool is_scientific() const ETL_NOEXCEPT
{
return scientific_;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR friend bool operator==(const basic_format_spec& lhs, const basic_format_spec& rhs)
{
return (lhs.base_ == rhs.base_) && (lhs.width_ == rhs.width_) && (lhs.precision_ == rhs.precision_) && (lhs.upper_case_ == rhs.upper_case_)
&& (lhs.left_justified_ == rhs.left_justified_) && (lhs.boolalpha_ == rhs.boolalpha_) && (lhs.show_base_ == rhs.show_base_)
&& (lhs.fill_ == rhs.fill_) && (lhs.scientific_ == rhs.scientific_);
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR friend bool operator!=(const basic_format_spec& lhs, const basic_format_spec& rhs)
{
return !(lhs == rhs);
}
private:
uint_least8_t base_;
uint_least8_t width_;
uint_least8_t precision_;
bool upper_case_;
bool left_justified_;
bool boolalpha_;
bool show_base_;
typename TString::value_type fill_;
bool scientific_;
};
} // namespace etl
#endif
etl::basic_format_spec
basic_format_spec
Definition basic_format_spec.h:203
etl::basic_format_spec::basic_format_spec
ETL_CONSTEXPR basic_format_spec(uint_least8_t base__, uint_least8_t width__, uint_least8_t precision__, bool upper_case__, bool left_justified__, bool boolalpha__, bool show_base__, typename TString::value_type fill__, bool scientific__=false) ETL_NOEXCEPT
Constructor.
Definition basic_format_spec.h:225
etl::basic_format_spec::right
ETL_CONSTEXPR14 basic_format_spec & right() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:516
etl::basic_format_spec::base
ETL_CONSTEXPR14 basic_format_spec & base(uint32_t b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:259
etl::basic_format_spec::fill
ETL_CONSTEXPR14 basic_format_spec & fill(typename TString::value_type c) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:462
etl::basic_format_spec::get_base
ETL_CONSTEXPR uint32_t get_base() const ETL_NOEXCEPT
Gets the base.
Definition basic_format_spec.h:345
etl::basic_format_spec::width
ETL_CONSTEXPR14 basic_format_spec & width(uint32_t w) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:381
etl::basic_format_spec::is_show_base
ETL_CONSTEXPR bool is_show_base() const ETL_NOEXCEPT
Gets the show base flag.
Definition basic_format_spec.h:372
etl::basic_format_spec::is_right
ETL_CONSTEXPR bool is_right() const ETL_NOEXCEPT
Gets the right justify flag.
Definition basic_format_spec.h:534
etl::basic_format_spec::operator!=
ETL_CONSTEXPR friend bool operator!=(const basic_format_spec &lhs, const basic_format_spec &rhs)
Inequality operator.
Definition basic_format_spec.h:606
etl::basic_format_spec::upper_case
ETL_CONSTEXPR14 basic_format_spec & upper_case(bool u) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:435
etl::basic_format_spec::is_upper_case
ETL_CONSTEXPR bool is_upper_case() const ETL_NOEXCEPT
Gets the upper case flag.
Definition basic_format_spec.h:453
etl::basic_format_spec::precision
ETL_CONSTEXPR14 basic_format_spec & precision(uint32_t p) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:408
etl::basic_format_spec::hex
ETL_CONSTEXPR14 basic_format_spec & hex() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:329
etl::basic_format_spec::is_scientific
ETL_CONSTEXPR bool is_scientific() const ETL_NOEXCEPT
Gets the scientific flag.
Definition basic_format_spec.h:588
etl::basic_format_spec::operator==
ETL_CONSTEXPR friend bool operator==(const basic_format_spec &lhs, const basic_format_spec &rhs)
Equality operator.
Definition basic_format_spec.h:596
etl::basic_format_spec::is_left
ETL_CONSTEXPR bool is_left() const ETL_NOEXCEPT
Gets the left justify flag.
Definition basic_format_spec.h:507
etl::basic_format_spec::binary
ETL_CONSTEXPR14 basic_format_spec & binary() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:278
etl::basic_format_spec::basic_format_spec
ETL_CONSTEXPR basic_format_spec() ETL_NOEXCEPT
Default constructor.
Definition basic_format_spec.h:209
etl::basic_format_spec::octal
ETL_CONSTEXPR14 basic_format_spec & octal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:295
etl::basic_format_spec::clear
ETL_CONSTEXPR14 void clear() ETL_NOEXCEPT
Clears the format spec back to default.
Definition basic_format_spec.h:242
etl::basic_format_spec::left
ETL_CONSTEXPR14 basic_format_spec & left() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:489
etl::basic_format_spec::boolalpha
ETL_CONSTEXPR14 basic_format_spec & boolalpha(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:543
etl::basic_format_spec::get_fill
ETL_CONSTEXPR TString::value_type get_fill() const ETL_NOEXCEPT
Gets the fill character.
Definition basic_format_spec.h:480
etl::basic_format_spec::is_boolalpha
ETL_CONSTEXPR bool is_boolalpha() const ETL_NOEXCEPT
Gets the boolalpha flag.
Definition basic_format_spec.h:561
etl::basic_format_spec::get_width
ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT
Gets the width.
Definition basic_format_spec.h:399
etl::basic_format_spec::scientific
ETL_CONSTEXPR14 basic_format_spec & scientific(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:570
etl::basic_format_spec::decimal
ETL_CONSTEXPR14 basic_format_spec & decimal() ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:312
etl::basic_format_spec::show_base
ETL_CONSTEXPR14 basic_format_spec & show_base(bool b) ETL_LVALUE_REF_QUALIFIER ETL_NOEXCEPT
Definition basic_format_spec.h:354
etl::basic_format_spec::get_precision
ETL_CONSTEXPR uint32_t get_precision() const ETL_NOEXCEPT
Gets the precision.
Definition basic_format_spec.h:426
etl
Definition absolute.h:40
platform.h
etl::value_type
etl::private_basic_format_spec::base_spec
Definition basic_format_spec.h:48
etl::private_basic_format_spec::boolalpha_spec
Definition basic_format_spec.h:104
etl::private_basic_format_spec::fill_spec
Definition basic_format_spec.h:71
etl::private_basic_format_spec::left_spec
Definition basic_format_spec.h:126
etl::private_basic_format_spec::precision_spec
Definition basic_format_spec.h:82
etl::private_basic_format_spec::right_spec
Definition basic_format_spec.h:131
etl::private_basic_format_spec::showbase_spec
Definition basic_format_spec.h:115
etl::private_basic_format_spec::uppercase_spec
Definition basic_format_spec.h:93
type_traits.h
utility.h