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

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: RangeF unit tests 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') | ui/gfx/range/range_unittest.cc » ('J')
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..0391350ea9f9c30a74476d07a2727b1a9677a8b9 100644
--- a/ui/gfx/range/range.h
+++ b/ui/gfx/range/range.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2015 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.
@@ -6,6 +6,7 @@
#define UI_GFX_RANGE_RANGE_H_
#include <ostream>
+#include <sstream>
#include <string>
#include "base/basictypes.h"
@@ -26,71 +27,140 @@ typedef struct _NSRange NSRange;
namespace gfx {
-// A Range contains two integer values that represent a numeric range, like the
Robert Sesek 2015/01/30 20:47:02 This comment was lost; please restore it.
-// 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();
+template <typename T>
+class TRangeBase;
- // Initializes the range with a start and end.
- Range(size_t start, size_t end);
+namespace internal {
- // 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 <>
+GFX_EXPORT TRangeBase<size_t> InvalidRange<size_t>();
Robert Sesek 2015/01/30 20:47:02 Why do these need to be GFX_EXPORT if they're inte
ckocagil 2015/01/31 02:44:39 Done.
+
+template <>
+GFX_EXPORT TRangeBase<float> InvalidRange<float>();
+
+GFX_EXPORT bool IsValidRange(const TRangeBase<size_t>& range);
+GFX_EXPORT bool IsValidRange(const TRangeBase<float>& range);
+
+GFX_EXPORT size_t RangeLength(const TRangeBase<size_t>& range);
+GFX_EXPORT float RangeLength(const TRangeBase<float>& range);
+
+GFX_EXPORT bool RangeIsEmpty(const TRangeBase<size_t>& range);
+GFX_EXPORT bool RangeIsEmpty(const TRangeBase<float>& range);
- // Returns a range that is invalid, which is {size_t_max,size_t_max}.
- static const Range InvalidRange();
+} // namespace internal
- // Checks if the range is valid through comparision to InvalidRange().
- bool IsValid() const;
+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>();
+ }
+
+ // 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();
+ }
+ 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_;
+};
+
+// TODO(ckocagil): Move these constructors to TRangeBase and use delegated
+// constructors (C++11) after the feature is allowed.
+class Range : public TRangeBase<size_t> {
Robert Sesek 2015/01/30 20:47:02 This class should be GFX_EXPORT.
ckocagil 2015/01/31 02:44:39 Done. (ditto RangeF)
+ public:
+ // Creates an empty range {0,0}.
+ Range() : TRangeBase(0, 0) {}
+
+ // Initializes the range with a start and end.
+ Range(size_t start, size_t end) : TRangeBase(start, end) {}
+
+ Range(const TRangeBase<size_t>& other) : TRangeBase(other) {}
+
+ // Initializes the range with the same start and end positions.
+ explicit Range(size_t position) : TRangeBase(position, 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.
+ GFX_EXPORT Range(const CHARRANGE& range, LONG total_length = -1);
Robert Sesek 2015/01/30 20:47:02 Why is this ctor GFX_EXPORT but none of the others
ckocagil 2015/01/31 02:44:39 Removed, the class is itself GFX_EXPORT now. Same
+#endif
#if defined(OS_MACOSX)
Range& operator=(const NSRange& range);
@@ -99,18 +169,27 @@ class GFX_EXPORT Range {
// is_reversed(), the range will get flipped when converted to an NSRange.
NSRange ToNSRange() const;
#elif defined(OS_WIN)
- CHARRANGE ToCHARRANGE() const;
+ GFX_EXPORT CHARRANGE ToCHARRANGE() const;
Robert Sesek 2015/01/30 20:47:02 Same, why is this GFX_EXPORT?
#endif
// GTK+ has no concept of a range.
+};
- std::string ToString() const;
+class RangeF : public TRangeBase<float> {
Robert Sesek 2015/01/30 20:47:02 Same, GFX_EXPORT.
+ public:
+ // Creates an empty range {0,0}.
+ RangeF() : TRangeBase(0.0f, 0.0f) {}
- private:
- size_t start_;
- size_t end_;
+ // Initializes the range with a start and end.
+ RangeF(float start, float end) : TRangeBase(start, end) {}
+
+ RangeF(const TRangeBase<float>& other) : TRangeBase(other) {}
+
+ // Initializes the range with the same start and end positions.
+ explicit RangeF(float position) : TRangeBase(position, 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') | ui/gfx/range/range_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698