libpniio
rational.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio 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 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 // ===========================================================================
19 //
20 // Created on: Jun 17, 2011
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 #pragma once
25 
26 #include <iostream>
27 #include <pni/core/types.hpp>
28 
29 namespace pni{
30 namespace io{
31 namespace tiff{
32 
33 
41  template<typename T> class rational
42  {
43  protected:
46  public:
47  //=================constructors and destructor=================
49  rational();
51  rational(const rational &o);
53  explicit rational(const T &,const T&);
56 
57  //====================conversion operator=======================
65  template<typename U> operator U()
66  {
67  return (U)((U)(_numerator))/((U)(_denominator));
68  }
69 
70  //================assignment operator===========================
72  virtual rational<T> &operator=(const rational<T> &r);
73 
75  void numerator(const T &v) { _numerator = v; }
77  T numerator() const { return _numerator; }
78 
80  void denominator(T &v) { _denominator = v; }
82  T denominator() const { return _denominator; }
83 
84  };
85 
86  //=============implementation of template methods======================
87  //implementation of the default constructor
88  template<typename T> rational<T>::rational():
89  _numerator(0),
90  _denominator(0)
91  { }
92 
93  //------------------------------------------------------------------------
94  //implementation of the copy constructor
95  template<typename T> rational<T>::rational(const rational<T> &r):
96  _numerator(r._numerator),
97  _denominator(r._denominator)
98  { }
99 
100  //------------------------------------------------------------------------
101  //implementation of the standard constructor
102  template<typename T> rational<T>::rational(const T &n,const T &d):
103  _numerator(n),
104  _denominator(d)
105  { }
106 
107  //------------------------------------------------------------------------
108  //implementation of the copy assignment operator
109  template<typename T>
111  {
112  if(this != &r){
113  _numerator = r._numerator;
114  _denominator = r._denominator;
115  }
116  return *this;
117  }
118 
119  //------------------------------------------------------------------------
120  //implementation of the output operator
121  template<typename T>
122  std::ostream &operator<<(std::ostream &o,const rational<T> &r)
123  {
124  o<<r.numerator()<<"/"<<r.denominator();
125  return o;
126  }
127 
128 
129  //some default types
132 
133 //end namespaces
134 }
135 }
136 }
virtual rational< T > & operator=(const rational< T > &r)
copy assignment operator
Definition: rational.hpp:110
rational type for TIFF IFD entries
Definition: rational.hpp:41
Definition: cbf_reader.hpp:41
T numerator() const
get the numerator
Definition: rational.hpp:77
T _denominator
denominator of the rational number
Definition: rational.hpp:45
T denominator() const
get the denominator
Definition: rational.hpp:82
rational()
default constructor
Definition: rational.hpp:88
T _numerator
numerator of the rational number
Definition: rational.hpp:44
rational< pni::core::uint32 > uirational
Definition: rational.hpp:130
void denominator(T &v)
set the denominator
Definition: rational.hpp:80
~rational()
destructor
Definition: rational.hpp:55
void numerator(const T &v)
set the numerator
Definition: rational.hpp:75
rational< pni::core::int32 > irational
Definition: rational.hpp:131