Jpp
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Private Member Functions | List of all members
JTOOLS::JAutoPointer< JDerived_t, JBase_t > Class Template Reference

Template class for loading and storing of objects. More...

#include <JSelector.hh>

Inheritance diagram for JTOOLS::JAutoPointer< JDerived_t, JBase_t >:
JTOOLS::JAbstractAutoPointer< JBase_t > JTOOLS::JDriver< JDerived_t > JLANG::JSharedPointer< JClass_t, JMemory_t > JLANG::JStreamInput JLANG::JStreamOutput JLANG::JSharedCounter JLANG::JStorage< JClass_t, JMemory_t > JLANG::JPointer< JClass_t > JLANG::JAbstractPointer< JClass_t > JLANG::JEquals< JAbstractPointer< JClass_t > >

Public Types

typedef JPointer< JClass_t > pointer_type
 
typedef JStorage< JClass_t, JMemory_t > storage_type
 
typedef JMemory_t< JClass_t > memory_type
 

Public Member Functions

 JAutoPointer ()
 Default constructor. More...
 
 JAutoPointer (JDerived_t *p)
 Constructor. More...
 
virtual std::istream & read (std::istream &in)
 Stream input. More...
 
virtual std::ostream & write (std::ostream &out) const
 Stream output. More...
 
const JSharedPointergetSharedPointer () const
 Get shared pointer. More...
 
JSharedPointergetSharedPointer ()
 Get shared pointer. More...
 
void setSharedPointer (const JSharedPointer &object)
 Set shared pointer. More...
 
virtual void reset ()
 Reset pointer. More...
 
virtual void reset ()
 Reset pointer. More...
 
template<class T >
void reset (const JPointer< T > &pointer)
 Reset pointer. More...
 
template<class T >
void reset (const JPointer< T > &pointer)
 Reset pointer. More...
 
void reset (JClass_t *p)
 Reset pointer. More...
 
template<class T >
void set (const JPointer< T > &pointer)
 Set pointer. More...
 
void initialise ()
 Initialise counter. More...
 
void attach (const JSharedCounter &object)
 Attach this counter to given shared counter object. More...
 
bool detach ()
 Detach. More...
 
void recreate ()
 Recreate object in memory. More...
 
void create ()
 Create object in memory. More...
 
void create (const unsigned int size)
 Create array of objects in memory. More...
 
virtual JClass_t * get () const
 Get pointer. More...
 
JClass_t *const & getReference () const
 Get rereference to internal pointer. More...
 
JClass_t *& getReference ()
 Get rereference to internal pointer. More...
 
virtual bool equals (const JAbstractPointer &object) const
 Equals. More...
 
bool is_valid () const
 Check validity of pointer. More...
 
JClass_t * operator-> () const
 Smart pointer operator. More...
 
 operator JClass_t * () const
 Type conversion operator. More...
 

Protected Member Functions

void set (const JSharedPointer &object)
 Set pointer. More...
 
virtual void set (JClass_t *p)
 Set pointer. More...
 
void release ()
 Release memory. More...
 

Protected Attributes

int * counter
 
JClass_t * __p
 pointer to object More...
 

Static Private Member Functions

static JDerived_t * load (JLANG::JBool< false > option)
 Load object without input. More...
 
static JDerived_t * load (std::istream &in, JLANG::JBool< false > option)
 Load object without input. More...
 
static JDerived_t * load (std::istream &in, JLANG::JBool< true > option)
 Load object with input. More...
 
static JDerived_t * load ()
 Load object. More...
 
static JDerived_t * load (std::istream &in)
 Load object. More...
 
static void store (std::ostream &out, const JDerived_t *p, JBool< false > option)
 Store object without output. More...
 
static void store (std::ostream &out, const JDerived_t *p, JBool< true > option)
 Store object with output. More...
 
static void store (std::ostream &out, const T *p)
 Store object. More...
 

Detailed Description

template<class JDerived_t, class JBase_t>
class JTOOLS::JAutoPointer< JDerived_t, JBase_t >

Template class for loading and storing of objects.

This class implements the JAbstractAutoPointer interface.

Definition at line 61 of file JTools/JSelector.hh.

Member Typedef Documentation

◆ pointer_type

template<class JClass_t, template< class > class JMemory_t = JNew>
typedef JPointer<JClass_t> JLANG::JSharedPointer< JClass_t, JMemory_t >::pointer_type
inherited

Definition at line 34 of file JSharedPointer.hh.

◆ storage_type

