IMP  2.2.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-2014 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
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  kernel::Particles do not have to allocate memory for ununsed 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(kernel::Model *m, kernel::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
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 */
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.
Definition: kernel/Model.h:72