IMP  2.1.1
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 /** \deprecated_at{2.1} No longer needed, just destroy the ScoreState itself.
27 */
28 template <class SS>
31 
32  public:
33 
34  IMP_DEPRECATED_RAII(2.1, "Just create/destroy the ScoreState itself", KERNEL,
35  GenericScopedScoreState, (SS* ss, Model* m), {},
36  {
37  ss_ = ss;
38  m->add_score_state(ss);
39  },
40  {
41  if (ss_ && ss_->get_model()) {
42  IMP_CHECK_OBJECT(ss_);
43  IMP_CHECK_OBJECT(ss_->get_model());
44  ss_->get_model()->remove_score_state(ss_);
45  ss_ = nullptr;
46  }
47  },
48  {
49  if (ss_)
50  out << "(Scoped " << ss_->get_name() << ")";
51  else
52  out << "(Unset scoped score state)";
53  });
54  bool get_is_set() const { return ss_; }
55 #ifndef SWIG
56  const SS* operator->() const { return ss_; }
57  const SS& operator*() const { return *ss_; }
58  SS* operator->() { return ss_; }
59  SS& operator*() { return *ss_; }
60 #endif
61 };
62 
63 //! Removes the Restraint when the RAII object is destroyed
64 /** It is templated so it can act as a general pointer
65  to the restraint.
66  \deprecated_at{2.1} With the advent of the ScoringFunction, this
67  should not be needed.
68 */
69 template <class SS>
73 
74  public:
75 
76  IMP_DEPRECATED_RAII(2.1, "Just use an IMP::kernel::ScoringFunction", KERNEL,
77  GenericScopedRestraint, (SS* ss, RestraintSet* rs), {},
78  {
79  ss_ = ss;
80  rs_ = rs;
81  rs_->add_restraint(ss);
82  },
83  {
84  if (ss_ && ss_->get_model()) {
85  IMP_CHECK_OBJECT(ss_);
86  IMP_CHECK_OBJECT(ss_->get_model());
87  rs_->remove_restraint(ss_);
88  ss_ = nullptr;
89  rs_ = nullptr;
90  }
91  },
92  {
93  if (ss_)
94  out << "(Scoped " << ss_->get_name() << ")";
95  else
96  out << "(Unset scoped restraint)";
97  });
98  bool get_is_set() const { return ss_; }
99 #ifndef SWIG
100  const SS* operator->() const { return ss_; }
101  const SS& operator*() const { return *ss_; }
102  SS* operator->() { return ss_; }
103  SS& operator*() { return *ss_; }
104 #endif
105 };
106 
107 //! Removes the Restraint until RAII object is destroyed
108 /** It is templated so it can act as a general pointer
109  to the restraint.
110  \deprecated_at{2.1} With the advent of the ScoringFunction, this
111  should not be needed.
112 */
113 template <class SS>
115  base::Pointer<SS> ss_;
117  void cleanup() {
118  if (rs_ && rs_->get_model()) {
119  IMP_LOG_VERBOSE("Restoring restraint " << ss_->get_name() << " to "
120  << rs_->get_name() << std::endl);
121  IMP_CHECK_OBJECT(ss_);
122  IMP_CHECK_OBJECT(rs_->get_model());
123  rs_->add_restraint(ss_);
124  ss_ = nullptr;
125  rs_ = nullptr;
126  } else if (ss_) {
127  IMP_LOG_VERBOSE("Not restoring restraint " << ss_->get_name()
128  << std::endl);
129  }
130  }
131  void setup(Restraint* ss, RestraintSet* rs) {
132  ss_ = ss;
133  rs_ = rs;
134  rs_->remove_restraint(ss);
135  IMP_LOG_VERBOSE("Removing restraint " << ss_->get_name() << " from "
136  << rs_->get_name() << std::endl);
137  }
138 
139  public:
140  IMP_DEPRECATED_RAII(2.1, "Just use an IMP::kernel::ScoringFunction", KERNEL,
142  {}, { setup(ss, rs); }, { cleanup(); }, {
143  if (ss_)
144  out << "(Scoped removal of " << ss_->get_name() << ")";
145  else
146  out << "(Unset scoped restraint)";
147  });
148  bool get_is_set() const { return ss_; }
149 #ifndef SWIG
150  const SS* operator->() const { return ss_; }
151  const SS& operator*() const { return *ss_; }
152  SS* operator->() { return ss_; }
153  SS& operator*() { return *ss_; }
154 #endif
155 };
156 
157 /** \deprecated_at{2.1} This doesn't actually do anything any more.
158 */
159 template <class SS>
161  base::Pointer<SS> ss_;
163  void cleanup() {
164  if (rs_) {
165  IMP_LOG_VERBOSE("Restoring restraint " << ss_->get_name() << " to "
166  << rs_->get_name() << std::endl);
167  IMP_CHECK_OBJECT(ss_);
168  IMP_CHECK_OBJECT(rs_);
169  rs_->add_score_state(ss_);
170  ss_ = nullptr;
171  rs_ = nullptr;
172  }
173  }
174  void setup(ScoreState* ss, Model* rs) {
175  ss_ = ss;
176  rs_ = rs;
177  rs_->remove_score_state(ss);
178  IMP_LOG_VERBOSE("Removing restraint " << ss_->get_name() << " from "
179  << rs_->get_name() << std::endl);
180  }
181 
182  public:
183  IMP_DEPRECATED_RAII(2.1, "This doesn't work any more", KERNEL,
184  GenericScopedRemoveScoreState, (SS* ss, Model* rs), {},
185  {
186  IMPKERNEL_DEPRECATED_VALUE_DEF(
187  2.1, "This doesn't do anything any more");
188  setup(ss, rs);
189  },
190  { cleanup(); }, {
191  if (ss_)
192  out << "(Scoped removal of " << ss_->get_name() << ")";
193  else
194  out << "(Unset scoped restraint)";
195  });
196  bool get_is_set() const { return ss_; }
197 #ifndef SWIG
198  const SS* operator->() const { return ss_; }
199  const SS& operator*() const { return *ss_; }
200  SS* operator->() { return ss_; }
201  SS& operator*() { return *ss_; }
202 #endif
203 };
204 
205 //! Remove a score state when the object goes out of scope
207 //! Remove a restraint when the object goes out of scope
209 //! Remove a restraint until the object goes out of scope
211 //! Remove a score state until the object goes out of scope
213 
214 /** Add a cache attribute to a particle and then remove it
215  when this goes out of scope.
216 */
217 template <class Key, class Value>
220  ParticleIndex pi_;
221  Key key_;
222 
223  public:
224  IMP_RAII(ScopedAddCacheAttribute, (Particle* p, Key key, const Value& value),
225  { pi_ = base::get_invalid_index<ParticleIndexTag>(); },
226  {
227  m_ = p->get_model();
228  pi_ = p->get_index();
229  key_ = key;
230  m_->add_cache_attribute(key_, pi_, value);
231  },
232  {
233  if (pi_ != base::get_invalid_index<ParticleIndexTag>()) {
234  m_->remove_attribute(key_, pi_);
235  }
236  }, );
237 };
238 
239 /** Set an attribute to a given value and restore the old
240  value when this goes out of scope..
241 */
242 template <class Key, class Value>
245  ParticleIndex pi_;
246  Key key_;
247  Value old_;
248 
249  public:
250  IMP_RAII(ScopedSetAttribute, (Particle* p, Key key, const Value& value),
251  { pi_ = base::get_invalid_index<ParticleIndexTag>(); },
252  {
253  m_ = p->get_model();
254  pi_ = p->get_index();
255  key_ = key;
256  old_ = m_->get_attribute(key_, pi_);
257  m_->set_attribute(key_, pi_, value);
258  },
259  {
260  if (pi_ != base::get_invalid_index<ParticleIndexTag>()) {
261  m_->set_attribute(key_, pi_, old_);
262  }
263  }, );
264 };
265 
266 typedef ScopedSetAttribute<FloatKey, Float> ScopedSetFloatAttribute;
267 
268 IMPKERNEL_END_NAMESPACE
269 
270 #endif /* IMPKERNEL_SCOPED_H */
A base class for Keys.
Definition: kernel/Key.h:46
A nullptr-initialized pointer to an IMP Object.
IMP::kernel::ScopedSetFloatAttribute ScopedSetFloatAttribute
Object used to hold a set of restraints.
IMP::kernel::ScopedRemoveRestraint ScopedRemoveRestraint
#define IMP_DEPRECATED_RAII(version, help_message, MODULE, Name, args,Initialize, Set, Reset, Show)
ScoreStates maintian invariants in the Model.
Removes the Restraint when the RAII object is destroyed.
Definition: kernel/scoped.h:70
Control display of deprecation information.
Removes the Restraint until RAII object is destroyed.
Various general useful macros for IMP.
VectorD< D > operator*(double s, const VectorD< D > &o)
Definition: VectorD.h:385
IMP::kernel::ScopedRemoveScoreState ScopedRemoveScoreState
Control display of deprecation information.
#define IMP_CHECK_OBJECT(obj)
Perform some basic validity checks on the object for memory debugging.
Logging and error reporting support.
A restraint is a term in an IMP ScoringFunction.
Basic types used by IMP.
Used to hold a set of related restraints.
Shared score state.
Class to handle individual model particles.
Storage of a model, its restraints, constraints and particles.
#define IMP_RAII(Name, args, Initialize, Set, Reset, Show)
Declares RAII-style methods in a class.
Exception definitions and assertions.
IMP::kernel::ScopedRestraint ScopedRestraint
IMP::kernel::ScopedScoreState ScopedScoreState
#define IMP_LOG_VERBOSE(expr)
Class for storing model, its restraints, constraints, and particles.