栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

山东大学软件工程应用与实践——Rime输入法核心代码分析(二)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

山东大学软件工程应用与实践——Rime输入法核心代码分析(二)

2021SC@SDUSC

目录

 一、项目源代码总体结构:

# 二、RimeWithWeasel核心代码具体分析

1.WeaselUI初始化具体分析

1.创建输入法界面:

2.销毁界面:

3.界面显示和隐藏:

1 数据隐藏

2 节省编译时间

3 副作用

4 C++11 风格的 PIMPL


 一、项目源代码总体结构:

        此次主要分析rime输入法的windows版本——小狼毫weasel的源码,从GitHub下载并解包得到以下文件:

        include,lib,librime,output,plum,pyWeasel,resource,RimeWithWeasel,test,update,WeaselDeployer,WeaselIME,WeaselIPC,WeaselIPCServer,WeaselServer,WeaselSetup,WeaselTSF,WeaselIUI18个模块组成了完整的项目,include文件主要包含了项目中其他部分所引用的头文件,lib和librime涉及到对输入法自定义时所新导入的代码库,由于现在项目处于初始状态,没有导入任何库,所以这两个文件夹是空白的,output文件中包含了对输入法外在样式的设计,plum是东风破文件夹,pyWeasel是将Linux版本程序转化为Windows版本weasel时,所涉及到的python脚本,resource中是图标文件,RimeWithWeasel是本项目的核心代码,test是测试文件,用来测试程序是否出现错误,update文件是输入法的更新文件,里面具体写了输入法进行更新操作时所需要的URL,weaselDeployer是weasel的部署代码,实现weasel的部署,后面的文件夹,每一个都是一个子项目,可以看出,本项目的核心代码是RimeWithWeasel文件夹中的代码,下面将对此文件夹中的核心代码进行分析。

# 二、RimeWithWeasel核心代码具体分析

1.WeaselUI初始化具体分析

        ui模块位于WeaselUI文件夹中,主要负责对输入法ui的更新和构建,并依赖于include文件夹中的WeaselUI.h头文件:

        在WeaselUI.h头文件中,定义了一个输入法界面接口类UI,它是一个抽象类,其中包含了对输入法界面的各种操作,并在WeaselUI.cpp文件中给出了具体的实现:

1.创建输入法界面:
bool UI::Create(HWND parent)
{
	if (pimpl_)
		return true;

	pimpl_ = new UIImpl(*this);
	if (!pimpl_)
		return false;

	pimpl_->panel.Create(parent, 0, 0, WS_POPUP, WS_EX_TOOLWINDOW | WS_EX_TOPMOST, 0U, 0);
	return true;
}

        如果界面已经存在,则调用pimpl_ = new UIImpl(*this);对界面进行初始化,以便于后续显示新的内容,如果界面还不存在,则使用panel.Create()方法重新绘制出一个空白的界面,并等待后续处理。

2.销毁界面:
if (pimpl_)
	{
		if (pimpl_->panel.IsWindow())
			pimpl_->panel.DestroyWindow();
		delete pimpl_;
		pimpl_ = 0;
	}

        当pimpl_已经存在的前提下,继续判断当前pimpl_是否是一个窗口,如果是一个panel窗口的话,则调用panel.DestroyWindow()方法将窗口删除掉,并同时删除变量pimpl_,为变量pimpl_赋值为0(为什么赋值为0暂时还没有搞清楚)

3.界面显示和隐藏:
void UI::Show()
{
	if (pimpl_)
	{
		pimpl_->Show();
	}
}

void UI::Hide()
{
	if (pimpl_)
	{
		pimpl_->Hide();
	}
}

void UI::ShowWithTimeout(DWORD millisec)
{
	if (pimpl_)
	{
		pimpl_->ShowWithTimeout(millisec);
	}
}

bool UI::IsCountingDown() const
{
	return pimpl_ && pimpl_->timer != 0;
}

