NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
options.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <nvbio/basic/types.h>
31 #include <nvbio/basic/console.h>
32 #include <stdlib.h>
33 #include <string>
34 
35 namespace nvbio {
36 
37 template <typename options_type>
38 bool bool_option(const options_type& options, const char* name, const bool val)
39 {
40  return ( (options.find( std::string(name) ) != options.end()) ?
41  atoi( options.find(std::string(name))->second.c_str() ) :
42  val ) ? true : false;
43 }
44 
45 template <typename options_type>
46 bool bool_option(const options_type& options, const char* name1, const char* name2, const bool val)
47 {
48  return (
49  (options.find( std::string(name1) ) != options.end()) ?
50  atoi( options.find(std::string(name1))->second.c_str() ) :
51  (options.find( std::string(name2) ) != options.end()) ?
52  atoi( options.find(std::string(name2))->second.c_str() ) :
53  val ) ? true : false;
54 }
55 
56 template <typename options_type>
57 uint32 uint_option(const options_type& options, const char* name, const uint32 val)
58 {
59  return (options.find( std::string(name) ) != options.end()) ?
60  atoi( options.find(std::string(name))->second.c_str() ) :
61  val;
62 }
63 
64 template <typename options_type>
65 uint32 uint_option(const options_type& options, const char* name1, const char* name2, const uint32 val)
66 {
67  return
68  (options.find( std::string(name1) ) != options.end()) ?
69  atoi( options.find(std::string(name1))->second.c_str() ) :
70  (options.find( std::string(name2) ) != options.end()) ?
71  atoi( options.find(std::string(name2))->second.c_str() ) :
72  val;
73 }
74 
75 template <typename options_type>
76 int32 int_option(const options_type& options, const char* name, const int32 val)
77 {
78  return (options.find( std::string(name) ) != options.end()) ?
79  atoi( options.find(std::string(name))->second.c_str() ) :
80  val;
81 }
82 
83 template <typename options_type>
84 int32 int_option(const options_type& options, const char* name1, const char* name2, const uint32 val)
85 {
86  return
87  (options.find( std::string(name1) ) != options.end()) ?
88  atoi( options.find(std::string(name1))->second.c_str() ) :
89  (options.find( std::string(name2) ) != options.end()) ?
90  atoi( options.find(std::string(name2))->second.c_str() ) :
91  val;
92 }
93 
94 template <typename options_type>
95 int64 int64_option(const options_type& options, const char* name, const int64 val)
96 {
97  return (options.find( std::string(name) ) != options.end()) ?
98  atoi( options.find(std::string(name))->second.c_str() ) :
99  val;
100 }
101 
102 template <typename options_type>
103 int64 int64_option(const options_type& options, const char* name1, const char* name2, const uint32 val)
104 {
105  return
106  (options.find( std::string(name1) ) != options.end()) ?
107  atoi( options.find(std::string(name1))->second.c_str() ) :
108  (options.find( std::string(name2) ) != options.end()) ?
109  atoi( options.find(std::string(name2))->second.c_str() ) :
110  val;
111 }
112 
113 template <typename options_type>
114 float float_option(const options_type& options, const char* name, const float val)
115 {
116  return (options.find( std::string(name) ) != options.end()) ?
117  (float)atof( options.find(std::string(name))->second.c_str() ) :
118  val;
119 }
120 
121 template <typename options_type>
122 float float_option(const options_type& options, const char* name1, const char* name2, const uint32 val)
123 {
124  return
125  (options.find( std::string(name1) ) != options.end()) ?
126  atof( options.find(std::string(name1))->second.c_str() ) :
127  (options.find( std::string(name2) ) != options.end()) ?
128  atof( options.find(std::string(name2))->second.c_str() ) :
129  val;
130 }
131 
132 template <typename options_type>
133 std::string string_option(const options_type& options, const char* name, const char* val)
134 {
135  return (options.find( std::string(name) ) != options.end()) ?
136  options.find(std::string(name))->second :
137  std::string( val );
138 }
139 
140 template <typename options_type>
141 std::string string_option(const options_type& options, const char* name1, const char* name2, const char* val)
142 {
143  return
144  (options.find( std::string(name1) ) != options.end()) ?
145  options.find(std::string(name1))->second :
146  (options.find( std::string(name2) ) != options.end()) ?
147  options.find(std::string(name2))->second :
148  std::string( val );
149 }
150 
151 template <typename options_type>
152 int2 int2_option(const options_type& options, const char* name, const int2 val)
153 {
154  if (options.find( std::string(name) ) != options.end())
155  {
156  const std::string str = options.find(std::string(name))->second;
157  const size_t c = str.find(',');
158  if (c == std::string::npos)
159  {
160  log_warning( stderr, "int2_option() : parsing error, missing comma\n" );
161  return val;
162  }
163  const std::string num1 = str.substr( 0, c );
164  const std::string num2 = str.substr( c + 1, str.size() );
165  return make_int2( atoi( num1.c_str() ), atoi( num2.c_str() ) );
166  }
167  return val;
168 }
169 
170 } // namespace nvbio