PhaseRange.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2016 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/PhaseRanges.h
10
11#ifndef __PhaseRanges_H
12#define __PhaseRanges_H
13
14#include "Ranges.h"
15#include "TextInterfaceAttribute.h"
16
17namespace Phase
18{
20 enum Unit { Bins, Turns, Degrees, Radians,
21 Milliseconds, Seconds, Minutes, Hours };
22
23 std::ostream& operator << (std::ostream&, Unit);
24 std::istream& operator >> (std::istream&, Unit&);
25
27 class HasUnit
28 {
29 protected:
30 Unit unit;
31 unsigned nbin;
32 double period;
33 bool insert_units;
34
35 std::ostream& insertion (std::ostream&) const;
36 std::istream& extraction (std::istream&);
37
38 public:
39 HasUnit (Unit u = Turns) { nbin=0; period=0; unit=u; insert_units = false; }
40
41 unsigned get_bin (double value) const;
42 double get_as (Unit, double value, bool round_down=false) const;
43 double get_scale (Unit) const;
44
45 void set_unit( Unit u ) { unit = u; }
46 Unit get_unit () const { return unit; }
47
48 void set_nbin( unsigned N ) { nbin = N; }
49
51 void set_period( double P_ms ) { period = P_ms; }
52 };
53
55 class Value : public HasUnit
56 {
57 double value;
58
59 public:
60 Value (double v=0.0, Unit u=Turns) : HasUnit(u) { value = v; }
61
62 void set_value( double v ) { value = v; }
63 double get_value () const { return value; }
64
65 unsigned get_bin () const { return HasUnit::get_bin(value); }
66 double get_as (Unit u) const { return HasUnit::get_as(u, value); }
67
68 Value as (Unit) const;
69
70 friend std::ostream& operator<< (std::ostream&, const Value&);
71 friend std::istream& operator>> (std::istream&, Value&);
72 };
73
74 std::ostream& operator<< (std::ostream&, const Value&);
75 std::istream& operator>> (std::istream&, Value&);
76
78 class Range : public HasUnit, public ::Range
79 {
80 ::Range& asRange () { return *this; }
81 const ::Range& asRange () const { return *this; }
82
83 public:
84
85 Range () {}
86 Range (const HasUnit& u, const ::Range& r) : HasUnit(u), ::Range(r) { }
87
88 friend std::ostream& operator<< (std::ostream&, const Range&);
89 friend std::istream& operator>> (std::istream&, Range&);
90
91 std::pair<unsigned,unsigned> get_bins () const;
92 std::pair<double,double> get_as (Unit) const;
93
94 Range as (Unit) const;
95 };
96
98 class Ranges : public HasUnit, public ::Ranges
99 {
100 ::Ranges& asRanges () { return *this; }
101 const ::Ranges& asRanges () const { return *this; }
102
103 public:
104 friend std::ostream& operator<< (std::ostream&, const Ranges&);
105 friend std::istream& operator>> (std::istream&, Ranges&);
106
107 Ranges as (Unit) const;
108 };
109
111 template<class Type>
112 class ToStringPolicy
113 {
114 protected:
115
116 mutable ToString tostr;
117 mutable Phase::Unit unit;
118 mutable bool unit_specified;
119
120 public:
121
122 ToStringPolicy () { unit_specified = false; }
123
124 void set_modifiers (const std::string& modifiers) const
125 {
126#if _DEBUG
127 std::cerr << "ToStringPolicy<Phase::HasUnit>::set_modifiers" << std::endl;
128#endif
129
130 unit = fromstring<Phase::Unit> (modifiers);
131 unit_specified = true;
132 }
133
134 void reset_modifiers () const
135 {
136 unit_specified = false;
137 // tostring.reset_modifiers ();
138 }
139
140 std::string operator () (const Type& t) const
141 {
142#if _DEBUG
143 std::cerr << "ToStringPolicy<Phase::HasUnit>::operator" << std::endl;
144#endif
145 if (this->unit_specified)
146 return tostr( t.as(this->unit) );
147 else
148 return tostr( t );
149 }
150 };
151}
152
153namespace TextInterface
154{
156 template<>
157 class ToStringPolicy<Phase::Value>
158 : public Phase::ToStringPolicy<Phase::Value>
159 {
160 };
161
163 template<>
164 class ToStringPolicy<Phase::Range>
165 : public Phase::ToStringPolicy<Phase::Range>
166 {
167 };
168
170 template<>
171 class ToStringPolicy<Phase::Ranges>
172 : public Phase::ToStringPolicy<Phase::Ranges>
173 {
174 };
175}
176
177// Specialize the std C++ numeric_limits traits for Phase::* classes
178namespace std {
179 template<>
180 class numeric_limits<Phase::Value> {
181 public:
182 static const int digits10 = numeric_limits<double>::digits10;
183 };
184
185 template<>
186 class numeric_limits<Phase::Range> {
187 public:
188 static const int digits10 = numeric_limits<double>::digits10;
189 };
190
191 template<>
192 class numeric_limits<Phase::Ranges> {
193 public:
194 static const int digits10 = numeric_limits<double>::digits10;
195 };
196}
197
198#endif // !__PhaseRanges_H
void set_period(double P_ms)
Set the period in milliseconds.
Definition PhaseRange.h:51
A set of ranges of values with a common Phase::Unit.
Definition PhaseRange.h:99
Policy for converting any object with a Phase::Unit to a string.
Definition PhaseRange.h:113
A value with a Phase::Unit.
Definition PhaseRange.h:56
Text interface to a value of undefined type.
Definition TextInterfaceValue.h:31
@ Phase
Pulsar Phase, for instance.
Definition Types.h:29
STL namespace.

Generated using doxygen 1.14.0