File: IpOptionsList.hpp

package info (click to toggle)
coinor-ipopt 3.11.9-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,616 kB
  • sloc: cpp: 75,373; sh: 11,170; ansic: 2,798; makefile: 2,247; java: 632; fortran: 342
file content (289 lines) | stat: -rw-r--r-- 9,433 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (C) 2004, 2006 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: IpOptionsList.hpp 1861 2010-12-21 21:34:47Z andreasw $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#ifndef __IPOPTLIST_HPP__
#define __IPOPTLIST_HPP__

#include "IpUtils.hpp"
#include "IpReferenced.hpp"
#include "IpException.hpp"
#include "IpRegOptions.hpp"

#include <iostream>
#include <map>

namespace Ipopt
{
  /** Exception that can be used to indicate errors with options */
  DECLARE_STD_EXCEPTION(OPTION_INVALID);

  /** This class stores a list of user set options.  Each options is
   *  identified by a case-insensitive keyword (tag).  Its value is
   *  stored internally as a string (always lower case), but for
   *  convenience set and get methods are provided to obtain Index and
   *  Number type values.  For each keyword we also keep track of how
   *  often the value of an option has been requested by a get method.
   */
  class OptionsList : public ReferencedObject
  {
    /** Class for storing the value and counter for each option in
     *  OptionsList. */
    class OptionValue
    {
    public:
      /**@name Constructors/Destructors */
      //@{
      /** Default constructor (needed for the map) */
      OptionValue()
          :
          initialized_(false)
      {}

      /** Constructor given the value */
      OptionValue(std::string value, bool allow_clobber, bool dont_print)
          :
          value_(value),
          counter_(0),
          initialized_(true),
          allow_clobber_(allow_clobber),
          dont_print_(dont_print)
      {}

      /** Copy Constructor */
      OptionValue(const OptionValue& copy)
          :
          value_(copy.value_),
          counter_(copy.counter_),
          initialized_(copy.initialized_),
          allow_clobber_(copy.allow_clobber_),
          dont_print_(copy.dont_print_)
      {}

      /** Equals operator */
      void operator=(const OptionValue& copy)
      {
        value_=copy.value_;
        counter_=copy.counter_;
        initialized_=copy.initialized_;
        allow_clobber_=copy.allow_clobber_;
        dont_print_=copy.dont_print_;
      }

      /** Default Destructor */
      ~OptionValue()
      {}
      //@}

      /** Method for retrieving the value of an option.  Calling this
       *  method will increase the counter by one. */
      std::string GetValue() const
      {
        DBG_ASSERT(initialized_);
        counter_++;
        return value_;
      }

      /** Method for retrieving the value without increasing the
       *  counter */
      std::string Value() const
      {
        DBG_ASSERT(initialized_);
        return value_;
      }

      /** Method for accessing current value of the request counter */
      Index Counter() const
      {
        DBG_ASSERT(initialized_);
        return counter_;
      }

      /** True if the option can be overwritten */
      bool AllowClobber() const
      {
        DBG_ASSERT(initialized_);
        return allow_clobber_;
      }

      /** True if this option is not to show up in the
       *  print_user_options output */
      bool DontPrint() const
      {
        DBG_ASSERT(initialized_);
        return dont_print_;
      }

    private:
      /** Value for this option */
      std::string value_;

      /** Counter for requests */
      mutable Index counter_;

      /** for debugging */
      bool initialized_;

      /** True if the option can be overwritten */
      bool allow_clobber_;

      /** True if this option is not to show up in the
       *  print_user_options output */
      bool dont_print_;
    };

  public:
    /**@name Constructors/Destructors */
    //@{
    OptionsList(SmartPtr<RegisteredOptions> reg_options, SmartPtr<Journalist> jnlst)
        : reg_options_(reg_options), jnlst_(jnlst)
    {}

    OptionsList()
    {}

    /** Copy Constructor */
    OptionsList(const OptionsList& copy)
    {
      // copy all the option strings and values
      options_ = copy.options_;
      // copy the registered options pointer
      reg_options_ = copy.reg_options_;
    }

    /** Default destructor */
    virtual ~OptionsList()
    {}

