STK++ 0.9.13
STK_Proxy.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::STKernel::Base
27 * Purpose: Define the Proxy classes for wrapping any types.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_PROXY_H
37#define STK_PROXY_H
38
39namespace STK
40{
50 template<class Type>
51 class Proxy
52 {
53 protected:
54 Type& x_;
55 Type const& y_;
56
57 public:
63 template< typename U >
65 : x_(x), y_(x) {}
69 inline Proxy(Type const& x) : x_(const_cast<Type&>(x)), y_(x) {}
73 inline Proxy(Proxy const& p) : x_(p.x_), y_(p.y_) {}
77 Proxy& operator=(Proxy const& p) { x_ = p.x_; return *this;}
79 inline ~Proxy() {}
81 inline operator Type &() { return x_;}
83 inline operator Type const &() const { return y_;}
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 }
129 inline friend ostream& operator << (ostream& os, Proxy<Type> const& p)
130 { return Arithmetic<Type>::isNA(p.y_) ? (os << stringNa) : (os << p.y_);}
131};
132
136 template<>
138 {
139 protected:
141 String const& y_;
142
143 public:
147 inline Proxy(String &x) : x_(x), y_(x) {}
151 inline Proxy(String const& x) : x_(const_cast<String&>(x)), y_(x) {}
155 inline Proxy(Proxy const& p) : x_(p.x_), y_(p.y_) {}
159 Proxy& operator=(Proxy const& p) { x_ = p.x_; return *this;}
161 inline ~Proxy() {}
163 inline operator String &() { return x_;}
165 inline operator String const &() const { return y_;}
172 {
173 String buff;
174 is >> std::skipws >> buff;
176 return is;
177 }
183 inline friend ostream& operator << (ostream& os, Proxy<String> const& p)
184 { return Arithmetic<String>::isNA(p.x_) ? (os << stringNa) : (os << p.y_);}
185 };
186
187} // namespace STK
188
189#endif /*STK_PROXY_H*/
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
String & x_
A reference on the String wrapped.
Definition STK_Proxy.h:140
Proxy(String &x)
constructor : create a reference of the String x.
Definition STK_Proxy.h:147
Proxy & operator=(Proxy const &p)
copy operator
Definition STK_Proxy.h:159
~Proxy()
destructor.
Definition STK_Proxy.h:161
String const & y_
A constant reference on the String wrapped.
Definition STK_Proxy.h:141
Proxy(String const &x)
constructor : create a constant reference of the String x.
Definition STK_Proxy.h:151
Proxy(Proxy const &p)
copy constructor
Definition STK_Proxy.h:155
The Proxy class allow to surdefine operators and methods for every kind of class without using dynami...
Definition STK_Proxy.h:52
friend ostream & operator<<(ostream &os, Proxy< Type > const &p)
Overloading of the operator << for the type Type using a constant Proxy.
Definition STK_Proxy.h:129
~Proxy()
destructor.
Definition STK_Proxy.h:79
Proxy(typename hidden::If< hidden::isSame< Type const, U >::value_, Type, U >::Result &x)
constructor : create a reference of the Real x.
Definition STK_Proxy.h:64
friend istream & operator>>(istream &is, Proxy< Type > p)
Overloading of the operator >> for the type Type using a Proxy.
Definition STK_Proxy.h:100
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(Proxy const &p)
copy constructor
Definition STK_Proxy.h:73
Proxy & operator=(Proxy const &p)
copy operator
Definition STK_Proxy.h:77
Proxy(Type const &x)
constructor : create a reference of the Real x.
Definition STK_Proxy.h:69
int stringNaSize
Size (in number of Char) of a Not Available value.
String stringNa
Representation of a Not Available value.
char Char
STK fundamental type of a Char.
std::basic_string< Char > String
STK fundamental type of a String.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)
check if T and U are of the same type.