TextParameters.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2009 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/tempo/Pulsar/TextParameters.h
10 
11 #ifndef __TextParameters_h
12 #define __TextParameters_h
13 
14 #include "Pulsar/Parameters.h"
15 
16 namespace Pulsar {
17 
19  class TextParameters : public Parameters {
20 
21  public:
22 
24  Parameters* clone () const;
25 
27  bool equals (const Parameters* that) const;
28 
30  void load (FILE*);
31 
33  void unload (FILE*) const;
34 
36  std::string get_name () const;
37 
38  //
39  //
40  //
41 
43  void set_text (const std::string& t) { text = t; }
44 
46  std::string get_value (const std::string& keyword) const;
47 
49  template<typename T>
50  bool get_value (T& value, const std::string& keyword,
51  bool exception=true) const;
52 
54  template<typename T>
55  T get (const std::string& keyword) const
56  { T value; get_value (value,keyword); return value; }
57 
58  protected:
59 
60  std::string text;
61 
62  struct Row
63  {
64  std::string keyword;
65  std::string value;
66  std::string flag;
67 
68  bool equals (const Row& that)
69  { return
70  keyword == that.keyword &&
71  value == that.value &&
72  flag == that.flag;
73  }
74  };
75 
76  mutable std::vector<Row> rows;
77 
78  void parse_rows () const;
79  };
80 
81 }
82 
83 template<typename T>
84 T parse (const std::string& value)
85 {
86  return fromstring<T> (value);
87 }
88 
89 inline std::string defortran (std::string value)
90 {
91  std::string::size_type D = value.find ('D');
92  if (D != std::string::npos)
93  value[D] = 'E';
94  return value;
95 }
96 
97 // specializations for float and double address Fortran formatting issue
98 template<>
99 inline float parse<float> (const std::string& value)
100 {
101  return fromstring<float> ( defortran(value) );
102 }
103 
104 template<>
105 inline double parse<double> (const std::string& value)
106 {
107  return fromstring<double> ( defortran(value) );
108 }
109 
111 template<typename T>
113  const std::string& keyword,
114  bool exception) const
115 {
116  std::string value_string = get_value (keyword);
117 
118  if (value_string.empty())
119  {
120  if (!exception)
121  return false;
122 
123  throw Error (InvalidParam, "Pulsar::TextParameters::get_value<T>",
124  "keyword='" + keyword + "' not found");
125  }
126 
127  value = parse<T> (value_string);
128  return true;
129 }
130 
131 #endif
@ Pulsar
A pulsar.
Definition: Types.h:63
A convenient exception handling class.
Definition: Error.h:54
void unload(FILE *) const
Unload to an open stream.
Definition: TextParameters.C:52
void load(FILE *)
Load from an open stream.
Definition: TextParameters.C:42
bool equals(const Parameters *that) const
Return true if *this == *that.
Definition: TextParameters.C:19
Stores pulsar parameters as uninterpreted text.
Definition: TextParameters.h:24
std::string get_value(const std::string &keyword) const
Retrieve a string from the text.
Definition: TextParameters.C:85
T get(const std::string &keyword) const
Return a value from the text, throw exeception if not found.
Definition: TextParameters.h:65
Storage of pulsar parameters used to create a Predictor.
Definition: Parameters.h:28
Parameters * clone() const
Return a new, copy constructed instance of self.
Definition: TextParameters.C:14
std::string get_name() const
Return the pulsar name.
Definition: TextParameters.C:129
void set_text(const std::string &t)
Set the text.
Definition: TextParameters.h:53

Generated using doxygen 1.8.17