C++ (Qt)struct SkeletonManager { typedef std::shared_ptr<Skeleton> TShared; typedef std::weak_ptr<Skeleton> TWeak; bool AddSkeleton( const std::string & key, Skeleton * sk ) { // check already in map and alive TMap::iterator it = mMap.find(key); if (it != mMap.end()) if (!it->second.second.expired()) return false; // create shared + weak TPair & p = mMap[key]; p.first.reset(sk); p.second = TWeak(p.first); return true; } bool DeleteSkeleton( const std::string & key ) { TMap::iterator it = mMap.find(key); if (it == mMap.end()) return false; TPair & p = it->second; // release shared if (p.first.get()) p.first.reset(); // get rid pf empty slot if (p.second.expired()) mMap.erase(it); return true; } TWeak GetSkeleton( const std::string & key ) { TMap::iterator it = mMap.find(key); if (it != mMap.end()) { // return weak if alive if (!it->second.second.expired()) return it->second.second; // get rid of empty slot else mMap.erase(it); } return TWeak(); } private: typedef std::pair<TShared, TWeak> TPair; typedef std::map<std::string, TPair> TMap; TMap mMap;};
C++ (Qt)struct SkeletonManager { typedef std::shared_ptr<Skeleton> TShared; typedef std::weak_ptr<Skeleton> TWeak; bool AddSkeleton( const std::string & key, Skeleton * sk ) { // check already in map and alive TMap::iterator it = mMap.find(key); if (it != mMap.end()) return false; // create shared + weak mMap[key] = TShared( sk ); return true; } bool DeleteSkeleton( const std::string & key ) { TMap::iterator it = mMap.find(key); if (it == mMap.end()) return false; mMap.erase(it); return true; } TWeak GetSkeleton( const std::string & key ) { TMap::iterator it = mMap.find(key); if (it != mMap.end()) return it.second; return TWeak(); } private: typedef std::map<std::string, TSahred> TMap; TMap mMap;};
C++ (Qt) template < _Skeleton > bool AddSkeleton( const std::string & key ) { // check already in map and alive TMap::iterator it = mMap.find(key); if (it != mMap.end()) return false; // create shared + weak mMap[key] = std::make_shared< _Skeleton >(); //mMap[key] = TShared( new _Skeleton ); return true; }
C++ (Qt)SkeletonManager manager;...SkeletonManager::TShared skeleton = manager.GetSkeleton( key );if ( skeleton ){ ...}
C++ (Qt) mMap[key] = std::make_shared< _Skeleton >(); //mMap[key] = TShared( new _Skeleton ); }
template < typename _Skeleton, typename ... _Arguments > bool AddSkeleton( const std::string & key, _Arguments ... arguments ) { // check already in map and alive TMap::iterator it = mMap.find(key); if (it != mMap.end()) return false; // create shared + weak mMap[key] = std::make_shared< _Skeleton >(); //mMap[key] = TShared( new _Skeleton( arguments ... ) ); return true; }