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
25namespace 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
A convenient exception handling class.
Definition Error.h:54
Manages Reference::To references to the instance.
Definition ReferenceAble.h:35
Template class manages Reference::Able objects.
Definition ReferenceTo.h:25
An array of Value interfaces.
Definition TextInterfaceParser.h:31
void set_value(const std::string &text)
Set the value as text.
Definition TextInterfaceValue.h:137
ValueGetSet(T *ptr, const std::string &name, const std::string &desc)
Set all attributes on construction.
Definition TextInterfaceValue.h:133
void set_detailed_description(const std::string &text)
Set the detailed description of the value.
Definition TextInterfaceValue.h:114
std::string get_value() const
Get the value as text.
Definition TextInterfaceValue.h:93
std::string get_detailed_description() const
Get the detailed description of the value.
Definition TextInterfaceValue.h:110
void set_description(const std::string &text)
Set the description of the value.
Definition TextInterfaceValue.h:106
std::string get_name() const
Get the name of the value.
Definition TextInterfaceValue.h:89
std::string get_description() const
Get the description of the value.
Definition TextInterfaceValue.h:102
ValueGet(T *ptr, const std::string &_name, const std::string &_describe)
Set all attributes on construction.
Definition TextInterfaceValue.h:85
void set_value(const std::string &)
Set the value as text.
Definition TextInterfaceValue.h:97
Value()
Explicit default constructor required to delay definition of Reference::To<Parser>
Definition TextInterfaceValue.C:13
virtual std::string get_value() const =0
Get the value as text.
virtual void set_value(const std::string &value)=0
Set the value as text.
virtual void set_modifiers(const std::string &) const =0
Parse any modifiers that will alter the behaviour of the output stream.
Definition TextInterfaceValue.C:24
Reference::To< Parser, false > parent
The Parser composite in which this Value component is integrated.
Definition TextInterfaceValue.h:75
virtual std::string get_description() const =0
Get the description of the value.
virtual std::string get_name() const =0
Get the name of the value.
~Value()
Explicit destructor required to delay definition of Reference::To<Parser>
Definition TextInterfaceValue.C:15
virtual bool matches(const std::string &name) const
Return true if the name argument matches the value name.
Definition TextInterfaceValue.h:56
virtual void reset_modifiers() const =0
Reset any output stream modifiers.
Definition TextInterfaceValue.C:22
virtual std::string get_detailed_description() const
Get the detailed description of the value.
Definition TextInterfaceValue.h:53

Generated using doxygen 1.14.0