This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathIntType.cpp
More file actions
99 lines (82 loc) · 2.99 KB
/
IntType.cpp
File metadata and controls
99 lines (82 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright 2011-2015 Quickstep Technologies LLC.
* Copyright 2015 Pivotal Software, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "types/IntType.hpp"
#include <cstdint>
#include <cstdio>
#include <string>
#include "types/NullCoercibilityCheckMacro.hpp"
#include "types/Type.hpp"
#include "types/TypeID.hpp"
#include "types/TypedValue.hpp"
#include "glog/logging.h"
namespace quickstep {
const TypeID IntType::kStaticTypeID = kInt;
bool IntType::isSafelyCoercibleFrom(const Type &original_type) const {
QUICKSTEP_NULL_COERCIBILITY_CHECK();
return original_type.getTypeID() == kInt;
}
TypedValue IntType::coerceValue(const TypedValue &original_value,
const Type &original_type) const {
DCHECK(isCoercibleFrom(original_type))
<< "Can't coerce value of Type " << original_type.getName()
<< " to Type " << getName();
if (original_value.isNull()) {
return makeNullValue();
}
switch (original_type.getTypeID()) {
case kInt:
return original_value;
case kLong:
return TypedValue(static_cast<int>(original_value.getLiteral<std::int64_t>()));
case kFloat:
return TypedValue(static_cast<int>(original_value.getLiteral<float>()));
case kDouble:
return TypedValue(static_cast<int>(original_value.getLiteral<double>()));
default:
LOG(FATAL) << "Attempted to coerce Type " << original_type.getName()
<< " (not recognized as a numeric Type) to " << getName();
}
}
std::string IntType::printValueToString(const TypedValue &value) const {
DCHECK(!value.isNull());
return std::to_string(value.getLiteral<int>());
}
void IntType::printValueToFile(const TypedValue &value,
FILE *file,
const int padding) const {
DCHECK(!value.isNull());
std::fprintf(file,
"%*d",
static_cast<int>(padding),
value.getLiteral<int>());
}
bool IntType::parseValueFromString(const std::string &value_string,
TypedValue *value) const {
int parsed_int;
int read_chars;
int matched = std::sscanf(value_string.c_str(),
"%d%n",
&parsed_int,
&read_chars);
if ((matched != 1)
|| (static_cast<std::string::size_type>(read_chars) != value_string.length())) {
return false;
}
*value = TypedValue(parsed_int);
return true;
}
} // namespace quickstep