IMP logo
IMP Reference Guide  develop.36b5e40f34,2024/10/14
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>
16 #include <IMP/decorator_macros.h>
17 #include <IMP/exception.h>
18 
19 IMPEXAMPLE_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 */
32 class 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.*/
72 };
73 
74 /** Define a collection of them. Also look at example.i*/
76 
77 IMPEXAMPLE_END_NAMESPACE
78 
79 #endif /* IMPEXAMPLE_EXAMPLE_DECORATOR_H */
The base class for decorators.
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
Storage of a model, its restraints, constraints and particles.
std::string get_decorator_name() const
Get the name added to the particle.
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
A simple decorator which adds a name to a particle.
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
add particle attribute with the specified key and initial value
Helper macros for implementing Decorators.
A base class for Keys.
Definition: Key.h:45
Particle * get_particle() const
Returns the particle decorated by this decorator.
Definition: Decorator.h:194
Interface to specialized Particle types (e.g. atoms)
Definition: Decorator.h:119
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
#define IMP_DECORATOR_METHODS(Name, Parent)
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const
return true if particle has attribute with the specified key
static bool get_is_setup(Model *m, ParticleIndex pi)
return true if the particle has a name
void set_decorator_name(std::string nm)
Set the name added to the particle.