TextInterfaceFunction.h
1//-*-C++-*-
2
3/***************************************************************************
4 *
5 * Copyright (C) 2003 - 2023 by Willem van Straten
6 * Licensed under the Academic Free License version 2.1
7 *
8 ***************************************************************************/
9
10// psrchive/Util/units/TextInterfaceFunction.h
11
12#ifndef __TextInterfaceFunction_h
13#define __TextInterfaceFunction_h
14
15#include "TextInterfaceAttribute.h"
16
17namespace TextInterface
18{
19
21
22 template<class C, class Get>
23 class GetFunction : public Attribute<C> {
24
25 public:
26
28 GetFunction (const std::string& _name, Get _get)
29 : get (_get) { name = _name; }
30
33 : get (copy.get) { name = copy.name; description = copy.description; }
34
36 Attribute<C>* clone () const { return new GetFunction(*this); }
37
39 std::string get_name () const { return name; }
40
42 std::string get_description () const { return description; }
43
45 void set_description (const std::string& d) { description = d; }
46
48 std::string get_detailed_description () const
49 { return detailed_description; }
50
52 void set_detailed_description (const std::string& d)
54
56 std::string get_value (const C* ptr) const
57 { if (!ptr) return ""; return tostring( get(ptr) ); }
58
60 void set_value (C*, const std::string&)
61 { throw Error (InvalidState, "GetFunction::set_value",
62 name + " cannot be set"); }
63
64 void set_modifiers (const std::string& modifiers) const
65 {
66 tostring_precision = fromstring<unsigned> (modifiers);
67 }
68
69 void reset_modifiers () const
70 {
71 tostring_precision = 0;
72 }
73
74 protected:
75
77 std::string name;
78
80 std::string description;
81
84
86 Get get;
87
88 };
89
91 template<class C, class Get, class Set>
92 class GetSetFunction : public GetFunction<C, Get>
93 {
94
95 public:
96
98 GetSetFunction (const std::string& _name, Get _get, Set _set)
99 : GetFunction<C,Get> (_name, _get), set (_set) { }
100
102 Attribute<C>* clone () const { return new GetSetFunction(*this); }
103
105 void set_value (C* ptr, const std::string& value)
106 { set (ptr, fromstring<typename Set::second_argument_type> (value)); }
107
108 protected:
109
111 Set set;
112 };
113
114}
115
116#endif
A convenient exception handling class.
Definition Error.h:54
Text interface to a class attribute.
Definition TextInterfaceAttribute.h:23
Attribute()
Default constructor.
Definition TextInterfaceAttribute.h:28
void set_modifiers(const std::string &modifiers) const
Parse any modifiers that will alter the behaviour of the output stream.
Definition TextInterfaceFunction.h:64
Get get
The get function object (functor)
Definition TextInterfaceFunction.h:86
GetFunction(const GetFunction &copy)
Copy constructor.
Definition TextInterfaceFunction.h:32
std::string get_description() const
Get the description of the attribute.
Definition TextInterfaceFunction.h:42
std::string detailed_description
The detailed description of the attribute.
Definition TextInterfaceFunction.h:83
void set_description(const std::string &d)
Get the description of the attribute.
Definition TextInterfaceFunction.h:45
void set_detailed_description(const std::string &d)
Get the detailed description of the attribute.
Definition TextInterfaceFunction.h:52
GetFunction(const std::string &_name, Get _get)
Constructor.
Definition TextInterfaceFunction.h:28
void reset_modifiers() const
Reset any output stream modifiers.
Definition TextInterfaceFunction.h:69
void set_value(C *, const std::string &)
Set the value of the attribute.
Definition TextInterfaceFunction.h:60
std::string get_name() const
Get the name of the attribute.
Definition TextInterfaceFunction.h:39
std::string get_value(const C *ptr) const
Get the value of the attribute.
Definition TextInterfaceFunction.h:56
std::string get_detailed_description() const
Get the detailed description of the attribute.
Definition TextInterfaceFunction.h:48
Attribute< C > * clone() const
Return a clone.
Definition TextInterfaceFunction.h:36
std::string description
The description of the attribute.
Definition TextInterfaceFunction.h:80
std::string name
The name of the attribute.
Definition TextInterfaceFunction.h:77
Set set
The set method.
Definition TextInterfaceFunction.h:111
GetSetFunction(const std::string &_name, Get _get, Set _set)
Constructor.
Definition TextInterfaceFunction.h:98
void set_value(C *ptr, const std::string &value)
Set the value of the attribute.
Definition TextInterfaceFunction.h:105
Attribute< C > * clone() const
Return a clone.
Definition TextInterfaceFunction.h:102

Generated using doxygen 1.14.0