[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / interfaces / legacy / AddonClass.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "AddonClass.h"
23
24 #include "utils/log.h"
25
26 #include "LanguageHook.h"
27 #include "AddonUtils.h"
28
29 using namespace XBMCAddonUtils;
30
31 namespace XBMCAddon
32 {
33   // need a place to put the vtab
34   AddonClass::~AddonClass()
35   {
36     m_isDeallocating= true;
37
38     if (languageHook != NULL)
39       languageHook->Release();
40
41 #ifdef ENABLE_TRACE_API
42     TraceGuard tg_;
43     CLog::Log(LOGDEBUG, "%sNEWADDON destroying %s 0x%lx", tg_.getSpaces(), classname.c_str(), (long)(((void*)this)));
44 #endif
45
46 #ifdef XBMC_ADDON_DEBUG_MEMORY
47     isDeleted = false;
48 #endif
49   }
50
51   AddonClass::AddonClass(const char* cname) : refs(0L), classname(cname), m_isDeallocating(false), 
52                                               languageHook(NULL)
53   {
54 #ifdef ENABLE_TRACE_API
55     TraceGuard tg_;
56     CLog::Log(LOGDEBUG, "%sNEWADDON constructing %s 0x%lx", tg_.getSpaces(), classname.c_str(), (long)(((void*)this)));
57 #endif
58
59 #ifdef XBMC_ADDON_DEBUG_MEMORY
60     isDeleted = false;
61 #endif
62
63     // check to see if we have a language hook that was prepared for this instantiation
64     languageHook = LanguageHook::GetLanguageHook();
65     if (languageHook != NULL)
66     {
67       languageHook->Acquire();
68
69       // here we assume the language hook was set for the single instantiation of
70       //  this AddonClass (actually - its subclass - but whatever). So we
71       //  will now reset the Tls. This avoids issues if the constructor of the
72       //  subclass throws an exception.
73       LanguageHook::ClearLanguageHook();
74     }
75   }
76
77 #ifdef XBMC_ADDON_DEBUG_MEMORY
78   void AddonClass::Release() const
79   {
80     if (isDeleted)
81       CLog::Log(LOGERROR,"NEWADDON REFCNT Releasing dead class %s 0x%lx", 
82                 classname.c_str(), (long)(((void*)this)));
83
84     long ct = AtomicDecrement((long*)&refs);
85 #ifdef LOG_LIFECYCLE_EVENTS
86     CLog::Log(LOGDEBUG,"NEWADDON REFCNT decrementing to %ld on %s 0x%lx", ct,classname.c_str(), (long)(((void*)this)));
87 #endif
88     if(ct == 0)
89     {
90         ((AddonClass*)this)->isDeleted = true;
91         // we're faking a delete but not doing it so call the destructor explicitly
92         this->~AddonClass();
93     }
94   }
95
96   void AddonClass::Acquire() const
97   {
98     if (isDeleted)
99       CLog::Log(LOGERROR,"NEWADDON REFCNT Acquiring dead class %s 0x%lx", 
100                 classname.c_str(), (long)(((void*)this)));
101
102 #ifdef LOG_LIFECYCLE_EVENTS
103     CLog::Log(LOGDEBUG,"NEWADDON REFCNT incrementing to %ld on %s 0x%lx", 
104               AtomicIncrement((long*)&refs),classname.c_str(), (long)(((void*)this)));
105 #else
106     AtomicIncrement((long*)&refs);
107 #endif
108   }
109
110 #endif
111 }
112
113