|
|
813345 |
/* === S Y N F I G ========================================================= */
|
|
|
813345 |
/*! \file synfig/rendering/resource.cpp
|
|
|
813345 |
** \brief Resource
|
|
|
813345 |
**
|
|
|
813345 |
** $Id$
|
|
|
813345 |
**
|
|
|
813345 |
** \legal
|
|
|
813345 |
** ......... ... 2015 Ivan Mahonin
|
|
|
813345 |
**
|
|
|
813345 |
** This package is free software; you can redistribute it and/or
|
|
|
813345 |
** modify it under the terms of the GNU General Public License as
|
|
|
813345 |
** published by the Free Software Foundation; either version 2 of
|
|
|
813345 |
** the License, or (at your option) any later version.
|
|
|
813345 |
**
|
|
|
813345 |
** This package is distributed in the hope that it will be useful,
|
|
|
813345 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
813345 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
813345 |
** General Public License for more details.
|
|
|
813345 |
** \endlegal
|
|
|
813345 |
*/
|
|
|
813345 |
/* ========================================================================= */
|
|
|
813345 |
|
|
|
813345 |
/* === H E A D E R S ======================================================= */
|
|
|
813345 |
|
|
|
813345 |
#ifdef USING_PCH
|
|
|
813345 |
# include "pch.h"
|
|
|
813345 |
#else
|
|
|
813345 |
#ifdef HAVE_CONFIG_H
|
|
|
813345 |
# include <config.h></config.h>
|
|
|
813345 |
#endif
|
|
|
813345 |
|
|
|
813345 |
#include "resource.h"
|
|
|
813345 |
|
|
|
813345 |
#endif
|
|
|
813345 |
|
|
|
813345 |
using namespace synfig;
|
|
|
813345 |
using namespace rendering;
|
|
|
813345 |
|
|
|
813345 |
/* === M A C R O S ========================================================= */
|
|
|
813345 |
|
|
|
813345 |
/* === G L O B A L S ======================================================= */
|
|
|
813345 |
|
|
|
813345 |
/* === P R O C E D U R E S ================================================= */
|
|
|
813345 |
|
|
|
813345 |
/* === M E T H O D S ======================================================= */
|
|
|
813345 |
|
|
|
371bd9 |
Resource::Id Resource::last_id = 0;
|
|
|
371bd9 |
|
|
Rodolfo Ribeiro Gomes |
9b0489 |
Resource::Storage::Storage(): refcount(0) { }
|
|
|
813345 |
|
|
|
813345 |
Resource::Storage::~Storage() { }
|
|
|
813345 |
|
|
|
813345 |
void
|
|
|
813345 |
Resource::Storage::ref() const
|
|
|
813345 |
{
|
|
|
813345 |
++refcount;
|
|
|
813345 |
}
|
|
|
813345 |
|
|
|
813345 |
bool
|
|
|
813345 |
Resource::Storage::unref_inactive() const
|
|
|
813345 |
{
|
|
|
813345 |
return refcount == 0 || --refcount != (int)resources.size();
|
|
|
813345 |
}
|
|
|
813345 |
|
|
|
813345 |
bool
|
|
|
813345 |
Resource::Storage::unref() const
|
|
|
813345 |
{
|
|
|
813345 |
if (refcount == 0) return true;
|
|
|
813345 |
if (--refcount == (int)resources.size())
|
|
|
813345 |
{
|
|
|
813345 |
refcount = 0;
|
|
|
813345 |
const_cast<storage*>(this)->resources.clear();</storage*>
|
|
|
813345 |
#ifdef ETL_SELF_DELETING_SHARED_OBJECT
|
|
|
813345 |
delete this;
|
|
|
813345 |
#endif
|
|
|
813345 |
return true;
|
|
|
813345 |
}
|
|
|
813345 |
return true;
|
|
|
813345 |
}
|
|
|
813345 |
|
|
|
813345 |
int
|
|
|
813345 |
Resource::Storage::count() const
|
|
|
813345 |
{
|
|
|
813345 |
return refcount;
|
|
|
813345 |
}
|
|
|
813345 |
|
|
|
813345 |
|
|
|
813345 |
void
|
|
|
813345 |
Resource::set_alternative(Handle other) const
|
|
|
813345 |
{
|
|
|
813345 |
assert(other);
|
|
|
813345 |
|
|
|
813345 |
if (alternatives && other->alternatives)
|
|
|
813345 |
{
|
|
|
813345 |
if (alternatives != other->alternatives)
|
|
|
813345 |
{
|
|
|
813345 |
if (other->alternatives->resources.size() > alternatives->resources.size())
|
|
|
813345 |
{ other->set_alternative(Handle(const_cast<resource*>(this))); }</resource*>
|
|
|
813345 |
else
|
|
|
813345 |
{
|
|
|
813345 |
// merge
|
|
|
813345 |
Storage::Handle other_alternatives = other->alternatives;
|
|
|
813345 |
while(!other->alternatives->resources.empty())
|
|
|
813345 |
{
|
|
|
813345 |
alternatives->resources.push_back(
|
|
|
813345 |
other->alternatives->resources.back() );
|
|
|
813345 |
other_alternatives->resources.pop_back();
|
|
|
813345 |
other->alternatives = alternatives;
|
|
|
813345 |
}
|
|
|
813345 |
}
|
|
|
813345 |
}
|
|
|
813345 |
}
|
|
|
813345 |
else
|
|
|
813345 |
if (alternatives)
|
|
|
813345 |
{
|
|
|
813345 |
// add
|
|
|
813345 |
other->alternatives = alternatives;
|
|
|
813345 |
alternatives->resources.push_back(other);
|
|
|
813345 |
}
|
|
|
813345 |
else
|
|
|
813345 |
if (other->alternatives)
|
|
|
813345 |
{ other->set_alternative(Handle(const_cast<resource*>(this))); }</resource*>
|
|
|
813345 |
else
|
|
|
813345 |
{
|
|
|
813345 |
// new storage
|
|
|
813345 |
alternatives = new Storage();
|
|
|
813345 |
alternatives->resources.push_back(Handle(const_cast<resource*>(this)));</resource*>
|
|
|
813345 |
alternatives->resources.push_back(other);
|
|
|
813345 |
}
|
|
|
813345 |
}
|
|
|
813345 |
|
|
|
813345 |
void
|
|
|
813345 |
Resource::unset_alternative() const
|
|
|
813345 |
{
|
|
|
813345 |
if (alternatives)
|
|
|
813345 |
{
|
|
|
813345 |
List &resources = alternatives->resources;
|
|
|
813345 |
for(List::iterator i = resources.begin(); i != resources.end(); ++i)
|
|
|
813345 |
if (i->get() == this)
|
|
|
813345 |
{ resources.erase(i); break; }
|
|
|
813345 |
alternatives.reset();
|
|
|
813345 |
}
|
|
|
813345 |
}
|
|
|
813345 |
|
|
|
813345 |
/* === E N T R Y P O I N T ================================================= */
|