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
42  GetFileName(const char* path, char (&filename)[kMaxFileName]) NLIB_NOEXCEPT NLIB_NONNULL;
43  static errno_t
44  GetDirName(const char* path, 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*
80  GetStringFromEnv(UniquePtr<char[]>& buf, const char* varname, const char* defval) NLIB_NOEXCEPT;
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;
84 #endif
85 
86  private:
87  Nflags();
89 };
90 
91 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
92 template<class DUMMY>
93 #endif
94 inline const char* Nflags::GetStringFromEnv(UniquePtr<char[]>& buf, const char* varname,
95  const char* defval) NLIB_NOEXCEPT {
96  if (!varname) return defval;
97  size_t n;
98  errno_t e;
99  e = nlib_getenv(&n, nullptr, 0, varname);
100  if (e != 0 || n == 0) return defval;
101  // to avoid crossing the DLL boundary
102  char* bufmem = new (std::nothrow) char[n];
103  if (!bufmem) return defval;
104  size_t n2;
105  e = nlib_getenv(&n2, bufmem, n, varname);
106  if (e != 0) {
107  delete[] bufmem;
108  return defval;
109  }
110  buf.reset(bufmem);
111  return buf.get();
112 }
113 
114 #ifdef __cpp_rvalue_references
115 std::pair<const char*, std::unique_ptr<char[]> > inline Nflags::GetStringFromEnv(
116  const char* varname, const char* defval) NLIB_NOEXCEPT {
117  if (!varname) return std::make_pair(defval, nullptr);
118  size_t n;
119  errno_t e;
120  e = nlib_getenv(&n, nullptr, 0, varname);
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);
124  size_t n2;
125  e = nlib_getenv(&n2, mem.get(), n, varname);
126  if (e != 0) {
127  return std::make_pair(defval, nullptr);
128  }
129  return std::make_pair(mem.get(), std::move(mem));
130 }
131 #endif
132 
133 namespace nflags {
134 
135 NLIB_VIS_PUBLIC void AddDefOptBool(const char* opt_name, bool def_value, const char* desc,
136  bool* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
137 NLIB_VIS_PUBLIC void AddDefOptInt32(const char* opt_name, int32_t def_value, const char* desc,
138  int32_t* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
139 NLIB_VIS_PUBLIC void AddDefOptDouble(const char* opt_name, double def_value, const char* desc,
140  double* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
141 NLIB_VIS_PUBLIC void AddDefOptString(const char* opt_name, const char* def_value, const char* desc,
142  const char** ptr) NLIB_NOEXCEPT NLIB_NONNULL;
143 
144 } // namespace nflags
145 
146 #define NLIB_FLAGS_DEFINE_bool(opt_name, def_value, desc) \
147  bool NLIB_FLAGS_##opt_name; \
148  class NLIB_FLAGS_CL_##opt_name { \
149  public: \
150  NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \
151  ::nlib_ns::nflags::AddDefOptBool(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \
152  } \
153  } NLIB_FLAGS_CL_##opt_name##_
154 
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 { \
158  public: \
159  NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \
160  ::nlib_ns::nflags::AddDefOptInt32(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \
161  } \
162  } NLIB_FLAGS_CL_##opt_name##_
163 
164 #define NLIB_FLAGS_DEFINE_double(opt_name, def_value, desc) \
165  double NLIB_FLAGS_##opt_name; \
166  class NLIB_FLAGS_CL_##opt_name { \
167  public: \
168  NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \
169  ::nlib_ns::nflags::AddDefOptDouble(#opt_name, def_value, desc, \
170  &NLIB_FLAGS_##opt_name); \
171  } \
172  } NLIB_FLAGS_CL_##opt_name##_
173 
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 { \
177  public: \
178  NLIB_FLAGS_CL_##opt_name() NLIB_NOEXCEPT { \
179  ::nlib_ns::nflags::AddDefOptString(#opt_name, def_value, desc, \
180  &NLIB_FLAGS_##opt_name); \
181  } \
182  } NLIB_FLAGS_CL_##opt_name##_
183 
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
188 
189 NLIB_NAMESPACE_END
190 
191 // Predefined option:
192 // NLIB_FLAGS_help : set with '--help'
194 
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.
Definition: Nflags.h:66
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:183
static errno_t GetStringCommaList(char *arg, char *(&vec)[N], size_t *written_count) noexcept
A template overload of the above function.
Definition: Nflags.h:50
In the C++11 environment (which supports alias templates), std::unique_ptr is made an alias template...
Definition: UniquePtr.h:108
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:87
#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:184
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:109
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.
static errno_t GetInt32CommaList(char *arg, int32_t(&vec)[N], size_t *written_count) noexcept
A template overload of the above function.
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