IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
Object.h
Go to the documentation of this file.
1/**
2 * \file IMP/Object.h
3 * \brief A shared base class to help in debugging and things.
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef IMPKERNEL_OBJECT_H
10#define IMPKERNEL_OBJECT_H
11
12#include <IMP/kernel_config.h>
13#include <IMP/kernel_config.h>
14#include "exception.h"
15#include "utility_macros.h"
16#include "NonCopyable.h"
17#include "ref_counted_macros.h"
18#include "enums.h"
19#include "hash_macros.h"
20#include "warning_macros.h"
21#include "showable_macros.h"
22#include "VersionInfo.h"
23#include "utility_macros.h"
24#include <IMP/hash.h>
25#include "hash.h"
26#include <boost/scoped_array.hpp>
27#include <cereal/access.hpp>
28
29// Make sure that binary archives are registered with cereal
30// before any Object subclass that uses CEREAL_REGISTER_TYPE
31#include <cereal/archives/binary.hpp>
32
33#if !defined(IMP_HAS_CHECKS)
34#error "IMP_HAS_CHECKS not defined, something is broken"
35#endif
36#if !defined(IMP_NONE)
37#error "IMP_NONE not defined, something is broken"
38#endif
39#if !defined(IMP_HAS_LOG)
40#error "IMP_HAS_LOG not defined, something is broken"
41#endif
42#if !defined(IMP_SILENT)
43#error "IMP_SILENT not defined, something is broken"
44#endif
45
46IMPKERNEL_BEGIN_NAMESPACE
47
48//! Common base class for heavy weight \imp objects.
49/**
50The base class for non \ref introduction_values "value-type classes" in
51\imp. Anything inheriting from Object has the following properties:
52
53- has embedded information about the module and version which can
54be accessed using Object::get_version_info(). This information can
55be used to log what version of software is used to compute a
56result.
57
58- it has a local logging level which can override the global one
59allowing fine grained logging control.
60
61- in Python, there is a method Class::get_from(Object *o) that
62attempts to cast o to an object of type Class and throws an
63exception if it fails.
64
65- the object keeps track of whether it has been been used. See the
66Object::set_was_used() method for an explanation.
67
68- It is reference counted
69
70Types inheriting from Object should always be created using \c new
71in C++ and passed using pointers and stored using Pointer
72objects. Note that you have to be careful of cycles and so must
73use WeakPointer objects to break cycles. IMP_NEW() can help shorten
74creating a ref counted pointer. See Pointer for more
75information.
76
77See example::ExampleObject for a simple example.
78
79Reference counting is a technique for managing memory and
80automatically freeing memory (destroying objects) when it is no
81longer needed. In reference counting, each object has a reference
82count, which tracks how many different places are using the
83object. When this count goes to 0, the object is freed.
84
85Python internally reference counts everything. C++, on the other
86hand, requires extra steps be taken to ensure that objects are
87reference counted properly.
88
89In \imp, reference counting is done through the Pointer,
90PointerMember and Object classes. The former should be used
91instead of a raw C++ pointer when storing a pointer to any object
92inheriting from Object.
93
94Any time one is using reference
95counting, one needs to be aware of cycles, since if, for example,
96object A contains an IMP::Pointer to object B and object B
97contains an Pointer to object A, their reference counts will never
98go to 0 even if both A and B are no longer used. To avoid this,
99use an WeakPointer in one of A or B.
100
101Functions that create new objects should follow the following pattern
102
103 ObjectType *create_object(arguments) {
104 IMP_NEW(ObjectType, ret, (args));
105 do_stuff;
106 return ret.release();
107 }
108
109using Pointer::release() to safely return the new object without freeing it.
110*/
111class IMPKERNELEXPORT Object : public NonCopyable {
112 std::string name_;
113#if IMP_KERNEL_HAS_LOG4CXX
114 boost::scoped_array<char> quoted_name_;
115#endif
116
117 static unsigned int live_objects_;
118 mutable int count_;
119
120#if IMP_HAS_LOG != IMP_NONE
121 LogLevel log_level_;
122#endif
123
124#if IMP_HAS_CHECKS >= IMP_USAGE
125 CheckLevel check_level_;
126 mutable bool was_owned_;
127 double check_value_;
128#endif
129
130#if IMP_HAS_CHECKS >= IMP_INTERNAL
131 static void add_live_object(Object* o);
132 static void remove_live_object(Object* o);
133#endif
134
135 friend class cereal::access;
136
137 template<class Archive> void serialize(Archive &ar) {
138 ar(name_);
139#if IMP_HAS_LOG != IMP_NONE
140 ar(log_level_);
141#endif
142#if IMP_HAS_CHECKS >= IMP_USAGE
143 ar(check_level_, was_owned_, check_value_);
144#endif
145 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
146 // set quoted_name from name
147 set_name_internal(name_);
148 }
149 }
150
151 void set_name_internal(std::string name);
152
153 void initialize(std::string name);
154
155 int compare(const Object& o) const {
156 if (&o < this)
157 return 1;
158 else if (&o > this)
159 return -1;
160 else
161 return 0;
162 }
163
164 typedef std::function<void(Object *, cereal::BinaryOutputArchive &)> SaveFunc;
165 typedef std::function<Object*(cereal::BinaryInputArchive &)> LoadFunc;
166 struct OutputSerializer {
167 std::string class_name;
168 SaveFunc save_func;
169 };
170
171 static std::map<std::string, OutputSerializer> &get_output_serializers();
172 static std::map<std::string, LoadFunc> &get_input_serializers();
173
174 protected:
175 //! Construct an object with the given name
176 /** An instance of "%1%" in the string will be replaced by a unique
177 index.
178 */
179 Object(std::string name);
180
181 public:
182 virtual ~Object();
183 // needed for Python to make sure all wrapper objects are equivalent
184 IMP_HASHABLE_INLINE(Object, return boost::hash_value(this););
185
186 //! Set the logging level used in this object
187 /** Each object can be assigned a different log level in order to,
188 for example, suppress messages for verbose and uninteresting
189 objects. If set to DEFAULT, the global level as returned by
190 IMP::get_log_level() is used, otherwise
191 the local one is used. Methods in classes inheriting from
192 Object should start with IMP_OBJECT_LOG to change the log
193 level to the local one for this object and increase
194 the current indent.
195
196 \see IMP::set_log_level()
197 \see IMP::get_log_level()
198 */
200
201 /** Each object can be assigned a different check level too.
202 */
204 LogLevel get_log_level() const;
205 CheckLevel get_check_level() const;
206
207 //! Get information about the module and version of the object
209 return VersionInfo("none", "none");
210 }
211
212 /** @name Names
213 All objects have names to aid in debugging and inspection
214 of the state of the system. These names are not necessarily unique
215 and should not be used to store data or as keys into a table. Use
216 the address of the object instead since objects cannot be copied.
217 @{
218 */
219 //! Get the (not necessarily unique) name of this object.
220 const std::string& get_name() const { return name_; }
221 void set_name(std::string name);
222 virtual std::string get_type_name() const { return "unknown object type"; }
223 /* @} */
224
225 /** \imp provides warnings when objects are never used before they are
226 destroyed. Examples of use include adding an IMP::Restraint to an
227 IMP::ScoringFunction. If an object is not properly marked as used, or
228 your code is the one using it, call set_was_used(true) on the object.
229 */
230 void set_was_used(bool tf) const;
231
233
234#ifndef IMP_DOXYGEN
235 //! Return a string version of the object, can be used in the debugger
236 std::string get_string() const {
237 std::ostringstream oss;
238 show(oss);
239 return oss.str();
240 }
241
242#ifndef SWIG
243 void ref() const { ++count_; }
244 void unref() const;
245 void release() const;
246#if IMP_KERNEL_HAS_LOG4CXX
247 const char* get_quoted_name_c_string() const { return quoted_name_.get(); }
248#endif
249
250 /** \see IMP_OBJECT_SERIALIZE_DECL */
251 static bool register_serialize(const std::type_info &t, std::string name,
252 SaveFunc save_func, LoadFunc load_func);
253
254 //! Save the most-derived Object subclass to the given binary archive
255 void poly_serialize(cereal::BinaryOutputArchive &ar);
256
257 //! Create most-derived Object subclass from the given binary archive
258 static Object *poly_unserialize(cereal::BinaryInputArchive &ar);
259#endif
260
261 void _on_destruction();
262
263 //! Return whether the object already been freed
264 bool get_is_valid() const;
265
266 unsigned int get_ref_count() const { return count_; }
267
268 static unsigned int get_number_of_live_objects() { return live_objects_; }
269 bool get_is_shared() const { return count_ > 1; }
270#endif // IMP_DOXYGEN
271
272 /** Objects can have internal caches. This method resets them returning
273 the object to its just-initialized state.
274 */
275 virtual void clear_caches() {}
276
277 protected:
278 /** Override this method to take action on destruction. */
279 virtual void do_destroy() {}
280};
281
282#if !defined(SWIG) && !defined(IMP_DOXYGEN)
283inline void Object::set_check_level(CheckLevel l) {
284 IMP_CHECK_VARIABLE(l);
285#if IMP_HAS_CHECKS != IMP_NONE
286 check_level_ = l;
287#endif
288}
289
290inline LogLevel Object::get_log_level() const {
291#if IMP_HAS_LOG == IMP_SILENT
292 return SILENT;
293#else
294 return log_level_;
295#endif
296}
297inline CheckLevel Object::get_check_level() const {
298#if IMP_HAS_CHECKS == IMP_NONE
299 return NONE;
300#else
301 return check_level_;
302#endif
303}
304
305inline void Object::set_was_used(bool tf) const {
306 IMP_CHECK_VARIABLE(tf);
307#if IMP_HAS_CHECKS >= IMP_USAGE
308 was_owned_ = tf;
309#endif
310}
311
312inline bool Object::get_is_valid() const {
313#if IMP_HAS_CHECKS == IMP_NONE
314 return true;
315#else
316 return static_cast<int>(check_value_) == 111111111;
317#endif
318}
319#endif
320
321#if !defined(IMP_DOXYGEN) && !defined(SWIG)
322/** Send the whole show output to a stream*/
323class ShowFull {
324 std::string showed_;
325
326 public:
327 ShowFull(Object* o) {
328 std::ostringstream oss;
329 o->show(oss);
330 showed_ = oss.str();
331 }
332 const std::string& get_string() const { return showed_; }
333};
334inline std::ostream& operator<<(std::ostream& o, const ShowFull& sf) {
335 o << sf.get_string();
336 return o;
337}
338#endif
339IMPKERNEL_END_NAMESPACE
340
341#endif /* IMPKERNEL_OBJECT_H */
Base class for all objects that cannot be copied.
Version and authorship of IMP objects.
virtual VersionInfo get_version_info() const
Get information about the module and version of the object.
Definition Object.h:208
void set_log_level(LogLevel l)
Set the logging level used in this object.
Object(std::string name)
Construct an object with the given name.
void set_check_level(CheckLevel l)
virtual void do_destroy()
Definition Object.h:279
const std::string & get_name() const
Get the (not necessarily unique) name of this object.
Definition Object.h:220
void set_was_used(bool tf) const
virtual void clear_caches()
Definition Object.h:275
Version and module information for Objects.
Definition VersionInfo.h:29
VersionInfo(std::string module, std::string version)
Create a VersionInfo object with the given module and version.
Basic enumeration types used by IMP.
Exception definitions and assertions.
Helper functions for implementing hashes.
Helper macros for implementing hashable classes.
#define IMP_HASHABLE_INLINE(name, hashret)
Definition hash_macros.h:18
CheckLevel
Specify the level of runtime checks performed.
Definition enums.h:51
@ NONE
Definition enums.h:55
LogLevel
The log levels supported by IMP.
Definition enums.h:19
@ SILENT
Do not output any text.
Definition enums.h:24
Macros to help with reference counting.
Macros to help with objects that can be printed to a stream.
#define IMP_SHOWABLE(Name)
Various general useful macros for IMP.
Macros to control compiler warnings.