template<class JClass_t, template< class > class JMemory_t = JNew>
typedef JStorage<JClass_t, JMemory_t> JLANG::JSharedPointer< JClass_t, JMemory_t >::storage_type
inherited

Definition at line 35 of file JSharedPointer.hh.

◆ memory_type

template<class JClass_t, template< class > class JMemory_t = JNew>
typedef JMemory_t<JClass_t> JLANG::JStorage< JClass_t, JMemory_t >::memory_type
inherited

Definition at line 33 of file JStorage.hh.

Constructor & Destructor Documentation

◆ JAutoPointer() [1/2]

template<class JDerived_t , class JBase_t >
JTOOLS::JAutoPointer< JDerived_t, JBase_t >::JAutoPointer ( )
inline

Default constructor.

Definition at line 69 of file JTools/JSelector.hh.

69  :
71  {}

◆ JAutoPointer() [2/2]

template<class JDerived_t , class JBase_t >
JTOOLS::JAutoPointer< JDerived_t, JBase_t >::JAutoPointer ( JDerived_t *  p)
inline

Constructor.

This class owns the pointer.

Parameters
ppointer to object

Definition at line 80 of file JTools/JSelector.hh.

80  :
82  {
83  this->reset(p);
84  }

Member Function Documentation

◆ read()

template<class JDerived_t , class JBase_t >
virtual std::istream& JTOOLS::JAutoPointer< JDerived_t, JBase_t >::read ( std::istream &  in)
inlinevirtual

Stream input.

This method creates a new object and read the corresponding input data if the redirect operator >>(isteam&) is defined for this data type.

Parameters
ininput stream
Returns
input stream

Implements JLANG::JStreamInput.

Definition at line 95 of file JTools/JSelector.hh.

96  {
97  JDerived_t* p = this->load(in);
98 
99  if (p != NULL) {
100  this->reset(p);
101  }
102 
103  return in;
104  }

◆ write()

template<class JDerived_t , class JBase_t >
virtual std::ostream& JTOOLS::JAutoPointer< JDerived_t, JBase_t >::write ( std::ostream &  out) const
inlinevirtual

Stream output.

This method writes the corresponding data if the redirect operator <<(osteam&) is defined for this data type.

Parameters
outoutput stream
Returns
output stream

Implements JLANG::JStreamOutput.

Definition at line 115 of file JTools/JSelector.hh.

116  {
117  this->store(out, this->get());
118 
119  return out;
120  }

◆ getSharedPointer() [1/2]

template<class JClass_t, template< class > class JMemory_t = JNew>
const JSharedPointer& JLANG::JSharedPointer< JClass_t, JMemory_t >::getSharedPointer ( ) const
inlineinherited

Get shared pointer.

Returns
this shared pointer

Definition at line 95 of file JSharedPointer.hh.

96  {
97  return static_cast<const JSharedPointer&>(*this);
98  }

◆ getSharedPointer() [2/2]

template<class JClass_t, template< class > class JMemory_t = JNew>
JSharedPointer& JLANG::JSharedPointer< JClass_t, JMemory_t >::getSharedPointer ( )
inlineinherited

Get shared pointer.

Returns
this shared pointer

Definition at line 106 of file JSharedPointer.hh.

107  {
108  return static_cast<JSharedPointer&>(*this);
109  }

◆ setSharedPointer()

template<class JClass_t, template< class > class JMemory_t = JNew>
void JLANG::JSharedPointer< JClass_t, JMemory_t >::setSharedPointer ( const JSharedPointer< JClass_t, JMemory_t > &  object)
inlineinherited

Set shared pointer.

Parameters
objectshared pointer

Definition at line 117 of file JSharedPointer.hh.

118  {
119  if (this->get() != object.get()) {
120 
121  this->reset();
122 
123  if (object.is_valid()) {
124  this->set(object);
125  }
126  }
127  }

◆ reset() [1/5]

template<class JClass_t, template< class > class JMemory_t = JNew>
virtual void JLANG::JSharedPointer< JClass_t, JMemory_t >::reset ( )
inlinevirtualinherited

Reset pointer.

The reference counter is decremented by one and the object pointed to previously is deleted when its reference counter is zero.

Reimplemented from JLANG::JStorage< JClass_t, JMemory_t >.

Definition at line 171 of file JSharedPointer.hh.

172  {
173  if (this->detach()) {
175  }
176 
178  }

◆ reset() [2/5]

template<class JClass_t, template< class > class JMemory_t = JNew>
virtual void JLANG::JPointer< JClass_t >::reset
inlineinherited

Reset pointer.

Definition at line 84 of file JPointer.hh.

85  {
86  this->__p = NULL;
87  }

◆ reset() [3/5]

template<class JClass_t, template< class > class JMemory_t = JNew>
template<class T >
void JLANG::JPointer< JClass_t >::reset ( class T  )
inlineinherited

Reset pointer.

Parameters
pointerpointer to object

Definition at line 108 of file JPointer.hh.

109  {
110  this->reset(pointer.get());
111  }

◆ reset() [4/5]

template<class JClass_t>
template<class T >
void JLANG::JPointer< JClass_t >::reset ( const JPointer< T > &  pointer)
inlineinherited

Reset pointer.

Parameters
pointerpointer to object

Definition at line 108 of file JPointer.hh.

109  {
110  this->reset(pointer.get());
111  }

◆ reset() [5/5]

template<class JClass_t>
void JLANG::JAbstractPointer< JClass_t >::reset ( JClass_t *  p)
inlineinherited

Reset pointer.

Parameters
ppointer to object

Definition at line 94 of file JAbstractPointer.hh.

95  {
96  if (this->get() != p) {
97 
98  this->reset();
99 
100  if (p != NULL) {
101  this->set(p);
102  }
103  }
104  }

◆ set() [1/3]

template<class JClass_t, template< class > class JMemory_t = JNew>
void JLANG::JSharedPointer< JClass_t, JMemory_t >::set ( const JSharedPointer< JClass_t, JMemory_t > &  object)
inlineprotectedinherited

Set pointer.

The reference counter of the shared object pointed to is incremented by one.

Parameters
objectshared pointer

Definition at line 188 of file JSharedPointer.hh.

189  {
190  pointer_type::set(object.get());
191 
192  this->attach(object);
193  }

◆ set() [2/3]

template<class JClass_t, template< class > class JMemory_t = JNew>
virtual void JLANG::JSharedPointer< JClass_t, JMemory_t >::set ( JClass_t *  p)
inlineprotectedvirtualinherited

Set pointer.

The reference counter of the shared object pointed to is initialised to one.

Parameters
ppointer to derived class object

Reimplemented from JLANG::JPointer< JClass_t >.

Definition at line 202 of file JSharedPointer.hh.

203  {
205 
206  this->initialise();
207  }

◆ set() [3/3]

template<class JClass_t>
template<class T >
void JLANG::JPointer< JClass_t >::set ( const JPointer< T > &  pointer)
inlineinherited

Set pointer.

Parameters
pointerpointer to object

Definition at line 96 of file JPointer.hh.

97  {
98  this->set(pointer.get());
99  }

◆ initialise()

void JLANG::JSharedCounter::initialise ( )
inlineinherited

Initialise counter.

Definition at line 33 of file JSharedCounter.hh.

34  {
35  detach();
36 
37  counter = new int(1);
38  }

◆ attach()

void JLANG::JSharedCounter::attach ( const JSharedCounter object)
inlineinherited

Attach this counter to given shared counter object.

Parameters
objectshared counter

Definition at line 46 of file JSharedCounter.hh.

47  {
48  detach();
49 
50  counter = object.counter;
51 
52  if (counter != NULL) {
53  ++(*counter);
54  }
55  }

◆ detach()

bool JLANG::JSharedCounter::detach ( )
inlineinherited

Detach.

Returns
true if counter at zero; else false

Definition at line 63 of file JSharedCounter.hh.

64  {
65  if (counter != NULL) {
66 
67  if (--(*counter) == 0) {
68 
69  delete counter;
70 
71  counter = NULL;
72 
73  return true;
74  }
75 
76  counter = NULL;
77  }
78 
79  return false;
80  }

◆ recreate()

template<class JClass_t, template< class > class JMemory_t = JNew>
void JLANG::JStorage< JClass_t, JMemory_t >::recreate ( )
inlineinherited

Recreate object in memory.

A new object is created if no memory is allocated yet, else the previously created object is maintained.

Definition at line 57 of file JStorage.hh.

58  {
59  if (!this->is_valid()) {
60  this->set(memory_type::create());
61  }
62  }

◆ create() [1/2]

template<class JClass_t, template< class > class JMemory_t = JNew>
void JLANG::JStorage< JClass_t, JMemory_t >::create ( )
inlineinherited

Create object in memory.

The memory allocated by a previously created object will be released.

Definition at line 69 of file JStorage.hh.

70  {
71  this->reset(memory_type::create());
72  }

◆ create() [2/2]

template<class JClass_t, template< class > class JMemory_t = JNew>
void JLANG::JStorage< JClass_t, JMemory_t >::create ( const unsigned int  size)
inlineinherited

Create array of objects in memory.

The memory allocated by previously created objects will be released.

Parameters
sizenumber of elements

Definition at line 81 of file JStorage.hh.

82  {
83  this->reset(memory_type::create(size));
84  }

◆ release()

template<class JClass_t, template< class > class JMemory_t = JNew>
void JLANG::JStorage< JClass_t, JMemory_t >::release ( )
inlineprotectedinherited

Release memory.

Definition at line 91 of file JStorage.hh.

92  {
93  memory_type::release(this->get());
94  }

◆ get()

template<class JClass_t>
virtual JClass_t* JLANG::JPointer< JClass_t >::get ( ) const
inlinevirtualinherited

◆ getReference() [1/2]

template<class JClass_t>
JClass_t* const& JLANG::JPointer< JClass_t >::getReference ( ) const
inlineinherited

Get rereference to internal pointer.

Returns
reference to internal pointer

Definition at line 119 of file JPointer.hh.

120  {
121  return __p;
122  }

◆ getReference() [2/2]

template<class JClass_t>
JClass_t* & JLANG::JPointer< JClass_t >::getReference ( )
inlineinherited

Get rereference to internal pointer.

Returns
reference to internal pointer

Definition at line 130 of file JPointer.hh.

131  {
132  return __p;
133  }

◆ equals()

template<class JClass_t>
virtual bool JLANG::JAbstractPointer< JClass_t >::equals ( const JAbstractPointer< JClass_t > &  object) const
inlinevirtualinherited

Equals.

The equality is evaluated by comparison of the internal pointers.

Parameters
objectabstract pointer
Returns
true if equals; else false

Definition at line 50 of file JAbstractPointer.hh.

51  {
52  return this->get() == object.get();
53  }

◆ is_valid()

template<class JClass_t>
bool JLANG::JAbstractPointer< JClass_t >::is_valid ( ) const
inlineinherited

Check validity of pointer.

Returns
true if pointer not null; else false

Definition at line 83 of file JAbstractPointer.hh.

84  {
85  return this->get() != NULL;
86  }

◆ operator->()

template<class JClass_t>
JClass_t* JLANG::JAbstractPointer< JClass_t >::operator-> ( ) const
inlineinherited

Smart pointer operator.

Returns
pointer to object

Definition at line 112 of file JAbstractPointer.hh.

113  {
114  if (!is_valid())
115  throw JNullPointerException("JAbstractPointer::operator->()");
116  else
117  return this->get();
118  }

◆ operator JClass_t *()

template<class JClass_t>
JLANG::JAbstractPointer< JClass_t >::operator JClass_t * ( ) const
inlineinherited

Type conversion operator.

Returns
pointer to object

Definition at line 126 of file JAbstractPointer.hh.

127  {
128  return this->get();
129  }

◆ load() [1/5]

static JDerived_t * JTOOLS::JDriver< JDerived_t >::load ( JLANG::JBool< false >  option)
inlinestaticprivateinherited

Load object without input.

Parameters
optionI/O option
Returns
pointer to newly loaded object

Definition at line 35 of file JDriver.hh.

36  {
37  return new JClass_t();
38  }

◆ load() [2/5]

static JDerived_t * JTOOLS::JDriver< JDerived_t >::load ( std::istream &  in,
JLANG::JBool< false >  option 
)
inlinestaticprivateinherited

Load object without input.

Parameters
ininput stream
optionI/O option
Returns
pointer to newly loaded object

Definition at line 48 of file JDriver.hh.

49  {
50  return load(option);
51  }

◆ load() [3/5]

static JDerived_t * JTOOLS::JDriver< JDerived_t >::load ( std::istream &  in,
JLANG::JBool< true >  option 
)
inlinestaticprivateinherited

Load object with input.

Parameters
ininput stream
optionI/O option
Returns
pointer to newly loaded object

Definition at line 61 of file JDriver.hh.

62  {
63  JClass_t* p = load(JLANG::JBool<false>());
64 
65  in >> *p;
66 
67  if (in.fail()) {
68 
69  delete p;
70 
71  p = NULL;
72  }
73 
74  return p;
75  }

◆ load() [4/5]

static JDerived_t * JTOOLS::JDriver< JDerived_t >::load ( )
inlinestaticinherited

Load object.

Returns
pointer to newly loaded object

Definition at line 111 of file JDriver.hh.

112  {
114  }

◆ load() [5/5]

static JDerived_t * JTOOLS::JDriver< JDerived_t >::load ( std::istream &  in)
inlinestaticinherited

Load object.

Parameters
ininput stream
Returns
pointer to newly loaded object

Definition at line 123 of file JDriver.hh.

124  {
125  return load(in, JBool<JStreamAvailable<JClass_t>::has_istream>());
126  }

◆ store() [1/3]

static void JTOOLS::JDriver< JDerived_t >::store ( std::ostream &  out,
const JDerived_t *  p,
JBool< false >  option 
)
inlinestaticprivateinherited

Store object without output.

This implementation doesn't do anything.

Parameters
outoutput stream
optionI/O option
ppointer to object

Definition at line 86 of file JDriver.hh.

87  {}

◆ store() [2/3]

static void JTOOLS::JDriver< JDerived_t >::store ( std::ostream &  out,
const JDerived_t *  p,
JBool< true >  option 
)
inlinestaticprivateinherited

Store object with output.

Parameters
outoutput stream
optionI/O option
ppointer object

Definition at line 97 of file JDriver.hh.

98  {
99  if (p != NULL) {
100  out << *p;
101  }
102  }

◆ store() [3/3]

static void JTOOLS::JDriver< JDerived_t >::store ( std::ostream &  out,
const T *  p 
)
inlinestaticinherited

Store object.

Parameters
outoutput stream
ppointer to object

Definition at line 136 of file JDriver.hh.

137  {
138  store(out, dynamic_cast<const JClass_t*>(p), JLANG::JBool<JStreamAvailable<JClass_t>::has_ostream>());
139  }

Member Data Documentation

◆ counter

int* JLANG::JSharedCounter::counter
protectedinherited

Definition at line 83 of file JSharedCounter.hh.

◆ __p

template<class JClass_t>
JClass_t* JLANG::JPointer< JClass_t >::__p
protectedinherited

pointer to object

Definition at line 136 of file JPointer.hh.


The documentation for this class was generated from the following file:
JLANG::JSharedCounter::detach
bool detach()
Detach.
Definition: JSharedCounter.hh:63
JLANG::JPointer::set
virtual void set(JClass_t *p)
Set pointer.
Definition: JPointer.hh:75
JLANG::JPointer::get
virtual JClass_t * get() const
Get pointer.
Definition: JPointer.hh:64
JLANG::JPointer::__p
JClass_t * __p
pointer to object
Definition: JPointer.hh:136
JLANG::JSharedPointer::set
void set(const JSharedPointer &object)
Set pointer.
Definition: JSharedPointer.hh:188
JLANG::JSharedCounter::counter
int * counter
Definition: JSharedCounter.hh:83
JTOOLS::JDriver< JDerived_t >::load
static JDerived_t * load()
Load object.
Definition: JDriver.hh:111
JLANG::JNullPointerException
Exception for null pointer operation.
Definition: JException.hh:216
JLANG::JAbstractPointer::reset
virtual void reset()=0
Reset pointer.
JLANG::JSharedCounter::attach
void attach(const JSharedCounter &object)
Attach this counter to given shared counter object.
Definition: JSharedCounter.hh:46
JLANG::JPointer::reset
virtual void reset()
Reset pointer.
Definition: JPointer.hh:84
JStreamAvailable
Test availability of stream operators.
Definition: JStreamAvailable.hh:46
JLANG::JSharedCounter::initialise
void initialise()
Initialise counter.
Definition: JSharedCounter.hh:33
JLANG::JBool
Auxiliary template class for type bool.
Definition: JBool.hh:20
JTOOLS::JAbstractAutoPointer
Abstract class of an automatic pointer.
Definition: JTools/JSelector.hh:39
JLANG::JAbstractPointer::get
virtual JClass_t * get() const =0
Get pointer.
JLANG::JSharedPointer::reset
virtual void reset()
Reset pointer.
Definition: JSharedPointer.hh:171
JLANG::JStorage::reset
virtual void reset()
Reset pointer.
Definition: JStorage.hh:42
JTOOLS::JDriver< JDerived_t >::store
static void store(std::ostream &out, const JDerived_t *p, JBool< false > option)
Store object without output.
Definition: JDriver.hh:86
JLANG::JAbstractPointer::is_valid
bool is_valid() const
Check validity of pointer.
Definition: JAbstractPointer.hh:83
JLANG::JAbstractPointer::set
virtual void set(JClass_t *p)=0
Set pointer.