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
29class Value final
30{
31public:
32 friend class DataType;
33 friend class IntegerType;
34 friend class FloatType;
35 friend class BooleanType;
36 friend class CharType;
37 friend class VectorType;
38
39 Value() = default;
40
41 ~Value() { reset(); }
42
43 Value(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) { this->set_data(data, length); }
44
45 explicit Value(int val);
46 explicit Value(float val);
47 explicit Value(bool val);
48 explicit Value(const char *s, int len = 0);
49
50 Value(const Value &other);
51 Value(Value &&other);
52
53 Value &operator=(const Value &other);
54 Value &operator=(Value &&other);
55
56 void reset();
57
58 static RC add(const Value &left, const Value &right, Value &result)
59 {
60 return DataType::type_instance(result.attr_type())->add(left, right, result);
61 }
62
63 static RC subtract(const Value &left, const Value &right, Value &result)
64 {
65 return DataType::type_instance(result.attr_type())->subtract(left, right, result);
66 }
67
68 static RC multiply(const Value &left, const Value &right, Value &result)
69 {
70 return DataType::type_instance(result.attr_type())->multiply(left, right, result);
71 }
72
73 static RC divide(const Value &left, const Value &right, Value &result)
74 {
75 return DataType::type_instance(result.attr_type())->divide(left, right, result);
76 }
77
78 static RC negative(const Value &value, Value &result)
79 {
80 return DataType::type_instance(result.attr_type())->negative(value, result);
81 }
82
83 static RC cast_to(const Value &value, AttrType to_type, Value &result)
84 {
85 return DataType::type_instance(value.attr_type())->cast_to(value, to_type, result);
86 }
87
88 void set_type(AttrType type) { this->attr_type_ = type; }
89 void set_data(char *data, int length);
90 void set_data(const char *data, int length) { this->set_data(const_cast<char *>(data), length); }
91 void set_value(const Value &value);
92 void set_boolean(bool val);
93
94 string to_string() const;
95
96 int compare(const Value &other) const;
97
98 const char *data() const;
99
100 int length() const { return length_; }
101 AttrType attr_type() const { return attr_type_; }
102
103public:
108 int get_int() const;
109 float get_float() const;
110 string get_string() const;
111 bool get_boolean() const;
112
113private:
114 void set_int(int val);
115 void set_float(float val);
116 void set_string(const char *s, int len = 0);
117 void set_string_from_other(const Value &other);
118
119private:
120 AttrType attr_type_ = AttrType::UNDEFINED;
121 int length_ = 0;
122
123 union Val
124 {
125 int32_t int_value_;
126 float float_value_;
127 bool bool_value_;
128 char *pointer_value_;
129 } value_ = {.int_value_ = 0};
130
132 bool own_data_ = false;
133};
固定长度的字符串类型
Definition: char_type.h:21
Definition: data_type.h:28
virtual RC divide(const Value &left, const Value &right, Value &result) const
计算 left / right,并将结果保存到 result 中
Definition: data_type.h:68
virtual RC cast_to(const Value &val, AttrType type, Value &result) const
将 val 转换为 type 类型,并将结果保存到 result 中
Definition: data_type.h:78
virtual RC subtract(const Value &left, const Value &right, Value &result) const
计算 left - right,并将结果保存到 result 中
Definition: data_type.h:58
virtual RC multiply(const Value &left, const Value &right, Value &result) const
计算 left * right,并将结果保存到 result 中
Definition: data_type.h:63
virtual RC add(const Value &left, const Value &right, Value &result) const
计算 left + right,并将结果保存到 result 中
Definition: data_type.h:53
virtual RC negative(const Value &val, Value &result) const
计算 -val,并将结果保存到 result 中
Definition: data_type.h:73
浮点型数据类型
Definition: float_type.h:20
整型类型
Definition: integer_type.h:20
属性的值
Definition: value.h:30
int get_int() const
Definition: value.cpp:234
bool own_data_
是否申请并占有内存, 目前对于 CHARS 类型 own_data_ 为true, 其余类型 own_data_ 为false
Definition: value.h:132
向量类型
Definition: vector_type.h:20
Definition: value.h:124