IMP  2.3.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-2014 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 /** Add a cache attribute to a particle and then remove it
27  when this goes out of scope.
28 */
29 template <class Key, class Value>
32  ParticleIndex pi_;
33  Key key_;
34 
35  public:
36  IMP_RAII(ScopedAddCacheAttribute, (Particle* p, Key key, const Value& value),
37  { pi_ = base::get_invalid_index<ParticleIndexTag>(); },
38  {
39  m_ = p->get_model();
40  pi_ = p->get_index();
41  key_ = key;
42  m_->add_cache_attribute(key_, pi_, value);
43  },
44  {
45  if (pi_ != base::get_invalid_index<ParticleIndexTag>()) {
46  m_->remove_attribute(key_, pi_);
47  }
48  }, );
49 };
50 
51 /** Set an attribute to a given value and restore the old
52  value when this goes out of scope..
53 */
54 template <class Key, class Value>
57  ParticleIndex pi_;
58  Key key_;
59  Value old_;
60 
61  public:
62  IMP_RAII(ScopedSetAttribute, (Particle* p, Key key, const Value& value),
63  { pi_ = base::get_invalid_index<ParticleIndexTag>(); },
64  {
65  m_ = p->get_model();
66  pi_ = p->get_index();
67  key_ = key;
68  old_ = m_->get_attribute(key_, pi_);
69  m_->set_attribute(key_, pi_, value);
70  },
71  {
72  if (pi_ != base::get_invalid_index<ParticleIndexTag>()) {
73  m_->set_attribute(key_, pi_, old_);
74  }
75  }, );
76 };
77 
78 typedef ScopedSetAttribute<FloatKey, Float> ScopedSetFloatAttribute;
79 
80 IMPKERNEL_END_NAMESPACE
81 
82 #endif /* IMPKERNEL_SCOPED_H */
Control display of deprecation information.
A base class for Keys.
Definition: kernel/Key.h:46
Control display of deprecation information.
ParticleIndex get_index() const
returns the particle index of this particle in its model
#define IMP_RAII(Name, args, Initialize, Set, Reset, Show)
Declares RAII-style methods in a class.
Definition: raii_macros.h:34
A smart pointer to a reference counted object.
Definition: Pointer.h:87
Basic types used by IMP.
Logging and error reporting support.
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.
A nullptr-initialized pointer to an IMP Object.
Exception definitions and assertions.
Various general useful macros for IMP.