libcopp  1.2.0
cmd_option_value.h
Go to the documentation of this file.
1 #ifndef UTIL_CLI_CMDOPTIONVALUE_H
2 #define UTIL_CLI_CMDOPTIONVALUE_H
3 
4 #pragma once
5 
6 /*
7  * cmd_option_value.h
8  *
9  * Created on: 2011-12-29
10  * Author: OWenT
11  *
12  * 应用程序命令处理
13  *
14  */
15 
16 #include <cstring>
17 #include <sstream>
18 #include <stdint.h>
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 
23 
25 
26 #include <common/string_oprs.h>
27 
28 namespace util {
29  namespace cli {
31  protected:
32  std::string data_;
33 
34  struct string2any {
35 
36  template <typename Tt>
37  static inline Tt conv(const std::string &s, Tt *) {
38  Tt ret;
39  std::stringstream ss;
40  ss.str(s);
41  ss >> ret;
42  return ret;
43  }
44 
45  static inline const std::string &conv(const std::string &s, std::string *) { return s; }
46 
47  // static const char *conv(const std::string &s, const char **) { return s.c_str(); }
48 
49  static inline char conv(const std::string &s, char *) { return s.empty() ? 0 : s[0]; }
50 
51  static inline unsigned char conv(const std::string &s, unsigned char *) {
52  return static_cast<unsigned char>(s.empty() ? 0 : s[0]);
53  }
54 
55  static inline int16_t conv(const std::string &s, int16_t *) { return util::string::to_int<int16_t>(s.c_str()); }
56 
57  static inline uint16_t conv(const std::string &s, uint16_t *) { return util::string::to_int<int16_t>(s.c_str()); }
58 
59  static inline int32_t conv(const std::string &s, int32_t *) { return util::string::to_int<int32_t>(s.c_str()); }
60 
61  static inline uint32_t conv(const std::string &s, uint32_t *) { return util::string::to_int<uint32_t>(s.c_str()); }
62 
63  static inline int64_t conv(const std::string &s, int64_t *) { return util::string::to_int<int64_t>(s.c_str()); }
64  static inline uint64_t conv(const std::string &s, uint64_t *) { return util::string::to_int<uint64_t>(s.c_str()); }
65 
66  static inline bool conv(const std::string &s, bool *) { return !s.empty() && "0" != s; }
67  };
68 
69  public:
70  cmd_option_value(const char *str_data);
71  cmd_option_value(const char *begin, const char *end);
72  cmd_option_value(const std::string &str_data);
73 
74  template <typename Tr>
75  Tr to() const {
76  typedef typename ::std::remove_cv<Tr>::type cv_type;
77  return string2any::conv(data_, reinterpret_cast<cv_type *>(NULL));
78  }
79 
80  // 获取存储对象的字符串
81  const std::string &to_cpp_string() const;
82 
83  bool to_bool() const;
84 
85  char to_char() const;
86 
87  short to_short() const;
88 
89  int to_int() const;
90 
91  long to_long() const;
92 
93  long long to_longlong() const;
94 
95  double to_double() const;
96 
97  float to_float() const;
98 
99  const char *to_string() const;
100 
101  unsigned char to_uchar() const;
102 
103  unsigned short to_ushort() const;
104 
105  unsigned int to_uint() const;
106 
107  unsigned long to_ulong() const;
108 
109  unsigned long long to_ulonglong() const;
110 
111  int8_t to_int8() const;
112 
113  uint8_t to_uint8() const;
114 
115  int16_t to_int16() const;
116 
117  uint16_t to_uint16() const;
118 
119  int32_t to_int32() const;
120 
121  uint32_t to_uint32() const;
122 
123  int64_t to_int64() const;
124 
125  uint64_t to_uint64() const;
126 
127  // ============ logic operation ============
128  bool to_logic_bool() const;
129 
130  void split(char delim, std::vector<cmd_option_value> &out);
131  };
132  } // namespace cli
133 } // namespace util
134 
135 #endif /* _CMDOPTIONVALUE_H_ */
unsigned int to_uint() const
unsigned char to_uchar() const
static unsigned char conv(const std::string &s, unsigned char *)
static uint64_t conv(const std::string &s, uint64_t *)
const char * to_string() const
static uint16_t conv(const std::string &s, uint16_t *)
字符串相关操作 Licensed under the MIT licenses.
unsigned long long to_ulonglong() const
static uint32_t conv(const std::string &s, uint32_t *)
static int64_t conv(const std::string &s, int64_t *)
static bool conv(const std::string &s, bool *)
static char conv(const std::string &s, char *)
cmd_option_value(const char *str_data)
long long to_longlong() const
static int16_t conv(const std::string &s, int16_t *)
static Tt conv(const std::string &s, Tt *)
unsigned long to_ulong() const
导入智能指针库 Licensed under the MIT licenses.
unsigned short to_ushort() const
static const std::string & conv(const std::string &s, std::string *)
const std::string & to_cpp_string() const
void split(char delim, std::vector< cmd_option_value > &out)
static int32_t conv(const std::string &s, int32_t *)