C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[386] Property ANSI C++ 버전
서정국.새더 [mmouse] 7082 읽음    2003-04-09 20:34
안녕하세요
아래의 WIN32 API 버전 프라퍼티에 이어.. 템플릿버전 프라퍼티 입니다 --;;

코드프로젝트가 갔더니 템플릿을 사용해서 누가 프라퍼티를 만들어 놨더군요..

다음은 소스코드 ㅡㅡ;;

#define READ_ONLY 1
#define WRITE_ONLY 2
#define READ_WRITE 3

//-----------------
// 프라퍼티 클래스
//-----------------

template<typename Container, typename ValueType, int nPropType>
class property
{
public:
  property()
  {
    m_cObject = NULL;
    Set = NULL;
    Get = NULL;
  }
  //-- This to set a pointer to the class that contain the property --
  void setContainer(Container* cObject)
  {
    m_cObject = cObject;
  }
  //-- Set the set member function that will change the value --
  void setter(void (Container::*pSet)(ValueType value))
  {
    if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE))
      Set = pSet;
    else
      Set = NULL;
  }
  //-- Set the get member function that will retrieve the value --
  void getter(ValueType (Container::*pGet)())
  {
    if((nPropType == READ_ONLY) || (nPropType == READ_WRITE))
     Get = pGet;
    else
      Get = NULL;
  }
  //-- Overload the '=' sign to set the value using the set member --
  ValueType operator =(const ValueType& value)
  {
    assert(m_cObject != NULL);
    assert(Set != NULL);
    (m_cObject->*Set)(value);   
    return value;
  }

  //-- To make possible to cast the property class to the internal type --
  operator ValueType()
  {
    assert(m_cObject != NULL);
    assert(Get != NULL);
    return (m_cObject->*Get)();
  }

private:
  Container* m_cObject;//-- Pointer to the module that contain the property --
  void (Container::*Set)(ValueType value);//-- Pointer to set member function --
  ValueType (Container::*Get)();//-- Pointer to get member function --
};

//-----------------
// 예제
//-----------------

class PropTest
{
public:
  PropTest()
  {
    Count.setContainer(this);
    Count.setter(&PropTest::setCount);
    Count.getter(&PropTest::getCount);
  }
  int getCount(){return m_nCount;}
  void setCount(int nCount){ m_nCount = nCount;}

  property<PropTest,int,READ_WRITE> Count;
private:
  int m_nCount;
};

int i = 5,j;
PropTest test;
test.Count = i;    //-- call the set method --
j= test.Count;     //-- call the get method --



P.S : 실은 전에 저도 프라퍼티 개념을 VC++ 가지고 만든적이 있었습니다. 그때는 거의 MFC 매크로에 버금가는 장황한 코드였는데.. 지금 이건 너무나도 간단하네요 ㅡㅡ;;..
김성연 [sykim25]   2004-05-17 10:49 X
잘 사용하겠습니다!! ^^

+ -

관련 글 리스트
386 Property ANSI C++ 버전 서정국.새더 7082 2003/04/09
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.