IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
object_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/object_macros.h
3  * \brief Helper macros for implementing \imp Objects.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_OBJECT_MACROS_H
10 #define IMPKERNEL_OBJECT_MACROS_H
11 #include <IMP/kernel_config.h>
12 #include "doxygen_macros.h"
13 #include "ref_counted_macros.h"
14 #include "Vector.h"
15 #include "Pointer.h"
16 #include "WeakPointer.h"
17 #include "SetLogState.h"
18 
19 //! Define the basic things needed by any Object
20 /** This defines
21  - IMP::Object::get_version_info()
22  - IMP::Object::get_type_name()
23  - a virtual destructor
24 */
25 #define IMP_OBJECT_METHODS(Name) \
26  public: \
27  virtual std::string get_type_name() const override { return #Name; } \
28  virtual ::IMP::VersionInfo get_version_info() const override { \
29  return ::IMP::VersionInfo(get_module_name(), get_module_version()); \
30  } \
31  \
32  public: \
33  virtual ~Name() { IMP::Object::_on_destruction(); } \
34  \
35  public:
36 
37 //! Define the types for storing lists of object pointers
38 /** The macro defines the types PluralName and PluralNameTemp,
39  which are vectors of either reference-counting or non reference-counting
40  pointers to Name objects, respectively.
41  PluralName should be Names unless the English spelling is
42  different.
43  */
44 #define IMP_OBJECTS(Name, PluralName) \
45  /** A vector of reference-counting object pointers.*/ \
46  typedef IMP::Vector<IMP::Pointer<Name> > PluralName; \
47  /** A vector of weak (non reference-counting) pointers to specified objects. \see Name */ \
48  typedef IMP::Vector<IMP::WeakPointer<Name> > PluralName##Temp;
49 
50 //! Typedefs a default instantiation for a generic (templated) object
51 /**
52  Define type [Name] to be an instantiation of Generic[Name] with
53  template targument, and a function create_[lcname]() that generates a
54  newly allocated object of type [Name], taking parameters [carguments]
55  and internally paassing [cparguments] to the constructor.
56 
57  @note doxygen documentation prior to this macro will be applied to the type
58  definition
59  */
60 #define IMP_GENERIC_OBJECT(Name, lcname, targument, carguments, cparguments) \
61  typedef Generic##Name<targument> Name; \
62  template <class targument> \
63  Generic##Name<targument> *create_##lcname carguments { \
64  return new Generic##Name<targument> cparguments; \
65  }
66 
67 //! Declare a ref counted pointer to a new object
68 /** \param[in] Typename The namespace qualified type being declared
69  \param[in] varname The name for the ref counted pointer
70  \param[in] args The arguments to the constructor, or ()
71  if there are none.
72  Please read the documentation for IMP::Pointer before using.
73 */
74 #define IMP_NEW(Typename, varname, args) \
75  IMP::Pointer<Typename> varname(new Typename args)
76 
77 //! Declare methods needed for serialization of Object pointers
78 /** When an Object subclass is serialized via an IMP::Pointer, the
79  serialization subsystem needs to know the most derived type so that
80  the full information is saved/loaded. This macro ensures that the
81  necessary machinery is added, and should be placed in the header file
82  inside the declaration of the Object subclass.
83  It is similar to cereal's CEREAL_REGISTER_TYPE macro, but stores the
84  type information in precisely one place (the IMP::Object class) rather
85  than relying on the linker to keep this information unique, as cereal
86  attempts to do.
87 
88  This macro needs to be paired with IMP_OBJECT_SERIALIZE_IMPL, which is
89  usually placed in the corresponding .cpp file for the class.
90 
91  \see IMP_OBJECT_SERIALIZE_IMPL
92 
93  \param[in] Name The name of the class.
94  */
95 #define IMP_OBJECT_SERIALIZE_DECL(Name) \
96 private: \
97  static bool dummy_serialize_init_; \
98  static void save_cereal(Object *o, cereal::BinaryOutputArchive &ar) { \
99  Name *cast = dynamic_cast<Name *>(o); \
100  if (!cast) { std::cerr << "bad cast" << std::endl; } \
101  ar(*cast); \
102  } \
103  static Object *load_cereal(cereal::BinaryInputArchive &ar) { \
104  std::unique_ptr<Name> p(new Name()); \
105  ar(*p); \
106  return p.release(); \
107  }
108 
109 //! Add machinery needed for serialization of Object pointers
110 /** \see IMP_OBJECT_SERIALIZE_DECL
111 
112  \param[in] Name The fully-qualified name of the class.
113  */
114 #define IMP_OBJECT_SERIALIZE_IMPL(Name) \
115 bool Name::dummy_serialize_init_ = Object::register_serialize( \
116  typeid(Name), #Name, Name::save_cereal, Name::load_cereal);
117 
118 #endif /* IMPKERNEL_OBJECT_MACROS_H */
Macros to help with reference counting.
A class to change and restore log state.
Helper macros for writing doxygen documentation.
A weak pointer to an Object or RefCountedObject.
A class for storing lists of IMP items.
A nullptr-initialized pointer to an IMP Object.