TrinityCore
Field.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "Field.h"
19#include "Errors.h"
20#include "FieldValueConverter.h"
21#include <cstring>
22
23Field::Field() : _value(nullptr), _length(0), _meta(nullptr)
24{
25}
26
27Field::~Field() = default;
28
30{
31 if (!_value)
32 return 0;
33
35}
36
38{
39 if (!_value)
40 return 0;
41
43}
44
46{
47 if (!_value)
48 return 0;
49
51}
52
54{
55 if (!_value)
56 return 0;
57
59}
60
62{
63 if (!_value)
64 return 0;
65
67}
68
70{
71 if (!_value)
72 return 0;
73
75}
76
78{
79 if (!_value)
80 return 0;
81
83}
84
86{
87 if (!_value)
88 return 0;
89
91}
92
93float Field::GetFloat() const
94{
95 if (!_value)
96 return 0.0f;
97
99}
100
101double Field::GetDouble() const
102{
103 if (!_value)
104 return 0.0;
105
107}
108
110{
111 if (!_value)
112 return SystemTimePoint::min();
113
115}
116
117char const* Field::GetCString() const
118{
119 if (!_value)
120 return nullptr;
121
123}
124
125std::string Field::GetString() const
126{
127 if (!_value)
128 return "";
129
130 char const* string = GetCString();
131 if (!string)
132 return "";
133
134 return std::string(string, _length);
135}
136
137std::string_view Field::GetStringView() const
138{
139 if (!_value)
140 return {};
141
142 char const* const string = GetCString();
143 if (!string)
144 return {};
145
146 return { string, _length };
147}
148
149std::vector<uint8> Field::GetBinary() const
150{
151 std::vector<uint8> result;
152 if (!_value || !_length)
153 return result;
154
155 result.resize(_length);
156 memcpy(result.data(), _value, _length);
157 return result;
158}
159
160void Field::GetBinarySizeChecked(uint8* buf, size_t length) const
161{
162 ASSERT(_value && (_length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, _value ? "" : "no ", _length);
163 memcpy(buf, _value, length);
164}
165
166void Field::SetValue(char const* newValue, uint32 length)
167{
168 // This value stores raw bytes that have to be explicitly cast later
169 _value = newValue;
170 _length = length;
171}
172
174{
175 _meta = meta;
176}
uint8_t uint8
Definition: Define.h:144
int64_t int64
Definition: Define.h:137
int16_t int16
Definition: Define.h:139
int8_t int8
Definition: Define.h:140
int32_t int32
Definition: Define.h:138
uint64_t uint64
Definition: Define.h:141
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
std::chrono::system_clock::time_point SystemTimePoint
Definition: Duration.h:42
#define ASSERT
Definition: Errors.h:68
virtual uint64 GetUInt64(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual SystemTimePoint GetDate(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual float GetFloat(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual double GetDouble(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual uint8 GetUInt8(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual int64 GetInt64(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual uint16 GetUInt16(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual int32 GetInt32(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual uint32 GetUInt32(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual int8 GetInt8(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual int16 GetInt16(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
virtual char const * GetCString(char const *data, uint32 size, QueryResultFieldMetadata const *meta) const =0
char const * _value
Definition: Field.h:134
uint8 GetUInt8() const
Definition: Field.cpp:29
QueryResultFieldMetadata const * _meta
Definition: Field.h:139
Field()
Definition: Field.cpp:23
SystemTimePoint GetDate() const
Definition: Field.cpp:109
std::string GetString() const
Definition: Field.cpp:125
uint32 _length
Definition: Field.h:135
void SetMetadata(QueryResultFieldMetadata const *meta)
Definition: Field.cpp:173
double GetDouble() const
Definition: Field.cpp:101
std::vector< uint8 > GetBinary() const
Definition: Field.cpp:149
int64 GetInt64() const
Definition: Field.cpp:85
int8 GetInt8() const
Definition: Field.cpp:37
std::string_view GetStringView() const
Definition: Field.cpp:137
uint64 GetUInt64() const
Definition: Field.cpp:77
int16 GetInt16() const
Definition: Field.cpp:53
uint16 GetUInt16() const
Definition: Field.cpp:45
char const * GetCString() const
Definition: Field.cpp:117
float GetFloat() const
Definition: Field.cpp:93
void GetBinarySizeChecked(uint8 *buf, size_t size) const
Definition: Field.cpp:160
void SetValue(char const *newValue, uint32 length)
Definition: Field.cpp:166
uint32 GetUInt32() const
Definition: Field.cpp:61
int32 GetInt32() const
Definition: Field.cpp:69
BaseDatabaseResultValueConverter const * Converter
Definition: Field.h:58