先熟悉set的find原理

http://www.cnblogs.com/Clingingboy/p/3252136.html

这个类改造下,还是非常实用的,可以对于不同的类型做数据存储

一.ViewProp

// 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. #ifndef UI_BASE_VIEW_PROP_H_
#define UI_BASE_VIEW_PROP_H_ namespace ui { // ViewProp maintains a key/value pair for a particular view. ViewProp is
// designed as a replacement for the Win32's SetProp
, but does not make use of
// window manager memory. ViewProp shares similar semantics as SetProp, the
// value for a particular view/key pair comes from the last ViewProp created.
class UI_EXPORT ViewProp {
public:
// Associates data with a view/key pair. If a ViewProp has already been
// created for the specified pair |data| replaces the current value.
//
// ViewProp does *not* make a copy of the char*, the pointer is used for
// sorting.
ViewProp(gfx::AcceleratedWidget view, const char* key, void* data);
~ViewProp(); // Returns the value associated with the view/key pair, or NULL if there is
// none.
static void* GetValue(gfx::AcceleratedWidget view, const char* key); // Returns the key.
const char* Key() const; private:
class Data; // Stores the actual data.
scoped_refptr<Data> data_;
}; } // namespace ui #endif // UI_BASE_VIEW_PROP_H_

二.实现

ViewProp::ViewProp(gfx::AcceleratedWidget 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::AcceleratedWidget 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();
}

三.测试代码

TEST(ViewPropTest, Basic) {
gfx::AcceleratedWidget nv1 = reinterpret_cast<gfx::AcceleratedWidget>(1);
void* data1 = reinterpret_cast<void*>(11);
// Register a value for a view/key pair.
const char kKey1[] = "key_1";
ViewProp prop(nv1, kKey1, data1);
EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1));
}
  1. 构建一个ViewProp
  2. 然后使用ViewProp::GetValue静态方法查找

四.Data类的实现

ViewProp构造函数用Data::Get创建Data,查找的时候还是用Data::Get方法,区别在于构造函数时创建,查找时不会创建

// 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::AcceleratedWidget 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::AcceleratedWidget 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::AcceleratedWidget view_;
const char* key_;
void* data_; DISALLOW_COPY_AND_ASSIGN(Data);
}; // static
ViewProp::Data::DataSet* ViewProp::Data::data_set_ = NULL;

chrome ui源码剖析-ViewProp的更多相关文章

  1. chrome ui源码剖析-Accelerator(快捷键)

      好久没有自己写东西了,chrome有着取之不尽的技术精华供学习,记录一下. 源码目录: http://src.chromium.org/viewvc/chrome/trunk/src/ui/bas ...

  2. Google Chrome源码剖析【序】

    [序(本人什么都没做,完全转载)] 开源是口好东西,它让这个充斥着大量工业垃圾代码和教材玩具代码的行业,多了一些艺术气息和美的潜质.它使得每个人,无论你来自米国纽约还是中国铁岭,都有机会站在巨人的肩膀 ...

  3. Spark源码剖析 - SparkContext的初始化(三)_创建并初始化Spark UI

    3. 创建并初始化Spark UI 任何系统都需要提供监控功能,用浏览器能访问具有样式及布局并提供丰富监控数据的页面无疑是一种简单.高效的方式.SparkUI就是这样的服务. 在大型分布式系统中,采用 ...

  4. Chrome V8 引擎源码剖析

    Chrome V8 引擎源码剖析 V8 https://github.com/v8/v8 array & sort https://github.com/v8/v8/search?l=Java ...

  5. (升级版)Spark从入门到精通(Scala编程、案例实战、高级特性、Spark内核源码剖析、Hadoop高端)

    本课程主要讲解目前大数据领域最热门.最火爆.最有前景的技术——Spark.在本课程中,会从浅入深,基于大量案例实战,深度剖析和讲解Spark,并且会包含完全从企业真实复杂业务需求中抽取出的案例实战.课 ...

  6. Appuim源码剖析(Bootstrap)

    Appuim源码剖析(Bootstrap) SkySeraph Jan. 26th 2017 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www. ...

  7. 老李推荐:第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-面向控件编程VS面向坐标编程

    老李推荐:第14章1节<MonkeyRunner源码剖析> HierarchyViewer实现原理-面向控件编程VS面向坐标编程   poptest是国内唯一一家培养测试开发工程师的培训机 ...

  8. 老李推荐:第2章2节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NotesList简介

    老李推荐:第2章2节<MonkeyRunner源码剖析>了解你的测试对象: NotePad窗口Activity之NotesList简介   NotePad窗口Activity之NotesL ...

  9. 老李推荐: 第1章1节《MonkeyRunner源码剖析》概述:前言

    老李推荐: 第1章1节<MonkeyRunner源码剖析>概述:前言   前言 相信大家做过安卓移动平台UI自动化开发的必然会用过,至少听过MonkeyRunner这个名字.MonkeyR ...

随机推荐

  1. 永不改变的PCB设计黄金法则

    尽管目前半导体集成度越来越高,许多应用也都有随时可用的片上系统,同时许多功能强大且开箱即用的开发板也越来越可轻松获取,但许多使用案例中电子产品的应用仍然需要使用定制PCB.在一次性开发当中,即使一个普 ...

  2. MySQL管理工具MySQL Utilities — 介绍与安装(1)

    MySQL Utilities介绍 MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括: 管理工具 (克隆.复制.比较.差异.导出.导入) 复制工具 (安装.配 ...

  3. mac 安装gevent报错

    运行pip install gevent报错 错误信息如下 xcrun: error: invalid active developer path (/Library/Developer/Comman ...

  4. 百度编辑器ueditor 字符限制

    百度编辑器ueditor 字符限制 默认只能输入10000个字符 修改 ueditor.config.js // ,wordCount:true //是否开启字数统计 // ,maximumWords ...

  5. How to fix Eclipse Tomcat startup timeout in 45 seconds?

    命题:怎么修复 Eclipse 里启动 Tomcat 时, 默认启动超时时间为 45 秒的问题? 在 Eclipse 的 J2EE 项目里启动 Tomcat 来调试 Servlet 之类的,断点以后, ...

  6. [转] HTML 获取屏幕、浏览器、页面的高度宽度

    本篇主要介绍Web环境中屏幕.浏览器及页面的高度.宽度信息. 目录 1. 介绍:介绍页面的容器(屏幕.浏览器及页面).物理尺寸与分辨率.展示等内容. 2. 屏幕信息:介绍屏幕尺寸信息:如:屏幕.软件可 ...

  7. shell中信号处理

    参考: Shell 脚本中信号处理实践 Linux Shell 的信号 trap 功能你必须知道的细节   在 unix 里,可能发生的每一种类型的事件都是由一个独立的信号来描述,每一个信号都是一个小 ...

  8. Mvc+Dapper+存储过程分页10万条数据

    10万条数据采用存储过程分页实现(Mvc+Dapper+存储过程) 有时候大数据量进行查询操作的时候,查询速度很大强度上可以影响用户体验,因此自己简单写了一个demo,简单总结记录一下: 技术:Mvc ...

  9. 【LOJ】#2108. 「JLOI2015」装备购买

    题解 换成long double才过--出题人丧心病狂卡精度 只要按照费用排序从小到大排序,一个个插入线性基,插入的时候加上费用即可 代码 #include <bits/stdc++.h> ...

  10. bzoj [ZJOI2008]生日聚会Party

    思路:dp, 用dp[ i ][ j ][ u ][ v ] 表示, 有n个人,其中有j个是男生,后缀区间中男生人数减去女生人数的最大值为u, 女生人数减去男生人数 的最大值为v, 然后就能写出状态转 ...