1 // Copyright (C) 2012  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #undef DLIB_GET_OPTiON_ABSTRACT_Hh_
4 #ifdef DLIB_GET_OPTiON_ABSTRACT_Hh_
5 
6 #inclue <string>
7 
8 namespace dlib
9 {
10 
11 // ----------------------------------------------------------------------------------------
12 
13     class option_parse_error : public error
14     {
15         /*!
16             WHAT THIS OBJECT REPRESENTS
17                 This is the exception thrown by the get_option() functions.  It is
18                 thrown when the option string given by a command line parser or
19                 config reader can't be converted into the type T.
20         !*/
21     };
22 
23 // ----------------------------------------------------------------------------------------
24 
25     template <
26         typename config_reader_type,
27         typename T
28         >
29     T get_option (
30         const config_reader_type& cr,
31         const std::string& option_name,
32         T default_value
33     );
34     /*!
35         requires
36             - T is a type which can be read from an input stream
37             - config_reader_type == an implementation of config_reader/config_reader_kernel_abstract.h
38         ensures
39             - option_name is used to index into the given config_reader.
40             - if (cr contains an entry corresponding to option_name) then
41                 - converts the string value in cr corresponding to option_name into
42                   an object of type T and returns it.
43             - else
44                 - returns default_value
45             - The scheme for indexing into cr based on option_name is best
46               understood by looking at a few examples:
47                 - an option name of "name" corresponds to cr["name"]
48                 - an option name of "block1.name" corresponds to cr.block("block1")["name"]
49                 - an option name of "block1.block2.name" corresponds to cr.block("block1").block("block2")["name"]
50         throws
51             - option_parse_error
52               This exception is thrown if we attempt but fail to convert the string value
53               in cr into an object of type T.
54     !*/
55 
56 // ----------------------------------------------------------------------------------------
57 
58     template <
59         typename command_line_parser_type,
60         typename T
61         >
62     T get_option (
63         const command_line_parser_type& parser,
64         const std::string& option_name,
65         T default_value
66     );
67     /*!
68         requires
69             - parser.option_is_defined(option_name) == true
70             - parser.option(option_name).number_of_arguments() == 1
71             - T is a type which can be read from an input stream
72             - command_line_parser_type == an implementation of cmd_line_parser/cmd_line_parser_kernel_abstract.h
73         ensures
74             - if (parser.option(option_name)) then
75                 - converts parser.option(option_name).argument() into an object
76                   of type T and returns it.  That is, the string argument to this
77                   command line option is converted into a T and returned.
78             - else
79                 - returns default_value
80         throws
81             - option_parse_error
82               This exception is thrown if we attempt but fail to convert the string
83               argument into an object of type T.
84     !*/
85 
86 // ----------------------------------------------------------------------------------------
87 
88     template <
89         typename command_line_parser_type,
90         typename config_reader_type,
91         typename T
92         >
93     T get_option (
94         const command_line_parser_type& parser,
95         const config_reader_type& cr,
96         const std::string& option_name,
97         T default_value
98     );
99     /*!
100         requires
101             - parser.option_is_defined(option_name) == true
102             - parser.option(option_name).number_of_arguments() == 1
103             - T is a type which can be read from an input stream
104             - command_line_parser_type == an implementation of cmd_line_parser/cmd_line_parser_kernel_abstract.h
105             - config_reader_type == an implementation of config_reader/config_reader_kernel_abstract.h
106         ensures
107             - if (parser.option(option_name)) then
108                 - returns get_option(parser, option_name, default_value)
109             - else
110                 - returns get_option(cr, option_name, default_value)
111     !*/
112 
113 // ----------------------------------------------------------------------------------------
114 
115     template <
116         typename command_line_parser_type,
117         typename config_reader_type,
118         typename T
119         >
120     T get_option (
121         const config_reader_type& cr,
122         const command_line_parser_type& parser,
123         const std::string& option_name,
124         T default_value
125     );
126     /*!
127         requires
128             - parser.option_is_defined(option_name) == true
129             - parser.option(option_name).number_of_arguments() == 1
130             - T is a type which can be read from an input stream
131             - command_line_parser_type == an implementation of cmd_line_parser/cmd_line_parser_kernel_abstract.h
132             - config_reader_type == an implementation of config_reader/config_reader_kernel_abstract.h
133         ensures
134             - if (parser.option(option_name)) then
135                 - returns get_option(parser, option_name, default_value)
136             - else
137                 - returns get_option(cr, option_name, default_value)
138     !*/
139 
140 // ----------------------------------------------------------------------------------------
141 
142 }
143 
144 #endif // DLIB_GET_OPTiON_ABSTRACT_Hh_
145 
146 
147