From 68b5746cca38273a13e0cdfbf6649fa374eb48f4 Mon Sep 17 00:00:00 2001
From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Sat, 10 Apr 2010 08:56:34 +0000
Subject: [PATCH] teach stream operators to deal with invalid handles

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@1963 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/mol/base/src/atom_base.cc       |  9 ++++++---
 modules/mol/base/src/chain_base.cc      | 12 ++++++++----
 modules/mol/base/src/entity_base.cc     | 13 +++++++++----
 modules/mol/base/src/residue_base.cc    | 11 +++++++----
 modules/seq/base/src/sequence_handle.cc |  7 ++++++-
 5 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/modules/mol/base/src/atom_base.cc b/modules/mol/base/src/atom_base.cc
index c0e1f94b2..95141733f 100644
--- a/modules/mol/base/src/atom_base.cc
+++ b/modules/mol/base/src/atom_base.cc
@@ -126,10 +126,13 @@ long AtomBase::GetHashCode() const
   return reinterpret_cast<long>(Impl().get());
 }
 
-std::ostream& operator<<(std::ostream& os, 
-                         const AtomBase& atom) 
+std::ostream& operator<<(std::ostream& os, const AtomBase& atom) 
 {
-  os << atom.GetQualifiedName();
+  if (atom.IsValid()) {
+    os << atom.GetQualifiedName();
+  } else {
+    os << "invalid atom";
+  }
   return os;
 }
 
diff --git a/modules/mol/base/src/chain_base.cc b/modules/mol/base/src/chain_base.cc
index 53385715c..29a40f3a2 100644
--- a/modules/mol/base/src/chain_base.cc
+++ b/modules/mol/base/src/chain_base.cc
@@ -49,7 +49,7 @@ ChainBase::operator bool() const {
 }
 
 bool ChainBase::IsValid() const {
-  return impl_;
+  return impl_.get()!=0;
 }
 
 void ChainBase::CheckValidity() const {
@@ -57,9 +57,13 @@ void ChainBase::CheckValidity() const {
     throw InvalidHandle();
 }
 
-DLLEXPORT_OST_MOL std::ostream& operator<<(std::ostream& os, 
-                                            const ChainBase& chain) {
-  os << chain.GetName();                                              
+std::ostream& operator<<(std::ostream& os, const ChainBase& chain) 
+{
+  if (chain.IsValid()) {
+    os << chain.GetName();
+  } else {
+    os << "invalid chain";
+  }
   return os;
 }
 
diff --git a/modules/mol/base/src/entity_base.cc b/modules/mol/base/src/entity_base.cc
index 546db3743..df3fc636e 100644
--- a/modules/mol/base/src/entity_base.cc
+++ b/modules/mol/base/src/entity_base.cc
@@ -47,11 +47,11 @@ const GenericPropertyContainerImpl* EntityBase::GpImpl() const
 }
 
 bool EntityBase::IsValid() const {
-  return Impl();
+  return impl_.get()!=0;
 }
 
 EntityBase::operator bool() const {
-  return impl_;
+  return impl_.get()!=0;
 }
 
 void EntityBase::CheckValidity() const {
@@ -69,9 +69,14 @@ void EntityBase::SetName(const String& ent_name) {
   impl_->SetName(ent_name);
 }
 
-DLLEXPORT_OST_MOL std::ostream& operator<<(std::ostream& os, 
-                                            const EntityBase& ent) {
+DLLEXPORT_OST_MOL std::ostream& operator<<(std::ostream& os,   
+                                           const EntityBase& ent) {
+  if (ent.IsValid()) {
     os << "entity";
+  } else {
+    os << "invalid";
+  }
+
   return os;
 }
 }} //ns
diff --git a/modules/mol/base/src/residue_base.cc b/modules/mol/base/src/residue_base.cc
index e53801668..60e684806 100644
--- a/modules/mol/base/src/residue_base.cc
+++ b/modules/mol/base/src/residue_base.cc
@@ -70,7 +70,7 @@ ResidueBase::operator bool() const {
 }
 
 bool ResidueBase::IsValid() const {
-  return impl_;
+  return impl_.get()!=0;
 }
 
 bool ResidueBase::IsPeptideLinking() const 
@@ -141,9 +141,12 @@ void ResidueBase::CheckValidity() const
     throw InvalidHandle();
 }
 
-DLLEXPORT_OST_MOL std::ostream& operator<<(std::ostream& os, 
-                                            const ResidueBase& residue) {
-   os << residue.GetQualifiedName();  
+std::ostream& operator<<(std::ostream& os, const ResidueBase& residue) {
+  if (residue.IsValid()) {
+    os << residue.GetQualifiedName();
+  } else {
+    os << "invalid residue"; 
+  }
    return os;                                                                              
 }
 
diff --git a/modules/seq/base/src/sequence_handle.cc b/modules/seq/base/src/sequence_handle.cc
index 903b4edd4..d24fff1ae 100644
--- a/modules/seq/base/src/sequence_handle.cc
+++ b/modules/seq/base/src/sequence_handle.cc
@@ -219,7 +219,12 @@ SequenceHandle SequenceFromInfo(info::InfoGroup& group)
 
 std::ostream& operator<<(std::ostream& os, const ConstSequenceHandle& sequence)
 {
-  os << sequence.GetName() << ": " << sequence.GetString();
+  if (sequence.IsValid()) {
+    os << sequence.GetName() << ": " << sequence.GetString();    
+  } else {
+    os << "invalid";
+  }
+
   return os;
 }
 
-- 
GitLab