libcopp  2.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 <stdint.h>
17 #include <cstring>
18 #include <memory>
19 #include <sstream>
20 #include <string>
21 #include <type_traits>
22 #include <vector>
23 
24 #include <common/string_oprs.h>
25 
26 namespace util {
27 namespace cli {
28 
29 template <typename Tt>
30 struct string2any;
31 
32 template <>
33 struct string2any<std::string> {
34  inline std::string operator()(const std::string &s) const { return s; }
35 };
36 
37 template <>
38 struct string2any<char> {
39  inline char operator()(const std::string &s) const { return s.empty() ? 0 : s[0]; }
40 };
41 
42 template <>
43 struct string2any<unsigned char> {
44  inline unsigned char operator()(const std::string &s) const {
45  return static_cast<unsigned char>(s.empty() ? 0 : s[0]);
46  }
47 };
48 
49 template <>
50 struct string2any<int16_t> {
51  inline int16_t operator()(const std::string &s) const { return util::string::to_int<int16_t>(s.c_str()); }
52 };
53 
54 template <>
55 struct string2any<uint16_t> {
56  inline uint16_t operator()(const std::string &s) const { return util::string::to_int<uint16_t>(s.c_str()); }
57 };
58 
59 template <>
60 struct string2any<int32_t> {
61  inline int32_t operator()(const std::string &s) const { return util::string::to_int<int32_t>(s.c_str()); }
62 };
63 
64 template <>
65 struct string2any<uint32_t> {
66  inline uint32_t operator()(const std::string &s) const { return util::string::to_int<uint32_t>(s.c_str()); }
67 };
68 
69 template <>
70 struct string2any<int64_t> {
71  inline int64_t operator()(const std::string &s) const { return util::string::to_int<int64_t>(s.c_str()); }
72 };
73 
74 template <>
75 struct string2any<uint64_t> {
76  inline uint64_t operator()(const std::string &s) const { return util::string::to_int<uint64_t>(s.c_str()); }
77 };
78 
79 template <>
80 struct string2any<bool> {
81  inline bool operator()(const std::string &s) const { return !s.empty() && "0" != s; }
82 };
83 
84 template <typename Tt>
85 struct string2any {
86  inline Tt operator()(const std::string &s) const {
87  Tt ret;
88  std::stringstream ss;
89  ss.str(s);
90  ss >> ret;
91  return ret;
92  }
93 };
94 
96  protected:
97  std::string data_;
98 
99  public:
100  cmd_option_value(const char *str_data);
101  cmd_option_value(const char *begin, const char *end);
102  cmd_option_value(const std::string &str_data);
103 
104  template <typename Tr>
105  Tr to() const {
106  typedef typename ::std::remove_cv<Tr>::type cv_type;
107  return string2any<cv_type>()(data_);
108  }
109 
110  // 获取存储对象的字符串
111  const std::string &to_cpp_string() const;
112 
113  bool to_bool() const;
114 
115  char to_char() const;
116 
117  short to_short() const;
118 
119  int to_int() const;
120 
121  long to_long() const;
122 
123  long long to_longlong() const;
124 
125  double to_double() const;
126 
127  float to_float() const;
128 
129  const char *to_string() const;
130 
131  unsigned char to_uchar() const;
132 
133  unsigned short to_ushort() const;
134 
135  unsigned int to_uint() const;
136 
137  unsigned long to_ulong() const;
138 
139  unsigned long long to_ulonglong() const;
140 
141  int8_t to_int8() const;
142 
143  uint8_t to_uint8() const;
144 
145  int16_t to_int16() const;
146 
147  uint16_t to_uint16() const;
148 
149  int32_t to_int32() const;
150 
151  uint32_t to_uint32() const;
152 
153  int64_t to_int64() const;
154 
155  uint64_t to_uint64() const;
156 
157  // ============ logic operation ============
158  bool to_logic_bool() const;
159 
160  void split(char delim, std::vector<cmd_option_value> &out);
161 };
162 } // namespace cli
163 } // namespace util
164 
165 #endif /* _CMDOPTIONVALUE_H_ */
unsigned short to_ushort() const
void split(char delim, std::vector< cmd_option_value > &out)
cmd_option_value(const char *str_data)
unsigned char to_uchar() const
unsigned int to_uint() const
unsigned long long to_ulonglong() const
const char * to_string() const
unsigned long to_ulong() const
const std::string & to_cpp_string() const
字符串相关操作 Licensed under the MIT licenses.
bool operator()(const std::string &s) const
char operator()(const std::string &s) const
int16_t operator()(const std::string &s) const
int32_t operator()(const std::string &s) const
int64_t operator()(const std::string &s) const
std::string operator()(const std::string &s) const
uint16_t operator()(const std::string &s) const
uint32_t operator()(const std::string &s) const
uint64_t operator()(const std::string &s) const
unsigned char operator()(const std::string &s) const
Tt operator()(const std::string &s) const