STK++ 0.9.13
STK::Proxy< Type > Class Template Reference

The Proxy class allow to surdefine operators and methods for every kind of class without using dynamic_cast. More...

#include <STK_Proxy.h>

Public Member Functions

template<typename U >
 Proxy (typename hidden::If< hidden::isSame< Type const, U >::value_, Type, U >::Result &x)
 constructor : create a reference of the Real x.
 
 Proxy (Type const &x)
 constructor : create a reference of the Real x.
 
 Proxy (Proxy const &p)
 copy constructor
 
Proxyoperator= (Proxy const &p)
 copy operator
 
 ~Proxy ()
 destructor.
 
 operator Type & ()
 Conversion operator to Type.
 
 operator Type const & () const
 Constant Conversion operator to Type.
 

Protected Attributes

Type & x_
 A reference on the object wrapped.
 
Type consty_
 A reference on the object wrapped.
 

Friends

istreamoperator>> (istream &is, Proxy< Type > p)
 Overloading of the operator >> for the type Type using a Proxy.
 
ostreamoperator<< (ostream &os, Proxy< Type > const &p)
 Overloading of the operator << for the type Type using a constant Proxy.
 

Detailed Description

template<class Type>
class STK::Proxy< Type >

The Proxy class allow to surdefine operators and methods for every kind of class without using dynamic_cast.

This class allow:

  • To avoid the use of dynamic_cast
  • To surdefine the predefined operators (like << and >>) for the C++ fundamental types.
  • To handle in a transparent way the NA values.

Definition at line 51 of file STK_Proxy.h.

Constructor & Destructor Documentation

◆ Proxy() [1/3]

template<class Type >
template<typename U >
STK::Proxy< Type >::Proxy ( typename hidden::If< hidden::isSame< Type const, U >::value_, Type, U >::Result &  x)
inline

constructor : create a reference of the Real x.

Avoid the case x is of type Type const and (thus ambiguity in the choice of the constructor) using sfinae.

Parameters
xthe object to wrap

Definition at line 64 of file STK_Proxy.h.

65 : x_(x), y_(x) {}
Type & x_
A reference on the object wrapped.
Definition STK_Proxy.h:54
Type const & y_
A reference on the object wrapped.
Definition STK_Proxy.h:55

◆ Proxy() [2/3]

template<class Type >
STK::Proxy< Type >::Proxy ( Type const x)
inline

constructor : create a reference of the Real x.

Parameters
xthe object to wrap

Definition at line 69 of file STK_Proxy.h.

69: x_(const_cast<Type&>(x)), y_(x) {}

◆ Proxy() [3/3]

template<class Type >
STK::Proxy< Type >::Proxy ( Proxy< Type > const p)
inline

copy constructor

Parameters
pthe object to copy

Definition at line 73 of file STK_Proxy.h.

73: x_(p.x_), y_(p.y_) {}

◆ ~Proxy()

template<class Type >
STK::Proxy< Type >::~Proxy ( )
inline

destructor.

Definition at line 79 of file STK_Proxy.h.

79{}

Member Function Documentation

◆ operator Type &()

template<class Type >
STK::Proxy< Type >::operator Type & ( )
inline

Conversion operator to Type.

Definition at line 81 of file STK_Proxy.h.

81{ return x_;}

References STK::Proxy< Type >::x_.

◆ operator Type const &()

template<class Type >
STK::Proxy< Type >::operator Type const & ( ) const
inline

Constant Conversion operator to Type.

Definition at line 83 of file STK_Proxy.h.

83{ return y_;}

References STK::Proxy< Type >::y_.

◆ operator=()

template<class Type >
Proxy & STK::Proxy< Type >::operator= ( Proxy< Type > const p)
inline

copy operator

Parameters
pthe proxy to copy

Definition at line 77 of file STK_Proxy.h.

77{ x_ = p.x_; return *this;}

References STK::Proxy< Type >::x_.

Friends And Related Symbol Documentation

◆ operator<<

template<class Type >
ostream & operator<< ( ostream os,
Proxy< Type > const p 
)
friend

Overloading of the operator << for the type Type using a constant Proxy.

All output stream should use a Proxy in a STK application. For the enumerated types, we have also to define the standard output.

Parameters
osthe output stream
pthe value to send to the stream

Definition at line 129 of file STK_Proxy.h.

130 { return Arithmetic<Type>::isNA(p.y_) ? (os << stringNa) : (os << p.y_);}
String stringNa
Representation of a Not Available value.
static bool isNA(Type const &x)

◆ operator>>

template<class Type >
istream & operator>> ( istream is,
Proxy< Type >  p 
)
friend

Overloading of the operator >> for the type Type using a Proxy.

All input stream should use a Proxy in a STK application. For the enumerated and String types, we have to overload the method. Due to the instruction

is >> buff
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...

this operator will only work for the fundamental C/C++ types. For the other types, the operator

friend istream & operator>>(istream &is, Proxy< Type > p)
Overloading of the operator >> for the type Type using a Proxy.
Definition STK_Proxy.h:100
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55

have to be implemented.

Parameters
isthe input stream
pthe value to get from the stream

Definition at line 100 of file STK_Proxy.h.

101 {
102 // get current file position
103 std::ios::pos_type pos = is.tellg();
104 // failed to read directly the Type value
105 if ( (is >> p.x_).fail() )
106 {
107 p.x_ = Arithmetic<Type>::NA();
108 // clear and reset position
109 is.clear(); is.seekg(pos);
110 // Try to read a NA value, in all case value is a NA object
111 Char* buffer = new Char[stringNaSize+1];
112
113 if (is.get(buffer,stringNaSize+1).fail())
114 { is.clear(); is.seekg(pos); is.setstate(std::ios::failbit);}
115 else if (!(stringNa.compare(buffer) == 0))
116 { is.clear(); is.seekg(pos); is.setstate(std::ios::failbit);}
117
118 delete[] buffer;
119 }
120 return is;
121 }
int stringNaSize
Size (in number of Char) of a Not Available value.
char Char
STK fundamental type of a Char.
static Type NA()
Adding a Non Available (NA) special number.

Member Data Documentation

◆ x_

template<class Type >
Type& STK::Proxy< Type >::x_
protected

◆ y_

template<class Type >
Type const& STK::Proxy< Type >::y_
protected

A reference on the object wrapped.

Definition at line 55 of file STK_Proxy.h.

Referenced by STK::Proxy< String >::operator String const &(), and STK::Proxy< Type >::operator Type const &().


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