nlib
Nflags.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_NFLAGS_H_
17 #define INCLUDE_NN_NLIB_NFLAGS_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/UniquePtr.h"
21 
22 NLIB_NAMESPACE_BEGIN
23 
24 // You can define command line switches by using the macros:
25 // NLIB_FLAGS_DEFINE_[bool|int32|double|string](option_name, default value, "help message");
26 // Then Nflags::Parse() and you can access the specified value through NLIB_FLAGS_option_name.
27 // See misc/nflags/nflags.cpp sample codes for detailed usage.
29  public:
30  static errno_t Parse(int* argc, char*** argv) NLIB_NOEXCEPT NLIB_NONNULL;
31  static errno_t Parse(int* argc, wchar_t*** argv) NLIB_NOEXCEPT NLIB_NONNULL;
32  // use like this:
33  // if (NLIB_FLAGS_help) { Nflags::PrintHelp(); }
34  static void PrintHelp() NLIB_NOEXCEPT;
35  static const char* GetErrorMessage() NLIB_NOEXCEPT;
36 
37  static const int kMaxPath = 512;
38  static const int kMaxFileName = 256;
39  static const int kMaxDirName = 512;
40  static const int kMaxExtension = 256;
41  static errno_t GetFileName(const char* path,
42  char (&filename)[kMaxFileName]) NLIB_NOEXCEPT NLIB_NONNULL;
43  static errno_t GetDirName(const char* path,
44  char (&dirname)[kMaxDirName]) NLIB_NOEXCEPT NLIB_NONNULL;
45 
46  static errno_t GetStringCommaList(char* arg, char** vec, size_t vec_count,
47  size_t* written_count) NLIB_NOEXCEPT NLIB_NONNULL;
48  template <size_t N>
49  static errno_t
50  GetStringCommaList(char* arg, char* (&vec)[N], size_t* written_count) NLIB_NOEXCEPT {
51  return GetStringCommaList(arg, &vec[0], N, written_count);
52  }
53 
54  static errno_t GetInt32CommaList(char* arg, int32_t* vec, size_t vec_count,
55  size_t* written_count) NLIB_NOEXCEPT NLIB_NONNULL;
56  template <size_t N>
57  static errno_t
58  GetInt32CommaList(char* arg, int32_t (&vec)[N], size_t* written_count) NLIB_NOEXCEPT {
59  return GetInt32CommaList(arg, &vec[0], N, written_count);
60  }
61 
62  static errno_t GetDoubleCommaList(char* arg, double* vec, size_t vec_count,
63  size_t* written_count) NLIB_NOEXCEPT NLIB_NONNULL;
64  template <size_t N>
65  static errno_t
66  GetDoubleCommaList(char* arg, double (&vec)[N], size_t* written_count) NLIB_NOEXCEPT {
67  return GetDoubleCommaList(arg, &vec[0], N, written_count);
68  }
69 
70  // 1,t,y,yes,true -> true / 0,f,n,no,false -> false / otherwise -> defval
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;
75  // do not delete the return value.
76 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
77  template<class DUMMY = void>
78 #endif
79  static const char* GetStringFromEnv(UniquePtr<char[]>& buf, // NOLINT
80  const char* varname,
81  const char* defval) NLIB_NOEXCEPT;
82 
83  private:
84  Nflags();
85 };
86 
87 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
88 template<class DUMMY>
89 #endif
90 inline const char* Nflags::GetStringFromEnv(UniquePtr<char[]>& buf, // NOLINT
91  const char* varname,
92  const char* defval) NLIB_NOEXCEPT {
93  if (!varname) return defval;
94  size_t n;
95  errno_t e;
96  e = nlib_getenv(&n, NULL, 0, varname);
97  if (e != 0 || n == 0) return defval;
98  // to avoid crossing the DLL boundary
99  char* bufmem = new (std::nothrow) char[n];
100  if (!bufmem) return defval;
101  size_t n2;
102  e = nlib_getenv(&n2, bufmem, n, varname);
103  if (e != 0) {
104  delete[] bufmem;
105  return defval;
106  }
107  buf.reset(bufmem);
108  return buf.get();
109 }
110 
111 namespace nflags {
112 
113 NLIB_VIS_PUBLIC void AddDefOptBool(const char* opt_name, bool def_value, const char* desc,
114  bool* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
115 NLIB_VIS_PUBLIC void AddDefOptInt32(const char* opt_name, int32_t def_value, const char* desc,
116  int32_t* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
117 NLIB_VIS_PUBLIC void AddDefOptDouble(const char* opt_name, double def_value, const char* desc,
118  double* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
119 NLIB_VIS_PUBLIC void AddDefOptString(const char* opt_name, const char* def_value, const char* desc,
120  const char** ptr) NLIB_NOEXCEPT NLIB_NONNULL;
121 
122 } // namespace nflags
123 
124 #define NLIB_FLAGS_DEFINE_bool(opt_name, def_value, desc) \
125 bool NLIB_FLAGS_##opt_name; \
126 class NLIB_FLAGS_CL_##opt_name { \
127  public: /* NOLINT */ \
128  NLIB_FLAGS_CL_##opt_name() { \
129  ::nlib_ns::nflags::AddDefOptBool(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \
130  } \
131 } NLIB_FLAGS_CL_##opt_name##_
132 
133 #define NLIB_FLAGS_DEFINE_int32(opt_name, def_value, desc) \
134 int32_t NLIB_FLAGS_##opt_name; \
135 class NLIB_FLAGS_CL_##opt_name { \
136  public: /* NOLINT */ \
137  NLIB_FLAGS_CL_##opt_name() { \
138  ::nlib_ns::nflags::AddDefOptInt32(#opt_name, def_value, desc, \
139  &NLIB_FLAGS_##opt_name); \
140  } \
141 } NLIB_FLAGS_CL_##opt_name##_
142 
143 #define NLIB_FLAGS_DEFINE_double(opt_name, def_value, desc) \
144 double NLIB_FLAGS_##opt_name; \
145 class NLIB_FLAGS_CL_##opt_name { \
146  public: /* NOLINT */ \
147  NLIB_FLAGS_CL_##opt_name() { \
148  ::nlib_ns::nflags::AddDefOptDouble(#opt_name, def_value, desc, \
149  &NLIB_FLAGS_##opt_name); \
150  } \
151 } NLIB_FLAGS_CL_##opt_name##_
152 
153 #define NLIB_FLAGS_DEFINE_string(opt_name, def_value, desc) \
154 const char* NLIB_FLAGS_##opt_name; \
155 class NLIB_FLAGS_CL_##opt_name { \
156  public: /* NOLINT */ \
157  NLIB_FLAGS_CL_##opt_name() { \
158  ::nlib_ns::nflags::AddDefOptString(#opt_name, def_value, desc, \
159  &NLIB_FLAGS_##opt_name); \
160  } \
161 } NLIB_FLAGS_CL_##opt_name##_
162 
163 #define NLIB_FLAGS_DECLARE_bool(opt_name) extern bool NLIB_FLAGS_##opt_name
164 #define NLIB_FLAGS_DECLARE_int32(opt_name) extern int32_t NLIB_FLAGS_##opt_name
165 #define NLIB_FLAGS_DECLARE_double(opt_name) extern double NLIB_FLAGS_##opt_name
166 #define NLIB_FLAGS_DECLARE_string(opt_name) extern const char* NLIB_FLAGS_##opt_name
167 
168 NLIB_NAMESPACE_END
169 
170 // Predefined option:
171 // NLIB_FLAGS_help : set with '--help'
173 
174 #endif // INCLUDE_NN_NLIB_NFLAGS_H_
static errno_t GetDoubleCommaList(char *arg, double(&vec)[N], size_t *written_count) noexcept
The function template version of GetDoubleCommaList.
Definition: Nflags.h:66
static errno_t GetStringCommaList(char *arg, char *(&vec)[N], size_t *written_count) noexcept
The function template version of GetStringCommaList.
Definition: Nflags.h:50
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:109
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
#define NLIB_FLAGS_DECLARE_bool(opt_name)
Enables the use of an NLIB_FLAGS_option name that was defined in a different place.
Definition: Nflags.h:163
The class for parsing command line flags.
Definition: Nflags.h:28
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
A file that contains the configuration information for each development environment.
errno_t nlib_getenv(size_t *result, char *buf, size_t bufsize, const char *varname)
Gets the value for the environment variable as a string.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
static errno_t GetInt32CommaList(char *arg, int32_t(&vec)[N], size_t *written_count) noexcept
The function template version of GetInt32CommaList.
Definition: Nflags.h:58
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37