1212#include < boost/any.hpp>
1313#include < boost/function/function1.hpp>
1414#include < boost/lexical_cast.hpp>
15+ #include < boost/smart_ptr/enable_shared_from_this.hpp>
1516
1617#include < string>
1718#include < vector>
@@ -178,7 +179,8 @@ namespace boost { namespace program_options {
178179
179180 /* * Class which handles value of a specific type. */
180181 template <class T , class charT = char >
181- class typed_value : public value_semantic_codecvt_helper <charT>
182+ class typed_value : public enable_shared_from_this <typed_value<T, charT>>,
183+ public value_semantic_codecvt_helper<charT>
182184#ifndef BOOST_NO_RTTI
183185 , public typed_value_base
184186#endif
@@ -196,11 +198,11 @@ namespace boost { namespace program_options {
196198 if none is explicitly specified. The type 'T' should
197199 provide operator<< for ostream.
198200 */
199- typed_value* default_value (const T& v)
201+ shared_ptr< typed_value> default_value (const T& v)
200202 {
201203 m_default_value = boost::any (v);
202204 m_default_value_as_text = boost::lexical_cast<std::string>(v);
203- return this ;
205+ return this -> shared_from_this () ;
204206 }
205207
206208 /* * Specifies default value, which will be used
@@ -209,30 +211,30 @@ namespace boost { namespace program_options {
209211 but textual representation of default value must be provided
210212 by the user.
211213 */
212- typed_value* default_value (const T& v, const std::string& textual)
214+ shared_ptr< typed_value> default_value (const T& v, const std::string& textual)
213215 {
214216 m_default_value = boost::any (v);
215217 m_default_value_as_text = textual;
216- return this ;
218+ return this -> shared_from_this () ;
217219 }
218220
219221 /* * Specifies an implicit value, which will be used
220222 if the option is given, but without an adjacent value.
221223 Using this implies that an explicit value is optional,
222224 */
223- typed_value* implicit_value (const T &v)
225+ shared_ptr< typed_value> implicit_value (const T &v)
224226 {
225227 m_implicit_value = boost::any (v);
226228 m_implicit_value_as_text =
227229 boost::lexical_cast<std::string>(v);
228- return this ;
230+ return this -> shared_from_this () ;
229231 }
230232
231233 /* * Specifies the name used to to the value in help message. */
232- typed_value* value_name (const std::string& name)
234+ shared_ptr< typed_value> value_name (const std::string& name)
233235 {
234236 m_value_name = name;
235- return this ;
237+ return this -> shared_from_this () ;
236238 }
237239
238240 /* * Specifies an implicit value, which will be used
@@ -245,36 +247,36 @@ namespace boost { namespace program_options {
245247 operator<< for ostream, but textual representation of default
246248 value must be provided by the user.
247249 */
248- typed_value* implicit_value (const T &v, const std::string& textual)
250+ shared_ptr< typed_value> implicit_value (const T &v, const std::string& textual)
249251 {
250252 m_implicit_value = boost::any (v);
251253 m_implicit_value_as_text = textual;
252- return this ;
254+ return this -> shared_from_this () ;
253255 }
254256
255257 /* * Specifies a function to be called when the final value
256258 is determined. */
257- typed_value* notifier (function1<void , const T&> f)
259+ shared_ptr< typed_value> notifier (function1<void , const T&> f)
258260 {
259261 m_notifier = f;
260- return this ;
262+ return this -> shared_from_this () ;
261263 }
262264
263265 /* * Specifies that the value is composing. See the 'is_composing'
264266 method for explanation.
265267 */
266- typed_value* composing ()
268+ shared_ptr< typed_value> composing ()
267269 {
268270 m_composing = true ;
269- return this ;
271+ return this -> shared_from_this () ;
270272 }
271273
272274 /* * Specifies that the value can span multiple tokens.
273275 */
274- typed_value* multitoken ()
276+ shared_ptr< typed_value> multitoken ()
275277 {
276278 m_multitoken = true ;
277- return this ;
279+ return this -> shared_from_this () ;
278280 }
279281
280282 /* * Specifies that no tokens may be provided as the value of
@@ -284,17 +286,17 @@ namespace boost { namespace program_options {
284286 'implicit_value' method should be also used. In most
285287 cases, you can use the 'bool_switch' function instead of
286288 using this method. */
287- typed_value* zero_tokens ()
289+ shared_ptr< typed_value> zero_tokens ()
288290 {
289291 m_zero_tokens = true ;
290- return this ;
292+ return this -> shared_from_this () ;
291293 }
292294
293295 /* * Specifies that the value must occur. */
294- typed_value* required ()
296+ shared_ptr< typed_value> required ()
295297 {
296298 m_required = true ;
297- return this ;
299+ return this -> shared_from_this () ;
298300 }
299301
300302 public: // value semantic overrides
@@ -381,39 +383,39 @@ namespace boost { namespace program_options {
381383 value of option into program variable.
382384 */
383385 template <class T >
384- typed_value<T>*
386+ shared_ptr< typed_value<T>>
385387 value ();
386388
387389 /* * @overload
388390 */
389391 template <class T >
390- typed_value<T>*
392+ shared_ptr< typed_value<T>>
391393 value (T* v);
392394
393395 /* * Creates a typed_value<T> instance. This function is the primary
394396 method to create value_semantic instance for a specific type, which
395397 can later be passed to 'option_description' constructor.
396398 */
397399 template <class T >
398- typed_value<T, wchar_t >*
400+ shared_ptr< typed_value<T, wchar_t >>
399401 wvalue ();
400402
401403 /* * @overload
402404 */
403405 template <class T >
404- typed_value<T, wchar_t >*
406+ shared_ptr< typed_value<T, wchar_t >>
405407 wvalue (T* v);
406408
407409 /* * Works the same way as the 'value<bool>' function, but the created
408410 value_semantic won't accept any explicit value. So, if the option
409411 is present on the command line, the value will be 'true'.
410412 */
411- BOOST_PROGRAM_OPTIONS_DECL typed_value<bool >*
413+ BOOST_PROGRAM_OPTIONS_DECL shared_ptr< typed_value<bool >>
412414 bool_switch ();
413415
414416 /* * @overload
415417 */
416- BOOST_PROGRAM_OPTIONS_DECL typed_value<bool >*
418+ BOOST_PROGRAM_OPTIONS_DECL shared_ptr< typed_value<bool >>
417419 bool_switch (bool * v);
418420
419421}}
0 commit comments