    /** Overloaded Equals Operator */
    virtual void operator=(const OptionsList& source)
    {
      options_ = source.options_;
      reg_options_ = source.reg_options_;
      jnlst_ = source.jnlst_;
    }
    //@}

    /** Method for clearing all previously set options */
    virtual void clear()
    {
      options_.clear();
    }

    /** @name Get / Set Methods */
    //@{
    virtual void SetRegisteredOptions(const SmartPtr<RegisteredOptions> reg_options)
    {
      reg_options_ = reg_options;
    }
    virtual void SetJournalist(const SmartPtr<Journalist> jnlst)
    {
      jnlst_ = jnlst;
    }
    //@}
    /** @name Methods for setting options */
    //@{
    virtual bool SetStringValue(const std::string& tag, const std::string& value,
                                bool allow_clobber = true, bool dont_print = false);
    virtual bool SetNumericValue(const std::string& tag, Number value,
                                 bool allow_clobber = true, bool dont_print = false);
    virtual bool SetIntegerValue(const std::string& tag, Index value,
                                 bool allow_clobber = true, bool dont_print = false);
    //@}

    /** @name Methods for setting options only if they have not been
     *  set before*/
    //@{
    virtual bool SetStringValueIfUnset(const std::string& tag, const std::string& value,
                                       bool allow_clobber = true, bool dont_print = false);
    virtual bool SetNumericValueIfUnset(const std::string& tag, Number value,
                                        bool allow_clobber = true, bool dont_print = false);
    virtual bool SetIntegerValueIfUnset(const std::string& tag, Index value,
                                        bool allow_clobber = true, bool dont_print = false);
    //@}

    /** @name Methods for retrieving values from the options list.  If
     *  a tag is not found, the methods return false, and value is set
     *  to the default value defined in the registered options. */
    //@{
    virtual bool GetStringValue(const std::string& tag, std::string& value,
                                const std::string& prefix) const;
    virtual bool GetEnumValue(const std::string& tag, Index& value,
                              const std::string& prefix) const;
    virtual bool GetBoolValue(const std::string& tag, bool& value,
                              const std::string& prefix) const;
    virtual bool GetNumericValue(const std::string& tag, Number& value,
                                 const std::string& prefix) const;
    virtual bool GetIntegerValue(const std::string& tag, Index& value,
                                 const std::string& prefix) const;
    //@}

    /** Get a string with the list of all options (tag, value, counter) */
    virtual void PrintList(std::string& list) const;

    /** Get a string with the list of all options set by the user
     *  (tag, value, use/notused).  Here, options with dont_print flag
     *  set to true are not printed. */
    virtual void PrintUserOptions(std::string& list) const;

    /** Read options from the stream is.  Returns false if
     *  an error was encountered. */
    virtual bool ReadFromStream(const Journalist& jnlst, std::istream& is);

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Default Constructor */
    //    OptionsList();

    //@}

    /** map for storing the options */
    std::map< std::string, OptionValue > options_;

    /** list of all the registered options to validate against */
    SmartPtr<RegisteredOptions> reg_options_;

    /** Journalist for writing error messages, etc. */
    SmartPtr<Journalist> jnlst_;

    /** auxilliary method for converting sting to all lower-case
     *  letters */
    const std::string& lowercase(const std::string tag) const;

    /** auxilliary method for finding the value for a tag in the
     *  options list.  This method first looks for the concatenated
     *  string prefix+tag (if prefix is not ""), and if this is not
     *  found, it looks for tag.  The return value is true iff
     *  prefix+tag or tag is found.  In that case, the corresponding
     *  string value is copied into value. */
    bool find_tag(const std::string& tag, const std::string& prefix,
                  std::string& value) const;

    /** tells whether or not we can clobber a particular option.
     *  returns true if the option does not already exist, or if
     *  the option exists but is set to allow_clobber
     */
    bool will_allow_clobber(const std::string& tag) const;

    /** read the next token from stream is.  Returns false, if EOF was
     *  reached before a tokens was ecountered. */
    bool readnexttoken(std::istream& is, std::string& token);

    /** auxilliary string set by lowercase method */
    mutable std::string lowercase_buffer_;
  };

} // namespace Ipopt

#endif