bool UI::IsShown() const
{
	return pimpl_ && pimpl_->IsShown();
}

   关于pimpl_:C++ 中的“柴郡猫技术”(Cheshire Cat Idiom),又称为 PIMPL(Pointer to IMPLementation) ,Opaque Pointer 等,是一种在类中只定义接口,而将私有数据成员封装在另一个实现类中的惯用法。该方法主要是为了隐藏类的数据以及减轻编译时的压力。

1 数据隐藏

C++ 中我们在头文件中定义类,比如一个简单的 Student 类由如下方式定义:

// student.h

class Student

{

public:

Student(); // Constructor

~Student(); // Destructor

void sayHello(std::ostream &out);

std::string getName() const;

void setName(std::string name);

int getAge() const;

void setAge(int age);

private:

string _name;

unsigned int _age;

};

这里 _name 和 _age 是 Student 类的私有数据成员。然而使用该类的客户往往更关心类的接口(该类能提供哪些服务),我们希望隐藏 Student 类的私有数据成员,这时候就可以利用 PIMPL 模式:定义一个实现类,将 Student 类的数据封装到这个实现类中,同时在 Student 类中保留一个指向该实现类的指针变量。用代码解释更清楚:

// student.h

class Student

{

public:

Student(); // Constructor

~Student(); // Destructor

void sayHello(std::ostream &out);

std::string getName() const;

void setName(std::string name);

int getAge() const;

void setAge(int age);

private:

class CheshireCat; // Forward declaration

CheshireCat *_smileCat;

};

// student.cpp

#include "student.h"

#include

#include

using namespace std;

class Student::CheshireCat

{

public:

CheshireCat() :

_name(string("Guy")), _age(18) {}

~CheshireCat() {}

string _name;

int _age;

};

Student::Student() :

_smileCat(new CheshireCat())

{

}

Student::~Student()

{

delete _smileCat;

}

void Student::sayHello(std::ostream &out)

{

out << "Hello! My name is " <<

_smileCat->_name << "." << endl;

out << "I am " << _smileCat->_age <<

" years old." << endl;

}

string Student::getName()

{

return _smileCat->_name;

}

void Student::setName(string name)

{

_smileCat->_name = name;

}

int Student::getAge()

{

return _smileCat->_age;

}

void Student::setAge(int age)

{

_smileCat->_age = age;

}

好了,现在Student类的接口没有任何变化,但是头文件中原有的私有数据成员消失了,只留下一只微笑的柴郡猫(CheshireCat *_smileCat;)。


2 节省编译时间

使用 PIMPL 可以帮助我们节省程序编译的时间。考虑下面这个类:

// A.h

#include "BigClass.h"

#include "VeryBigClass"

class A

{

//...

private:

BigClass big;

VeryBigClass veryBig;

};

我们知道C++中有头文件(.h)和实现文件(.cpp),一旦头文件发生变化,不管多小的变化,所有引用它的文件都必须重新编译。对于一个很大的项目,C++一次编译可能就会耗费大量的时间,如果代码需要频繁改动,那真的是不能忍。这里如果我们把 BigClass big; 和 VeryBigClass veryBig; 利用 PIMPL 封装到一个实现类中,就可以减少 A.h 的编译依赖,起到减少编译时间的效果:

// A.h

class A

{

public:

// 与原来相同的接口

private:

struct AImp;

AImp *pimpl;

};


3 副作用

使用 PIMPL 需要在堆空间上分配和释放内存,内存开销增加,同时也需要更多的间接指针跳转,因此有一些副作用。

虽然如此,PIMPL 仍然是一种实现数据隐藏、减少编译时间的有效方法。除非会引起显著的程序性能下降,推荐使用 PIMPL 进行设计。


4 C++11 风格的 PIMPL

现代 C++ 中不提倡使用 owning raw pointers 和 delete ,我们可以用智能指针实现 PIMPL。

// myclass.h

class MyClass

{

public:

private:

class AImpl; // forward declaration

unique_ptr _pimpl; // opaque type here

};

// myclass.cpp

class MyClass::AImpl

{

};

MyClass::MyClass() : _pimpl(new AImpl())

{

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/308829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号