1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
/* scope.cpp
* Copyright (C) 2003-2005 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <tnt/scope.h>
#include <cxxtools/thread.h>
#include <cxxtools/log.h>
namespace tnt
{
log_define("tntnet.scope")
static unsigned scopes_total = 0;
Scope::Scope()
: refs(1)
{
++scopes_total;
log_debug("new Scope " << this << " total=" << scopes_total);
}
Scope::~Scope()
{
--scopes_total;
log_debug("Scope " << this << " deleted; " << scopes_total << " left");
}
void Scope::addRef()
{
++refs;
log_debug("Scope::addRef(); this=" << this << " refs=" << refs);
}
void Scope::release()
{
log_debug("Scope::release(); this=" << this << " refs=" << refs);
if (--refs == 0)
{
log_debug("delete Scope " << this);
delete this;
}
}
Object* Scope::get(const std::string& key)
{
container_type::iterator it = data.find(key);
log_debug("Scope::get(\"" << key << "\") Scope=" << this
<< " => " << (it == data.end() ? 0 : it->second));
return it == data.end() ? 0 : it->second.getPtr();
}
void Scope::replace(const std::string& key, Object* o)
{
log_debug("Scope::replace(\"" << key << ", " << o << "\") Scope=" << this);
o->addRef();
container_type::iterator it = data.find(key);
if (it == data.end())
data.insert(container_type::value_type(key, o));
else
{
it->second->release();
it->second = o;
}
}
Object* Scope::putNew(const std::string& key, Object* o)
{
log_debug("Scope::putNew(\"" << key << "\", " << o << ") Scope=" << this);
container_type::iterator it = data.find(key);
if (it == data.end())
{
data.insert(container_type::value_type(key, o));
return o;
}
else
{
o->addRef();
o->release();
return it->second.getPtr();
}
}
void Scope::erase(const std::string& key)
{
container_type::iterator it = data.find(key);
if (it != data.end())
{
it->second->release();
data.erase(it);
}
}
}
|