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();
86 };
87 
88 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
89 template<class DUMMY>
90 #endif
91 inline const char* Nflags::GetStringFromEnv(UniquePtr<char[]>& buf, // NOLINT
92  const char* varname,
93  const char* defval) NLIB_NOEXCEPT {
94  if (!varname) return defval;
95  size_t n;
96  errno_t e;
97  e = nlib_getenv(&n, nullptr, 0, varname);
98  if (e != 0 || n == 0) return defval;
99  // to avoid crossing the DLL boundary
100  char* bufmem = new (std::nothrow) char[n];
101  if (!bufmem) return defval;
102  size_t n2;
103  e = nlib_getenv(&n2, bufmem, n, varname);
104  if (e != 0) {
105  delete[] bufmem;
106  return defval;
107  }
108  buf.reset(bufmem);
109  return buf.get();
110 }
111 
112 namespace nflags {
113 
114 NLIB_VIS_PUBLIC void AddDefOptBool(const char* opt_name, bool def_value, const char* desc,
115  bool* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
116 NLIB_VIS_PUBLIC void AddDefOptInt32(const char* opt_name, int32_t def_value, const char* desc,
117  int32_t* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
118 NLIB_VIS_PUBLIC void AddDefOptDouble(const char* opt_name, double def_value, const char* desc,
119  double* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
120 NLIB_VIS_PUBLIC void AddDefOptString(const char* opt_name, const char* def_value, const char* desc,
121  const char** ptr) NLIB_NOEXCEPT NLIB_NONNULL;
122 
123 } // namespace nflags
124 
125 #define NLIB_FLAGS_DEFINE_bool(opt_name, def_value, desc) \
126 bool NLIB_FLAGS_##opt_name; \
127 class NLIB_FLAGS_CL_##opt_name { \
128  public: /* NOLINT */ \
129  NLIB_FLAGS_CL_##opt_name() { \
130  ::nlib_ns::nflags::AddDefOptBool(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \
131  } \
132 } NLIB_FLAGS_CL_##opt_name##_
133 
134 #define NLIB_FLAGS_DEFINE_int32(opt_name, def_value, desc) \
135 int32_t NLIB_FLAGS_##opt_name; \
136 class NLIB_FLAGS_CL_##opt_name { \
137  public: /* NOLINT */ \
138  NLIB_FLAGS_CL_##opt_name() { \
139  ::nlib_ns::nflags::AddDefOptInt32(#opt_name, def_value, desc, \
140  &NLIB_FLAGS_##opt_name); \
141  } \
142 } NLIB_FLAGS_CL_##opt_name##_
143 
144 #define NLIB_FLAGS_DEFINE_double(opt_name, def_value, desc) \
145 double NLIB_FLAGS_##opt_name; \
146 class NLIB_FLAGS_CL_##opt_name { \
147  public: /* NOLINT */ \
148  NLIB_FLAGS_CL_##opt_name() { \
149  ::nlib_ns::nflags::AddDefOptDouble(#opt_name, def_value, desc, \
150  &NLIB_FLAGS_##opt_name); \
151  } \
152 } NLIB_FLAGS_CL_##opt_name##_
153 
154 #define NLIB_FLAGS_DEFINE_string(opt_name, def_value, desc) \
155 const char* NLIB_FLAGS_##opt_name; \
156 class NLIB_FLAGS_CL_##opt_name { \
157  public: /* NOLINT */ \
158  NLIB_FLAGS_CL_##opt_name() { \
159  ::nlib_ns::nflags::AddDefOptString(#opt_name, def_value, desc, \
160  &NLIB_FLAGS_##opt_name); \
161  } \
162 } NLIB_FLAGS_CL_##opt_name##_
163 
164 #define NLIB_FLAGS_DECLARE_bool(opt_name) extern bool NLIB_FLAGS_##opt_name
165 #define NLIB_FLAGS_DECLARE_int32(opt_name) extern int32_t NLIB_FLAGS_##opt_name
166 #define NLIB_FLAGS_DECLARE_double(opt_name) extern double NLIB_FLAGS_##opt_name
167 #define NLIB_FLAGS_DECLARE_string(opt_name) extern const char* NLIB_FLAGS_##opt_name
168 
169 NLIB_NAMESPACE_END
170 
171 // Predefined option:
172 // NLIB_FLAGS_help : set with '--help'
174 
175 #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
#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:179
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:164
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:105
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
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