IMP  2.1.1
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-2013 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/kernel/Particle.h>
14 #include <IMP/kernel/Model.h>
15 #include <IMP/Decorator.h>
16 #include <IMP/decorator_macros.h>
17 #include <IMP/base/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 kernel::Particle and the invariant is that the name is always
25  non-empty.
26 
27  The source code is as follows:
28  \include ExampleDecorator.h
29  \include ExampleDecorator.cpp
30 */
31 class IMPEXAMPLEEXPORT ExampleDecorator : public Decorator {
32  /* Use a static variable in a static method to create the key
33  so that it is only done once and is only done when it is first
34  needed. Lazy initialization of keys makes \imp more efficient as
35  kernel::Particles do not have to allocate memory for ununsed keys.
36  */
37  static StringKey get_name_key();
38  //! Add a name to the particle
39  /** The create function should take arguments which allow
40  the initial state of the Decorator to be reasonable (i.e.
41  make sure there is a non-empty name).
42  */
43  static void do_setup_particle(kernel::Model *m, kernel::ParticleIndex pi, std::string name) {
44  // use the usage check macro to make sure that arguments are correct
45  IMP_USAGE_CHECK(!name.empty(), "The name cannot be empty.");
46  m->add_attribute(get_name_key(), pi, name);
47  }
48 
49  public:
50  //! return true if the particle has a name
52  return m->get_has_attribute(get_name_key(), pi);
53  }
54 
55  //! Get the name added to the particle
56  std::string get_decorator_name() const {
57  return get_particle()->get_value(get_name_key());
58  }
59 
60  //! Set the name added to the particle
61  void set_decorator_name(std::string nm) {
62  // use the usage check macro to check that functions are called properly
63  IMP_USAGE_CHECK(!nm.empty(), "The name cannot be empty");
64  get_particle()->set_value(get_name_key(), nm);
65  }
66 
67  /* Declare the basic constructors and the cast function.*/
70 };
71 
72 /** Define a collection of them. Also look at example.i*/
74 
75 IMPEXAMPLE_END_NAMESPACE
76 
77 #endif /* IMPEXAMPLE_EXAMPLE_DECORATOR_H */
Import IMP/kernel/Decorator.h in the namespace.
A base class for Keys.
Definition: kernel/Key.h:46
Particle * get_particle() const
#define IMP_DECORATOR_METHODS(Name, Parent)
std::string get_decorator_name() const
Get the name added to the particle.
static bool get_is_setup(kernel::Model *m, kernel::ParticleIndex pi)
return true if the particle has a name
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A simple decorator which adds a name to a particle.
Import IMP/kernel/decorator_macros.h in the namespace.
Storage of a model, its restraints, constraints and particles.
Classes to handle individual model particles.
Exception definitions and assertions.
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
void set_decorator_name(std::string nm)
Set the name added to the particle.
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
Class for storing model, its restraints, constraints, and particles.