map 컨테이너를 이용해 클래스 멤버함수의 콜백함수 등록과 호출하기.

이글은 예전 제 싸이월드 블로그에 작성한 글을 가져온 것입니다.

 

원문 : http://cy.cyworld.com/home/21147242/post/4D75C4B98555739A07C68401
원문 작성일 : 2011.03.08

--------------------------------------------------------------------------------------------------------------


제목 참 거창하네...
일하다가 번뜩 떠올라 짜봤습니다.
map 컨테이너를 이용해 등록된 콜백함수를 좀 더 빠르게 찾지 않을까 하는 기대감입니다.
등록되는 콜백함수는 일반함수가 될 수도 있으나 클래스 멤버함수도 등록될 수 있음을 보였습니다.
클래스 멤버함수와 함수포인터의 관계는 데브피아 강의에 잘 나와있습니다. (아래 링크 참조)


http://www.devpia.com/MAEUL/Contents/Detail.aspx?BoardID=51&MAEULNo=20&no=7431&ref=7431


아래와 같이 구상하고 흐믓해하며 자만심에 잠깐 빠져있다가
누군가도 나와같은 생각을 했을텐데.... 하고 검색해 봤습니다.
역시나... 기는 놈 위에 나는 분 계시더군요.
template 을 이용해 일반화까지 구현하여 블로그에 올리신 분이 계시더군요.
클래스 멤버함수 콜백 : http://blog.naver.com/yiyousung?Redirect=Log&logNo=80113313235
아래는 허접한 제 코드입니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<cstdio>
#include<map>
 
using namespace std ;
 
enum Psi
{
        eDescVideo,
        eDescAudio,
        eDescSubtitle
};
 
class CPsiDescriptor
{
public :
        int switchDesc(Psi psi) ;
 
 
private :
        int descVideo(int a, int b) ;
        int descAudio(int a, int b) ;
        int descSubtitle(int a, int b) ;
};
 
typedef int (CPsiDescriptor::*FUNC_DESC)(int a, int b) ;
 
int CPsiDescriptor::switchDesc(Psi psi)
{
        static int flagSwitchDesc = 0 ;
 
 
        static map<Psi, FUNC_DESC>mapDesc ;
        static map<Psi, FUNC_DESC>::iterator mapDescIter ;
 
 
        static FUNC_DESC funcDesc ;
 
        if(!flagSwitchDesc)
        {
                mapDesc.insert(make_pair(eDescVideo,&CPsiDescriptor::descVideo)) ;
                mapDesc.insert(make_pair(eDescAudio,&CPsiDescriptor::descAudio)) ;
                mapDesc.insert(make_pair(eDescSubtitle,&CPsiDescriptor::descSubtitle)) ;
 
 
                flagSwitchDesc = 1 ;
        }
 
 
        mapDescIter = mapDesc.find(psi) ;
 
 
        if(mapDescIter != mapDesc.end())
        {
                funcDesc = mapDescIter->second ;
                (this->*funcDesc)(2, 3) ;
        }
        else
        {
                printf("cannot find PSI tags\n") ;
        }
 
 
        return 1 ;
}
 
int CPsiDescriptor::descVideo(int a, int b)
{
        printf("callback : descVideo()\n") ;
        return 1 ;
}
 
 
int CPsiDescriptor::descAudio(int a, int b)
{
        printf("callback : descAudio()\n") ;
        return 1 ;
}
 
int CPsiDescriptor::descSubtitle(int a, int b)
{
        printf("callback : Subtitle()\n") ;
        return 1 ;
}
 
int main()
{
        CPsiDescriptor psiDesc ;
 
        psiDesc.switchDesc(eDescVideo) ;
        psiDesc.switchDesc(eDescAudio) ;
        psiDesc.switchDesc(eDescSubtitle) ;
 
        return 1;
}
 

 

+ Recent posts