IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
scoped.h
Go to the documentation of this file.
1/**
2 * \file IMP/scoped.h
3 * \brief Temporarily set particle attributes.
4 *
5 * Copyright 2007-2026 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>
19#include <IMP/Pointer.h>
20#include <IMP/raii_macros.h>
21#include <IMP/check_macros.h>
22#include <IMP/log_macros.h>
23
24IMPKERNEL_BEGIN_NAMESPACE
25
26/** Add a cache attribute to a particle and then remove it
27 when this goes out of scope.
28*/
29template <class Key, class Value>
30class ScopedAddCacheAttribute : public RAII {
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*/
54template <class Key, class Value>
55class ScopedSetAttribute : public RAII {
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 pi_ = get_invalid_index<ParticleIndexTag>();
75 }
76 }, );
77};
78
79typedef ScopedSetAttribute<FloatKey, Float> ScopedSetFloatAttribute;
80
81IMPKERNEL_END_NAMESPACE
82
83#endif /* IMPKERNEL_SCOPED_H */
Storage of a model, its restraints, constraints and particles.
A nullptr-initialized pointer to an IMP Object.
Temporarily change something; undo the change when object destroyed.
Used to hold a set of related restraints.
Shared score state.
Helper macros for throwing and handling exceptions.
A base class for Keys.
Definition Key.h:45
Class to handle individual particles of a Model object.
Definition Particle.h:45
Base class for a simple primitive-like type.
Definition Value.h:23
Control display of deprecation information.
Control display of deprecation information.
Logging and error reporting support.
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
Macros to aid in writing RAII-style classes.
#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:88