IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
ExampleDecorator.h
Go to the documentation of this file.
1/**
2 * \file IMP/example/ExampleDecorator.h \brief Add a name to a particle.
3 *
4 * Copyright 2007-2022 IMP Inventors. All rights reserved.
5 *
6 */
7
8#ifndef IMPEXAMPLE_EXAMPLE_DECORATOR_H
9#define IMPEXAMPLE_EXAMPLE_DECORATOR_H
10
11#include <IMP/example/example_config.h>
12
13#include <IMP/Particle.h>
14#include <IMP/Model.h>
15#include <IMP/Decorator.h>
17#include <IMP/exception.h>
18
19IMPEXAMPLE_BEGIN_NAMESPACE
20
21//! A simple decorator which adds a name to a particle.
22/** A decorator adds functionality to a particle and ensures that invariants
23 are preserved. In this case, the functionality is the setting and access
24 of a name for the Particle and the invariant is that the name is
25 always
26 non-empty.
27
28 The source code is as follows:
29 \include ExampleDecorator.h
30 \include ExampleDecorator.cpp
31*/
32class IMPEXAMPLEEXPORT ExampleDecorator : public Decorator {
33 /* Use a static variable in a static method to create the key
34 so that it is only done once and is only done when it is first
35 needed. Lazy initialization of keys makes \imp more efficient as
36 Particles do not have to allocate memory for unused keys.
37 */
38 static StringKey get_name_key();
39 //! Add a name to the particle
40 /** The create function should take arguments which allow
41 the initial state of the Decorator to be reasonable (i.e.
42 make sure there is a non-empty name).
43 */
44 static void do_setup_particle(Model *m, ParticleIndex pi,
45 std::string name) {
46 // use the usage check macro to make sure that arguments are correct
47 IMP_USAGE_CHECK(!name.empty(), "The name cannot be empty.");
48 m->add_attribute(get_name_key(), pi, name);
49 }
50
51 public:
52 //! return true if the particle has a name
53 static bool get_is_setup(Model *m, ParticleIndex pi) {
54 return m->get_has_attribute(get_name_key(), pi);
55 }
56
57 //! Get the name added to the particle
58 std::string get_decorator_name() const {
59 return get_particle()->get_value(get_name_key());
60 }
61
62 //! Set the name added to the particle
63 void set_decorator_name(std::string nm) {
64 // use the usage check macro to check that functions are called properly
65 IMP_USAGE_CHECK(!nm.empty(), "The name cannot be empty");
66 get_particle()->set_value(get_name_key(), nm);
67 }
68
69 /* Declare the basic constructors and the cast function.*/
71 IMP_DECORATOR_SETUP_1(ExampleDecorator, std::string, name);
72};
73
74/** Define a collection of them. Also look at example.i*/
75IMP_DECORATORS(ExampleDecorator, ExampleDecorators, Particles);
76
77IMPEXAMPLE_END_NAMESPACE
78
79#endif /* IMPEXAMPLE_EXAMPLE_DECORATOR_H */
The base class for decorators.
Storage of a model, its restraints, constraints and particles.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Interface to specialized Particle types (e.g. atoms).
Definition Decorator.h:119
Particle * get_particle() const
Returns the particle decorated by this decorator.
Definition Decorator.h:194
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
A simple decorator which adds a name to a particle.
void set_decorator_name(std::string nm)
Set the name added to the particle.
static bool get_is_setup(Model *m, ParticleIndex pi)
return true if the particle has a name
std::string get_decorator_name() const
Get the name added to the particle.
Helper macros for implementing Decorators.
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
#define IMP_DECORATOR_METHODS(Name, Parent)
Exception definitions and assertions.
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
Key< 2 > StringKey
The type used to identify string attributes in the Particles.
Definition base_types.h:40
Vector< Pointer< Particle > > Particles
A list of reference-counted Particle objects.
Definition base_types.h:138