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

Unified Diff: ui/gfx/range/range.h

Issue 876873003: Add float version of gfx::Range (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: newline at EOF Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ui/gfx/range/range.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/range/range.h
diff --git a/ui/gfx/range/range.h b/ui/gfx/range/range.h
index d0d2a3ec954112016b8058084c133d09571c2d14..763d98d3e95a4271d0c7a703ccaff10c90be79bf 100644
--- a/ui/gfx/range/range.h
+++ b/ui/gfx/range/range.h
@@ -1,11 +1,13 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2015 The Chromium Authors. All rights reserved.
Robert Sesek 2015/02/02 21:42:11 The copyright year should actually remain the same
ckocagil 2015/02/03 00:51:05 Done.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_GFX_RANGE_RANGE_H_
#define UI_GFX_RANGE_RANGE_H_
+#include <algorithm>
#include <ostream>
+#include <sstream>
#include <string>
#include "base/basictypes.h"
@@ -26,71 +28,145 @@ typedef struct _NSRange NSRange;
namespace gfx {
-// A Range contains two integer values that represent a numeric range, like the
-// range of characters in a text selection. A range is made of a start and end
-// position; when they are the same, the Range is akin to a caret. Note that
-// |start_| can be greater than |end_| to respect the directionality of the
-// range.
-class GFX_EXPORT Range {
- public:
- // Creates an empty range {0,0}.
- Range();
+namespace internal {
- // Initializes the range with a start and end.
- Range(size_t start, size_t end);
+template <typename T>
+class TRangeBase;
- // Initializes the range with the same start and end positions.
- explicit Range(size_t position);
+template <typename T>
+TRangeBase<T> InvalidRange();
- // Platform constructors.
-#if defined(OS_MACOSX)
- explicit Range(const NSRange& range);
-#elif defined(OS_WIN)
- // The |total_length| paramater should be used if the CHARRANGE is set to
- // {0,-1} to indicate the whole range.
- Range(const CHARRANGE& range, LONG total_length = -1);
-#endif
+template <>
+TRangeBase<size_t> InvalidRange<size_t>();
- // Returns a range that is invalid, which is {size_t_max,size_t_max}.
- static const Range InvalidRange();
+template <>
+TRangeBase<float> InvalidRange<float>();
- // Checks if the range is valid through comparision to InvalidRange().
- bool IsValid() const;
+bool IsValidRange(const TRangeBase<size_t>& range);
+bool IsValidRange(const TRangeBase<float>& range);
+
+size_t RangeLength(const TRangeBase<size_t>& range);
+float RangeLength(const TRangeBase<float>& range);
+
+bool RangeIsEmpty(const TRangeBase<size_t>& range);
+bool RangeIsEmpty(const TRangeBase<float>& range);
+
+template <typename T>
+class TRangeBase {
+ public:
+ // Initializes the range with a start and end.
+ TRangeBase(T start, T end) : start_(start), end_(end) {}
+
+ TRangeBase(const TRangeBase<T>& other)
+ : start_(other.start()), end_(other.end()) {}
+
+ // Returns a range that is invalid.
+ static const TRangeBase<T> InvalidRange() {
+ return internal::InvalidRange<T>();
Robert Sesek 2015/02/02 21:42:11 Don't need internal:: since this is in the namespa
ckocagil 2015/02/03 00:51:05 In this case we actually need it since TRangeBase:
+ }
+
+ // Checks if the range is valid.
+ bool IsValid() const {
+ return internal::IsValidRange(*this);
+ }
// Getters and setters.
- size_t start() const { return start_; }
- void set_start(size_t start) { start_ = start; }
+ T start() const { return start_; }
+ void set_start(T start) { start_ = start; }
- size_t end() const { return end_; }
- void set_end(size_t end) { end_ = end; }
+ T end() const { return end_; }
+ void set_end(T end) { end_ = end; }
// Returns the absolute value of the length.
- size_t length() const {
- ptrdiff_t length = end() - start();
- return length >= 0 ? length : -length;
+ T length() const {
+ return internal::RangeLength(*this);
}
- bool is_reversed() const { return start() > end(); }
- bool is_empty() const { return start() == end(); }
+ bool is_reversed() const { return start_ > end_; }
+ bool is_empty() const {
+ return internal::RangeIsEmpty(*this);
+ }
// Returns the minimum and maximum values.
- size_t GetMin() const;
- size_t GetMax() const;
+ T GetMin() const { return std::min(start_, end_); }
+ T GetMax() const { return std::max(start_, end_); }
- bool operator==(const Range& other) const;
- bool operator!=(const Range& other) const;
- bool EqualsIgnoringDirection(const Range& other) const;
+ bool operator==(const TRangeBase& other) const {
+ return start_ == other.start() && end_ == other.end();
Robert Sesek 2015/02/02 21:42:11 Use other.start_ and other.end_ for consistency.
ckocagil 2015/02/03 00:51:05 Done.
+ }
+ bool operator!=(const TRangeBase& other) const {
+ return !(*this == other);
+ }
+ bool EqualsIgnoringDirection(const TRangeBase& other) const {
+ return GetMin() == other.GetMin() && GetMax() == other.GetMax();
+ }
// Returns true if this range intersects the specified |range|.
- bool Intersects(const Range& range) const;
+ bool Intersects(const TRangeBase& range) const {
+ return IsValid() && range.IsValid() &&
+ !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
+ }
// Returns true if this range contains the specified |range|.
- bool Contains(const Range& range) const;
+ bool Contains(const TRangeBase& range) const {
+ return IsValid() && range.IsValid() &&
+ GetMin() <= range.GetMin() && range.GetMax() <= GetMax();
+ }
// Computes the intersection of this range with the given |range|.
// If they don't intersect, it returns an InvalidRange().
// The returned range is always empty or forward (never reversed).
- Range Intersect(const Range& range) const;
+ TRangeBase Intersect(const TRangeBase& range) const {
+ T min = std::max(GetMin(), range.GetMin());
+ T max = std::min(GetMax(), range.GetMax());
+
+ if (min >= max) // No intersection.
+ return InvalidRange();
+
+ return TRangeBase(min, max);
+ }
+
+ std::string ToString() const {
+ std::ostringstream stream;
+ stream << "{" << start_ << "," << end_ << "}";
+ return stream.str();
+ }
+
+ private:
+ T start_;
+ T end_;
+};
+
+} // namespace internal
+
+// A Range contains two integer values that represent a numeric range, like the
+// range of characters in a text selection. A range is made of a start and end
+// position; when they are the same, the Range is akin to a caret. Note that
+// |start_| can be greater than |end_| to respect the directionality of the
+// range.
+// TODO(ckocagil): Move these constructors to TRangeBase and use delegated
+// constructors (C++11) after the feature is allowed.
+class GFX_EXPORT Range : public internal::TRangeBase<size_t> {
+ public:
+ // Creates an empty range {0,0}.
+ Range();
+
+ // Initializes the range with a start and end.
+ Range(size_t start, size_t end);
+
+ Range(const internal::TRangeBase<size_t>& other);
Robert Sesek 2015/02/02 21:42:11 Do you have to use the internal name here, or can
ckocagil 2015/02/03 00:51:05 We need to construct a Range from a TRangeBase her
+
+ // Initializes the range with the same start and end positions.
+ explicit Range(size_t position);
+
+ // Platform constructors.
+#if defined(OS_MACOSX)
+ explicit Range(const NSRange& range);
+#elif defined(OS_WIN)
+ // The |total_length| paramater should be used if the CHARRANGE is set to
+ // {0,-1} to indicate the whole range.
+ Range(const CHARRANGE& range, LONG total_length = -1);
+#endif
#if defined(OS_MACOSX)
Range& operator=(const NSRange& range);
@@ -102,15 +178,28 @@ class GFX_EXPORT Range {
CHARRANGE ToCHARRANGE() const;
#endif
// GTK+ has no concept of a range.
+};
- std::string ToString() const;
+// A RangeF contains two float values that represent a numeric range, like a
+// range of positions on a line. A RangeF is made of a start and end position;
+// when they are the same, the RangeF is considered empty. Note that |start_|
+// can be greater than |end_| to respect the directionality of the range.
+class GFX_EXPORT RangeF : public internal::TRangeBase<float> {
+ public:
+ // Creates an empty range {0,0}.
+ RangeF();
- private:
- size_t start_;
- size_t end_;
+ // Initializes the range with a start and end.
+ RangeF(float start, float end);
+
+ RangeF(const internal::TRangeBase<float>& other);
Robert Sesek 2015/02/02 21:42:11 Same.
+
+ // Initializes the range with the same start and end positions.
+ explicit RangeF(float position);
};
GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range);
+GFX_EXPORT std::ostream& operator<<(std::ostream& os, const RangeF& range);
} // namespace gfx
« no previous file with comments | « no previous file | ui/gfx/range/range.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698