TextInterfaceParser.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2003-2013 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/psrchive/Util/units/TextInterfaceParser.h
10 
11 #ifndef __TextInterfaceParser_h
12 #define __TextInterfaceParser_h
13 
14 #include "TextInterfaceValue.h"
15 
16 #include "Alias.h"
17 #include "Error.h"
18 #include "tostring.h"
19 
20 // #define _DEBUG 1
21 
22 #ifdef _DEBUG
23 #include <iostream>
24 #endif
25 
26 namespace TextInterface
27 {
28 
30  class Parser : public Reference::Able
31  {
32 
33  public:
34 
36  Parser ();
37 
39  ~Parser ();
40 
42  std::string get_value (const std::string& name) const;
43 
45  std::string get_name_value (std::string& name) const;
46 
48  void set_value (const std::string& name, const std::string& value);
49 
51  Value* find (const std::string& name, bool throw_exception = true) const;
52 
54  bool found (const std::string& name) const;
55 
57  bool found (const std::string& prefix, const std::string& name) const;
58 
60  virtual void setup (const Value*) { }
61 
63  unsigned get_nvalue () const;
64 
66  std::string get_name (unsigned) const;
67 
69  std::string get_value (unsigned) const;
70 
72  std::string get_description (unsigned) const;
73 
75  virtual std::string process (const std::string& command);
76 
78  virtual std::string process (const std::vector<std::string>& commands);
79 
81  virtual std::string help (bool show_default_values = false,
82  bool show_header = false,
83  const char* indent = 0);
84 
86  virtual std::string get_interface_name () const { return ""; }
87 
89  virtual std::string get_interface_description () const { return ""; }
90 
92  void set_indentation (const std::string& indent) { indentation = indent; }
93  std::string get_indentation () const { return indentation; }
94 
96  void set_delimiter (const std::string&);
97  std::string get_delimiter () const { return delimiter; }
98 
100  void set_prefix_name (bool flag) { prefix_name = flag; }
101 
103  void set_aliases (const Alias* alias) { aliases = alias; }
104 
106 
107  void insert (Parser*);
108 
110 
111  void insert (const std::string& prefix, Parser*);
112 
114  void clean ();
115 
116  protected:
117 
119  std::string indentation;
120 
122  std::string delimiter;
123 
125  bool alphabetical;
126 
128  bool import_filter;
129 
131  bool prefix_name;
132 
135 
137  void add_value (Value* value);
138 
140  void remove (const std::string& name);
141 
143  void clean_invalid ();
144 
146  std::vector< Reference::To<Value> > values;
147 
149  std::vector< Reference::To<Parser> > inserted;
150  };
151 
153  bool matches (const std::string& name,
154  const std::string& prefix, const Value* value);
155 
156 }
157 
158 #endif
void clean_invalid()
Clean up invalid references in values vector.
Definition: TextInterfaceParser.C:188
void set_indentation(const std::string &indent)
Set the indentation that precedes the output of a call to process.
Definition: TextInterfaceParser.h:102
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
virtual void setup(const Value *)
Allow derived types to setup a Value instance before use.
Definition: TextInterfaceParser.h:70
virtual void reset_modifiers() const =0
Reset any output stream modifiers.
Definition: TextInterfaceValue.C:17
virtual std::string get_interface_description() const
Get a short description of this interface.
Definition: TextInterfaceParser.h:99
virtual std::string get_name() const =0
Get the name of the value.
~Parser()
Destructor.
Definition: TextInterfaceParser.C:214
std::string get_name_value(std::string &name) const
Get the named value and update name for display purposes.
Definition: TextInterfaceParser.C:230
void set_delimiter(const std::string &)
Set the delimiter used to separate the elements of a container.
Definition: TextInterfaceParser.C:23
std::string get_name(unsigned) const
Get the name of the value.
Definition: TextInterfaceParser.C:268
A convenient exception handling class.
Definition: Error.h:54
std::vector< Reference::To< Value > > values
The vector of values.
Definition: TextInterfaceParser.h:156
std::vector< Reference::To< Parser > > inserted
Any inserted Parser is owned by this.
Definition: TextInterfaceParser.h:159
std::string get_value(const std::string &name) const
Get the named value.
Definition: TextInterfaceParser.C:223
void set_value(const std::string &name, const std::string &value)
Set the named value.
Definition: TextInterfaceParser.C:256
ErrorCode get_code() const
Get the error code.
Definition: Error.h:92
An array of Value interfaces.
Definition: TextInterfaceParser.h:35
Value * find(const std::string &name, bool throw_exception=true) const
Find the named value.
Definition: TextInterfaceParser.C:304
bool prefix_name
Prefix "name=" when retrieving a value.
Definition: TextInterfaceParser.h:141
bool found(const std::string &name) const
Return true if the named value is found.
Definition: TextInterfaceParser.C:287
void remove(const std::string &name)
Remove the named value interface.
Definition: TextInterfaceParser.C:182
Reference::To< const Alias > aliases
The aliases for the value names.
Definition: TextInterfaceParser.h:144
Text interface to a value of undefined type.
Definition: TextInterfaceValue.h:35
Proxy enables value interfaces to be imported with a name.
Definition: NestedTextInterface.h:29
bool alphabetical
Maintain alphabetical order of parameter names.
Definition: TextInterfaceParser.h:135
Manages Reference::To references to the instance.
Definition: ReferenceAble.h:40
Stores keyword-value pairs.
Definition: Alias.h:21
unsigned get_nvalue() const
Get the number of values.
Definition: TextInterfaceParser.C:262
std::string delimiter
The delimiter used to separate the elements of a container.
Definition: TextInterfaceParser.h:132
std::string indentation
The indentation that precedes the output of a call to process.
Definition: TextInterfaceParser.h:129
Parser()
Default constructor.
Definition: TextInterfaceParser.C:202
virtual std::string get_interface_name() const
Get the name of this interface.
Definition: TextInterfaceParser.h:96
void set_aliases(const Alias *alias)
Set aliases for value names.
Definition: TextInterfaceParser.h:113
virtual std::string get_value() const =0
Get the value as text.
void set_prefix_name(bool flag)
Prefix output with "name=".
Definition: TextInterfaceParser.h:110
virtual std::string help(bool show_default_values=false, bool show_header=false, const char *indent=0)
Return the list of available values.
Definition: TextInterfaceParser.C:99
void add_value(Value *value)
Add a new value interface.
Definition: TextInterfaceParser.C:167
void clean()
Clear all nested interfaces.
Definition: TextInterfaceParser.C:377
virtual std::string process(const std::string &command)
Process a command.
Definition: TextInterfaceParser.C:42
bool import_filter
Filter duplicate value names during import.
Definition: TextInterfaceParser.h:138
std::string get_description(unsigned) const
Get the description of the value.
Definition: TextInterfaceParser.C:281
void insert(Parser *)
Insert Parser into self.
Definition: TextInterfaceParser.C:337

Generated using doxygen 1.8.17