Units.h Source File documentation

Units.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libpagemaker project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #ifndef LIBPAGEMAKER_UNITS_H
11 #define LIBPAGEMAKER_UNITS_H
12
13 #include <string>
14
15 // TODO: remove this pointless abstraction.
16
17 namespace libpagemaker
18 {
19 const unsigned SHAPE_UNITS_PER_INCH = 1440;
20
21 template <unsigned PER_INCH> class LengthUnit
22 {
23 typedef LengthUnit<PER_INCH> T;
24 public:
25 int m_value;
26
27 LengthUnit(int value) : m_value(value) { }
28
29 double toInches() const
30 {
31 return m_value / ((double)PER_INCH);
32 }
33 };
34
35 template<unsigned PER_INCH> const LengthUnit<PER_INCH>
36 operator+(LengthUnit<PER_INCH> left, LengthUnit<PER_INCH> right)
37 {
38 return LengthUnit<PER_INCH>(left.m_value + right.m_value);
39 }
40
41 template<unsigned PER_INCH> const LengthUnit<PER_INCH>
42 operator*(LengthUnit<PER_INCH> left, int right)
43 {
44 return LengthUnit<PER_INCH>(left.m_value * right);
45 }
46
47 template<unsigned PER_INCH> const LengthUnit<PER_INCH>
48 operator*(int left, LengthUnit<PER_INCH> right)
49 {
50 return right * left;
51 }
52
53 template<unsigned PER_INCH> const LengthUnit<PER_INCH>
54 operator-(LengthUnit<PER_INCH> left, LengthUnit<PER_INCH> right)
55 {
56 return LengthUnit<PER_INCH>(left.m_value - right.m_value);
57 }
58
59 typedef LengthUnit<SHAPE_UNITS_PER_INCH> PMDShapeUnit;
60
61 }
62
63 #endif /* LIBPAGEMAKER_UNITS_H */
64
65 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libpagemaker::LengthUnit::toInches
double toInches() const
Definition: Units.h:29
libpagemaker::SHAPE_UNITS_PER_INCH
const unsigned SHAPE_UNITS_PER_INCH
Definition: Units.h:19
libpagemaker::LengthUnit::LengthUnit
LengthUnit(int value)
Definition: Units.h:27
libpagemaker::PMDShapeUnit
LengthUnit< SHAPE_UNITS_PER_INCH > PMDShapeUnit
Definition: Units.h:59
libpagemaker::operator*
const LengthUnit< PER_INCH > operator*(LengthUnit< PER_INCH > left, int right)
Definition: Units.h:42
libpagemaker::LengthUnit::T
LengthUnit< PER_INCH > T
Definition: Units.h:23
libpagemaker::LengthUnit::m_value
int m_value
Definition: Units.h:25
libpagemaker::operator-
const LengthUnit< PER_INCH > operator-(LengthUnit< PER_INCH > left, LengthUnit< PER_INCH > right)
Definition: Units.h:54
libpagemaker::LengthUnit
Definition: Units.h:21
libpagemaker
Definition: geometry.h:22
libpagemaker::operator+
const LengthUnit< PER_INCH > operator+(LengthUnit< PER_INCH > left, LengthUnit< PER_INCH > right)
Definition: Units.h:36