2012-01-10 2 views
3

참조 계산을 위해 아래 예제 코드 에서처럼 std :: tr1 :: shared_ptr을 사용하는 것이 안전하고 올바른가요? ,참조 계산을위한 내부 메커니즘으로 std :: tr1 :: shared_ptr 사용

struct fclose_deleter 
{ 
    void operator()(FILE* f) 
    { 
     if (f) 
     { 
      std::fclose(f); 
     } 
    } 
}; 

다음 :

class File 
{ 
public: 
    File(const char* path, const char* mode) : 
     _refcount(new int(0)) 
    { 
     this->_file = fopen(path, mode); 
    } 

    ~File() 
    { 
     if (this->_refcount.unique()) 
     { 
      if (this->_file != NULL) 
      { 
       fclose(this->_file); 
      } 
     } 
    } 

    int write(void* buff, size_t size) 
    { 
     fwrite(buff, size, 1, this->_file); 
    } 

private: 
    FILE* _file; 
    std::tr1::shared_ptr<int> _refcount; 
}; 
+1

어떤 의미로 안전합니까? 메모리가 안전합니까? 스레드 안전? – kennytm

+0

@KennyTM 예, 메모리 안전 및 스레드 안전 및 다른 모든 관점에서. (스레드 안전하지 않은 것 같아요, 고정 수있는 방법을 설명 할 수 있습니다.) –

+0

스레드 안전하지 않습니다;)하지만 나는 명시 적 ref- 계수기. – kennytm

답변

9

대신 shared_ptr<FILE>와 사용자 정의 Deleter가 사용을 고려 (이 클래스 대신 파일 *의) * 무효 무엇을 (포함 할 수 있습니다 단지 특정 샘플입니다) File 클래스는 훨씬 간단합니다 (수정 자) :

class File 
{ 
public: 
    File(const char* path, const char* mode) 
     : _file(std::fopen(path, mode), fclose_deleter()) 
    { 
    } 

    int write(void const* buff, size_t size) 
    { 
     // You'll want to verify that _file.get() is valid, or you'll want to 
     // throw in the constructor if the call to 'std::fopen()' fails. 
     std::fwrite(buff, size, 1, _file.get()); 
    } 

private: 
    std::tr1::shared_ptr<FILE> _file; 
}; 
+0

클래스에 모든 데이터 (void *)를 포함 할 수 있습니다. 파일은 단지 예일뿐입니다 –

+3

이 기술은 정리가 필요한 참조 카운팅 된 모든 개체에 적용됩니다. –

+1

이것과 다른 흥미로운'shared_ptr' 기술은 다음에서 찾을 수 있습니다 : http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/sp_techniques.html :) –

관련 문제