Configurable.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Configurable_H
21 #define MIRTK_Configurable_H
22 
23 #include "mirtk/Observable.h"
24 
25 #include "mirtk/Array.h"
26 
27 
28 namespace mirtk {
29 
30 
31 /**
32  * Base class of observable objects which have a primary name and optionally
33  * one or more alternative names
34  */
35 class Configurable : public Observable
36 {
37  mirtkAbstractMacro(Configurable);
38 
39  // ---------------------------------------------------------------------------
40  // Attributes
41 
42  /// Primary name of object
43  mirtkPublicAttributeMacro(string, Name);
44 
45  /// Parameter name prefixes recognized by Set
46  mirtkPublicAttributeMacro(Array<string>, ParameterPrefix);
47 
48  /// Copy attributes of this class from another instance
49  void CopyAttributes(const Configurable &other);
50 
51  // ---------------------------------------------------------------------------
52  // Construction/Destruction
53 protected:
54 
55  /// Constructor
56  Configurable(const char * = "");
57 
58  /// Copy constructor
59  Configurable(const Configurable &);
60 
61  /// Assignment operator
63 
64 public:
65 
66  /// Destructor
67  virtual ~Configurable();
68 
69  /// Default name derived from class name
70  ///
71  /// Example usage:
72  /// \code
73  /// irtkConfigurable *obj = new MyConfigurableObject();
74  /// obj->Name(obj->DefaultName());
75  /// \endcode
76  string DefaultName() const;
77 
78  // ---------------------------------------------------------------------------
79  // Parameters
80 
81 protected:
82 
83  /// Whether this object has an explicit name
84  bool HasName() const;
85 
86  /// Whether this object has either an explicit name or default prefix
87  bool HasPrefix() const;
88 
89  /// Get default object name prefix (if any)
90  string DefaultPrefix() const;
91 
92  /// Get name of parameter with default object name prefix
93  string ParameterNameWithPrefix(const string &) const;
94 
95  /// Get name of parameter with default object name prefix
96  string ParameterNameWithPrefix(const char *) const;
97 
98  /// Get name of parameter without object name prefix
99  string ParameterNameWithoutPrefix(const char *) const;
100 
101  /// Insert parameter into name/value list with object name prefix
102  template <class T> bool InsertWithPrefix(ParameterList &, string, T) const;
103 
104  /// Insert parameters into name/value list with object name prefix
105  bool InsertWithPrefix(ParameterList &, const ParameterList &) const;
106 
107  /// Set parameter value from string
108  virtual bool SetWithPrefix(const char *, const char *);
109 
110  /// Set parameter value from string
111  virtual bool SetWithoutPrefix(const char *, const char *);
112 
113 public:
114 
115  /// Set parameter value from string
116  virtual bool Set(const char *, const char *);
117 
118 };
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 // Inline definitions
122 ////////////////////////////////////////////////////////////////////////////////
123 
124 // -----------------------------------------------------------------------------
125 template <class T>
126 bool Configurable::InsertWithPrefix(ParameterList &params, string name, T value) const
127 {
128  if (name.empty() || !HasPrefix()) return false;
129  Insert(params, ParameterNameWithPrefix(name), value);
130  return true;
131 }
132 
133 // -----------------------------------------------------------------------------
134 inline bool Configurable::InsertWithPrefix(ParameterList &params, const ParameterList &other) const
135 {
136  string prefix = DefaultPrefix();
137  if (prefix.empty()) return false;
138  if (prefix.back() == ' ') prefix.back() = '\0';
139  Insert(params, other, prefix.c_str());
140  return true;
141 }
142 
143 
144 } // namespace mirtk
145 
146 #endif // MIRTK_Configurable_H
string ParameterNameWithoutPrefix(const char *) const
Get name of parameter without object name prefix.
string DefaultPrefix() const
Get default object name prefix (if any)
Configurable & operator=(const Configurable &)
Assignment operator.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
Definition: Object.h:38
virtual ~Configurable()
Destructor.
Definition: IOConfig.h:41
virtual bool Set(const char *, const char *)
Set parameter value from string.
string ParameterNameWithPrefix(const string &) const
Get name of parameter with default object name prefix.
Configurable(const char *="")
Constructor.
bool InsertWithPrefix(ParameterList &, string, T) const
Insert parameter into name/value list with object name prefix.
Definition: Configurable.h:126
bool HasPrefix() const
Whether this object has either an explicit name or default prefix.
string DefaultName() const
virtual bool SetWithPrefix(const char *, const char *)
Set parameter value from string.
ParameterList & Insert(ParameterList &params, string name, T value)
Insert/replace value into/in parameters list.
Definition: Object.h:212
virtual bool SetWithoutPrefix(const char *, const char *)
Set parameter value from string.
bool HasName() const
Whether this object has an explicit name.