Use observer_ptr in OrthophotoManager

This commit is contained in:
Nathaniel Warner 2020-11-29 15:02:09 -08:00
parent 023a3de5f1
commit fee2c592c0
2 changed files with 12 additions and 24 deletions

View File

@ -347,9 +347,9 @@ namespace simgear {
}
void OrthophotoManager::registerOrthophoto(const long bucket_idx, const OrthophotoRef& orthophoto) {
OrthophotoRef& entry = _orthophotos[bucket_idx];
OrthophotoWeakRef& entry = _orthophotos[bucket_idx];
if (entry) {
if (entry.valid()) {
SG_LOG(SG_TERRAIN, SG_WARN, "OrthophotoManager::registerOrthophoto(): Bucket index " << bucket_idx << " already has a registered orthophoto.");
}
@ -362,26 +362,13 @@ namespace simgear {
SG_LOG(SG_TERRAIN, SG_INFO, "Registered orthophoto for bucket index " << bucket_idx);
}
void OrthophotoManager::unregisterOrthophoto(const long bucket_idx) {
if (_orthophotos[bucket_idx]) {
_orthophotos.erase(bucket_idx);
SG_LOG(SG_TERRAIN, SG_INFO, "Unregistered orthophoto with bucket index " << bucket_idx);
} else {
SG_LOG(SG_TERRAIN, SG_WARN, "OrthophotoManager::unregisterOrthophoto(): Attempted to unregister orthophoto with bucket index that is not currently registered.");
}
}
void OrthophotoManager::unregisterAll() {
_orthophotos.clear();
SG_LOG(SG_TERRAIN, SG_INFO, "Unregistered all orthophotos");
}
OrthophotoRef OrthophotoManager::getOrthophoto(const long bucket_idx) {
if (_orthophotos[bucket_idx]) {
return _orthophotos[bucket_idx];
} else {
return nullptr;
OrthophotoWeakRef weak_ref = _orthophotos[bucket_idx];
OrthophotoRef ref;
if (weak_ref.valid()) {
weak_ref.lock(ref);
}
return ref;
}
OrthophotoRef OrthophotoManager::getOrthophoto(const std::vector<SGVec3d>& nodes, const SGVec3d& center) {

View File

@ -22,9 +22,11 @@
#include <unordered_map>
#include <osg/Referenced>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/Referenced>
#include <osg/ref_ptr>
#include <osg/observer_ptr>
#include <osgDB/ReaderWriter>
#include <osgDB/ReadFile>
#include <simgear/misc/sg_dir.hxx>
@ -41,6 +43,7 @@ namespace simgear {
class Orthophoto;
using OrthophotoRef = osg::ref_ptr<Orthophoto>;
using OrthophotoWeakRef = osg::observer_ptr<Orthophoto>;
class OrthophotoBounds {
private:
@ -85,13 +88,11 @@ namespace simgear {
class OrthophotoManager : public osg::Referenced {
private:
std::unordered_map<long, OrthophotoRef> _orthophotos;
std::unordered_map<long, OrthophotoWeakRef> _orthophotos;
public:
static OrthophotoManager* instance();
void registerOrthophoto(const long bucket_idx, const OrthophotoRef& orthophoto);
void unregisterOrthophoto(const long bucket_idx);
void unregisterAll();
/**
* Get an orthophoto by bucket index