IMP  2.0.0
The Integrative Modeling Platform
kernel/scoped.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kernel/scoped.h
3  * \brief Various general useful functions for IMP.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_SCOPED_H
10 #define IMPKERNEL_SCOPED_H
11 
12 #include <IMP/kernel/kernel_config.h>
13 #include "RestraintSet.h"
14 #include "ScoreState.h"
15 #include "Model.h"
16 #include <IMP/base/RAII.h>
17 #include <IMP/base/deprecation.h>
19 #include <IMP/base/Pointer.h>
20 #include <IMP/base/raii_macros.h>
21 #include <IMP/base/check_macros.h>
22 #include <IMP/base/log_macros.h>
23 
24 IMPKERNEL_BEGIN_NAMESPACE
25 
26 
27 //! Removes the ScoreState when the RAII object is destroyed
28 /** It is templated so it can act as a general pointer
29  to the score state.
30 */
31 template <class SS>
34 public:
35  IMP_RAII(GenericScopedScoreState, (SS *ss, Model *m),{}, {
36  ss_=ss;
37  m->add_score_state(ss);
38  }, {
39  if (ss_ && ss_->get_is_part_of_model()) {
40  IMP_CHECK_OBJECT(ss_);
41  IMP_CHECK_OBJECT(ss_->get_model());
42  ss_->get_model()->remove_score_state(ss_);
43  ss_=nullptr;
44  }
45  },{
46  if (ss_) out << "(Scoped " <<ss_->get_name() << ")";
47  else out << "(Unset scoped score state)";
48  });
49  bool get_is_set() const {return ss_;}
50 #ifndef SWIG
51  const SS* operator->() const {return ss_;}
52  const SS& operator*() const {return *ss_;}
53  SS* operator->() {return ss_;}
54  SS& operator*() {return *ss_;}
55 #endif
56 };
57 
58 //! Removes the Restraint when the RAII object is destroyed
59 /** It is templated so it can act as a general pointer
60  to the restraint.
61  \deprecated{With the advent of the ScoringFunction, this
62  should not be needed.}
63 */
64 template <class SS>
68 public:
69  IMP_RAII(GenericScopedRestraint, (SS *ss, RestraintSet *rs),{}, {
70  ss_=ss;
71  rs_=rs;
72  rs_->add_restraint(ss);
74  }, {
75  if (ss_ && ss_->get_is_part_of_model()) {
76  IMP_CHECK_OBJECT(ss_);
77  IMP_CHECK_OBJECT(ss_->get_model());
78  rs_->remove_restraint(ss_);
79  ss_=nullptr;
80  rs_=nullptr;
81  }
82  }, {
83  if (ss_) out << "(Scoped " <<ss_->get_name() << ")";
84  else out << "(Unset scoped restraint)";
85  });
86  bool get_is_set() const {return ss_;}
87 #ifndef SWIG
88  const SS* operator->() const {return ss_;}
89  const SS& operator*() const {return *ss_;}
90  SS* operator->() {return ss_;}
91  SS& operator*() {return *ss_;}
92 #endif
93 };
94 
95 //! Removes the Restraint until RAII object is destroyed
96 /** It is templated so it can act as a general pointer
97  to the restraint.
98  \deprecated{With the advent of the ScoringFunction, this
99  should not be needed.}
100 */
101 template <class SS>
103  base::Pointer<SS> ss_;
105  void cleanup() {
106  if (rs_ && rs_->get_is_part_of_model()) {
107  IMP_LOG_VERBOSE( "Restoring restraint "
108  << ss_->get_name() << " to "
109  << rs_->get_name() << std::endl);
110  IMP_CHECK_OBJECT(ss_);
111  IMP_CHECK_OBJECT(rs_->get_model());
112  rs_->add_restraint(ss_);
113  ss_=nullptr;
114  rs_=nullptr;
115  } else if (ss_) {
116  IMP_LOG_VERBOSE( "Not restoring restraint "
117  << ss_->get_name() << std::endl);
118  }
119  }
120  void setup(Restraint* ss, RestraintSet *rs) {
121  ss_=ss;
122  rs_=rs;
123  rs_->remove_restraint(ss);
124  IMP_LOG_VERBOSE( "Removing restraint "
125  << ss_->get_name() << " from "
126  << rs_->get_name() << std::endl);
127  }
128 public:
130  setup(ss, rs);
132  }, {
133  cleanup();
134  }, {
135  if (ss_) out << "(Scoped removal of " <<ss_->get_name() << ")";
136  else out << "(Unset scoped restraint)";
137  });
138  bool get_is_set() const {return ss_;}
139 #ifndef SWIG
140  const SS* operator->() const {return ss_;}
141  const SS& operator*() const {return *ss_;}
142  SS* operator->() {return ss_;}
143  SS& operator*() {return *ss_;}
144 #endif
145 };
146 
147 
148 //! Removes the ScoreState until RAII object is destroyed
149 /** It is templated so it can act as a general pointer
150  to the restraint.
151 */
152 template <class SS>
154  base::Pointer<SS> ss_;
156  void cleanup() {
157  if (rs_) {
158  IMP_LOG_VERBOSE( "Restoring restraint "
159  << ss_->get_name() << " to "
160  << rs_->get_name() << std::endl);
161  IMP_CHECK_OBJECT(ss_);
162  IMP_CHECK_OBJECT(rs_);
163  rs_->add_score_state(ss_);
164  ss_=nullptr;
165  rs_=nullptr;
166  }
167  }
168  void setup(ScoreState* ss, Model *rs) {
169  ss_=ss;
170  rs_=rs;
171  rs_->remove_score_state(ss);
172  IMP_LOG_VERBOSE( "Removing restraint "
173  << ss_->get_name() << " from "
174  << rs_->get_name() << std::endl);
175  }
176 public:
177  IMP_RAII(GenericScopedRemoveScoreState, (SS *ss, Model *rs),{}, {
178  setup(ss, rs);
179  }, {
180  cleanup();
181  }, {
182  if (ss_) out << "(Scoped removal of " <<ss_->get_name() << ")";
183  else out << "(Unset scoped restraint)";
184  });
185  bool get_is_set() const {return ss_;}
186 #ifndef SWIG
187  const SS* operator->() const {return ss_;}
188  const SS& operator*() const {return *ss_;}
189  SS* operator->() {return ss_;}
190  SS& operator*() {return *ss_;}
191 #endif
192 };
193 
194 //! Remove a score state when the object goes out of scope
196 //! Remove a restraint when the object goes out of scope
198 //! Remove a restraint until the object goes out of scope
200 //! Remove a score state until the object goes out of scope
202 
203 /** Add a cache attribute to a particle and then remove it
204  when this goes out of scope.
205 */
206 template <class Key, class Value>
209  ParticleIndex pi_;
210  Key key_;
211 public:
213  Key key, const Value &value),
214  {pi_=base::get_invalid_index<ParticleIndexTag>();},
215  {
216  m_=p->get_model();
217  pi_=p->get_index();
218  key_=key;
219  m_->add_cache_attribute(key_, pi_, value);
220  },
221  {
222  if (pi_ !=base::get_invalid_index<ParticleIndexTag>()) {
223  m_->remove_attribute(key_, pi_);
224  }
225  },);
226 };
227 
228 
229 /** Set an attribute to a given value and restore the old
230  value when this goes out of scope..
231 */
232 template <class Key, class Value>
235  ParticleIndex pi_;
236  Key key_;
237  Value old_;
238 public:
240  Key key, const Value &value),
241  {pi_=base::get_invalid_index<ParticleIndexTag>();},
242  {
243  m_=p->get_model();
244  pi_=p->get_index();
245  key_=key;
246  old_= m_->get_attribute(key_, pi_);
247  m_->set_attribute(key_, pi_, value);
248  },
249  {
250  if (pi_ != base::get_invalid_index<ParticleIndexTag>()) {
251  m_->set_attribute(key_, pi_, old_);
252  }
253  },);
254 };
255 
257 
258 
259 IMPKERNEL_END_NAMESPACE
260 
261 #endif /* IMPKERNEL_SCOPED_H */