Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 // Helper class used to find the RenderViewObservers for a given RenderView. | |
| 6 // | |
| 7 // Example usage: | |
| 8 // | |
| 9 // class MyRVO : public RenderViewObserver, | |
| 10 // public RenderViewObserverTracker<MyRVO> { | |
| 11 // ... | |
| 12 // }; | |
| 13 // | |
| 14 // RENDER_VIEW_OBSERVER_TRACKER_MAP(MyRVO); | |
|
jam
2011/04/05 18:10:07
I like this approach a lot more, very nice :)
Can
Lei Zhang
2011/04/05 21:03:16
I couldn't put it in the constructor, but I put it
jam
2011/04/05 21:08:35
wouldn't putting it in the header, outside any cla
Matt Perry
2011/04/07 01:32:21
Since it's declared as a static member in the clas
| |
| 15 // | |
| 16 // MyRVO::MyRVO(RenderView* render_view) | |
| 17 // : RenderViewObserver(render_view), | |
| 18 // RenderViewObserverTracker<SearchBox>(render_view) { | |
| 19 // ... | |
| 20 // } | |
| 21 // | |
| 22 // void SomeFunction(RenderView* rv) { | |
| 23 // MyRVO* my_rvo = new MyRVO(rv); | |
| 24 // MyRVO* my_rvo_tracked = MyRVO::Get(rv); | |
| 25 // // my_rvo == my_rvo_tracked | |
| 26 // } | |
| 27 | |
| 28 #ifndef CONTENT_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_ | |
| 29 #define CONTENT_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_ | |
| 30 #pragma once | |
| 31 | |
| 32 #include <map> | |
| 33 | |
| 34 #include "base/lazy_instance.h" | |
| 35 | |
| 36 class RenderView; | |
| 37 | |
| 38 #define RENDER_VIEW_OBSERVER_TRACKER_MAP(ClassType) \ | |
| 39 template <> \ | |
| 40 base::LazyInstance<std::map<RenderView*, ClassType*> > \ | |
| 41 RenderViewObserverTracker<ClassType>::render_view_map_( \ | |
| 42 base::LINKER_INITIALIZED) | |
| 43 | |
| 44 template <class T> | |
| 45 class RenderViewObserverTracker { | |
| 46 public: | |
| 47 static T* Get(RenderView* render_view) { | |
| 48 return render_view_map_.Get()[render_view]; | |
| 49 } | |
| 50 | |
| 51 explicit RenderViewObserverTracker(RenderView* render_view) | |
| 52 : render_view_(render_view) { | |
| 53 render_view_map_.Get()[render_view] = static_cast<T*>(this); | |
| 54 } | |
| 55 ~RenderViewObserverTracker() { | |
| 56 render_view_map_.Get().erase(render_view_); | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 RenderView* render_view_; | |
| 61 | |
| 62 static base::LazyInstance<std::map<RenderView*, T*> > render_view_map_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(RenderViewObserverTracker<T>); | |
| 65 }; | |
| 66 | |
| 67 #endif // CONTENT_RENDERER_RENDER_VIEW_OBSERVER_TRACKER_H_ | |
| OLD | NEW |