IMP logo
IMP Reference Guide  develop.d4e9f3251e,2024/04/26
The Integrative Modeling Platform
ExampleObject.h
Go to the documentation of this file.
1 /**
2  * \file IMP/example/ExampleObject.h
3  * \brief An example showing how to make a simple ref counted object
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPEXAMPLE_EXAMPLE_OBJECT_H
9 #define IMPEXAMPLE_EXAMPLE_OBJECT_H
10 
11 #include <IMP/example/example_config.h>
12 
13 #include <IMP/Object.h>
14 #include <IMP/object_macros.h>
15 #include <IMP/types.h>
16 #include <IMP/Pointer.h>
17 #include <vector>
18 #include <cereal/access.hpp>
19 #include <cereal/types/vector.hpp>
20 
21 IMPEXAMPLE_BEGIN_NAMESPACE
22 
23 //! An example simple object which is reference counted.
24 /** Only IMP::Pointer objects should be used to store pointers to
25  instances of these objects.
26 
27  The source code is as follows:
28  \include ExampleObject.h
29  \include ExampleObject.cpp
30  */
31 class IMPEXAMPLEEXPORT ExampleObject : public Object {
32  Floats data_;
33 
34  public:
35  ExampleObject(const Floats &data);
36 
37  // Default constructor, needed for serialization or Python pickle support
38  ExampleObject() : Object("") {}
39 
40  double get_data(unsigned int i) const {
41  IMP_USAGE_CHECK(i < data_.size(), "Index " << i << " out of range.");
42  return data_[i];
43  }
44 
45  /* Make sure that it can't be allocated on the stack
46  The macro defines an empty destructor. In general,
47  you want destructors to be empty since they are hard
48  to maintain.
49  */
51 
52  private:
53  // Serialization support
54  friend class cereal::access;
55  template<class Archive> void serialize(Archive &ar) {
56  // We must save/load everything in the Object base class
57  // (e.g. name) plus our own variables
58  ar(cereal::base_class<Object>(this), data_);
59  }
60 };
61 
62 typedef Vector<Pointer<ExampleObject> > ExampleObjects;
63 typedef Vector<WeakPointer<ExampleObject> > ExampleObjectsTemp;
64 
65 IMPEXAMPLE_END_NAMESPACE
66 
67 #endif /* IMPEXAMPLE_EXAMPLE_OBJECT_H */
Helper macros for implementing IMP Objects.
Basic types used by IMP.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
An example simple object which is reference counted.
Definition: ExampleObject.h:31
Common base class for heavy weight IMP objects.
Definition: Object.h:111
A nullptr-initialized pointer to an IMP Object.
A shared base class to help in debugging and things.
Object(std::string name)
Construct an object with the given name.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168