0

Clean up ui::TableModel.

- Reduce the size of the header.
- Break a big DCHECK() into several smaller DCHECK() calls.
- Use nullptr.

Change-Id: I94d060e0f63fc8e6e66e9bf429c534508a5f785f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3352610
Reviewed-by: Sadrul Chowdhury <sadrul@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#956548}
This commit is contained in:
Lei Zhang
2022-01-07 16:58:30 +00:00
committed by Chromium LUCI CQ
parent a401c73b5e
commit be3f6916ba
2 changed files with 18 additions and 8 deletions

@ -7,6 +7,7 @@
#include "base/check.h"
#include "base/i18n/string_compare.h"
#include "base/notreached.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
@ -43,7 +44,7 @@ TableColumn& TableColumn::operator=(const TableColumn& other) = default;
// TableModel -----------------------------------------------------------------
// Used for sorting.
static icu::Collator* g_collator = NULL;
static icu::Collator* g_collator = nullptr;
ui::ImageModel TableModel::GetIcon(int row) {
return ui::ImageModel();
@ -54,8 +55,11 @@ std::u16string TableModel::GetTooltip(int row) {
}
int TableModel::CompareValues(int row1, int row2, int column_id) {
DCHECK(row1 >= 0 && row1 < RowCount() &&
row2 >= 0 && row2 < RowCount());
DCHECK_GE(row1, 0);
DCHECK_LT(row1, RowCount());
DCHECK_GE(row2, 0);
DCHECK_LT(row2, RowCount());
std::u16string value1 = GetText(row1, column_id);
std::u16string value2 = GetText(row2, column_id);
icu::Collator* collator = GetCollator();
@ -69,15 +73,17 @@ int TableModel::CompareValues(int row1, int row2, int column_id) {
void TableModel::ClearCollator() {
delete g_collator;
g_collator = NULL;
g_collator = nullptr;
}
TableModel::~TableModel() = default;
icu::Collator* TableModel::GetCollator() {
if (!g_collator) {
UErrorCode create_status = U_ZERO_ERROR;
g_collator = icu::Collator::createInstance(create_status);
if (!U_SUCCESS(create_status)) {
g_collator = NULL;
g_collator = nullptr;
NOTREACHED();
}
}

@ -6,10 +6,14 @@
#define UI_BASE_MODELS_TABLE_MODEL_H_
#include <string>
#include <vector>
#include "base/component_export.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
#include "third_party/icu/source/common/unicode/uversion.h"
// third_party/icu/source/common/unicode/uversion.h will set namespace icu.
namespace U_ICU_NAMESPACE {
class Collator;
}
namespace ui {
@ -55,7 +59,7 @@ class COMPONENT_EXPORT(UI_BASE) TableModel {
void ClearCollator();
protected:
virtual ~TableModel() {}
virtual ~TableModel();
// Returns the collator used by CompareValues.
icu::Collator* GetCollator();