OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #include "app/view_prop.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 namespace app { |
| 10 namespace win { |
| 11 |
| 12 // Maints the actual view, key and data. |
| 13 class ViewProp::Data : public base::RefCounted<ViewProp::Data> { |
| 14 public: |
| 15 // Returns the Data* for the view/key pair. If |create| is false and |Get| |
| 16 // has not been invoked for the view/key pair, NULL is returned. |
| 17 static void Get(gfx::NativeView view, |
| 18 const char* key, |
| 19 bool create, |
| 20 scoped_refptr<Data>* data) { |
| 21 if (!data_set_) |
| 22 data_set_ = new DataSet; |
| 23 scoped_refptr<Data> new_data(new Data(view, key)); |
| 24 DataSet::const_iterator i = data_set_->find(new_data.get()); |
| 25 if (i != data_set_->end()) { |
| 26 *data = *i; |
| 27 return; |
| 28 } |
| 29 if (!create) |
| 30 return; |
| 31 data_set_->insert(new_data.get()); |
| 32 *data = new_data.get(); |
| 33 } |
| 34 |
| 35 // The data. |
| 36 void set_data(void* data) { data_ = data; } |
| 37 void* data() const { return data_; } |
| 38 |
| 39 const char* key() const { return key_; } |
| 40 |
| 41 private: |
| 42 friend class base::RefCounted<Data>; |
| 43 |
| 44 // Used to order the Data in the map. |
| 45 class DataComparator { |
| 46 public: |
| 47 bool operator()(const Data* d1, const Data* d2) const { |
| 48 return (d1->view_ == d2->view_) ? (d1->key_ < d2->key_) : |
| 49 (d1->view_ < d2->view_); |
| 50 } |
| 51 }; |
| 52 |
| 53 typedef std::set<Data*, DataComparator> DataSet; |
| 54 |
| 55 Data(gfx::NativeView view, const char* key) |
| 56 : view_(view), |
| 57 key_(key), |
| 58 data_(NULL) {} |
| 59 |
| 60 ~Data() { |
| 61 DataSet::iterator i = data_set_->find(this); |
| 62 // Also check for equality using == as |Get| creates dummy values in order |
| 63 // to look up a value. |
| 64 if (i != data_set_->end() && *i == this) |
| 65 data_set_->erase(i); |
| 66 } |
| 67 |
| 68 // The existing set of Data is stored here. ~Data removes from the set. |
| 69 static DataSet* data_set_; |
| 70 |
| 71 const gfx::NativeView view_; |
| 72 const char* key_; |
| 73 void* data_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(Data); |
| 76 }; |
| 77 |
| 78 // static |
| 79 ViewProp::Data::DataSet* ViewProp::Data::data_set_ = NULL; |
| 80 |
| 81 ViewProp::ViewProp(gfx::NativeView view, const char* key, void* data) { |
| 82 Data::Get(view, key, true, &data_); |
| 83 data_->set_data(data); |
| 84 } |
| 85 |
| 86 ViewProp::~ViewProp() { |
| 87 // This is done to provide similar semantics to SetProp. In particular it's |
| 88 // assumed that ~ViewProp should behave as though RemoveProp was invoked. |
| 89 data_->set_data(NULL); |
| 90 } |
| 91 |
| 92 // static |
| 93 void* ViewProp::GetValue(gfx::NativeView view, const char* key) { |
| 94 scoped_refptr<Data> data; |
| 95 Data::Get(view, key, false, &data); |
| 96 return data.get() ? data->data() : NULL; |
| 97 } |
| 98 |
| 99 // static |
| 100 const char* ViewProp::Key() const { |
| 101 return data_->key(); |
| 102 } |
| 103 |
| 104 } // namespace win |
| 105 |
| 106 } // namespace app |
OLD | NEW |