1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// flags.h
2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Licensed under the Apache License, Version 2.0 (the "License");
4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// you may not use this file except in compliance with the License.
5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// You may obtain a copy of the License at
6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//      http://www.apache.org/licenses/LICENSE-2.0
8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Unless required by applicable law or agreed to in writing, software
10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// distributed under the License is distributed on an "AS IS" BASIS,
11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// See the License for the specific language governing permissions and
13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// limitations under the License.
14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Author: riley@google.com (Michael Riley)
16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// \file
18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Google-style flag handling declarations and inline definitions.
19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#ifndef FST_LIB_FLAGS_H__
21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define FST_LIB_FLAGS_H__
22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <iostream>
24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <map>
25dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin#include <set>
26dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin#include <sstream>
27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <string>
28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/types.h>
30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/lock.h>
31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonusing std::string;
33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// FLAGS USAGE:
36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Definition example:
38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//    DEFINE_int32(length, 0, "length");
40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// This defines variable FLAGS_length, initialized to 0.
42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Declaration example:
44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//    DECLARE_int32(length);
46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
47dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin// SET_FLAGS() can be used to set flags from the command line
48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// using, for example, '--length=2'.
49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// ShowUsage() can be used to print out command and flag usage.
51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DECLARE_bool(name) extern bool FLAGS_ ## name
54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DECLARE_string(name) extern string FLAGS_ ## name
55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DECLARE_int32(name) extern int32 FLAGS_ ## name
56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DECLARE_int64(name) extern int64 FLAGS_ ## name
57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DECLARE_double(name) extern double FLAGS_ ## name
58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <typename T>
60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct FlagDescription {
61dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  FlagDescription(T *addr, const char *doc, const char *type,
62dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin		  const char *file, const T val)
63dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      : address(addr),
64dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    doc_string(doc),
65dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    type_name(type),
66dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    file_name(file),
67dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    default_value(val) {}
68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  T *address;
70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const char *doc_string;
71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const char *type_name;
72dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  const char *file_name;
73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const T default_value;
74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <typename T>
77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass FlagRegister {
78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static FlagRegister<T> *GetRegister() {
80f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    fst::FstOnceInit(&register_init_, &FlagRegister<T>::Init);
81f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return register_;
82f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
84f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const FlagDescription<T> &GetFlagDescription(const string &name) const {
85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    fst::MutexLock l(register_lock_);
86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    typename std::map< string, FlagDescription<T> >::const_iterator it =
87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      flag_table_.find(name);
88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return it != flag_table_.end() ? it->second : 0;
89f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void SetDescription(const string &name,
91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                      const FlagDescription<T> &desc) {
92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    fst::MutexLock l(register_lock_);
93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    flag_table_.insert(make_pair(name, desc));
94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool SetFlag(const string &val, bool *address) const {
97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    if (val == "true" || val == "1" || val.empty()) {
98f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      *address = true;
99f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      return true;
100f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    } else if (val == "false" || val == "0") {
101f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      *address = false;
102f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      return true;
103f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
104f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    else {
105f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      return false;
106f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
107f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
108f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool SetFlag(const string &val, string *address) const {
109f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    *address = val;
110f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return true;
111f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
112f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool SetFlag(const string &val, int32 *address) const {
113f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    char *p = 0;
114f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    *address = strtol(val.c_str(), &p, 0);
115f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return !val.empty() && *p == '\0';
116f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
117f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool SetFlag(const string &val, int64 *address) const {
118f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    char *p = 0;
119f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    *address = strtoll(val.c_str(), &p, 0);
120f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return !val.empty() && *p == '\0';
121f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
122f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool SetFlag(const string &val, double *address) const {
123f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    char *p = 0;
124f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    *address = strtod(val.c_str(), &p);
125f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return !val.empty() && *p == '\0';
126f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
127f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
128f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool SetFlag(const string &arg, const string &val) const {
129dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    for (typename std::map< string, FlagDescription<T> >::const_iterator it =
130f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson           flag_table_.begin();
131f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson         it != flag_table_.end();
132f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson         ++it) {
133f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      const string &name = it->first;
134f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      const FlagDescription<T> &desc = it->second;
135f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      if (arg == name)
136f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson        return SetFlag(val, desc.address);
137f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
138f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return false;
139f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
140f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
141dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  void GetUsage(std::set< std::pair<string, string> > *usage_set) const {
142f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    for (typename std::map< string,
143f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson             FlagDescription<T> >::const_iterator it =
144f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson           flag_table_.begin();
145f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson         it != flag_table_.end();
146f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson         ++it) {
147f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      const string &name = it->first;
148f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      const FlagDescription<T> &desc = it->second;
149dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      string usage = "  --" + name;
150dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      usage += ": type = ";
151dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      usage += desc.type_name;
152dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      usage += ", default = ";
153dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      usage += GetDefault(desc.default_value) + "\n  ";
154dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      usage += desc.doc_string;
155dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin      usage_set->insert(make_pair(desc.file_name, usage));
156f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
157f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
158f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
159f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson private:
160f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static void Init() {
161f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    register_lock_ = new fst::Mutex;
162f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    register_ = new FlagRegister<T>;
163f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
164dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
165dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  std::map< string, FlagDescription<T> > flag_table_;
166dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
167dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  string GetDefault(bool default_value) const {
168dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    return default_value ? "true" : "false";
169dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  }
170dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
171dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  string GetDefault(const string &default_value) const {
172dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    return "\"" + default_value + "\"";
173dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  }
174dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
175dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  template<typename V> string GetDefault(const V& default_value) const {
176dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    std::ostringstream strm;
177dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    strm << default_value;
178dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    return strm.str();
179dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  }
180dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
181f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static fst::FstOnceType register_init_;   // ensures only called once
182f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static fst::Mutex* register_lock_;        // multithreading lock
183f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static FlagRegister<T> *register_;
184f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
185f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
186f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class T>
187f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonfst::FstOnceType FlagRegister<T>::register_init_ = fst::FST_ONCE_INIT;
188f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
189f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class T>
190f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonfst::Mutex *FlagRegister<T>::register_lock_ = 0;
191f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
192f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class T>
193f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonFlagRegister<T> *FlagRegister<T>::register_ = 0;
194f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
195f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
196f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <typename T>
197f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass FlagRegisterer {
198f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
199f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  FlagRegisterer(const string &name, const FlagDescription<T> &desc) {
200f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    FlagRegister<T> *registr = FlagRegister<T>::GetRegister();
201f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    registr->SetDescription(name, desc);
202f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
203f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
204f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson private:
205f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  DISALLOW_COPY_AND_ASSIGN(FlagRegisterer);
206f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
207f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
208f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
209f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DEFINE_VAR(type, name, value, doc)                                \
210f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  type FLAGS_ ## name = value;                                            \
211f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static FlagRegisterer<type>                                             \
212f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  name ## _flags_registerer(#name, FlagDescription<type>(&FLAGS_ ## name, \
213f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                                         doc,             \
214f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                                         #type,           \
215dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin                                                         __FILE__,        \
216f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                                         value))
217f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
218f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DEFINE_bool(name, value, doc) DEFINE_VAR(bool, name, value, doc)
219f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DEFINE_string(name, value, doc) \
220f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  DEFINE_VAR(string, name, value, doc)
221f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DEFINE_int32(name, value, doc) DEFINE_VAR(int32, name, value, doc)
222f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DEFINE_int64(name, value, doc) DEFINE_VAR(int64, name, value, doc)
223f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define DEFINE_double(name, value, doc) DEFINE_VAR(double, name, value, doc)
224f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
225f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
226f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Temporary directory
227f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDECLARE_string(tmpdir);
228f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
229dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkinvoid SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags,
230dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin              const char *src = "");
231dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
232dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin#define SET_FLAGS(usage, argc, argv, rmflags) \
233dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander GutkinSetFlags(usage, argc, argv, rmflags, __FILE__)
234f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
235f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Deprecated - for backward compatibility
236f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsoninline void InitFst(const char *usage, int *argc, char ***argv, bool rmflags) {
237f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  return SetFlags(usage, argc, argv, rmflags);
238f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}
239f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
240dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkinvoid ShowUsage(bool long_usage = true);
241f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
242f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#endif  // FST_LIB_FLAGS_H__
243