Commit adb045fd authored by danakj's avatar danakj Committed by Commit bot

gfx:: De-templatize Size and SizeF.

This shrinks the android ChromeShell binary by 1076 bytes, and makes the
code easier for developers to read.

Performance-wise this change seems neutral (as opposed to the perf wins
from similar changes for Rect and Point classes). Performance numbers
are at http://crbug.com/407444#c19

R=sky
BUG=407444

Review URL: https://codereview.chromium.org/662273002

Cr-Commit-Position: refs/heads/master@{#300285}
parent 6071e0f8
......@@ -40,7 +40,6 @@ component("geometry") {
"scroll_offset.h",
"size.cc",
"size.h",
"size_base.h",
"size_conversions.cc",
"size_conversions.h",
"size_f.cc",
......
......@@ -12,13 +12,16 @@
namespace gfx {
template class SizeBase<Size, int>;
#if defined(OS_MACOSX)
Size::Size(const CGSize& s)
: SizeBase<Size, int>(s.width, s.height) {
#if defined(OS_WIN)
SIZE Size::ToSIZE() const {
SIZE s;
s.cx = width();
s.cy = height();
return s;
}
#endif
#if defined(OS_MACOSX)
Size& Size::operator=(const CGSize& s) {
set_width(s.width);
set_height(s.height);
......@@ -26,18 +29,23 @@ Size& Size::operator=(const CGSize& s) {
}
#endif
#if defined(OS_WIN)
SIZE Size::ToSIZE() const {
SIZE s;
s.cx = width();
s.cy = height();
return s;
int Size::GetArea() const {
return width() * height();
}
#elif defined(OS_MACOSX)
CGSize Size::ToCGSize() const {
return CGSizeMake(width(), height());
void Size::Enlarge(int grow_width, int grow_height) {
SetSize(width() + grow_width, height() + grow_height);
}
void Size::SetToMin(const Size& other) {
width_ = width() <= other.width() ? width() : other.width();
height_ = height() <= other.height() ? height() : other.height();
}
void Size::SetToMax(const Size& other) {
width_ = width() >= other.width() ? width() : other.width();
height_ = height() >= other.height() ? height() : other.height();
}
#endif
std::string Size::ToString() const {
return base::StringPrintf("%dx%d", width(), height());
......
......@@ -9,7 +9,6 @@
#include <string>
#include "base/compiler_specific.h"
#include "ui/gfx/geometry/size_base.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/gfx_export.h"
......@@ -24,12 +23,15 @@ typedef struct tagSIZE SIZE;
namespace gfx {
// A size has width and height values.
class GFX_EXPORT Size : public SizeBase<Size, int> {
class GFX_EXPORT Size {
public:
Size() : SizeBase<Size, int>(0, 0) {}
Size(int width, int height) : SizeBase<Size, int>(width, height) {}
Size() : width_(0), height_(0) {}
Size(int width, int height)
: width_(width < 0 ? 0 : width), height_(height < 0 ? 0 : height) {}
#if defined(OS_MACOSX)
explicit Size(const CGSize& s);
explicit Size(const CGSize& s)
: width_(s.width < 0 ? 0 : s.width),
height_(s.height < 0 ? 0 : s.height) {}
#endif
~Size() {}
......@@ -41,14 +43,38 @@ class GFX_EXPORT Size : public SizeBase<Size, int> {
#if defined(OS_WIN)
SIZE ToSIZE() const;
#elif defined(OS_MACOSX)
CGSize ToCGSize() const;
CGSize ToCGSize() const { return CGSizeMake(width(), height()); }
#endif
int width() const { return width_; }
int height() const { return height_; }
void set_width(int width) { width_ = width < 0 ? 0 : width; }
void set_height(int height) { height_ = height < 0 ? 0 : height; }
int GetArea() const;
void SetSize(int width, int height) {
set_width(width);
set_height(height);
}
void Enlarge(int grow_width, int grow_height);
void SetToMin(const Size& other);
void SetToMax(const Size& other);
bool IsEmpty() const { return !width() || !height(); }
operator SizeF() const {
return SizeF(width(), height());
}
std::string ToString() const;
private:
int width_;
int height_;
};
inline bool operator==(const Size& lhs, const Size& rhs) {
......@@ -59,10 +85,6 @@ inline bool operator!=(const Size& lhs, const Size& rhs) {
return !(lhs == rhs);
}
#if !defined(COMPILER_MSVC) && !defined(__native_client__)
extern template class SizeBase<Size, int>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
......
// Copyright (c) 2012 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.
#ifndef UI_GFX_GEOMETRY_SIZE_BASE_H_
#define UI_GFX_GEOMETRY_SIZE_BASE_H_
#include "ui/gfx/gfx_export.h"
namespace gfx {
// A size has width and height values.
template<typename Class, typename Type>
class GFX_EXPORT SizeBase {
public:
Type width() const { return width_; }
Type height() const { return height_; }
Type GetArea() const { return width_ * height_; }
void SetSize(Type width, Type height) {
set_width(width);
set_height(height);
}
void Enlarge(Type width, Type height) {
set_width(width_ + width);
set_height(height_ + height);
}
void set_width(Type width) {
width_ = width < 0 ? 0 : width;
}
void set_height(Type height) {
height_ = height < 0 ? 0 : height;
}
void SetToMin(const Class& other) {
width_ = width_ <= other.width_ ? width_ : other.width_;
height_ = height_ <= other.height_ ? height_ : other.height_;
}
void SetToMax(const Class& other) {
width_ = width_ >= other.width_ ? width_ : other.width_;
height_ = height_ >= other.height_ ? height_ : other.height_;
}
bool IsEmpty() const {
return (width_ == 0) || (height_ == 0);
}
protected:
SizeBase(Type width, Type height)
: width_(width < 0 ? 0 : width),
height_(height < 0 ? 0 : height) {
}
// Destructor is intentionally made non virtual and protected.
// Do not make this public.
~SizeBase() {}
private:
Type width_;
Type height_;
};
} // namespace gfx
#endif // UI_GFX_GEOMETRY_SIZE_BASE_H_
......@@ -8,7 +8,23 @@
namespace gfx {
template class SizeBase<SizeF, float>;
float SizeF::GetArea() const {
return width() * height();
}
void SizeF::Enlarge(float grow_width, float grow_height) {
SetSize(width() + grow_width, height() + grow_height);
}
void SizeF::SetToMin(const SizeF& other) {
width_ = width() <= other.width() ? width() : other.width();
height_ = height() <= other.height() ? height() : other.height();
}
void SizeF::SetToMax(const SizeF& other) {
width_ = width() >= other.width() ? width() : other.width();
height_ = height() >= other.height() ? height() : other.height();
}
std::string SizeF::ToString() const {
return base::StringPrintf("%fx%f", width(), height());
......
......@@ -5,22 +5,43 @@
#ifndef UI_GFX_GEOMETRY_SIZE_F_H_
#define UI_GFX_GEOMETRY_SIZE_F_H_
#include <cmath>
#include <iosfwd>
#include <string>
#include "base/compiler_specific.h"
#include "ui/gfx/geometry/size_base.h"
#include "ui/gfx/gfx_export.h"
namespace gfx {
// A floating version of gfx::Size.
class GFX_EXPORT SizeF : public SizeBase<SizeF, float> {
class GFX_EXPORT SizeF {
public:
SizeF() : SizeBase<SizeF, float>(0, 0) {}
SizeF(float width, float height) : SizeBase<SizeF, float>(width, height) {}
SizeF() : width_(0.f), height_(0.f) {}
SizeF(float width, float height)
: width_(fmaxf(0, width)), height_(fmaxf(0, height)) {}
~SizeF() {}
float width() const { return width_; }
float height() const { return height_; }
void set_width(float width) { width_ = fmaxf(0, width); }
void set_height(float height) { height_ = fmaxf(0, height); }
float GetArea() const;
void SetSize(float width, float height) {
set_width(width);
set_height(height);
}
void Enlarge(float grow_width, float grow_height);
void SetToMin(const SizeF& other);
void SetToMax(const SizeF& other);
bool IsEmpty() const { return !width() || !height(); }
void Scale(float scale) {
Scale(scale, scale);
}
......@@ -30,6 +51,10 @@ class GFX_EXPORT SizeF : public SizeBase<SizeF, float> {
}
std::string ToString() const;
private:
float width_;
float height_;
};
inline bool operator==(const SizeF& lhs, const SizeF& rhs) {
......@@ -46,10 +71,6 @@ inline SizeF ScaleSize(const SizeF& p, float scale) {
return ScaleSize(p, scale, scale);
}
#if !defined(COMPILER_MSVC) && !defined(__native_client__)
extern template class SizeBase<SizeF, float>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
......
......@@ -4,7 +4,6 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_base.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/size_f.h"
......
......@@ -52,7 +52,6 @@
'geometry/scroll_offset.h',
'geometry/size.cc',
'geometry/size.h',
'geometry/size_base.h',
'geometry/size_conversions.cc',
'geometry/size_conversions.h',
'geometry/size_f.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment