1/*
2 Qalculate
3
4 Copyright (C) 2003-2006 Niklas Knutsson (nq@altern.org)
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10*/
11
12#ifndef PREFIX_H
13#define PREFIX_H
14
15
16#include <libqalculate/includes.h>
17#include <libqalculate/Number.h>
18
19/** @file */
20
21///Types for prefix classes.
22typedef enum {
23 PREFIX_DECIMAL,
24 PREFIX_BINARY,
25 PREFIX_NUMBER
26} PrefixType;
27
28///Abstract class for prefixes.
29/** A prefix is prepended to a unit to specificy a quantity multiplicator. A prefix has a numerical value which raised to the units power defines the quantity. In for example the expression "3 kilometers", meter is the unit, 3 is regular quantity, and kilo is a prefix with a value 1000, thus the example equals "3000 meters". If the unit instead had been squared, the value of the prefix would have been raised by two and the total quantity would have been 3.000.000.
30
31Prefixes can have up to free different three names -- a long name, a short name and a short unicode name. The unicode name is an alternative to the short name that is preferred if unicode characters can be displayed. The names or used to reference the prefix in mathematical expressions and to display a prefix in a result.
32 */
33class Prefix {
34 protected:
35 string l_name, s_name, u_name;
36 public:
37 /** Create a prefix.
38 *
39 * @param long_name Long name.
40 * @param short_name Short name.
41 * @param unicode_name Unicode name.
42 */
43 Prefix(string long_name, string short_name = "", string unicode_name = "");
44 virtual ~Prefix();
45 /** Returns the short name of the prefix.
46 *
47 * @param return_long_if_no_short If the long name shall be returned if the prefix has not got a short name (if it is empty).
48 * @param use_unicode If a unicode version of the name is allowed and preferred.
49 * @returns The short name of the prefix.
50 */
51 const string &shortName(bool return_long_if_no_short = true, bool use_unicode = false) const;
52 /** Returns the long name of the prefix.
53 *
54 * @param return_short_if_no_long If the short name shall be returned if the prefix has not got a long name (if it is empty).
55 * @param use_unicode If a unicode version of the name is allowed and preferred.
56 * @returns The long name of the prefix.
57 */
58 const string &longName(bool return_short_if_no_long = true, bool use_unicode = false) const;
59 /** Returns the unicode name of the prefix.
60 *
61 * @param return_short_if_no_uni If the short name shall be returned if the prefix has not got a unicode name (if it is empty).
62 * @returns The unicode name of the prefix.
63 */
64 const string &unicodeName(bool return_short_if_no_uni = true) const;
65 /** Sets the short name of the prefix.
66 *
67 * @param short_name The new short name for the prefix.
68 */
69 void setShortName(string short_name);
70 /** Sets the long name of the prefix.
71 *
72 * @param long_name The new long name for the prefix.
73 */
74 void setLongName(string long_name);
75 /** Sets the unicode name of the prefix. The unicode name is an alternative to the short name that is preferred if unicode characters can be displayed.
76 *
77 * @param unicode_name The new unicode name for the prefix.
78 */
79 void setUnicodeName(string unicode_name);
80 /** Returns a preferred name of the prefix.
81 *
82 * @param short_default If a short name is preferred.
83 * @param use_unicode If a unicode name is preferred.
84 * @param can_display_unicode_string_function Function that tests if the unicode characters in a name can be displayed. If the function returns false, the name will be rejected.
85 * @param can_display_unicode_string_arg Argument to pass to the above test function.
86 * @returns A preferred name.
87 */
88 const string &name(bool short_default = true, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
89 /** Returns the value of the prefix.
90 *
91 * @param nexp The power of the prefixed unit.
92 * @returns The value of the prefix.
93 */
94 virtual Number value(const Number &nexp) const = 0;
95 /** Returns the value of the prefix.
96 *
97 * @param iexp The power of the prefixed unit.
98 * @returns The value of the prefix.
99 */
100 virtual Number value(int iexp) const = 0;
101 /** Returns the value of the prefix.
102 *
103 * @returns The value of the prefix.
104 */
105 virtual Number value() const = 0;
106 /** Returns type, subclass, of the prefix. This can be PREFIX_DECIMAL for prefixes of the class DecimalPrefix, PREFIX_BINARY for BinaryPrefix, or PREFIX_NUMBER for NumberPrefix.
107 *
108 * @returns The type of the prefix.
109 */
110 virtual int type() const = 0;
111
112};
113
114///A decimal (metric) prefix.
115/** A metric or decimal prefix has an integer exponent which with a base of ten constitutes the value of the prefix (value=10^exponent).
116 */
117class DecimalPrefix : public Prefix {
118 protected:
119 int exp;
120 public:
121 /** Create a decimal prefix.
122 *
123 * @param exp10 Exponent for the value.
124 * @param long_name Long name.
125 * @param short_name Short name.
126 * @param unicode_name Unicode name.
127 */
128 DecimalPrefix(int exp10, string long_name, string short_name = "", string unicode_name = "");
129 ~DecimalPrefix();
130 /** Returns the exponent.
131 *
132 * @param iexp Exponent of the unit.
133 * @returns The exponent of the prefix.
134 */
135 int exponent(int iexp = 1) const;
136 /** Returns the exponent.
137 *
138 * @param nexp Exponent of the unit.
139 * @returns The exponent of the prefix.
140 */
141 Number exponent(const Number &nexp) const;
142 /** Sets the exponent of the prefix.
143 *
144 * @param iexp New exponent for the prefix.
145 */
146 void setExponent(int iexp);
147 Number value(const Number &nexp) const;
148 Number value(int iexp) const;
149 Number value() const;
150 int type() const;
151};
152
153///A binary prefix.
154/** A Binary prefix has an integer exponent which with a base of two constitutes the value of the prefix (value=2^exponent).
155 */
156class BinaryPrefix : public Prefix {
157 protected:
158 int exp;
159 public:
160 /** Create a binary prefix.
161 *
162 * @param exp2 Exponent for the value.
163 * @param long_name Long name.
164 * @param short_name Short name.
165 * @param unicode_name Unicode name.
166 */
167 BinaryPrefix(int exp2, string long_name, string short_name = "", string unicode_name = "");
168 ~BinaryPrefix();
169 /** Returns the exponent.
170 *
171 * @param iexp Exponent of the unit.
172 * @returns The exponent of the prefix.
173 */
174 int exponent(int iexp = 1) const;
175 /** Returns the exponent.
176 *
177 * @param nexp Exponent of the unit.
178 * @returns The exponent of the prefix.
179 */
180 Number exponent(const Number &nexp) const;
181 /** Sets the exponent of the prefix.
182 *
183 * @param iexp New exponent for the prefix.
184 */
185 void setExponent(int iexp);
186 Number value(const Number &nexp) const;
187 Number value(int iexp) const;
188 Number value() const;
189 int type() const;
190};
191
192///A prefix with a free numerical value.
193/** A prefix without any predefined base, which can use any number.
194 */
195class NumberPrefix : public Prefix {
196 protected:
197 Number o_number;
198 public:
199 /** Create a number prefix.
200 *
201 * @param nr Value of the prefix.
202 * @param long_name Long name.
203 * @param short_name Short name.
204 * @param unicode_name Unicode name.
205 */
206 NumberPrefix(const Number &nr, string long_name, string short_name = "", string unicode_name = "");
207 ~NumberPrefix();
208 /** Sets the value of the prefix.
209 *
210 * @param nr New value for the prefix.
211 */
212 void setValue(const Number &nr);
213 Number value(const Number &nexp) const;
214 Number value(int iexp) const;
215 Number value() const;
216 int type() const;
217};
218
219#endif
220