| [739] | 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 |  | 
|---|
| [2710] | 28 | #include <vector> | 
|---|
| [739] | 29 |  | 
|---|
|  | 30 | // Forward declare ticpp::Node, so it can be made a friend of TiCppRC | 
|---|
|  | 31 | namespace ticpp | 
|---|
|  | 32 | { | 
|---|
|  | 33 | class Base; | 
|---|
|  | 34 | } | 
|---|
|  | 35 |  | 
|---|
|  | 36 | // Forward declare TiCppRCImp so TiCppRC can hold a pointer to it | 
|---|
|  | 37 | class TiCppRCImp; | 
|---|
|  | 38 |  | 
|---|
|  | 39 | /** | 
|---|
|  | 40 | Base class for reference counting functionality | 
|---|
|  | 41 | */ | 
|---|
|  | 42 | class TiCppRC | 
|---|
|  | 43 | { | 
|---|
|  | 44 | // Allow ticpp::Node to directly modify reference count | 
|---|
|  | 45 | friend class ticpp::Base; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | private: | 
|---|
|  | 48 |  | 
|---|
|  | 49 | TiCppRCImp* m_tiRC; /**< Pointer to reference counter */ | 
|---|
|  | 50 |  | 
|---|
|  | 51 | public: | 
|---|
|  | 52 |  | 
|---|
|  | 53 | /** | 
|---|
|  | 54 | Constructor | 
|---|
|  | 55 | Spawns new reference counter with a pointer to this | 
|---|
|  | 56 | */ | 
|---|
|  | 57 | TiCppRC(); | 
|---|
|  | 58 |  | 
|---|
|  | 59 | /** | 
|---|
|  | 60 | Destructor | 
|---|
|  | 61 | Nullifies the pointer to this held by the reference counter | 
|---|
|  | 62 | Decrements reference count | 
|---|
|  | 63 | */ | 
|---|
|  | 64 | virtual ~TiCppRC(); | 
|---|
| [2710] | 65 |  | 
|---|
|  | 66 | std::vector< ticpp::Base* > m_spawnedWrappers; /**< Remember all wrappers that we've created with 'new' - ( e.g. NodeFactory, FirstChildElement, etc. )*/ | 
|---|
|  | 67 |  | 
|---|
|  | 68 | /** | 
|---|
|  | 69 | Delete all container objects we've spawned with 'new'. | 
|---|
|  | 70 | */ | 
|---|
|  | 71 | void DeleteSpawnedWrappers(); | 
|---|
| [739] | 72 | }; | 
|---|
|  | 73 |  | 
|---|
|  | 74 | class TiCppRCImp | 
|---|
|  | 75 | { | 
|---|
|  | 76 | private: | 
|---|
|  | 77 |  | 
|---|
|  | 78 | int m_count;            /**< Holds reference count to me, and to the node I point to */ | 
|---|
|  | 79 |  | 
|---|
|  | 80 | TiCppRC* m_tiCppRC;     /**< Holds pointer to an object inheriting TiCppRC */ | 
|---|
|  | 81 |  | 
|---|
|  | 82 | public: | 
|---|
|  | 83 |  | 
|---|
|  | 84 | /** | 
|---|
|  | 85 | Initializes m_tiCppRC pointer, and set reference count to 1 | 
|---|
|  | 86 | */ | 
|---|
|  | 87 | TiCppRCImp( TiCppRC* tiCppRC ); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | /** | 
|---|
|  | 90 | Allows the TiCppRC object to set the pointer to itself ( m_tiCppRc ) to NULL when the TiCppRC object is deleted | 
|---|
|  | 91 | */ | 
|---|
|  | 92 | void Nullify(); | 
|---|
|  | 93 |  | 
|---|
|  | 94 | /** | 
|---|
|  | 95 | Increment Reference Count | 
|---|
|  | 96 | */ | 
|---|
|  | 97 | void IncRef(); | 
|---|
|  | 98 |  | 
|---|
|  | 99 | /** | 
|---|
|  | 100 | Decrement Reference Count | 
|---|
|  | 101 | */ | 
|---|
|  | 102 | void DecRef(); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | /** | 
|---|
|  | 105 | Set Reference Count to 1 - dangerous! - Use only if you are sure of the consequences | 
|---|
|  | 106 | */ | 
|---|
|  | 107 | void InitRef(); | 
|---|
|  | 108 |  | 
|---|
|  | 109 | /** | 
|---|
|  | 110 | Get internal pointer to the TiCppRC object - not reference counted, use at your own risk | 
|---|
|  | 111 | */ | 
|---|
|  | 112 | TiCppRC* Get(); | 
|---|
|  | 113 |  | 
|---|
|  | 114 | /** | 
|---|
|  | 115 | Returns state of internal pointer - will be null if the object was deleted | 
|---|
|  | 116 | */ | 
|---|
|  | 117 | bool IsNull(); | 
|---|
|  | 118 | }; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | #endif // TICPP_INCLUDED | 
|---|
|  | 121 |  | 
|---|
|  | 122 | #endif // TIXML_USE_TICPP | 
|---|