Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: util/stdlib/string_number_conversion.cc

Issue 615923004: Convert COMPILE_ASSERT to static_assert (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/stdlib/string_number_conversion.h" 15 #include "util/stdlib/string_number_conversion.h"
16 16
17 #include <ctype.h> 17 #include <ctype.h>
18 #include <errno.h> 18 #include <errno.h>
19 #include <stdlib.h> 19 #include <stdlib.h>
20 #include <string.h> 20 #include <string.h>
21 21
22 #include <limits> 22 #include <limits>
23 23
24 #include "base/basictypes.h" 24 #include "base/basictypes.h"
Mark Mentovai 2014/10/01 03:55:12 Not needed.
scottmg 2014/10/01 16:37:19 Done.
25 #include "base/logging.h" 25 #include "base/logging.h"
26 #include "util/stdlib/cxx.h" 26 #include "util/stdlib/cxx.h"
27 27
28 // CONSTEXPR_COMPILE_ASSERT will be a normal COMPILE_ASSERT if the C++ library 28 // CONSTEXPR_COMPILE_ASSERT will be a normal COMPILE_ASSERT if the C++ library
29 // is the C++11 library. If using an older C++ library when compiling C++11 29 // is the C++11 library. If using an older C++ library when compiling C++11
30 // code, the std::numeric_limits<>::min() and max() functions will not be 30 // code, the std::numeric_limits<>::min() and max() functions will not be
31 // marked as constexpr, and thus won’t be usable with C++11’s static_assert(). 31 // marked as constexpr, and thus won’t be usable with C++11’s static_assert().
32 // In that case, a run-time CHECK() will have to do. 32 // In that case, a run-time CHECK() will have to do.
33 #if CXX_LIBRARY_VERSION >= 2011 33 #if CXX_LIBRARY_VERSION >= 2011
34 #define CONSTEXPR_COMPILE_ASSERT(condition, message) \ 34 #define CONSTEXPR_COMPILE_ASSERT(condition, message) \
Mark Mentovai 2014/10/01 03:55:12 Probably you should rename this to CONSTEXPR_STATI
scottmg 2014/10/01 16:37:19 Done.
35 COMPILE_ASSERT(condition, message) 35 static_assert(condition, #message)
36 #else 36 #else
37 #define CONSTEXPR_COMPILE_ASSERT(condition, message) CHECK(condition) 37 #define CONSTEXPR_COMPILE_ASSERT(condition, message) CHECK(condition)
Mark Mentovai 2014/10/01 03:55:12 You can make this CHECK(condition) << message, nuk
scottmg 2014/10/01 16:37:19 Done.
38 #endif 38 #endif
39 39
40 namespace { 40 namespace {
41 41
42 template <typename TIntType, typename TLongType> 42 template <typename TIntType, typename TLongType>
43 struct StringToIntegerTraits { 43 struct StringToIntegerTraits {
44 typedef TIntType IntType; 44 typedef TIntType IntType;
45 typedef TLongType LongType; 45 typedef TLongType LongType;
46 static void TypeCheck() { 46 static void TypeCheck() {
47 COMPILE_ASSERT(std::numeric_limits<TIntType>::is_integer && 47 static_assert(std::numeric_limits<TIntType>::is_integer &&
48 std::numeric_limits<TLongType>::is_integer, 48 std::numeric_limits<TLongType>::is_integer,
49 IntType_and_LongType_must_be_integer); 49 "IntType and LongType must be integer");
50 COMPILE_ASSERT(std::numeric_limits<TIntType>::is_signed == 50 static_assert(std::numeric_limits<TIntType>::is_signed ==
51 std::numeric_limits<TLongType>::is_signed, 51 std::numeric_limits<TLongType>::is_signed,
52 IntType_and_LongType_signedness_must_agree); 52 "IntType and LongType signedness must agree");
53 CONSTEXPR_COMPILE_ASSERT(std::numeric_limits<TIntType>::min() >= 53 CONSTEXPR_COMPILE_ASSERT(std::numeric_limits<TIntType>::min() >=
54 std::numeric_limits<TLongType>::min() && 54 std::numeric_limits<TLongType>::min() &&
55 std::numeric_limits<TIntType>::min() < 55 std::numeric_limits<TIntType>::min() <
56 std::numeric_limits<TLongType>::max(), 56 std::numeric_limits<TLongType>::max(),
57 IntType_min_must_be_in_LongType_range); 57 "IntType min must be in LongType range");
58 CONSTEXPR_COMPILE_ASSERT(std::numeric_limits<TIntType>::max() > 58 CONSTEXPR_COMPILE_ASSERT(std::numeric_limits<TIntType>::max() >
59 std::numeric_limits<TLongType>::min() && 59 std::numeric_limits<TLongType>::min() &&
60 std::numeric_limits<TIntType>::max() <= 60 std::numeric_limits<TIntType>::max() <=
61 std::numeric_limits<TLongType>::max(), 61 std::numeric_limits<TLongType>::max(),
62 IntType_max_must_be_in_LongType_range); 62 IntType_max_must_be_in_LongType_range);
63 } 63 }
64 }; 64 };
65 65
66 template <typename TIntType, typename TLongType> 66 template <typename TIntType, typename TLongType>
67 struct StringToSignedIntegerTraits 67 struct StringToSignedIntegerTraits
68 : public StringToIntegerTraits<TIntType, TLongType> { 68 : public StringToIntegerTraits<TIntType, TLongType> {
69 static void TypeCheck() { 69 static void TypeCheck() {
70 COMPILE_ASSERT(std::numeric_limits<TIntType>::is_signed, 70 static_assert(std::numeric_limits<TIntType>::is_signed,
71 StringToSignedTraits_IntType_must_be_signed); 71 "StringToSignedTraits IntType must be signed");
72 return super::TypeCheck(); 72 return super::TypeCheck();
73 } 73 }
74 static bool IsNegativeOverflow(TLongType value) { 74 static bool IsNegativeOverflow(TLongType value) {
75 return value < std::numeric_limits<TIntType>::min(); 75 return value < std::numeric_limits<TIntType>::min();
76 } 76 }
77 77
78 private: 78 private:
79 typedef StringToIntegerTraits<TIntType, TLongType> super; 79 typedef StringToIntegerTraits<TIntType, TLongType> super;
80 }; 80 };
81 81
82 template <typename TIntType, typename TLongType> 82 template <typename TIntType, typename TLongType>
83 struct StringToUnsignedIntegerTraits 83 struct StringToUnsignedIntegerTraits
84 : public StringToIntegerTraits<TIntType, TLongType> { 84 : public StringToIntegerTraits<TIntType, TLongType> {
85 static void TypeCheck() { 85 static void TypeCheck() {
86 COMPILE_ASSERT(!std::numeric_limits<TIntType>::is_signed, 86 static_assert(!std::numeric_limits<TIntType>::is_signed,
87 StringToUnsignedTraits_IntType_must_be_unsigned); 87 "StringToUnsignedTraits IntType must be unsigned");
88 return super::TypeCheck(); 88 return super::TypeCheck();
89 } 89 }
90 static bool IsNegativeOverflow(TLongType value) { return false; } 90 static bool IsNegativeOverflow(TLongType value) { return false; }
91 91
92 private: 92 private:
93 typedef StringToIntegerTraits<TIntType, TLongType> super; 93 typedef StringToIntegerTraits<TIntType, TLongType> super;
94 }; 94 };
95 95
96 struct StringToIntTraits : public StringToSignedIntegerTraits<int, long> { 96 struct StringToIntTraits : public StringToSignedIntegerTraits<int, long> {
97 static LongType Convert(const char* str, char** end, int base) { 97 static LongType Convert(const char* str, char** end, int base) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 148
149 bool StringToNumber(const base::StringPiece& string, int* number) { 149 bool StringToNumber(const base::StringPiece& string, int* number) {
150 return StringToIntegerInternal<StringToIntTraits>(string, number); 150 return StringToIntegerInternal<StringToIntTraits>(string, number);
151 } 151 }
152 152
153 bool StringToNumber(const base::StringPiece& string, unsigned int* number) { 153 bool StringToNumber(const base::StringPiece& string, unsigned int* number) {
154 return StringToIntegerInternal<StringToUnsignedIntTraits>(string, number); 154 return StringToIntegerInternal<StringToUnsignedIntTraits>(string, number);
155 } 155 }
156 156
157 } // namespace crashpad 157 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698