TextInterfaceValue.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/TextInterfaceValue.h
10 
11 #ifndef __TextInterfaceValue_h
12 #define __TextInterfaceValue_h
13 
14 #include "Reference.h"
15 #include "Error.h"
16 
17 #include <string>
18 
19 // #define _DEBUG 1
20 
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24 
25 namespace TextInterface {
26 
27  class Parser;
28 
30  class Value : public Reference::Able
31  {
32  public:
33 
35  Value ();
36 
38  ~Value ();
39 
41  virtual std::string get_name () const = 0;
42 
44  virtual std::string get_value () const = 0;
45 
47  virtual void set_value (const std::string& value) = 0;
48 
50  virtual std::string get_description () const = 0;
51 
53  virtual std::string get_detailed_description () const { return "none"; }
54 
56  virtual bool matches (const std::string& name) const
57  {
58 #ifdef _DEBUG
59  std::cerr << "TextInterface::Value::matches" << std::endl;
60 #endif
61  return name == get_name();
62  }
63 
65  virtual void set_modifiers (const std::string&) const = 0;
66 
68  virtual void reset_modifiers () const = 0;
69 
70  virtual void set_parent (Parser* p);
71 
72  protected:
73 
76  };
77 
79  template<class T>
80  class ValueGet : public Value {
81 
82  public:
83 
85  ValueGet (T* ptr, const std::string& _name, const std::string& _describe)
86  { value_ptr = ptr; name = _name; description = _describe; }
87 
89  std::string get_name () const
90  { return name; }
91 
93  std::string get_value () const
94  { return tostring (*value_ptr); }
95 
97  void set_value (const std::string&)
98  { throw Error (InvalidState, "ValueGet::set_value",
99  name + " cannot be set"); }
100 
102  std::string get_description () const
103  { return description; }
104 
106  void set_description (const std::string& text)
107  { description = text; }
108 
110  std::string get_detailed_description () const
111  { return detailed_description; }
112 
114  void set_detailed_description (const std::string& text)
115  { detailed_description = text; }
116 
117  protected:
118 
119  std::string name;
120  std::string description;
121  std::string detailed_description;
122  T* value_ptr;
123 
124  };
125 
127  template<class T>
128  class ValueGetSet : public ValueGet<T> {
129 
130  public:
131 
133  ValueGetSet (T* ptr, const std::string& name, const std::string& desc)
134  : ValueGet<T> (ptr, name, desc) { }
135 
137  void set_value (const std::string& text)
138  { *(this->value_ptr) = fromstring<T>(text); }
139 
140  };
141 }
142 
143 #endif
virtual void set_modifiers(const std::string &) const =0
Parse any modifiers that will alter the behaviour of the output stream.
Definition: TextInterfaceValue.C:19
std::string get_value() const
Get the value as text.
Definition: TextInterfaceValue.h:98
virtual std::string get_detailed_description() const
Get the detailed description of the value.
Definition: TextInterfaceValue.h:58
void set_detailed_description(const std::string &text)
Set the detailed description of the value.
Definition: TextInterfaceValue.h:119
virtual void reset_modifiers() const =0
Reset any output stream modifiers.
Definition: TextInterfaceValue.C:17
virtual std::string get_name() const =0
Get the name of the value.
void set_value(const std::string &text)
Set the value as text.
Definition: TextInterfaceValue.h:142
std::string get_name() const
Get the name of the value.
Definition: TextInterfaceValue.h:94
ValueGetSet(T *ptr, const std::string &name, const std::string &desc)
Set all attributes on construction.
Definition: TextInterfaceValue.h:138
A convenient exception handling class.
Definition: Error.h:54
Reference::To< Parser, false > parent
The Parser composite in which this Value component is integrated.
Definition: TextInterfaceValue.h:80
virtual std::string get_description() const =0
Get the description of the value.
An array of Value interfaces.
Definition: TextInterfaceParser.h:35
void set_value(const std::string &)
Set the value as text.
Definition: TextInterfaceValue.h:102
Template class manages Reference::Able objects.
Definition: Reference.h:74
virtual void set_value(const std::string &value)=0
Set the value as text.
std::string get_detailed_description() const
Get the detailed description of the value.
Definition: TextInterfaceValue.h:115
Text interface to a value of undefined type.
Definition: TextInterfaceValue.h:35
Value()
Explicit default constructor required to delay definition of Reference::To<Parser>
Definition: TextInterfaceValue.C:11
Manages Reference::To references to the instance.
Definition: ReferenceAble.h:40
void set_description(const std::string &text)
Set the description of the value.
Definition: TextInterfaceValue.h:111
~Value()
Explicit destructor required to delay definition of Reference::To<Parser>
Definition: TextInterfaceValue.C:13
virtual bool matches(const std::string &name) const
Return true if the name argument matches the value name.
Definition: TextInterfaceValue.h:61
ValueGet(T *ptr, const std::string &_name, const std::string &_describe)
Set all attributes on construction.
Definition: TextInterfaceValue.h:90
virtual std::string get_value() const =0
Get the value as text.
Read and write interface to a value of type T.
Definition: TextInterfaceValue.h:133
std::string get_description() const
Get the description of the value.
Definition: TextInterfaceValue.h:107
Read-only interface to a value of type T.
Definition: TextInterfaceValue.h:85

Generated using doxygen 1.8.17