31#ifndef MYSQLX_COMMON_VALUE_H
32#define MYSQLX_COMMON_VALUE_H
62 :
public virtual Printable
81 using string = std::string;
92 std::u16string m_ustr;
97 double v_double = 0.0;
104 void print(std::ostream&)
const override;
106 template <
typename T>
107 Value(
Type type, T &&init)
108 : Value(std::forward<T>(init))
116 Value() : m_type(VNULL)
121 Value(
const std::string& str) : m_type(STRING), m_str(str)
123 m_val.v_bool =
false;
126 Value(
const std::u16string &str)
127 : m_type(USTRING), m_ustr(str)
129 m_val.v_bool =
false;
134 Value(int64_t v) : m_type(INT64)
135 { m_val.v_sint = v; }
138 Value(uint64_t v) : m_type(UINT64)
139 { m_val.v_uint = v; }
142 Value(
float v) : m_type(FLOAT)
143 { m_val.v_float = v; }
146 Value(
double v) : m_type(DOUBLE)
147 { m_val.v_double = v; }
151 Value(
bool v) : m_type(BOOL)
152 { m_val.v_bool = v; }
155 Value(
const byte *ptr,
size_t len) : m_type(RAW)
158 m_str.assign((
const char*)ptr, len);
165 typename std::enable_if<std::is_unsigned<T>::value>::type* =
nullptr
168 : Value(uint64_t(val))
173 typename std::enable_if<!std::is_unsigned<T>::value>::type* =
nullptr,
174 typename std::enable_if<std::is_integral<T>::value>::type* =
nullptr
177 : Value(int64_t(val))
183 return VNULL == m_type;
186 bool get_bool()
const
190 case BOOL:
return m_val.v_bool;
191 case UINT64:
return 0 != m_val.v_uint;
192 case INT64:
return 0 != m_val.v_sint;
194 throw Error(
"Can not convert to Boolean value");
198 uint64_t get_uint()
const
200 if (UINT64 != m_type && INT64 != m_type && BOOL != m_type)
201 throw Error(
"Can not convert to integer value");
204 return m_val.v_bool ? 1 : 0;
206 if (INT64 == m_type && 0 > m_val.v_sint)
207 throw Error(
"Converting negative integer to unsigned value");
209 uint64_t val = (UINT64 == m_type ? m_val.v_uint : (uint64_t)m_val.v_sint);
214 int64_t get_sint()
const
219 uint64_t val = get_uint();
221 if (!check_num_limits<int64_t>(val))
222 throw Error(
"Value cannot be converted to signed integer number");
227 float get_float()
const
231 case INT64:
return 1.0F*m_val.v_sint;
232 case UINT64:
return 1.0F*m_val.v_uint;
233 case FLOAT:
return m_val.v_float;
235 throw Error(
"Value cannot be converted to float number");
239 double get_double()
const
243 case INT64:
return 1.0*m_val.v_sint;
244 case UINT64:
return 1.0*m_val.v_uint;
245 case FLOAT:
return m_val.v_float;
246 case DOUBLE:
return m_val.v_double;
248 throw Error(
"Value can not be converted to double number");
262 const byte* get_bytes(
size_t *size)
const
270 *size = m_ustr.size() *
sizeof(char16_t);
271 return (
const byte*)m_ustr.data();
277 throw Error(
"Value cannot be converted to raw bytes");
283 *size = m_str.length();
284 return (
const byte*)m_str.data();
291 const std::string& get_string()
const;
292 const std::u16string& get_ustring()
const;
294 Type get_type()
const
308 template <
typename T>
Classes used to access query and command execution results.
Type
Types that can be reported in result meta-data.
Definition: result.h:241