1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2008 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
41 /*! Returns whether an option with the given \a name is present in \a optSet.
43 \param[in] optSet OptionSet to check.
44 \param[in] name Name of the option.
45 \return Whether the option is present.
48 IOFileTypeBase::hasOption(const OptionSet &optSet, const std::string &name)
50 return (optSet.find(name) != optSet.end());
53 /*! Attempts to set option \a name to \a value in \a optSet.
54 If successful \c true is returned, false otherwise.
55 For the operation to succeed a \c boost::lexical_cast<> from the given type
56 has to succeed, usually that means an appropriate overload of
57 \c operator<< has to be available.
59 \param[in] optSet OptionSet to modify.
60 \param[in] name Name of the option.
61 \param[in] value Value of the option.
62 \return Whether the value was set successfully.
64 template <class ValueTypeT>
66 IOFileTypeBase::setOptionAs(
67 OptionSet &optSet, const std::string &name, const ValueTypeT &value)
73 setOption(optSet, name, boost::lexical_cast<std::string>(value));
76 catch(boost::bad_lexical_cast &blc)
78 SWARNING << "IOFileTypeBase::setOptionAs: Failed to store value "
79 << "for option [" << name << "] : "
80 << blc.what() << std::endl;
86 /*! Attempts to return the \a value associated with option \a name in \a optSet
87 as the requested type.
88 If the option is not present \c false is returned, \c true otherwise and
89 only in this case value is being set.
90 For the operation to succeed a \c boost::lexical_cast<> to the requested
91 type has to succeed, usually that means an appropriate overload of
92 \c operator>> has to be available.
94 \param[in] optSet OptionSet to read.
95 \param[in] name Name of the option.
96 \param[out] value Value of option.
97 \return Whether the option is present.
99 template <class ValueTypeT>
101 IOFileTypeBase::getOptionAs(
102 const OptionSet &optSet, const std::string &name, ValueTypeT &value)
105 std::string valueStr;
107 if(getOption(optSet, name, valueStr) == true)
111 value = boost::lexical_cast<ValueTypeT>(valueStr);
114 catch(boost::bad_lexical_cast &blc)
116 SWARNING << "IOFileTypeBase::getOptionAs: Failed to extract "
117 << "value of option [" << name << "] from string ["
118 << valueStr << "] : "
119 << blc.what() << std::endl;
127 /*! Returns the currently active option set, i.e. the top of the options stack.
129 inline IOFileTypeBase::OptionSet const &
130 IOFileTypeBase::getOptions(void) const
132 return _optStack.top();
135 /*! Returns the currently active option set, i.e. the top of the options stack.
137 inline IOFileTypeBase::OptionSet &
138 IOFileTypeBase::editOptions(void)
140 return _optStack.top();
143 /*! Returns whether an option with the given \a name is present.
145 \param[in] name Name of the option.
146 \return Whether the option is present.
149 IOFileTypeBase::hasOption(std::string const &name) const
151 return hasOption(_optStack.top(), name);
154 /*! Attempts to set option \a name to \a value.
155 If successful \c true is returned, false otherwise.
156 For the operation to succeed a \c boost::lexical_cast<> from the given type
157 has to succeed, usually that means an appropriate overload of
158 \c operator<< has to be available.
160 \param[in] name Name of the option.
161 \param[in] value Value of the option.
162 \return Whether the value was set successfully.
164 template <class ValueTypeT>
166 IOFileTypeBase::setOptionAs(
167 const std::string &name, const ValueTypeT &value)
169 return setOptionAs(_optStack.top(), name, value);
172 /*! Sets the option \a name to \a value overwriting any previous value.
174 \param[in] name Name of the option.
175 \param[in] value Value of the option.
178 IOFileTypeBase::setOption(const std::string &name, const std::string &value)
180 setOption(_optStack.top(), name, value);
183 /*! Removes the option \a name. If the option is not present
184 \c false is returned, \c true otherwise.
186 \param[in] name Name of the option.
187 \return Whether the option was successfully removed.
190 IOFileTypeBase::unsetOption(const std::string &name)
192 return unsetOption(_optStack.top(), name);
195 /*! Attempts to return the value associated with option \a name in \a value
196 as the requested type.
197 If the option is not present \c false is returned, \c true otherwise and
198 only in this case value is being set.
199 For the operation to succeed a \c boost::lexical_cast<> to the requested
200 type has to succeed, usually that means an appropriate overload of
201 \c operator>> has to be available.
203 \param[in] name Name of the option.
204 \param[out] value Value of option.
205 \return Whether the option is present.
207 template <class ValueTypeT>
209 IOFileTypeBase::getOptionAs(
210 const std::string &name, ValueTypeT &value) const
212 return getOptionAs(_optStack.top(), name, value);
215 /*! Attempts to return the \a value associated with option \a name.
216 If the option is not present \c false is returned,
217 \c true otherwise and only in this case value is being set.
219 \param[in] name Name of the option.
220 \param[out] value Value of option.
221 \return Whether the option is present.
224 IOFileTypeBase::getOption(const std::string &name, std::string &value) const
226 return getOption(_optStack.top(), name, value);