Main Page   Reference Manual   Namespace List   Compound List   Namespace Members   Compound Members   File Members  

smart_ptr.h
Go to the documentation of this file.
1 // $Header$
2 //
3 // Copyright (C) 2002 - 2004, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 
18 #ifndef LIBCWD_SMART_PTR_H
19 #define LIBCWD_SMART_PTR_H
20 
21 #ifndef LIBCWD_PRIVATE_STRUCT_TSD_H
23 #endif
24 
25 namespace libcwd {
26  namespace _private_ {
27 
28 class refcnt_charptr_ct {
29 private:
30  int M_reference_count; // Number of smart_ptr objects pointing to this stub.
31  char* M_ptr; // Pointer to the actual character string, allocated with new char[...].
32 public:
33  refcnt_charptr_ct(char* ptr) : M_reference_count(1), M_ptr(ptr) { }
34  void increment(void) { ++M_reference_count; }
35  bool decrement(void)
36  {
37  if (M_ptr && --M_reference_count == 0)
38  {
39  delete [] M_ptr;
40  M_ptr = NULL;
41  return true;
42  }
43  return false;
44  }
45  char* get(void) const { return M_ptr; }
46  int reference_count(void) const { return M_reference_count; }
47 };
48 
49 class smart_ptr {
50 private:
51  void* M_ptr;
52  bool M_string_literal;
53 
54 public:
55  // Default constructor and destructor.
56  smart_ptr(void) : M_ptr(NULL), M_string_literal(true) { }
57  ~smart_ptr() { if (!M_string_literal) reinterpret_cast<refcnt_charptr_ct*>(M_ptr)->decrement(); }
58 
59  // Copy constructor.
60  smart_ptr(smart_ptr const& ptr) : M_ptr(NULL), M_string_literal(true) { copy_from(ptr); }
61 
62  // Other constructors.
63  smart_ptr(char const* ptr) : M_string_literal(true) { copy_from(ptr); }
64  smart_ptr(char* ptr) : M_string_literal(true) { copy_from(ptr); }
65 
66 public:
67  // Assignment operators.
68  smart_ptr& operator=(smart_ptr const& ptr) { copy_from(ptr); return *this; }
69  smart_ptr& operator=(char const* ptr) { copy_from(ptr); return *this; }
70  smart_ptr& operator=(char* ptr) { copy_from(ptr); return *this; }
71 
72 public:
73  // Casting operator.
74  operator char const* (void) const { return get(); }
75 
76  // Comparison Operators.
77  bool operator==(smart_ptr const& ptr) const { return get() == ptr.get(); }
78  bool operator==(char const* ptr) const { return get() == ptr; }
79  bool operator!=(smart_ptr const& ptr) const { return get() != ptr.get(); }
80  bool operator!=(char const* ptr) const { return get() != ptr; }
81 
82 public:
83  bool is_null(void) const { return M_ptr == NULL; }
84  char const* get(void) const { return M_string_literal ? reinterpret_cast<char*>(M_ptr) : reinterpret_cast<refcnt_charptr_ct*>(M_ptr)->get(); }
85 
86 protected:
87  // Helper methods.
88  void copy_from(smart_ptr const& ptr);
89  void copy_from(char const* ptr);
90  void copy_from(char* ptr);
91 
92 private:
93  // Implementation.
94  void increment(void) { if (!M_string_literal) reinterpret_cast<refcnt_charptr_ct*>(M_ptr)->increment(); }
95  void decrement(LIBCWD_TSD_PARAM);
96 };
97 
98  } // namespace _private_
99 } // namespace libcwd
100 
101 #endif // LIBCWD_SMART_PTR_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.