basic AssetManager works now (creating, registering, querying),

including memory management. Ordering is still WIP.
This commit is contained in:
Fischlurch 2007-09-18 05:16:56 +02:00
parent 3927f7d5d8
commit 14d1c5a34b
9 changed files with 194 additions and 36 deletions

View file

@ -27,6 +27,8 @@
#include <boost/format.hpp>
#include <iostream>
#include <string>
using boost::format;
using util::cStr;
@ -46,12 +48,12 @@ namespace asset
*/
Asset::Asset (const Ident& idi)
: ident(idi), id(AssetManager::reg (this, idi))
{ TRACE (assetmem, "ctor Asset(id=%d) : %s p=%x", size_t(id), cStr(this->ident), this );
{ TRACE (assetmem, "ctor Asset(id=%lu) : adr=%x %s", size_t(id), this, cStr(this->ident) );
}
Asset::~Asset ()
{
TRACE (assetmem, "dtor Asset(id=%d) : %s p=%x", size_t(id), cStr(this->ident), this );
TRACE (assetmem, "dtor Asset(id=%lu) : adr=%x", size_t(id), this );
}

View file

@ -175,20 +175,30 @@ namespace asset
&& name == other.name
&& category == other.category;
}
bool operator!= (const Ident& other) const
{
return !operator==(other);
}
int compare (const Ident& other) const;
operator string () const;
};
/* ===== Asset ID and Datafields ===== */
public:
const Ident ident; ///< Asset identification tuple
virtual const ID<Asset>& getID() const { return id; }
virtual operator string () const;
protected:
const ID<Asset> id; ///< Asset primary key.
protected:
/** additional classification, selections or departments this asset belongs to.
* Groups are optional, non-exclusive and may be overlapping.
*/
@ -209,7 +219,7 @@ namespace asset
* Calling this base ctor causes registration with AssetManager.
*/
Asset (const Ident& idi);
virtual ~Asset() = 0;
virtual ~Asset() = 0; ///< @note Asset is abstract
/** release all links to other Asset objects held internally.
* The lifecycle of Asset objects is managed by smart pointers
@ -230,6 +240,7 @@ namespace asset
friend class AssetManager;
public:
/** List of entities this asset depends on or requires to be functional.
* May be empty. The head of this list can be considered the primary prerequisite

View file

@ -88,6 +88,7 @@ namespace asset
friend class cinelerra::singleton::Static<DB>;
public:
template<class KIND>
void put (ID<KIND> hash, shared_ptr<KIND>& ptr) { table[hash] = static_pointer_cast (ptr); }
@ -95,20 +96,33 @@ namespace asset
template<class KIND>
shared_ptr<KIND>
get (ID<KIND> hash)
get (ID<KIND> hash) const
{
return dynamic_pointer_cast<KIND,Asset> (table[hash]);
return dynamic_pointer_cast<KIND,Asset> (find (hash));
}
/** intended for diagnostics */
void
asList (list<PAsset>& output) const
asList (list<PAsset>& output) const
{
IdHashtable::const_iterator i = table.begin();
IdHashtable::const_iterator e = table.end();
for ( ; i!=e ; ++i )
output.push_back (i->second);
}
private:
const PAsset &
find (size_t hash) const
{
static const PAsset NULLP;
IdHashtable::const_iterator i = table.find (hash);
if (i == table.end())
return NULLP; // empty ptr signaling "not found"
else
return i->second;
}
};

View file

@ -102,8 +102,7 @@ namespace asset
throw(cinelerra::error::Invalid);
/** deleter function used by the Asset smart pointers to delet Asset objects */
static void destroy (Asset* m) { TRACE (assetmem, "call destroy (Asset(id=%d)) p=%x", m? size_t(m->getID()):0, m );
delete m; }
static void destroy (Asset* m) { delete m; }
friend Asset::Asset (const Asset::Ident& idi);

View file

