IMP logo
IMP Reference Guide  2.8.0
The Integrative Modeling Platform
scoped.h
Go to the documentation of this file.
1 /**
2  * \file IMP/scoped.h
3  * \brief Various general useful functions for IMP.
4  *
5  * Copyright 2007-2017 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_SCOPED_H
10 #define IMPKERNEL_SCOPED_H
11 
12 #include <IMP/kernel_config.h>
13 #include "RestraintSet.h"
14 #include "ScoreState.h"
15 #include "Model.h"
16 #include <IMP/RAII.h>
17 #include <IMP/deprecation.h>
18 #include <IMP/deprecation_macros.h>
19 #include <IMP/Pointer.h>
20 #include <IMP/raii_macros.h>
21 #include <IMP/check_macros.h>
22 #include <IMP/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>
30 class ScopedAddCacheAttribute : public RAII {
31  Pointer<Model> m_;
32  ParticleIndex pi_;
33  Key key_;
34 
35  public:
36  IMP_RAII(ScopedAddCacheAttribute, (Particle* p, Key key, const Value& value),
37  { pi_ = 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_ != 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>
55 class ScopedSetAttribute : public RAII {
56  Pointer<Model> m_;
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_ = 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_ != 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.
Control display of deprecation information.
Used to hold a set of related restraints.
Temporarily change something; undo the change when this object is destroyed.
Definition: RAII.h:26
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.
Definition: raii_macros.h:34
A smart pointer to a reference counted object.
Definition: Pointer.h:87
Basic types used by IMP.
Base for a simple primitive-like type.
Definition: Value.h:21
Logging and error reporting support.
A base class for Keys.
Definition: Key.h:44
Shared score state.
A nullptr-initialized pointer to an IMP Object.
Exception definitions and assertions.
Class to handle individual particles of a Model object.
Definition: Particle.h:41
ParticleIndex get_index() const
returns the particle index of this particle in its model
Various general useful macros for IMP.