strong_stream_cast()

1 view
Skip to first unread message

Alex Vinokur

unread,
Feb 7, 2006, 7:04:51 AM2/7/06
to programming.lang.c++.sources
Here is definition of template function strong_stream_cast().

The main diffrence between stream_cast() and strong_stream_cast() can
be demonstrated
on the following example:

stream_cast<string, int> ("12z", int_value) return true (SUCCESS) and
produces int_value = 12;
strong_stream_cast<string, int> ("12z", int_value) return false
(FAILURE) and produces no int_value.


######### sscast.cpp #########

#include <cstring>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;


// ====== Regular stream_cast ======
// --------------------
// The function was introduced by Dietmar Khuel.
template<typename To, typename From>
bool stream_cast (const From& from, To& to)
{
stringstream stream;
stream << from;
return (stream >> to);
}
// =================================

// ====== Strong stream_cast ======
// -------------------------------------
template <class To, class From>
bool bijective_stream_cast (const From& from, To& to, From& from_back)

{
return (stream_cast (from, to) && stream_cast (to, from_back));
}


// -------------------------------------
template <class To, class From>
bool strong_stream_cast (const From& from, To& to, From& from_back)
{
return (bijective_stream_cast<To, From>(from, to, from_back) &&
(are_identical (from, from_back)));
}


// -------------------------------------
template <class To, class From>
bool strong_stream_cast (const From& from, To& to)
{
>From from_back;
return (strong_stream_cast<To, From>(from, to, from_back));
}
// =================================


// --------------------
#define COUT cout << "[" << __FILE__ << ", " << setw (3) << __LINE__ <<
"] "
#define SHOW_SUCCESS(f,t) COUT << "SUCCESS: " << #f << "(" << f << ")
to " << #t << "; " << #t << " = " << t << endl
#define SHOW_FAILURE(f,t) COUT << "FAILURE: " << #f << "(" << f << ")
to " << #t << endl


// --------------------
bool are_identical (const char * const inst1, const char * const inst2)
{ return (strcmp (inst1, inst2) == 0); }
bool are_identical (const int inst1, const int inst2) { return (inst1
== inst2); }
bool are_identical (const string& inst1, const string& inst2) { return
(inst1 == inst2); }

// --------------------
int main ()
{
#define BUFFER_SIZE 10

int int_value;
string str_value;
char ch_array[BUFFER_SIZE];
char* ch_ptr = new (nothrow) char [BUFFER_SIZE];
char* ch_ptr_back = new (nothrow) char [sizeof (ch_ptr)];

if (ch_ptr == 0)
{
COUT << "First 'new' failed" << endl;
return 1;
}

if (ch_ptr_back == 0)
{
COUT << "Second 'new' failed" << endl;
return 1;
}

// ------------------
cout << endl;
cout << "--- stream_cast ---" << endl;
// ------------------
int_value = 123;
if (stream_cast (int_value, str_value))
{
SHOW_SUCCESS(int_value, str_value);
}
else
{
SHOW_FAILURE(int_value, str_value);
}


str_value = "123";
if (stream_cast (str_value, int_value))
{
SHOW_SUCCESS(str_value, int_value);
}
else
{
SHOW_FAILURE(str_value, int_value);
}


str_value = "xyz";
if (stream_cast (str_value, int_value))
{
SHOW_SUCCESS(str_value, int_value);
}
else
{
SHOW_FAILURE(str_value, int_value);
}


str_value = "12z";
if (stream_cast (str_value, int_value))
{
SHOW_SUCCESS(str_value, int_value);
}
else
{
SHOW_FAILURE(str_value, int_value);
}


int_value = 123;
if (stream_cast (int_value, ch_array))
{
SHOW_SUCCESS(int_value, ch_array);
}
else
{
SHOW_FAILURE(int_value, ch_array);
}

strcpy (ch_array, "123");
if (stream_cast (ch_array, int_value))
{
SHOW_SUCCESS(ch_array, int_value);
}
else
{
SHOW_FAILURE(ch_array, int_value);
}


strcpy (ch_array, "xyz");
if (stream_cast (ch_array, int_value))
{
SHOW_SUCCESS(ch_array, int_value);
}
else
{
SHOW_FAILURE(ch_array, int_value);
}

strcpy (ch_array, "12z");
if (stream_cast (ch_array, int_value))
{
SHOW_SUCCESS(ch_array, int_value);
}
else
{
SHOW_FAILURE(ch_array, int_value);
}

int_value = 123;
if (stream_cast (int_value, ch_ptr))
{
SHOW_SUCCESS(int_value, ch_ptr);
}
else
{
SHOW_FAILURE(int_value, ch_ptr);
}

strcpy (ch_ptr, "123");
if (stream_cast (ch_ptr, int_value))
{
SHOW_SUCCESS(ch_ptr, int_value);
}
else
{
SHOW_FAILURE(ch_ptr, int_value);
}


strcpy (ch_ptr, "xyz");
if (stream_cast (ch_ptr, int_value))
{
SHOW_SUCCESS(ch_ptr, int_value);
}
else
{
SHOW_FAILURE(ch_ptr, int_value);
}

strcpy (ch_ptr, "12z");
if (stream_cast (ch_ptr, int_value))
{
SHOW_SUCCESS(ch_ptr, int_value);
}
else
{
SHOW_FAILURE(ch_ptr, int_value);
}


// ------------------
cout << endl;
cout << "--- strong_stream_cast ---" << endl;
// ------------------
int_value = 123;
if (strong_stream_cast (int_value, str_value))
{
SHOW_SUCCESS(int_value, str_value);
}
else
{
SHOW_FAILURE(int_value, str_value);
}

str_value = "123";
if (strong_stream_cast (str_value, int_value))
{
SHOW_SUCCESS(str_value, int_value);
}
else
{
SHOW_FAILURE(str_value, int_value);
}


str_value = "xyz";
if (strong_stream_cast (str_value, int_value))
{
SHOW_SUCCESS(str_value, int_value);
}
else
{
SHOW_FAILURE(str_value, int_value);
}


str_value = "12z";
if (strong_stream_cast (str_value, int_value))
{
SHOW_SUCCESS(str_value, int_value);
}
else
{
SHOW_FAILURE(str_value, int_value);
}

int_value = 123;
if (strong_stream_cast (int_value, ch_array))
{
SHOW_SUCCESS(int_value, ch_array);
}
else
{
SHOW_FAILURE(int_value, ch_array);
}

strcpy (ch_array, "123");
if (strong_stream_cast (ch_array, int_value))
{
SHOW_SUCCESS(ch_array, int_value);
}
else
{
SHOW_FAILURE(ch_array, int_value);
}


strcpy (ch_array, "xyz");
if (strong_stream_cast (ch_array, int_value))
{
SHOW_SUCCESS(ch_array, int_value);
}
else
{
SHOW_FAILURE(ch_array, int_value);
}

strcpy (ch_array, "12z");
if (strong_stream_cast (ch_array, int_value))
{
SHOW_SUCCESS(ch_array, int_value);
}
else
{
SHOW_FAILURE(ch_array, int_value);
}


int_value = 123;
if (strong_stream_cast (int_value, ch_ptr))
{
SHOW_SUCCESS(int_value, ch_ptr);
}
else
{
SHOW_FAILURE(int_value, ch_ptr);
}

strcpy (ch_ptr, "123");
if (strong_stream_cast (ch_ptr, int_value, ch_ptr_back))
{
SHOW_SUCCESS(ch_ptr, int_value);
}
else
{
SHOW_FAILURE(ch_ptr, int_value);
}


strcpy (ch_ptr, "xyz");
if (strong_stream_cast (ch_ptr, int_value, ch_ptr_back))
{
SHOW_SUCCESS(ch_ptr, int_value);
}
else
{
SHOW_FAILURE(ch_ptr, int_value);
}

strcpy (ch_ptr, "12z");
if (strong_stream_cast (ch_ptr, int_value, ch_ptr_back))
{
SHOW_SUCCESS(ch_ptr, int_value);
}
else
{
SHOW_FAILURE(ch_ptr, int_value);
}

return 0;
}