@ -2,7 +2,8 @@ TESTING "Component Test Suite: Asset Manager" ./test-components --group=asset
PLANNED "CreateAsset_test" CreateAsset_test <<END
TEST "CreateAsset_test" CreateAsset_test <<END
return: 0
END
@ -12,3 +13,12 @@ END
PLANNED "DependantAssets_test" DependantAssets_test <<END
END
TEST "IdentityOfAssets_test" IdentityOfAssets_test <<END
return: 0
END
PLANNED "OrderingOfAssets_test" OrderingOfAssets_test <<END
END

View file

@ -56,8 +56,8 @@ namespace asset
cout << "Asset(NULL)\n";
else
{
format fmt("%s %|50T.| id=%d sP=%x ptr=%x use-count=%d");
cout << fmt % str(aa) % aa->getID() % &aa % aa.get() % aa.use_count() << "\n";
format fmt("%s %|50T.| id=%s adr=%x smart-ptr=%x use-count=%d");
cout << fmt % str(aa) % aa->getID() % aa.get() % &aa % (aa.use_count() - 1) << "\n";
} }
inline void dumpAssetManager ()

View file

@ -52,9 +52,14 @@ namespace asset
{
createMedia();
factoryVariants();
dumpAssetManager();
if (!isnil (arg))
dumpAssetManager();
TRACE (assetmem, "leaving CreateAsset_test::run()");
}
typedef shared_ptr<asset::Media> PM;
/** @test Creating and automatically registering Asset instances.
@ -79,9 +84,6 @@ namespace asset
ASSERT (aMang.getAsset (mm1->getID()) != mm2);
cout << "== 1 ==\n";
dumpAssetManager();
PAsset aa1 = aMang.getAsset (ID<Asset>(mm1->getID())); // note we get an Asset ref
ASSERT (aa1 == mm1);
PM mX1 = aMang.getAsset (mm1->getID()); // ..and now we get a Media ref
@ -92,22 +94,19 @@ namespace asset
ASSERT (aMang.known (mm2->getID()));
ASSERT (aMang.known (mm3->getID()));
cout << "== 2 ==\n";
dumpAssetManager();
ASSERT ( !aMang.known (mm3->getID(), Category(AUDIO))); // not found within AUDIO-Category
try
{ // can't be found if specifying wrong Asset kind....
aMang.getAsset (ID<asset::Proc>(mm1->getID()));
NOTREACHED;
}
catch (cinelerra::error::Invalid& exe) {ASSERT (exe.getID()==CINELERRA_ERROR_WRONG_ASSET_KIND);}
catch (cinelerra::error::Invalid& xxx) {ASSERT (xxx.getID()==CINELERRA_ERROR_WRONG_ASSET_KIND);}
try
{ // try accessing nonexistant ID
aMang.getAsset (ID<Asset> (1234567890));
NOTREACHED;
}
catch (cinelerra::error::Invalid& exe) {ASSERT (exe.getID()==CINELERRA_ERROR_UNKNOWN_ASSET_ID);}
catch (cinelerra::error::Invalid& xxx) {ASSERT (xxx.getID()==CINELERRA_ERROR_UNKNOWN_ASSET_ID);}
// checking the Ident-Fields
@ -132,12 +131,8 @@ namespace asset
ASSERT (mm2->getFilename() == "testfile1.mov");
ASSERT (mm3->getFilename() == "testfile2.mov");
dump (mm1);
dump (mm2);
dump (mm3);
dump (mX1);
cout << "== 3 ==\n";
dumpAssetManager();
TRACE (assetmem, "leaving test method scope");
}

View file

@ -53,9 +53,14 @@ namespace asset
virtual void run(Arg arg)
{
createDuplicate();
cout << "ausis\n";
if (!isnil (arg))
dumpAssetManager();
TRACE (assetmem, "leaving IdentityOfAssets_test::run()");
}
typedef shared_ptr<asset::Media> PM;
/** @test produce an ID clash.
@ -70,9 +75,9 @@ namespace asset
Asset::Ident idi (mm1->ident); // duplicate Ident record
PM mm1X = asset::Media::create (idi); // note: we actually don't call any ctor
ASSERT (mm1 == mm1X); // instead, we got mm1 back.
cout << "usi-v " << mm1.use_count() <<"\n";
PM mm2 = asset::Media::create (idi,"testfile2.mov");
cout << "usi-n " << mm1.use_count() <<"\n";
ASSERT (mm1->getID() == mm2->getID()); // different object, same hash
AssetManager& aMang = AssetManager::instance();
@ -86,11 +91,8 @@ namespace asset
ASSERT (mm1->getFilename() == "testfile1.mov");
ASSERT (mm2->getFilename() == "testfile2.mov");
cout << "use-cnt at end " << mm1.use_count() <<"\n";
dump (mm1);
dump (mm1X);
dump (mm2);
dumpAssetManager();
TRACE (assetmem, "leaving test method scope");
}
};

View file

@ -0,0 +1,125 @@
/*
OrderingOfAssets(Test) - equality and comparisons
Copyright (C) CinelerraCV
2007, Christian Thaeter <ct@pipapo.org>
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 WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 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., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
#include "common/test/run.hpp"
#include "common/util.hpp"
#include "proc/assetmanager.hpp"
#include "proc/asset/media.hpp"
#include "proc/asset/proc.hpp"
#include "proc/asset/assetdiagnostics.hpp"
using util::isnil;
using std::string;
namespace asset
{
namespace test
{
/******************************************************
* @test validate the equality and order relations of
* Asset::Ident and Asset objects.
* @see Asset::Ident#compare
*/
class OrderingOfAssets_test : public Test
{
virtual void run(Arg arg)
{
typedef shared_ptr<asset::Media> PM;
Asset::Ident key1("Au-1", Category(AUDIO), "ichthyo", 5);
PM mm1 = asset::Media::create(key1, "Name-1");
Asset::Ident key2("Au-1", Category(AUDIO), "ichthyo", 7);
PM mm2 = asset::Media::create(key2, "Name-2");
Asset::Ident key3("Au-2", Category(AUDIO), "ichthyo", 5);
PM mm3 = asset::Media::create(key3, "Name-3");
Asset::Ident key4("Au-2", Category(AUDIO), "stega", 5);
PM mm4 = asset::Media::create(key4, "Name-4");
Asset::Ident key5("Au-1", Category(VIDEO), "ichthyo", 5);
PM mm5 = asset::Media::create(key5, "Name-5");
// ordering of keys
ASSERT (key1 == key2);
ASSERT (key2 != key3);
ASSERT (key3 != key4);
ASSERT (key4 != key5);
ASSERT (key1 != key5);
ASSERT (-1 == key2.compare(key3));
ASSERT (+1 == key3.compare(key2));
ASSERT (-1 == key3.compare(key4));
ASSERT (-1 == key4.compare(key5));
ASSERT (-1 == key1.compare(key5));
ASSERT (-1 == key2.compare(key5));
ASSERT (-1 == key3.compare(key5));
ASSERT (-1 == key1.compare(key3));
ASSERT (-1 == key1.compare(key4));
ASSERT (-1 == key2.compare(key4));
// ordering of Asset smart ptrs
ASSERT (mm1 == mm2);
ASSERT (mm2 != mm3);
ASSERT (mm3 != mm4);
ASSERT (mm4 != mm5);
ASSERT (mm1 != mm5);
ASSERT (mm2 < mm3);
ASSERT (mm2 <= mm3);
ASSERT (mm3 > mm2);
ASSERT (mm3 >= mm2);
ASSERT (mm3 < mm4);
ASSERT (mm4 < mm5);
ASSERT (mm1 < mm5);
ASSERT (mm2 < mm5);
ASSERT (mm3 < mm5);
ASSERT (mm1 < mm3);
ASSERT (mm1 < mm4);
ASSERT (mm2 < mm4);
}
};
/** Register this test class... */
LAUNCHER (OrderingOfAssets_test, "unit asset");
} // namespace test
} // namespace asset