Archive for cpp

boost signal Observer pattern

boost signal 的观察者模式(Observer pattern):
函数指针是解耦对象关系的最佳利器。
Signal(如boost的signal和glib中的signal)机制是一个典型的例子,一个对象自身的状态可能是在变化的(或者会触发一些事件),
而其它对象关心它的变化。一旦该对象有变化发生,其它对象要执行相应的操作。

如果该对象直接去调用其它对象的函数,功能是完成了,但对象之间的耦合太紧了。

如何把这种耦 合降到最低呢,signal机制是很好的办法。它的原理大致如下:其它关注该对象变化的对象主动注册一个回调函数到该对象中。
一旦该对象有变化发生,就调 用这些回调函数通知其它对象。功能同样实现了,但它们之间的耦合度降低了。

http://www.scottcollins.net/articles/a-deeper-look-at-signals-and-slots.html

"Signals and Slots" are a generalized implementation of the Observer pattern.

A signal is an observable event, or at least notification that the event happened.

A slot is a potential observer, typically in the form a function to be called.

You connect a signal to a slot to establish the observable-observer relationship.

评论

使用 boost::thread boost::test

另一个文件使用 boost::thread  boost::test 

#include < boost/test/auto_unit_test.hpp >
#include < boost/thread/thread.hpp >
#include < boost/bind.hpp >
#include < windows.h >
#include < iostream >
int count = 0;
boost::mutex mutex;
void increment_count(int i, int j)
{
boost::mutex::scoped_lock lock(mutex);
count++;
Sleep(30);
std::cout << "count = " << count << " i= " <

}

BOOST_AUTO_TEST_CASE( thread )
{
boost::thread_group threads;
for (int i = 0; i < 10; ++i)
threads.create_thread(
boost::bind(&increment_count,i,i+1));
threads.join_all();
}

Comments (2)

boost test

boost test 免连接link

//如此顺序,才可以保证多个文件的test case ,可以在release版本运行正确
#define BOOST_AUTO_TEST_MAIN
#include < boost/test/auto_unit_test.hpp >
#include < boost/test/included/unit_test_framework.hpp >

BOOST_AUTO_TEST_CASE( test1 )
{
BOOST_CHECK( true );
}

BOOST_AUTO_TEST_CASE( test2 )
{
BOOST_CHECK( true );
}

Comments (5)

boost 安装

boost 安装

1)运行
D:\boost\tools\build\jam_src\build.bat
产生:
D:\boost\tools\build\jam_src\bin.ntx86\bjam.exe

2)bjam.exe 拷贝到d:\boost目录下

3)编译thread库
D:\boost>bjam -sTOOLS=msvc --with-thread install

4)boost库被自动安装到c:\boost

vc6菜单 /tools/options/directories/

include files 增加:
C:\BOOST\INCLUDE\BOOST-1_33_1
Library files 增加:
C:\BOOST\LIB

评论

菜单子类化

关于菜单子类化的一个例子
http://www.codeproject.com/dll/subhook.asp

很好玩的一个小例子,可以修改记事本的主菜单。

http://www.codeproject.com/dialog/AOTop.asp

可以修改系统菜单

关键词:

popup menu
setwindowlong

WM_INITMENUPOPUP

WM_CONTEXTMENU:

Superclassing

SetWindowsHookEx(WH_CALLWNDPROC

Cross Process Subclassing
GetMenu
WM_CONTEXTMENU
HMENU hMenu = CreateMenu();
HMENU hMenuPopup = CreatePopupMenu();
AppendMenu(hMenuPopup, MF_STRING|MF_OWNERDRAW, ID_FILE_NEW, (LPCTSTR)"&Pub");

 hAppMenu=GetMenu(hHookedWindow);
hAppendMenu=CreateMenu(); //Create the menu
AppendMenu(hAppMenu,MF_STRING + MF_POPUP,
(unsigned int)hAppendMenu,"HTML"); //add new menu

感慨:

以前,解决一个比较棘手的技术问题,比如屏幕取词,在98年,需要耗我半年的时间去在黑暗中摸索。

现在,借助Goolge,只需要知道一些大概的关键词,一般半天,最多一个星期,就可以把解决方法推演出来。 不过,怎么修改notepad的右键快捷菜单,(content menu),好象如果不借助屏幕取词的功夫,是很困难的事情。谁如果知道,告诉我呀。

 

评论


0.020 sec