MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
value.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Wangyunlai 2023/6/27
13//
14
15#pragma once
16
17#include "common/lang/string.h"
18#include "common/lang/memory.h"
19#include "common/type/attr_type.h"
20#include "common/type/data_type.h"
21#include "common/type/string_t.h"
22
30class Value final
31{
32public:
33 friend class DataType;
34 friend class IntegerType;
35 friend class FloatType;
36 friend class BooleanType;
37 friend class CharType;
38 friend class VectorType;
39
40 Value() = default;
41
42 ~Value() { reset(); }
43
44 Value(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) { this->set_data(data, length); }
45
46 explicit Value(int val);
47 explicit Value(float val);
48 explicit Value(bool val);
49 explicit Value(const char *s, int len = 0);
50 explicit Value(const string_t &val);
51
52 Value(const Value &other);
53 Value(Value &&other);
54
55 Value &operator=(const Value &other);
56 Value &operator=(Value &&other);
57
58 void reset();
59
60 static RC add(const Value &left, const Value &right, Value &result)
61 {
62 return DataType::type_instance(result.attr_type())->add(left, right, result);
63 }
64
65 static RC subtract(const Value &left, const Value &right, Value &result)
66 {
67 return DataType::type_instance(result.attr_type())->subtract(left, right, result);
68 }
69
70 static RC multiply(const Value &left, const Value &right, Value &result)
71 {
72 return DataType::type_instance(result.attr_type())->multiply(left, right, result);
73 }
74
75 static RC divide(const Value &left, const Value &right, Value &result)
76 {
77 return DataType::type_instance(result.attr_type())->divide(left, right, result);
78 }
79
80 static RC negative(const Value &value, Value &result)
81 {
82 return DataType::type_instance(result.attr_type())->negative(value, result);
83 }
84
85 static RC cast_to(const Value &value, AttrType to_type, Value &result)
86 {
87 return DataType::type_instance(value.attr_type())->cast_to(value, to_type, result);
88 }
89
90 void set_type(AttrType type) { this->attr_type_ = type; }
91 void set_data(char *data, int length);
92 void set_data(const char *data, int length) { this->set_data(const_cast<char *>(data), length); }
93 void set_value(const Value &value);
94 void set_boolean(bool val);
95
96 string to_string() const;
97
98 int compare(const Value &other) const;
99
100 char *data() const;
101
102 int length() const { return length_; }
103 AttrType attr_type() const { return attr_type_; }
104
105public:
110 int get_int() const;
111 float get_float() const;
112 string get_string() const;
113 string_t get_string_t() const;
114 bool get_boolean() const;
115
116public:
117 void set_int(int val);
118 void set_float(float val);
119 void set_string(const char *s, int len = 0);
120 void set_empty_string(int len);
121 void set_string_from_other(const Value &other);
122
123private:
124 AttrType attr_type_ = AttrType::UNDEFINED;
125 int length_ = 0;
126
127 union Val
128 {
129 int32_t int_value_;
130 float float_value_;
131 bool bool_value_;
132 char *pointer_value_;
133 } value_ = {.int_value_ = 0};
134
136 bool own_data_ = false;
137};
固定长度的字符串类型
Definition: char_type.h:21
Definition: data_type.h:30
virtual RC divide(const Value &left, const Value &right, Value &result) const
计算 left / right,并将结果保存到 result 中
Definition: data_type.h:72
virtual RC cast_to(const Value &val, AttrType type, Value &result) const
将 val 转换为 type 类型,并将结果保存到 result 中
Definition: data_type.h:82
virtual RC subtract(const Value &left, const Value &right, Value &result) const
计算 left - right,并将结果保存到 result 中
Definition: data_type.h:62
virtual RC multiply(const Value &left, const Value &right, Value &result) const
计算 left * right,并将结果保存到 result 中
Definition: data_type.h:67
virtual RC add(const Value &left, const Value &right, Value &result) const
计算 left + right,并将结果保存到 result 中
Definition: data_type.h:57
virtual RC negative(const Value &val, Value &result) const
计算 -val,并将结果保存到 result 中
Definition: data_type.h:77
浮点型数据类型
Definition: float_type.h:20
整型类型
Definition: integer_type.h:20
属性的值
Definition: value.h:31
int get_int() const
Definition: value.cpp:250
bool own_data_
是否申请并占有内存, 目前对于 CHARS 类型 own_data_ 为true, 其余类型 own_data_ 为false
Definition: value.h:136
向量类型
Definition: vector_type.h:20
Definition: string_t.h:18
Definition: value.h:128