IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
IMP::example::ExampleObject Class Reference

An example simple object which is reference counted. More...

#include <IMP/example/ExampleObject.h>

Inheritance diagram for IMP::example::ExampleObject:

Detailed Description

An example simple object which is reference counted.

Only IMP::Pointer objects should be used to store pointers to instances of these objects.

The source code is as follows:

/**
* \file IMP/example/ExampleObject.h
* \brief An example showing how to make a simple ref counted object
*
* Copyright 2007-2026 IMP Inventors. All rights reserved.
*/
#ifndef IMPEXAMPLE_EXAMPLE_OBJECT_H
#define IMPEXAMPLE_EXAMPLE_OBJECT_H
#include <IMP/example/example_config.h>
#include <IMP/Object.h>
#include <IMP/types.h>
#include <IMP/Pointer.h>
#include <vector>
#include <cereal/access.hpp>
#include <cereal/types/vector.hpp>
IMPEXAMPLE_BEGIN_NAMESPACE
//! An example simple object which is reference counted.
/** Only IMP::Pointer objects should be used to store pointers to
instances of these objects.
The source code is as follows:
\include ExampleObject.h
\include ExampleObject.cpp
*/
class IMPEXAMPLEEXPORT ExampleObject : public Object {
Floats data_;
public:
ExampleObject(const Floats &data);
// Default constructor, needed for serialization or Python pickle support
ExampleObject() : Object("") {}
double get_data(unsigned int i) const {
IMP_USAGE_CHECK(i < data_.size(), "Index " << i << " out of range.");
return data_[i];
}
/* Make sure that it can't be allocated on the stack
The macro defines an empty destructor. In general,
you want destructors to be empty since they are hard
to maintain.
*/
IMP_OBJECT_METHODS(ExampleObject);
private:
// Serialization support
friend class cereal::access;
template<class Archive> void serialize(Archive &ar) {
// We must save/load everything in the Object base class
// (e.g. name) plus our own variables
ar(cereal::base_class<Object>(this), data_);
}
};
typedef Vector<Pointer<ExampleObject>> ExampleObjects;
typedef Vector<WeakPointer<ExampleObject>> ExampleObjectsTemp;
IMPEXAMPLE_END_NAMESPACE
#endif /* IMPEXAMPLE_EXAMPLE_OBJECT_H */
A shared base class to help in debugging and things.
A nullptr-initialized pointer to an IMP Object.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Object(std::string name)
Construct an object with the given name.
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
Definition types.h:46
Helper macros for implementing IMP Objects.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Basic types used by IMP.
/**
* \file ExampleObject.cpp
* \brief An example reference counted object.
*
* Copyright 2007-2022 IMP Inventors. All rights reserved.
*
*/
#include "IMP/Pointer.h"
IMPEXAMPLE_BEGIN_NAMESPACE
ExampleObject::ExampleObject(const Floats &data)
: Object("ExampleObject%1%"), data_(data) {}
namespace {
#ifdef __clang__
IMP_CLANG_PRAGMA(diagnostic ignored "-Wunused-function")
#endif
/** An example of how to return a new object. */
ExampleObject *create_example_object(const Floats &data) {
IMP_NEW(ExampleObject, ret, (data));
// one could do some work here
// make sure it is not freed
return ret.release();
}
void usage_example() {
Floats data(1000, -1);
// this would not compile
// ExampleObject rcstack;
// create a new object and store it in a ref counted pointer
IMP_NEW(ExampleObject, rc, (data));
// reference count is 1
// another object with another copy of the data
Pointer<ExampleObject> rc_other = new ExampleObject(data);
// have two pointers point to the object
// reference count is 2
// the object is still around since rc2 points to it
rc = static_cast<ExampleObject *>(nullptr);
// reference count is 1
std::cout << rc2->get_data(100);
// the object will be deleted on exit
}
}
IMPEXAMPLE_END_NAMESPACE
An example showing how to make a simple ref counted object.
An example simple object which is reference counted.
#define IMP_NEW(Typename, varname, args)
Declare a ref counted pointer to a new object.
A smart pointer to a reference counted object.
Definition Pointer.h:88

Definition at line 31 of file ExampleObject.h.

Public Member Functions

 ExampleObject (const Floats &data)
double get_data (unsigned int i) const
virtual std::string get_type_name () const override
virtual ::IMP::VersionInfo get_version_info () const override
 Get information about the module and version of the object.
Public Member Functions inherited from IMP::Object
CheckLevel get_check_level () const
 get_derived_object (self)
 Return this object cast to the most derived subclass.
LogLevel get_log_level () const
void set_check_level (CheckLevel l)
void set_log_level (LogLevel l)
 Set the logging level used in this object.
const std::string & get_name () const
 Get the (not necessarily unique) name of this object.
void set_name (std::string name)
void set_was_used (bool tf) const
void show (std::ostream &out=std::cout) const
virtual void clear_caches ()

Additional Inherited Members

Protected Member Functions inherited from IMP::Object
 Object (std::string name)
 Construct an object with the given name.
virtual void do_destroy ()

Constructor & Destructor Documentation

◆ ExampleObject()

IMP::example::ExampleObject::ExampleObject ( )

Definition at line 38 of file ExampleObject.h.

◆ ~ExampleObject()

virtual IMP::example::ExampleObject::~ExampleObject ( )
virtual

Definition at line 50 of file ExampleObject.h.

Member Function Documentation

◆ get_data()

double IMP::example::ExampleObject::get_data ( unsigned int i) const

Definition at line 40 of file ExampleObject.h.

◆ get_type_name()

virtual std::string IMP::example::ExampleObject::get_type_name ( ) const
overridevirtual

Reimplemented from IMP::Object.

Definition at line 50 of file ExampleObject.h.

◆ get_version_info()

virtual ::IMP::VersionInfo IMP::example::ExampleObject::get_version_info ( ) const
overridevirtual

Get information about the module and version of the object.

Reimplemented from IMP::Object.

Definition at line 50 of file ExampleObject.h.


The documentation for this class was generated from the following file: