IMP  2.2.1
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:

Public Member Functions

 ExampleObject (const Floats &data)
 
double get_data (unsigned int i) const
 
virtual std::string get_type_name () const
 
virtual ::IMP::base::VersionInfo get_version_info () const
 Get information about the module and version of the object.
 
- Public Member Functions inherited from IMP::base::Object
virtual void clear_caches ()
 
virtual void do_destroy ()
 
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::base::Object
 Object (std::string name)
 Construct an object with the given name. More...
 

Detailed Description

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-2014 IMP Inventors. All rights reserved.
*/
#ifndef IMPEXAMPLE_EXAMPLE_OBJECT_H
#define IMPEXAMPLE_EXAMPLE_OBJECT_H
#include <IMP/example/example_config.h>
#include <IMP/base/types.h>
#include <vector>
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 base::Object {
Floats data_;
public:
ExampleObject(const Floats &data);
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);
};
typedef base::Vector<base::Pointer<ExampleObject> > ExampleObjects;
typedef base::Vector<base::WeakPointer<ExampleObject> > ExampleObjectsTemp;
IMPEXAMPLE_END_NAMESPACE
#endif /* IMPEXAMPLE_EXAMPLE_OBJECT_H */
/**
* \file ExampleObject.cpp
* \brief An example reference counted object.
*
* Copyright 2007-2014 IMP Inventors. All rights reserved.
*
*/
IMPEXAMPLE_BEGIN_NAMESPACE
ExampleObject::ExampleObject(const Floats &data)
: base::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
base::Pointer<ExampleObject> rc_other = new ExampleObject(data);
// have two pointers point to the object
base::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 29 of file ExampleObject.h.


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