##############################

######### Test Results #########

--- stream_cast ---
[sscast.cpp, 91] SUCCESS: int_value(123) to str_value; str_value = 123
[sscast.cpp, 102] SUCCESS: str_value(123) to int_value; int_value = 123
[sscast.cpp, 117] FAILURE: str_value(xyz) to int_value
[sscast.cpp, 124] SUCCESS: str_value(12z) to int_value; int_value = 12
[sscast.cpp, 135] SUCCESS: int_value(123) to ch_array; ch_array = 123
[sscast.cpp, 145] SUCCESS: ch_array(123) to int_value; int_value = 123
[sscast.cpp, 160] FAILURE: ch_array(xyz) to int_value
[sscast.cpp, 166] SUCCESS: ch_array(12z) to int_value; int_value = 12
[sscast.cpp, 176] SUCCESS: int_value(123) to ch_ptr; ch_ptr = 123
[sscast.cpp, 186] SUCCESS: ch_ptr(123) to int_value; int_value = 123
[sscast.cpp, 201] FAILURE: ch_ptr(xyz) to int_value
[sscast.cpp, 207] SUCCESS: ch_ptr(12z) to int_value; int_value = 12

--- strong_stream_cast ---
[sscast.cpp, 222] SUCCESS: int_value(123) to str_value; str_value = 123
[sscast.cpp, 232] SUCCESS: str_value(123) to int_value; int_value = 123
[sscast.cpp, 247] FAILURE: str_value(xyz) to int_value
[sscast.cpp, 258] FAILURE: str_value(12z) to int_value
[sscast.cpp, 264] SUCCESS: int_value(123) to ch_array; ch_array = 123
[sscast.cpp, 274] SUCCESS: ch_array(123) to int_value; int_value = 123
[sscast.cpp, 289] FAILURE: ch_array(xyz) to int_value
[sscast.cpp, 299] FAILURE: ch_array(12z) to int_value
[sscast.cpp, 306] SUCCESS: int_value(123) to ch_ptr; ch_ptr = 123
[sscast.cpp, 316] SUCCESS: ch_ptr(123) to int_value; int_value = 123
[sscast.cpp, 331] FAILURE: ch_ptr(xyz) to int_value
[sscast.cpp, 341] FAILURE: ch_ptr(12z) to int_value

################################


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Reply all
Reply to author
Forward
0 new messages