[859] | 1 | /* |
---|
| 2 | http://code.google.com/p/ticpp/ |
---|
| 3 | Copyright (c) 2006 Ryan Pusztai, Ryan Mulder |
---|
| 4 | |
---|
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
| 6 | this software and associated documentation files (the "Software"), to deal in |
---|
| 7 | the Software without restriction, including without limitation the rights to |
---|
| 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
---|
| 9 | the Software, and to permit persons to whom the Software is furnished to do so, |
---|
| 10 | subject to the following conditions: |
---|
| 11 | |
---|
| 12 | The above copyright notice and this permission notice shall be included in all |
---|
| 13 | copies or substantial portions of the Software. |
---|
| 14 | |
---|
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
---|
| 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
---|
| 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
| 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #ifdef TIXML_USE_TICPP |
---|
| 24 | |
---|
| 25 | #ifndef TICPPRC_INCLUDED |
---|
| 26 | #define TICPPRC_INCLUDED |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | // Forward declare ticpp::Node, so it can be made a friend of TiCppRC |
---|
| 30 | namespace ticpp |
---|
| 31 | { |
---|
| 32 | class Base; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | // Forward declare TiCppRCImp so TiCppRC can hold a pointer to it |
---|
| 36 | class TiCppRCImp; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | Base class for reference counting functionality |
---|
| 40 | */ |
---|
| 41 | class TiCppRC |
---|
| 42 | { |
---|
| 43 | // Allow ticpp::Node to directly modify reference count |
---|
| 44 | friend class ticpp::Base; |
---|
| 45 | |
---|
| 46 | private: |
---|
| 47 | |
---|
| 48 | TiCppRCImp* m_tiRC; /**< Pointer to reference counter */ |
---|
| 49 | |
---|
| 50 | public: |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | Constructor |
---|
| 54 | Spawns new reference counter with a pointer to this |
---|
| 55 | */ |
---|
| 56 | TiCppRC(); |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | Destructor |
---|
| 60 | Nullifies the pointer to this held by the reference counter |
---|
| 61 | Decrements reference count |
---|
| 62 | */ |
---|
| 63 | virtual ~TiCppRC(); |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | class TiCppRCImp |
---|
| 67 | { |
---|
| 68 | private: |
---|
| 69 | |
---|
| 70 | int m_count; /**< Holds reference count to me, and to the node I point to */ |
---|
| 71 | |
---|
| 72 | TiCppRC* m_tiCppRC; /**< Holds pointer to an object inheriting TiCppRC */ |
---|
| 73 | |
---|
| 74 | public: |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | Initializes m_tiCppRC pointer, and set reference count to 1 |
---|
| 78 | */ |
---|
| 79 | TiCppRCImp( TiCppRC* tiCppRC ); |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | Allows the TiCppRC object to set the pointer to itself ( m_tiCppRc ) to NULL when the TiCppRC object is deleted |
---|
| 83 | */ |
---|
| 84 | void Nullify(); |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | Increment Reference Count |
---|
| 88 | */ |
---|
| 89 | void IncRef(); |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | Decrement Reference Count |
---|
| 93 | */ |
---|
| 94 | void DecRef(); |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | Set Reference Count to 1 - dangerous! - Use only if you are sure of the consequences |
---|
| 98 | */ |
---|
| 99 | void InitRef(); |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | Get internal pointer to the TiCppRC object - not reference counted, use at your own risk |
---|
| 103 | */ |
---|
| 104 | TiCppRC* Get(); |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | Returns state of internal pointer - will be null if the object was deleted |
---|
| 108 | */ |
---|
| 109 | bool IsNull(); |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | #endif // TICPP_INCLUDED |
---|
| 113 | |
---|
| 114 | #endif // TIXML_USE_TICPP |
---|