IMP logo

scoped.h

Go to the documentation of this file.
00001 /**
00002  *  \file IMP/scoped.h    \brief Various general useful functions for IMP.
00003  *
00004  *  Copyright 2007-2011 IMP Inventors. All rights reserved.
00005  *
00006  */
00007 
00008 #ifndef IMP_SCOPED_H
00009 #define IMP_SCOPED_H
00010 
00011 #include "macros.h"
00012 #include "kernel_config.h"
00013 #include "RestraintSet.h"
00014 #include "ScoreState.h"
00015 #include "FailureHandler.h"
00016 #include "Model.h"
00017 
00018 IMP_BEGIN_NAMESPACE
00019 
00020 
00021 //! Removes the ScoreState when the RAII object is destroyed
00022 /** It is templated so it can act as a general pointer
00023     to the score state.
00024 */template <class SS>
00025 class GenericScopedScoreState {
00026   Pointer<SS> ss_;
00027 public:
00028   IMP_RAII(GenericScopedScoreState, (SS *ss, Model *m),{}, {
00029       ss_=ss;
00030       m->add_score_state(ss);
00031     }, {
00032       if (ss_ && ss_->get_has_model()) {
00033         IMP_CHECK_OBJECT(ss_);
00034         IMP_CHECK_OBJECT(ss_->get_model());
00035         ss_->get_model()->remove_score_state(ss_);
00036         ss_=NULL;
00037       }
00038     },{
00039       if (ss_) out << "(Scoped " <<ss_->get_name() << ")";
00040       else out << "(Unset scoped score state)";
00041     });
00042   bool get_is_set() const {return ss_;}
00043 #ifndef SWIG
00044   const SS* operator->() const {return ss_;}
00045   const SS& operator*() const {return *ss_;}
00046   SS* operator->() {return ss_;}
00047   SS& operator*() {return *ss_;}
00048 #endif
00049 };
00050 
00051 //! Removes the Restraint when the RAII object is destroyed
00052 /** It is templated so it can act as a general pointer
00053     to the restraint.
00054 */
00055 template <class SS>
00056 class GenericScopedRestraint {
00057   Pointer<SS> ss_;
00058   Pointer<RestraintSet> rs_;
00059 public:
00060   IMP_RAII(GenericScopedRestraint, (SS *ss, RestraintSet *rs),{}, {
00061       ss_=ss;
00062       rs_=rs;
00063       rs_->add_restraint(ss);
00064     }, {
00065       if (ss_ && ss_->get_is_part_of_model()) {
00066         IMP_CHECK_OBJECT(ss_);
00067         IMP_CHECK_OBJECT(ss_->get_model());
00068         rs_->remove_restraint(ss_);
00069         ss_=NULL;
00070         rs_=NULL;
00071       }
00072     }, {
00073       if (ss_) out << "(Scoped " <<ss_->get_name() << ")";
00074       else out << "(Unset scoped restraint)";
00075     });
00076   bool get_is_set() const {return ss_;}
00077 #ifndef SWIG
00078   const SS* operator->() const {return ss_;}
00079   const SS& operator*() const {return *ss_;}
00080   SS* operator->() {return ss_;}
00081   SS& operator*() {return *ss_;}
00082 #endif
00083 };
00084 
00085 //! Removes the Restraint until RAII object is destroyed
00086 /** It is templated so it can act as a general pointer
00087     to the restraint.
00088 */
00089 template <class SS>
00090 class GenericScopedRemoveRestraint {
00091   Pointer<SS> ss_;
00092   Pointer<RestraintSet> rs_;
00093 public:
00094   IMP_RAII(GenericScopedRemoveRestraint, (SS *ss, RestraintSet *rs),{}, {
00095       ss_=ss;
00096       rs_=rs;
00097       rs_->remove_restraint(ss);
00098     }, {
00099       if (rs_ && rs_->get_is_part_of_model()) {
00100         IMP_CHECK_OBJECT(ss_);
00101         IMP_CHECK_OBJECT(rs_->get_model());
00102         rs_->add_restraint(ss_);
00103         ss_=NULL;
00104         rs_=NULL;
00105       }
00106     }, {
00107       if (ss_) out << "(Scoped removal of " <<ss_->get_name() << ")";
00108       else out << "(Unset scoped restraint)";
00109     });
00110   bool get_is_set() const {return ss_;}
00111 #ifndef SWIG
00112   const SS* operator->() const {return ss_;}
00113   const SS& operator*() const {return *ss_;}
00114   SS* operator->() {return ss_;}
00115   SS& operator*() {return *ss_;}
00116 #endif
00117 };
00118 
00119 //! Remove a score state when the object goes out of scope
00120 typedef GenericScopedScoreState<ScoreState> ScopedScoreState;
00121 //! Remove a restraint when the object goes out of scope
00122 typedef GenericScopedRestraint<Restraint> ScopedRestraint;
00123 //! Remove a restraint until the object goes out of scope
00124 typedef GenericScopedRemoveRestraint<Restraint> ScopedRemoveRestraint;
00125 
00126 
00127 
00128 
00129 //! Control a scope-dependent failure handler
00130 /** The failure handler is added on construction and removed
00131     on destruction.
00132 */
00133 class ScopedFailureHandler {
00134   FailureHandler* fh_;
00135 public:
00136   IMP_RAII(ScopedFailureHandler, (FailureHandler *fh),
00137            {fh_=NULL;},
00138            {
00139              fh_=fh;
00140              if (fh_) add_failure_handler(fh_);
00141            },
00142            {
00143              if (fh_) remove_failure_handler(fh_);
00144              fh_=NULL;
00145            },
00146            );
00147 };
00148 
00149 IMP_END_NAMESPACE
00150 
00151 #endif  /* IMP_SCOPED_H */

Generated on Thu Mar 24 2011 02:01:39 for IMP by doxygen 1.7.3