Make safe_conversions suitable for rtc_base_approved.
Since we want to use checked_cast in WavReader.
R=kwiberg@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/32839004
git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@7937 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/base/BUILD.gn b/base/BUILD.gn
index acce9f0..ef02817 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -113,6 +113,8 @@
"md5digest.h",
"platform_file.cc",
"platform_file.h",
+ "safe_conversions.h",
+ "safe_conversions_impl.h",
"stringencode.cc",
"stringencode.h",
"stringutils.cc",
@@ -225,8 +227,6 @@
"ratelimiter.h",
"ratetracker.cc",
"ratetracker.h",
- "safe_conversions.h",
- "safe_conversions_impl.h",
"scoped_autorelease_pool.h",
"scoped_autorelease_pool.mm",
"scoped_ptr.h",
diff --git a/base/base.gyp b/base/base.gyp
index 380eee6..645c1dc 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -48,6 +48,8 @@
'md5digest.h',
'platform_file.cc',
'platform_file.h',
+ 'safe_conversions.h',
+ 'safe_conversions_impl.h',
'stringencode.cc',
'stringencode.h',
'stringutils.cc',
@@ -219,8 +221,6 @@
'refcount.h',
'referencecountedsingletonfactory.h',
'rollingaccumulator.h',
- 'safe_conversions.h',
- 'safe_conversions_impl.h',
'schanneladapter.cc',
'schanneladapter.h',
'scoped_autorelease_pool.h',
diff --git a/base/safe_conversions.h b/base/safe_conversions.h
index f6cb24e..7fc67cb 100644
--- a/base/safe_conversions.h
+++ b/base/safe_conversions.h
@@ -15,20 +15,11 @@
#include <limits>
-#include "webrtc/base/common.h"
-#include "webrtc/base/logging.h"
+#include "webrtc/base/checks.h"
#include "webrtc/base/safe_conversions_impl.h"
namespace rtc {
-inline void Check(bool condition) {
- if (!condition) {
- LOG(LS_ERROR) << "CHECK failed.";
- Break();
- // The program should have crashed at this point.
- }
-}
-
// Convenience function that returns true if the supplied value is in range
// for the destination type.
template <typename Dst, typename Src>
@@ -41,7 +32,7 @@
// overflow or underflow. NaN source will always trigger a CHECK.
template <typename Dst, typename Src>
inline Dst checked_cast(Src value) {
- Check(IsValueInRangeForNumericType<Dst>(value));
+ CHECK(IsValueInRangeForNumericType<Dst>(value));
return static_cast<Dst>(value);
}
@@ -66,11 +57,11 @@
// Should fail only on attempting to assign NaN to a saturated integer.
case internal::TYPE_INVALID:
- Check(false);
+ FATAL();
return std::numeric_limits<Dst>::max();
}
- Check(false); // NOTREACHED();
+ FATAL();
return static_cast<Dst>(value);
}
diff --git a/common_audio/wav_file.cc b/common_audio/wav_file.cc
index cc96e41..de83b03 100644
--- a/common_audio/wav_file.cc
+++ b/common_audio/wav_file.cc
@@ -15,6 +15,7 @@
#include <limits>
#include "webrtc/base/checks.h"
+#include "webrtc/base/safe_conversions.h"
#include "webrtc/common_audio/include/audio_util.h"
#include "webrtc/common_audio/wav_header.h"
@@ -58,17 +59,15 @@
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
- // TODO(ajm): Import Chromium's safe_conversions.h for this.
- CHECK_LE(num_samples, std::numeric_limits<uint32_t>::max());
// There could be metadata after the audio; ensure we don't read it.
- num_samples = std::min(static_cast<uint32_t>(num_samples),
+ num_samples = std::min(rtc::checked_cast<uint32_t>(num_samples),
num_samples_remaining_);
const size_t read =
fread(samples, sizeof(*samples), num_samples, file_handle_);
// If we didn't read what was requested, ensure we've reached the EOF.
CHECK(read == num_samples || feof(file_handle_));
CHECK_LE(read, num_samples_remaining_);
- num_samples_remaining_ -= static_cast<uint32_t>(read);
+ num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read);
return read;
}