forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction-templates-expected.cc
64 lines (51 loc) · 1.45 KB
/
function-templates-expected.cc
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
namespace WTF {
template <typename To, typename From>
bool IsInBounds(From value) {
return true;
}
template <typename To, typename From>
To SafeCast(From value) {
if (!IsInBounds<To>(value))
return 0;
return static_cast<To>(value);
}
template <typename T, typename OverflowHandler>
class Checked {
public:
template <typename U, typename V>
Checked(const Checked<U, V>& rhs) {
if (rhs.HasOverflowed())
this->Overflowed();
if (!IsInBounds<T>(rhs.value_))
this->Overflowed();
value_ = static_cast<T>(rhs.value_);
}
bool HasOverflowed() const { return false; }
void Overflowed() {}
private:
T value_;
};
template <typename To, typename From>
To bitwise_cast(From from) {
static_assert(sizeof(To) == sizeof(From), "msg");
return reinterpret_cast<To>(from);
}
} // namespace WTF
namespace mojo {
template <typename U>
struct ArrayTraits;
template <typename U>
struct ArrayTraits<WTF::Checked<U, int>> {
static bool HasOverflowed(WTF::Checked<U, int>& input) {
// |hasOverflowed| below should be rewritten to |HasOverflowed|
// (because this is a method of WTF::Checked; it doesn't matter
// that we are not in WTF namespace *here*).
return input.HasOverflowed();
}
};
} // namespace mojo
using WTF::bitwise_cast;
using WTF::SafeCast;