IMP logo
IMP Reference Guide  2.22.0
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-2022 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 */
/**
* \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
Pointer<ExampleObject> rc2 = rc;
// 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

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. More...
 
- Public Member Functions inherited from IMP::Object
virtual void clear_caches ()
 
CheckLevel get_check_level () const
 
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. More...
 
void set_was_used (bool tf) const
 
void show (std::ostream &out=std::cout) const
 
const std::string & get_name () const
 
void set_name (std::string name)
 

Additional Inherited Members

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

Member Function Documentation

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: