simple-options.cc
Go to the documentation of this file.
1 // util/simple-options.cc
2 
3 // Copyright 2013 Tanel Alumae, Tallinn University of Technology
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "util/simple-options.h"
22 
23 
24 namespace kaldi {
25 
26 void SimpleOptions::Register(const std::string &name,
27  bool *value,
28  const std::string &doc) {
29  bool_map_[name] = value;
30  option_info_list_.push_back(std::make_pair(name, OptionInfo(doc, kBool)));
31 }
32 
33 void SimpleOptions::Register(const std::string &name,
34  int32 *value,
35  const std::string &doc) {
36  int_map_[name] = value;
37  option_info_list_.push_back(std::make_pair(name, OptionInfo(doc, kInt32)));
38 }
39 
40 void SimpleOptions::Register(const std::string &name,
41  uint32 *value,
42  const std::string &doc) {
43  uint_map_[name] = value;
44  option_info_list_.push_back(std::make_pair(name, OptionInfo(doc, kUint32)));
45 }
46 
47 void SimpleOptions::Register(const std::string &name,
48  float *value,
49  const std::string &doc) {
50  float_map_[name] = value;
51  option_info_list_.push_back(std::make_pair(name, OptionInfo(doc, kFloat)));
52 }
53 
54 void SimpleOptions::Register(const std::string &name,
55  double *value,
56  const std::string &doc) {
57  double_map_[name] = value;
58  option_info_list_.push_back(std::make_pair(name, OptionInfo(doc, kDouble)));
59 }
60 
61 void SimpleOptions::Register(const std::string &name,
62  std::string *value,
63  const std::string &doc) {
64  string_map_[name] = value;
65  option_info_list_.push_back(std::make_pair(name, OptionInfo(doc, kString)));
66 }
67 
68 template<typename T>
69 static bool SetOptionImpl(const std::string &key, const T &value,
70  std::map<std::string, T*> &some_map) {
71  if (some_map.end() != some_map.find(key)) {
72  *(some_map[key]) = value;
73  return true;
74  }
75  return false;
76 }
77 
78 bool SimpleOptions::SetOption(const std::string &key, const bool &value) {
79  return SetOptionImpl(key, value, bool_map_);
80 }
81 
82 bool SimpleOptions::SetOption(const std::string &key, const int32 &value) {
83  if (!SetOptionImpl(key, value, int_map_)) {
84  if (!SetOptionImpl(key, static_cast<uint32>(value), uint_map_)) {
85  return false;
86  }
87  }
88  return true;
89 }
90 
91 bool SimpleOptions::SetOption(const std::string &key, const uint32 &value) {
92  if (!SetOptionImpl(key, value, uint_map_)) {
93  if (!SetOptionImpl(key, static_cast<int32>(value), int_map_)) {
94  return false;
95  }
96  }
97  return true;
98 }
99 
100 bool SimpleOptions::SetOption(const std::string &key, const float &value) {
101  if (!SetOptionImpl(key, value, float_map_)) {
102  if (!SetOptionImpl(key, static_cast<double>(value), double_map_)) {
103  return false;
104  }
105  }
106  return true;
107 }
108 
109 bool SimpleOptions::SetOption(const std::string &key, const double &value) {
110  if (!SetOptionImpl(key, value, double_map_)) {
111  if (!SetOptionImpl(key, static_cast<float>(value), float_map_)) {
112  return false;
113  }
114  }
115  return true;
116 }
117 
118 bool SimpleOptions::SetOption(const std::string &key,
119  const std::string &value) {
120  return SetOptionImpl(key, value, string_map_);
121 }
122 
123 bool SimpleOptions::SetOption(const std::string &key, const char *value) {
124  std::string str_value = std::string(value);
125  return SetOptionImpl(key, str_value, string_map_);
126 }
127 
128 
129 template<typename T>
130 static bool GetOptionImpl(const std::string &key, T *value,
131  std::map<std::string, T*> &some_map) {
132  typename std::map<std::string, T*>::iterator it = some_map.find(key);
133  if (it != some_map.end()) {
134  *value = *(it->second);
135  return true;
136  }
137  return false;
138 }
139 
140 bool SimpleOptions::GetOption(const std::string &key, bool *value) {
141  return GetOptionImpl(key, value, bool_map_);
142 }
143 
144 bool SimpleOptions::GetOption(const std::string &key, int32 *value) {
145  return GetOptionImpl(key, value, int_map_);
146 }
147 
148 bool SimpleOptions::GetOption(const std::string &key, uint32 *value) {
149  return GetOptionImpl(key, value, uint_map_);
150 }
151 
152 bool SimpleOptions::GetOption(const std::string &key, float *value) {
153  return GetOptionImpl(key, value, float_map_);
154 }
155 
156 bool SimpleOptions::GetOption(const std::string &key, double *value) {
157  return GetOptionImpl(key, value, double_map_);
158 }
159 
160 bool SimpleOptions::GetOption(const std::string &key, std::string *value) {
161  return GetOptionImpl(key, value, string_map_);
162 }
163 
164 std::vector<std::pair<std::string, SimpleOptions::OptionInfo> >
166  return option_info_list_;
167 }
168 
169 bool SimpleOptions::GetOptionType(const std::string &key, OptionType *type) {
170  for (std::vector <std::pair<std::string,
171  OptionInfo> >::iterator dx = option_info_list_.begin();
172  dx != option_info_list_.end(); dx++) {
173  std::pair<std::string, SimpleOptions::OptionInfo> info_pair = (*dx);
174  if (info_pair.first == key) {
175  *type = info_pair.second.type;
176  return true;
177  }
178  }
179  return false;
180 }
181 
182 
183 
184 } // namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
kaldi::int32 int32
std::map< std::string, bool * > bool_map_
bool GetOptionType(const std::string &key, OptionType *type)
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::vector< std::pair< std::string, OptionInfo > > GetOptionInfoList()
std::map< std::string, int32 * > int_map_
static bool SetOptionImpl(const std::string &key, const T &value, std::map< std::string, T *> &some_map)
std::map< std::string, uint32 * > uint_map_
std::map< std::string, float * > float_map_
bool SetOption(const std::string &key, const bool &value)
static bool GetOptionImpl(const std::string &key, T *value, std::map< std::string, T *> &some_map)
std::map< std::string, double * > double_map_
bool GetOption(const std::string &key, bool *value)
std::vector< std::pair< std::string, OptionInfo > > option_info_list_
std::map< std::string, std::string * > string_map_