1#include <FloatFormatParameters.h>
2
3// defaults
4static const size_t kDefaultMinimalFractionDigits = 0;
5static const size_t kDefaultMaximalFractionDigits = 6;
6static const bool kDefaultUseUpperCase = false;
7static const float_format_type kDefaultFloatFormatType = B_AUTO_FLOAT_FORMAT;
8static const bool kDefaultAlwaysUseFractionSeparator = false;
9static const bool kDefaultKeepTrailingFractionZeros = false;
10
11
12// flags
13enum {
14	MINIMAL_FRACTION_DIGITS_SET			= 0x01,
15	MAXIMAL_FRACTION_DIGITS_SET			= 0x02,
16	USE_UPPER_CASE_SET					= 0x04,
17	FLOAT_FORMAT_TYPE_SET				= 0x08,
18	ALWAYS_USE_FRACTION_SEPARATOR_SET	= 0x10,
19	KEEP_TRAILING_FRACTION_ZEROS_SET	= 0x20,
20};
21
22// constructor
23BFloatFormatParameters::BFloatFormatParameters(
24	const BFloatFormatParameters *parent)
25	: BNumberFormatParameters(parent),
26	  fParent(parent),
27	  fFlags(0)
28{
29}
30
31// copy constructor
32BFloatFormatParameters::BFloatFormatParameters(
33	const BFloatFormatParameters &other)
34	: BNumberFormatParameters(other),
35	  fParent(other.fParent),
36	  fMinimalFractionDigits(other.fMinimalFractionDigits),
37	  fMaximalFractionDigits(other.fMaximalFractionDigits),
38	  fUseUpperCase(other.fUseUpperCase),
39	  fFloatFormatType(other.fFloatFormatType),
40	  fAlwaysUseFractionSeparator(other.fAlwaysUseFractionSeparator),
41	  fKeepTrailingFractionZeros(other.fKeepTrailingFractionZeros),
42	  fFlags(other.fFlags)
43{
44}
45
46// destructor
47BFloatFormatParameters::~BFloatFormatParameters()
48{
49}
50
51// SetMinimalFractionDigits
52void
53BFloatFormatParameters::SetMinimalFractionDigits(size_t minFractionDigits)
54{
55	fMinimalFractionDigits = minFractionDigits;
56	fFlags |= MINIMAL_FRACTION_DIGITS_SET;
57}
58
59// MinimalFractionDigits
60size_t
61BFloatFormatParameters::MinimalFractionDigits() const
62{
63	if (fFlags & MINIMAL_FRACTION_DIGITS_SET)
64		return fMinimalFractionDigits;
65	if (fParent)
66		return fParent->MinimalFractionDigits();
67	return kDefaultMinimalFractionDigits;
68}
69
70// SetMaximalFractionDigits
71void
72BFloatFormatParameters::SetMaximalFractionDigits(size_t maxFractionDigits)
73{
74	fMaximalFractionDigits = maxFractionDigits;
75	fFlags |= MAXIMAL_FRACTION_DIGITS_SET;
76}
77
78// MaximalFractionDigits
79size_t
80BFloatFormatParameters::MaximalFractionDigits() const
81{
82	if (fFlags & MAXIMAL_FRACTION_DIGITS_SET)
83		return fMaximalFractionDigits;
84	if (fParent)
85		return fParent->MaximalFractionDigits();
86	return kDefaultMaximalFractionDigits;
87}
88
89// SetUseUpperCase
90void
91BFloatFormatParameters::SetUseUpperCase(bool useCapitals)
92{
93	fUseUpperCase = useCapitals;
94	fFlags |= USE_UPPER_CASE_SET;
95}
96
97// UseUpperCase
98bool
99BFloatFormatParameters::UseUpperCase() const
100{
101	if (fFlags & USE_UPPER_CASE_SET)
102		return fUseUpperCase;
103	if (fParent)
104		return fParent->UseUpperCase();
105	return kDefaultUseUpperCase;
106}
107
108// SetFloatFormatType
109void
110BFloatFormatParameters::SetFloatFormatType(float_format_type type)
111{
112	fFloatFormatType = type;
113	fFlags |= FLOAT_FORMAT_TYPE_SET;
114}
115
116// FloatFormatType
117float_format_type
118BFloatFormatParameters::FloatFormatType() const
119{
120	if (fFlags & FLOAT_FORMAT_TYPE_SET)
121		return fFloatFormatType;
122	if (fParent)
123		return fParent->FloatFormatType();
124	return kDefaultFloatFormatType;
125}
126
127// SetAlwaysUseFractionSeparator
128void
129BFloatFormatParameters::SetAlwaysUseFractionSeparator(
130	bool alwaysUseFractionSeparator)
131{
132	fAlwaysUseFractionSeparator = alwaysUseFractionSeparator;
133	fFlags |= ALWAYS_USE_FRACTION_SEPARATOR_SET;
134}
135
136// AlwaysUseFractionSeparator
137bool
138BFloatFormatParameters::AlwaysUseFractionSeparator() const
139{
140	if (fFlags & ALWAYS_USE_FRACTION_SEPARATOR_SET)
141		return fAlwaysUseFractionSeparator;
142	if (fParent)
143		return fParent->AlwaysUseFractionSeparator();
144	return kDefaultAlwaysUseFractionSeparator;
145}
146
147// SetKeepTrailingFractionZeros
148void
149BFloatFormatParameters::SetKeepTrailingFractionZeros(
150	bool keepTrailingFractionZeros)
151{
152	fKeepTrailingFractionZeros = keepTrailingFractionZeros;
153	fFlags |= KEEP_TRAILING_FRACTION_ZEROS_SET;
154}
155
156// KeepTrailingFractionZeros
157bool
158BFloatFormatParameters::KeepTrailingFractionZeros() const
159{
160	if (fFlags & KEEP_TRAILING_FRACTION_ZEROS_SET)
161		return fKeepTrailingFractionZeros;
162	if (fParent)
163		return fParent->KeepTrailingFractionZeros();
164	return kDefaultKeepTrailingFractionZeros;
165}
166
167// SetParentFloatParameters
168void
169BFloatFormatParameters::SetParentFloatParameters(
170	const BFloatFormatParameters *parent)
171{
172	fParent = parent;
173	SetParentNumberParameters(parent);
174}
175
176// ParentFloatParameters
177const BFloatFormatParameters *
178BFloatFormatParameters::ParentFloatParameters() const
179{
180	return fParent;
181}
182
183// =
184BFloatFormatParameters &
185BFloatFormatParameters::operator=(const BFloatFormatParameters &other)
186{
187	BNumberFormatParameters::operator=(other);
188	fParent = other.fParent;
189	fMinimalFractionDigits = other.fMinimalFractionDigits;
190	fMaximalFractionDigits = other.fMaximalFractionDigits;
191	fUseUpperCase = other.fUseUpperCase;
192	fFloatFormatType = other.fFloatFormatType;
193	fAlwaysUseFractionSeparator = other.fAlwaysUseFractionSeparator;
194	fKeepTrailingFractionZeros = other.fKeepTrailingFractionZeros;
195	fFlags = other.fFlags;
196	return *this;
197}
198
199