IpOptionsList.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright (C) 2004, 2006 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpOptionsList.hpp 1861 2010-12-21 21:34:47Z andreasw $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPOPTLIST_HPP__
  9. #define __IPOPTLIST_HPP__
  10. #include "IpUtils.hpp"
  11. #include "IpReferenced.hpp"
  12. #include "IpException.hpp"
  13. #include "IpRegOptions.hpp"
  14. #include <iostream>
  15. #include <map>
  16. namespace Ipopt
  17. {
  18. /** Exception that can be used to indicate errors with options */
  19. DECLARE_STD_EXCEPTION(OPTION_INVALID);
  20. /** This class stores a list of user set options. Each options is
  21. * identified by a case-insensitive keyword (tag). Its value is
  22. * stored internally as a string (always lower case), but for
  23. * convenience set and get methods are provided to obtain Index and
  24. * Number type values. For each keyword we also keep track of how
  25. * often the value of an option has been requested by a get method.
  26. */
  27. class OptionsList : public ReferencedObject
  28. {
  29. /** Class for storing the value and counter for each option in
  30. * OptionsList. */
  31. class OptionValue
  32. {
  33. public:
  34. /**@name Constructors/Destructors */
  35. //@{
  36. /** Default constructor (needed for the map) */
  37. OptionValue()
  38. :
  39. initialized_(false)
  40. {}
  41. /** Constructor given the value */
  42. OptionValue(std::string value, bool allow_clobber, bool dont_print)
  43. :
  44. value_(value),
  45. counter_(0),
  46. initialized_(true),
  47. allow_clobber_(allow_clobber),
  48. dont_print_(dont_print)
  49. {}
  50. /** Copy Constructor */
  51. OptionValue(const OptionValue& copy)
  52. :
  53. value_(copy.value_),
  54. counter_(copy.counter_),
  55. initialized_(copy.initialized_),
  56. allow_clobber_(copy.allow_clobber_),
  57. dont_print_(copy.dont_print_)
  58. {}
  59. /** Equals operator */
  60. void operator=(const OptionValue& copy)
  61. {
  62. value_=copy.value_;
  63. counter_=copy.counter_;
  64. initialized_=copy.initialized_;
  65. allow_clobber_=copy.allow_clobber_;
  66. dont_print_=copy.dont_print_;
  67. }
  68. /** Default Destructor */
  69. ~OptionValue()
  70. {}
  71. //@}
  72. /** Method for retrieving the value of an option. Calling this
  73. * method will increase the counter by one. */
  74. std::string GetValue() const
  75. {
  76. DBG_ASSERT(initialized_);
  77. counter_++;
  78. return value_;
  79. }
  80. /** Method for retrieving the value without increasing the
  81. * counter */
  82. std::string Value() const
  83. {
  84. DBG_ASSERT(initialized_);
  85. return value_;
  86. }
  87. /** Method for accessing current value of the request counter */
  88. Index Counter() const
  89. {
  90. DBG_ASSERT(initialized_);
  91. return counter_;
  92. }
  93. /** True if the option can be overwritten */
  94. bool AllowClobber() const
  95. {
  96. DBG_ASSERT(initialized_);
  97. return allow_clobber_;
  98. }
  99. /** True if this option is not to show up in the
  100. * print_user_options output */
  101. bool DontPrint() const
  102. {
  103. DBG_ASSERT(initialized_);
  104. return dont_print_;
  105. }
  106. private:
  107. /** Value for this option */
  108. std::string value_;
  109. /** Counter for requests */
  110. mutable Index counter_;
  111. /** for debugging */
  112. bool initialized_;
  113. /** True if the option can be overwritten */
  114. bool allow_clobber_;
  115. /** True if this option is not to show up in the
  116. * print_user_options output */
  117. bool dont_print_;
  118. };
  119. public:
  120. /**@name Constructors/Destructors */
  121. //@{
  122. OptionsList(SmartPtr<RegisteredOptions> reg_options, SmartPtr<Journalist> jnlst)
  123. : reg_options_(reg_options), jnlst_(jnlst)
  124. {}
  125. OptionsList()
  126. {}
  127. /** Copy Constructor */
  128. OptionsList(const OptionsList& copy)
  129. {
  130. // copy all the option strings and values
  131. options_ = copy.options_;
  132. // copy the registered options pointer
  133. reg_options_ = copy.reg_options_;
  134. }
  135. /** Default destructor */
  136. virtual ~OptionsList()
  137. {}
  138. /** Overloaded Equals Operator */
  139. virtual void operator=(const OptionsList& source)
  140. {
  141. options_ = source.options_;
  142. reg_options_ = source.reg_options_;
  143. jnlst_ = source.jnlst_;
  144. }
  145. //@}
  146. /** Method for clearing all previously set options */
  147. virtual void clear()
  148. {
  149. options_.clear();
  150. }
  151. /** @name Get / Set Methods */
  152. //@{
  153. virtual void SetRegisteredOptions(const SmartPtr<RegisteredOptions> reg_options)
  154. {
  155. reg_options_ = reg_options;
  156. }
  157. virtual void SetJournalist(const SmartPtr<Journalist> jnlst)
  158. {
  159. jnlst_ = jnlst;
  160. }
  161. //@}
  162. /** @name Methods for setting options */
  163. //@{
  164. virtual bool SetStringValue(const std::string& tag, const std::string& value,
  165. bool allow_clobber = true, bool dont_print = false);
  166. virtual bool SetNumericValue(const std::string& tag, Number value,
  167. bool allow_clobber = true, bool dont_print = false);
  168. virtual bool SetIntegerValue(const std::string& tag, Index value,
  169. bool allow_clobber = true, bool dont_print = false);
  170. //@}
  171. /** @name Methods for setting options only if they have not been
  172. * set before*/
  173. //@{
  174. virtual bool SetStringValueIfUnset(const std::string& tag, const std::string& value,
  175. bool allow_clobber = true, bool dont_print = false);
  176. virtual bool SetNumericValueIfUnset(const std::string& tag, Number value,
  177. bool allow_clobber = true, bool dont_print = false);
  178. virtual bool SetIntegerValueIfUnset(const std::string& tag, Index value,
  179. bool allow_clobber = true, bool dont_print = false);
  180. //@}
  181. /** @name Methods for retrieving values from the options list. If
  182. * a tag is not found, the methods return false, and value is set
  183. * to the default value defined in the registered options. */
  184. //@{
  185. virtual bool GetStringValue(const std::string& tag, std::string& value,
  186. const std::string& prefix) const;
  187. virtual bool GetEnumValue(const std::string& tag, Index& value,
  188. const std::string& prefix) const;
  189. virtual bool GetBoolValue(const std::string& tag, bool& value,
  190. const std::string& prefix) const;
  191. virtual bool GetNumericValue(const std::string& tag, Number& value,
  192. const std::string& prefix) const;
  193. virtual bool GetIntegerValue(const std::string& tag, Index& value,
  194. const std::string& prefix) const;
  195. //@}
  196. /** Get a string with the list of all options (tag, value, counter) */
  197. virtual void PrintList(std::string& list) const;
  198. /** Get a string with the list of all options set by the user
  199. * (tag, value, use/notused). Here, options with dont_print flag
  200. * set to true are not printed. */
  201. virtual void PrintUserOptions(std::string& list) const;
  202. /** Read options from the stream is. Returns false if
  203. * an error was encountered. */
  204. virtual bool ReadFromStream(const Journalist& jnlst, std::istream& is);
  205. private:
  206. /**@name Default Compiler Generated Methods
  207. * (Hidden to avoid implicit creation/calling).
  208. * These methods are not implemented and
  209. * we do not want the compiler to implement
  210. * them for us, so we declare them private
  211. * and do not define them. This ensures that
  212. * they will not be implicitly created/called. */
  213. //@{
  214. /** Default Constructor */
  215. // OptionsList();
  216. //@}
  217. /** map for storing the options */
  218. std::map< std::string, OptionValue > options_;
  219. /** list of all the registered options to validate against */
  220. SmartPtr<RegisteredOptions> reg_options_;
  221. /** Journalist for writing error messages, etc. */
  222. SmartPtr<Journalist> jnlst_;
  223. /** auxilliary method for converting sting to all lower-case
  224. * letters */
  225. const std::string& lowercase(const std::string tag) const;
  226. /** auxilliary method for finding the value for a tag in the
  227. * options list. This method first looks for the concatenated
  228. * string prefix+tag (if prefix is not ""), and if this is not
  229. * found, it looks for tag. The return value is true iff
  230. * prefix+tag or tag is found. In that case, the corresponding
  231. * string value is copied into value. */
  232. bool find_tag(const std::string& tag, const std::string& prefix,
  233. std::string& value) const;
  234. /** tells whether or not we can clobber a particular option.
  235. * returns true if the option does not already exist, or if
  236. * the option exists but is set to allow_clobber
  237. */
  238. bool will_allow_clobber(const std::string& tag) const;
  239. /** read the next token from stream is. Returns false, if EOF was
  240. * reached before a tokens was ecountered. */
  241. bool readnexttoken(std::istream& is, std::string& token);
  242. /** auxilliary string set by lowercase method */
  243. mutable std::string lowercase_buffer_;
  244. };
  245. } // namespace Ipopt
  246. #endif