summaryrefslogtreecommitdiffstats
path: root/ui/base/view_prop.cc
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-20 18:27:06 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-20 18:27:06 +0000
commit9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634 (patch)
tree3b7332926a05a1c8382eb27422c385b56b29cb24 /ui/base/view_prop.cc
parenta12f7fbe12afffb4b1b31ec0d6b0988f1f9a6554 (diff)
downloadchromium_src-9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634.zip
chromium_src-9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634.tar.gz
chromium_src-9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634.tar.bz2
Move a bunch of random other files to src/ui/base
BUG=none TEST=none TBR=brettw Review URL: http://codereview.chromium.org/6257006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71970 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/view_prop.cc')
-rw-r--r--ui/base/view_prop.cc103
1 files changed, 103 insertions, 0 deletions
diff --git a/ui/base/view_prop.cc b/ui/base/view_prop.cc
new file mode 100644
index 0000000..119228f
--- /dev/null
+++ b/ui/base/view_prop.cc
@@ -0,0 +1,103 @@
+// Copyright (c) 2011 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.
+
+#include "ui/base/view_prop.h"
+
+#include <set>
+
+namespace ui {
+
+// Maints the actual view, key and data.
+class ViewProp::Data : public base::RefCounted<ViewProp::Data> {
+ public:
+ // Returns the Data* for the view/key pair. If |create| is false and |Get|
+ // has not been invoked for the view/key pair, NULL is returned.
+ static void Get(gfx::NativeView view,
+ const char* key,
+ bool create,
+ scoped_refptr<Data>* data) {
+ if (!data_set_)
+ data_set_ = new DataSet;
+ scoped_refptr<Data> new_data(new Data(view, key));
+ DataSet::const_iterator i = data_set_->find(new_data.get());
+ if (i != data_set_->end()) {
+ *data = *i;
+ return;
+ }
+ if (!create)
+ return;
+ data_set_->insert(new_data.get());
+ *data = new_data.get();
+ }
+
+ // The data.
+ void set_data(void* data) { data_ = data; }
+ void* data() const { return data_; }
+
+ const char* key() const { return key_; }
+
+ private:
+ friend class base::RefCounted<Data>;
+
+ // Used to order the Data in the map.
+ class DataComparator {
+ public:
+ bool operator()(const Data* d1, const Data* d2) const {
+ return (d1->view_ == d2->view_) ? (d1->key_ < d2->key_) :
+ (d1->view_ < d2->view_);
+ }
+ };
+
+ typedef std::set<Data*, DataComparator> DataSet;
+
+ Data(gfx::NativeView view, const char* key)
+ : view_(view),
+ key_(key),
+ data_(NULL) {}
+
+ ~Data() {
+ DataSet::iterator i = data_set_->find(this);
+ // Also check for equality using == as |Get| creates dummy values in order
+ // to look up a value.
+ if (i != data_set_->end() && *i == this)
+ data_set_->erase(i);
+ }
+
+ // The existing set of Data is stored here. ~Data removes from the set.
+ static DataSet* data_set_;
+
+ const gfx::NativeView view_;
+ const char* key_;
+ void* data_;
+
+ DISALLOW_COPY_AND_ASSIGN(Data);
+};
+
+// static
+ViewProp::Data::DataSet* ViewProp::Data::data_set_ = NULL;
+
+ViewProp::ViewProp(gfx::NativeView view, const char* key, void* data) {
+ Data::Get(view, key, true, &data_);
+ data_->set_data(data);
+}
+
+ViewProp::~ViewProp() {
+ // This is done to provide similar semantics to SetProp. In particular it's
+ // assumed that ~ViewProp should behave as though RemoveProp was invoked.
+ data_->set_data(NULL);
+}
+
+// static
+void* ViewProp::GetValue(gfx::NativeView view, const char* key) {
+ scoped_refptr<Data> data;
+ Data::Get(view, key, false, &data);
+ return data.get() ? data->data() : NULL;
+}
+
+// static
+const char* ViewProp::Key() const {
+ return data_->key();
+}
+
+} // namespace ui