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 
17 namespace 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 
32  GetFunction (const GetFunction& copy)
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
50 
52  void set_detailed_description (const std::string& d)
53  { detailed_description = 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 
83  std::string detailed_description;
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
GetFunction(const std::string &_name, Get _get)
Constructor.
Definition: TextInterfaceFunction.h:38
std::string name
The name of the attribute.
Definition: TextInterfaceFunction.h:87
A convenient exception handling class.
Definition: Error.h:54
void set_modifiers(const std::string &modifiers) const
Parse any modifiers that will alter the behaviour of the output stream.
Definition: TextInterfaceFunction.h:74
void set_value(C *, const std::string &)
Set the value of the attribute.
Definition: TextInterfaceFunction.h:70
std::string get_description() const
Get the description of the attribute.
Definition: TextInterfaceFunction.h:52
GetSetFunction(const std::string &_name, Get _get, Set _set)
Constructor.
Definition: TextInterfaceFunction.h:103
Get is function object that receives C* and returns some type.
Definition: TextInterfaceFunction.h:28
void set_detailed_description(const std::string &d)
Get the detailed description of the attribute.
Definition: TextInterfaceFunction.h:62
void set_description(const std::string &d)
Get the description of the attribute.
Definition: TextInterfaceFunction.h:55
Attribute< C > * clone() const
Return a clone.
Definition: TextInterfaceFunction.h:46
std::string description
The description of the attribute.
Definition: TextInterfaceFunction.h:90
std::string get_detailed_description() const
Get the detailed description of the attribute.
Definition: TextInterfaceFunction.h:58
std::string get_value() const
Get the value of the attribute.
Definition: TextInterfaceAttribute.h:42
void reset_modifiers() const
Reset any output stream modifiers.
Definition: TextInterfaceFunction.h:79
std::string get_name() const
Get the name of the attribute.
Definition: TextInterfaceFunction.h:49
Interface to a class attribute with an accessor and modifier methods.
Definition: TextInterfaceFunction.h:97
std::string detailed_description
The detailed description of the attribute.
Definition: TextInterfaceFunction.h:93
Set set
The set method.
Definition: TextInterfaceFunction.h:116
Text interface to a class attribute.
Definition: TextInterfaceAttribute.h:27
void set_value(C *ptr, const std::string &value)
Set the value of the attribute.
Definition: TextInterfaceFunction.h:110
Attribute< C > * clone() const
Return a clone.
Definition: TextInterfaceFunction.h:107
Get get
The get function object (functor)
Definition: TextInterfaceFunction.h:96

Generated using doxygen 1.8.17