diff --git a/src/common/util.hpp b/src/common/util.hpp index 479142069..7b036e1be 100644 --- a/src/common/util.hpp +++ b/src/common/util.hpp @@ -81,7 +81,7 @@ namespace util */ template inline Oper - for_each (Container& c, Oper& doIt) + for_each (Container& c, Oper doIt) { return std::for_each (c.begin(),c.end(), doIt); } diff --git a/src/proc/asset.cpp b/src/proc/asset.cpp index 0e790becb..622311863 100644 --- a/src/proc/asset.cpp +++ b/src/proc/asset.cpp @@ -46,24 +46,25 @@ 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 ); +} Asset::~Asset () { - TRACE (assetmem, "dtor Asset(id=%d) : %s", size_t(id), cStr(this->ident) ); + TRACE (assetmem, "dtor Asset(id=%d) : %s p=%x", size_t(id), cStr(this->ident), this ); } Asset::Ident::operator string () const { - static format id_tuple("(%2%:%3%.%1% v%4%)"); // ignoring threadsafety + format id_tuple("(%2%:%3%.%1% v%4%)"); return str (id_tuple % name % category % org % version); } Asset::operator string () const { - static format id_tuple("Asset(%2%:%3%.%1% v%4%)"); // ignoring threadsafety + format id_tuple("Asset(%2%:%3%.%1% v%4%)"); return str (id_tuple % ident.name % ident.category % ident.org % ident.version); } diff --git a/src/proc/asset.hpp b/src/proc/asset.hpp index d36ab85f2..e6fd207c6 100644 --- a/src/proc/asset.hpp +++ b/src/proc/asset.hpp @@ -175,6 +175,7 @@ namespace asset && name == other.name && category == other.category; } + int compare (const Ident& other) const; operator string () const; }; @@ -261,14 +262,31 @@ namespace asset /** shorthand for refcounting Asset pointers */ typedef shared_ptr PAsset; + /** ordering of Assets based on Ident tuple */ + inline bool operator< (const PAsset& a1, const PAsset& a2) { return a1 && a2 && (-1==a1->ident.compare(a2->ident));} + inline bool operator> (const PAsset& a1, const PAsset& a2) { return a2 < a1; } + inline bool operator>= (const PAsset& a1, const PAsset& a2) { return !(a1 < a2); } + inline bool operator<= (const PAsset& a1, const PAsset& a2) { return !(a1 > a2); } + + /** ordering of Asset Ident tuples. + * @note version is irrelevant */ + inline int Asset::Ident::compare (const Asset::Ident& oi) const + { + int res; + if (1 != (res=category.compare (oi.category))) return res; + if (1 != (res=org.compare (oi.org))) return res; + return name.compare (oi.name); + } + + /** convienient for debugging */ inline string str (const PAsset& a) - { - if (a) - return string (*a.get()); - else - return "Asset(NULL)"; - } + { + if (a) + return string (*a.get()); + else + return "Asset(NULL)"; + } diff --git a/src/proc/asset/category.hpp b/src/proc/asset/category.hpp index f27f46845..5a1bf905c 100644 --- a/src/proc/asset/category.hpp +++ b/src/proc/asset/category.hpp @@ -89,12 +89,22 @@ namespace asset boost::hash_combine(hash, cat.kind_); boost::hash_combine(hash, cat.path_); return hash; - } + } + + int compare (const Category& co) const + { + int res = int(kind_) - int(co.kind_); + if (1 != res) + return res; + else + return path_.compare (co.path_); + } }; inline ostream& operator<< (ostream& os, const Category& cago) { return os << string(cago); } - + + } // namespace asset #endif diff --git a/src/proc/asset/db.hpp b/src/proc/asset/db.hpp index 8ac4207a6..76be677f0 100644 --- a/src/proc/asset/db.hpp +++ b/src/proc/asset/db.hpp @@ -99,6 +99,16 @@ namespace asset { return dynamic_pointer_cast (table[hash]); } + + /** intended for diagnostics */ + void + asList (list& output) const + { + IdHashtable::const_iterator i = table.begin(); + IdHashtable::const_iterator e = table.end(); + for ( ; i!=e ; ++i ) + output.push_back (i->second); + } }; diff --git a/src/proc/assetmanager.cpp b/src/proc/assetmanager.cpp index 0fba91b52..89843b1d6 100644 --- a/src/proc/assetmanager.cpp +++ b/src/proc/assetmanager.cpp @@ -27,7 +27,7 @@ #include "common/multithread.hpp" #include "common/util.hpp" -#include +//#include #include #include @@ -191,6 +191,17 @@ namespace asset } + + list + AssetManager::listContent() const + { + list res; + registry.asList (res); + res.sort(); + return res; + } + + } // namespace asset diff --git a/src/proc/assetmanager.hpp b/src/proc/assetmanager.hpp index 1dc05f584..8ccf0ac61 100644 --- a/src/proc/assetmanager.hpp +++ b/src/proc/assetmanager.hpp @@ -43,9 +43,11 @@ #include #include +#include #include using std::string; +using std::list; @@ -86,6 +88,10 @@ namespace asset void remove (IDA id) throw(cinelerra::error::Invalid, cinelerra::error::State); + /** extract a sorted list of all registered Assets */ + list listContent() const; + + protected: /** registers an asset object in the internal DB, providing its unique key. @@ -96,7 +102,8 @@ namespace asset throw(cinelerra::error::Invalid); /** deleter function used by the Asset smart pointers to delet Asset objects */ - static void destroy (Asset* m) { delete m; } + static void destroy (Asset* m) { TRACE (assetmem, "call destroy (Asset(id=%d)) p=%x", m? size_t(m->getID()):0, m ); + delete m; } friend Asset::Asset (const Asset::Ident& idi); diff --git a/tests/components/proc/asset/assetdiagnostics.hpp b/tests/components/proc/asset/assetdiagnostics.hpp new file mode 100644 index 000000000..d5425ee66 --- /dev/null +++ b/tests/components/proc/asset/assetdiagnostics.hpp @@ -0,0 +1,73 @@ +/* + ASSETDIAGNOSTICS.hpp - collection of test and debug helpers + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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. + +*/ + +/** @file assetdiagnostics.hpp + ** Small helper and diagnosic functions related to Asset and AssetManager + ** + ** @see assetmanager.hpp + ** @see CreateAsset_test + ** @see IdentityOfAssets_test + */ + + +#ifndef ASSET_ASSETDIAGNOSTICS_H +#define ASSET_ASSETDIAGNOSTICS_H + + +#include "proc/assetmanager.hpp" +#include "common/util.hpp" + +#include +#include +#include + +using util::for_each; +using boost::format; +using boost::bind; +using std::string; +using std::cout; + + +namespace asset + { + + inline void dump (const PAsset& aa) + { + if (!aa) + 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"; + } } + + inline void dumpAssetManager () + { + list assets (AssetManager::instance().listContent()); + cout << "----all-registered-Assets----\n"; + for_each (assets, bind (&dump, _1)); + } + + + +} // namespace asset +#endif diff --git a/tests/components/proc/asset/createassettest.cpp b/tests/components/proc/asset/createassettest.cpp index 3aee6c34b..b5f449787 100644 --- a/tests/components/proc/asset/createassettest.cpp +++ b/tests/components/proc/asset/createassettest.cpp @@ -28,13 +28,10 @@ #include "proc/asset/media.hpp" #include "proc/asset/proc.hpp" -#include -#include +#include "proc/asset/assetdiagnostics.hpp" -using boost::format; using util::isnil; using std::string; -using std::cout; namespace asset @@ -55,6 +52,7 @@ namespace asset { createMedia(); factoryVariants(); + dumpAssetManager(); } typedef shared_ptr PM; @@ -81,6 +79,9 @@ namespace asset ASSERT (aMang.getAsset (mm1->getID()) != mm2); + cout << "== 1 ==\n"; + dumpAssetManager(); + PAsset aa1 = aMang.getAsset (ID(mm1->getID())); // note we get an Asset ref ASSERT (aa1 == mm1); PM mX1 = aMang.getAsset (mm1->getID()); // ..and now we get a Media ref @@ -91,6 +92,9 @@ 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.... @@ -128,10 +132,12 @@ namespace asset ASSERT (mm2->getFilename() == "testfile1.mov"); ASSERT (mm3->getFilename() == "testfile2.mov"); - showPtr (mm1); - showPtr (mm2); - showPtr (mm3); - showPtr (mX1); + dump (mm1); + dump (mm2); + dump (mm3); + dump (mX1); + cout << "== 3 ==\n"; + dumpAssetManager(); } @@ -178,12 +184,6 @@ namespace asset return identity == object->ident && filename == object->getFilename(); } - - void showPtr (PM m) - { - static format fmt("Asset(%s) .... ptr=%d use-count=%d"); - cout << fmt % str(m) % &m % m.use_count() << "\n"; - } }; diff --git a/tests/components/proc/asset/identityofassetstest.cpp b/tests/components/proc/asset/identityofassetstest.cpp index d849fdc15..d55623538 100644 --- a/tests/components/proc/asset/identityofassetstest.cpp +++ b/tests/components/proc/asset/identityofassetstest.cpp @@ -28,13 +28,10 @@ #include "proc/asset/media.hpp" #include "proc/asset/proc.hpp" -#include -#include +#include "proc/asset/assetdiagnostics.hpp" -using boost::format; using util::isnil; using std::string; -using std::cout; namespace asset @@ -56,6 +53,7 @@ namespace asset virtual void run(Arg arg) { createDuplicate(); + cout << "ausis\n"; } typedef shared_ptr PM; @@ -72,8 +70,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(); @@ -87,16 +86,13 @@ namespace asset ASSERT (mm1->getFilename() == "testfile1.mov"); ASSERT (mm2->getFilename() == "testfile2.mov"); - showPtr (mm1); - showPtr (mm1X); - showPtr (mm2); + cout << "use-cnt at end " << mm1.use_count() <<"\n"; + dump (mm1); + dump (mm1X); + dump (mm2); + dumpAssetManager(); } - void showPtr (PM m) - { - static format fmt("Asset(%s) .... ptr=%d use-count=%d"); - cout << fmt % str(m) % &m % m.use_count() << "\n"; - } };