1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_GFX_SIZE_BASE_H_
6#define UI_GFX_SIZE_BASE_H_
7
8#include "ui/base/ui_export.h"
9
10namespace gfx {
11
12// A size has width and height values.
13template<typename Class, typename Type>
14class UI_EXPORT SizeBase {
15 public:
16  Type width() const { return width_; }
17  Type height() const { return height_; }
18
19  Type GetArea() const { return width_ * height_; }
20
21  void SetSize(Type width, Type height) {
22    set_width(width);
23    set_height(height);
24  }
25
26  void Enlarge(Type width, Type height) {
27    set_width(width_ + width);
28    set_height(height_ + height);
29  }
30
31  void set_width(Type width) {
32    width_ = width < 0 ? 0 : width;
33  }
34  void set_height(Type height) {
35    height_ = height < 0 ? 0 : height;
36  }
37
38  void SetToMin(const Class& other) {
39    width_ = width_ <= other.width_ ? width_ : other.width_;
40    height_ = height_ <= other.height_ ? height_ : other.height_;
41  }
42
43  void SetToMax(const Class& other) {
44    width_ = width_ >= other.width_ ? width_ : other.width_;
45    height_ = height_ >= other.height_ ? height_ : other.height_;
46  }
47
48  bool IsEmpty() const {
49    return (width_ == 0) || (height_ == 0);
50  }
51
52 protected:
53  SizeBase(Type width, Type height)
54      : width_(width < 0 ? 0 : width),
55      height_(height < 0 ? 0 : height) {
56  }
57
58  // Destructor is intentionally made non virtual and protected.
59  // Do not make this public.
60  ~SizeBase() {}
61
62 private:
63  Type width_;
64  Type height_;
65};
66
67}  // namespace gfx
68
69#endif  // UI_GFX_SIZE_BASE_H_
70