16 #ifndef INCLUDE_NN_NLIB_NFLAGS_H_ 17 #define INCLUDE_NN_NLIB_NFLAGS_H_ 37 static const int kMaxPath = 512;
38 static const int kMaxFileName = 256;
39 static const int kMaxDirName = 512;
40 static const int kMaxExtension = 256;
46 static errno_t GetStringCommaList(
char* arg,
char** vec,
size_t vec_count,
51 return GetStringCommaList(arg, &vec[0], N, written_count);
54 static errno_t GetInt32CommaList(
char* arg, int32_t* vec,
size_t vec_count,
59 return GetInt32CommaList(arg, &vec[0], N, written_count);
62 static errno_t GetDoubleCommaList(
char* arg,
double* vec,
size_t vec_count,
67 return GetDoubleCommaList(arg, &vec[0], N, written_count);
71 static bool GetBoolFromEnv(
const char* varname,
bool defval)
NLIB_NOEXCEPT;
72 static int32_t GetInt32FromEnv(
const char* varname, int32_t defval)
NLIB_NOEXCEPT;
73 static int64_t GetInt64FromEnv(
const char* varname, int64_t defval)
NLIB_NOEXCEPT;
74 static double GetDoubleFromEnv(
const char* varname,
double defval)
NLIB_NOEXCEPT;
76 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 77 template<
class DUMMY =
void>
81 #ifdef __cpp_rvalue_references 82 static std::pair<const char*, std::unique_ptr<char[]> >
83 GetStringFromEnv(
const char* varname,
const char* defval)
NLIB_NOEXCEPT;
91 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 94 inline const char* Nflags::GetStringFromEnv(
UniquePtr<
char[]>& buf,
const char* varname,
96 if (!varname)
return defval;
100 if (e != 0 || n == 0)
return defval;
102 char* bufmem =
new (std::nothrow)
char[n];
103 if (!bufmem)
return defval;
114 #ifdef __cpp_rvalue_references 115 std::pair<const char*, std::unique_ptr<char[]> >
inline Nflags::GetStringFromEnv(
117 if (!varname)
return std::make_pair(defval,
nullptr);
121 if (e != 0 || n == 0)
return std::make_pair(defval,
nullptr);
122 std::unique_ptr<char[]> mem(
new (std::nothrow)
char[n]);
123 if (!mem)
return std::make_pair(defval,
nullptr);
127 return std::make_pair(defval,
nullptr);
129 return std::make_pair(mem.get(), std::move(mem));
135 NLIB_VIS_PUBLIC void AddDefOptBool(
const char* opt_name,
bool def_value,
const char* desc,
137 NLIB_VIS_PUBLIC void AddDefOptInt32(
const char* opt_name, int32_t def_value,
const char* desc,
139 NLIB_VIS_PUBLIC void AddDefOptDouble(
const char* opt_name,
double def_value,
const char* desc,
141 NLIB_VIS_PUBLIC void AddDefOptString(
const char* opt_name,
const char* def_value,
const char* desc,
146 #define NLIB_FLAGS_DEFINE_bool(opt_name, def_value, desc) \ 147 bool NLIB_FLAGS_##opt_name; \ 148 class NLIB_FLAGS_CL_##opt_name { \ 150 NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \ 151 ::nlib_ns::nflags::AddDefOptBool(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \ 153 } NLIB_FLAGS_CL_##opt_name##_ 155 #define NLIB_FLAGS_DEFINE_int32(opt_name, def_value, desc) \ 156 int32_t NLIB_FLAGS_##opt_name; \ 157 class NLIB_FLAGS_CL_##opt_name { \ 159 NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \ 160 ::nlib_ns::nflags::AddDefOptInt32(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \ 162 } NLIB_FLAGS_CL_##opt_name##_ 164 #define NLIB_FLAGS_DEFINE_double(opt_name, def_value, desc) \ 165 double NLIB_FLAGS_##opt_name; \ 166 class NLIB_FLAGS_CL_##opt_name { \ 168 NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \ 169 ::nlib_ns::nflags::AddDefOptDouble(#opt_name, def_value, desc, \ 170 &NLIB_FLAGS_##opt_name); \ 172 } NLIB_FLAGS_CL_##opt_name##_ 174 #define NLIB_FLAGS_DEFINE_string(opt_name, def_value, desc) \ 175 const char* NLIB_FLAGS_##opt_name; \ 176 class NLIB_FLAGS_CL_##opt_name { \ 178 NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \ 179 ::nlib_ns::nflags::AddDefOptString(#opt_name, def_value, desc, \ 180 &NLIB_FLAGS_##opt_name); \ 182 } NLIB_FLAGS_CL_##opt_name##_ 184 #define NLIB_FLAGS_DECLARE_bool(opt_name) extern bool NLIB_FLAGS_##opt_name 185 #define NLIB_FLAGS_DECLARE_int32(opt_name) extern int32_t NLIB_FLAGS_##opt_name 186 #define NLIB_FLAGS_DECLARE_double(opt_name) extern double NLIB_FLAGS_##opt_name 187 #define NLIB_FLAGS_DECLARE_string(opt_name) extern const char* NLIB_FLAGS_##opt_name 195 #endif // INCLUDE_NN_NLIB_NFLAGS_H_ static errno_t GetDoubleCommaList(char *arg, double(&vec)[N], size_t *written_count) noexcept
A template overload of the above function.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
static errno_t GetStringCommaList(char *arg, char *(&vec)[N], size_t *written_count) noexcept
A template overload of the above function.
In the C++11 environment (which supports alias templates), std::unique_ptr is made an alias template...
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_FLAGS_DECLARE_bool(opt_name)
Enables the use of an NLIB_FLAGS_option name that was defined in a different place.
The class for parsing command line flags.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
A file that contains the configuration information for each development environment.
static errno_t GetInt32CommaList(char *arg, int32_t(&vec)[N], size_t *written_count) noexcept
A template overload of the above function.