casa
$Rev:20696$
|
00001 //# IO.h: IO utilities for dbus values 00002 //# Copyright (C) 2013 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id$ 00027 00028 #ifndef CASADBUS_IO_H_ 00029 #define CASADBUS_IO_H_ 00030 00031 #include <sstream> 00032 #include <iterator> 00033 00034 #include <casadbus/types/variant.h> 00035 00036 /******************************************************************************************** 00037 ********************************************************************************************* 00038 **** The first attempt at generalized std-c++ was based upon "copying" elements of data **** 00039 **** structures to an output stream, as with: **** 00040 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- **** 00041 **** std::ostream_iterator<std::pair<std::string,dbus::variant> > out( cout, ", " ); **** 00042 **** std::copy( details.begin( ), details.end( ), out ); **** 00043 **** std::cout << std::endl; **** 00044 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- **** 00045 **** This proved to be unsuccessful (and infact this example does not work) because **** 00046 **** there is no way to provide a shift operator for non-standard types which will allow **** 00047 **** std::pair<K,V> to be output. For example, defining a shift operator for this **** 00048 **** example like: **** 00049 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- **** 00050 **** ostream &operator<<( ostream &out, const std::pair<std::string, **** 00051 **** casa::dbus::variant> &p ); **** 00052 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- **** 00053 **** ONLY works if this operator is added to the "std" namespace. Instead, these **** 00054 **** functions for converting arbitrary types to strings are inteded to be used with the **** 00055 **** std::transform(...) function, like: **** 00056 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- **** 00057 **** std::string toStdString( const std::pair<size_t,size_t> & data) { **** 00058 **** std::ostringstream str; **** 00059 **** str << data.first << ", " << data.second; **** 00060 **** return str.str(); **** 00061 **** } **** 00062 **** **** 00063 **** std::transform( a_map.begin(), a_map.end(), **** 00064 **** std::ostream_iterator<std::string>( std::cout, "\n" ), **** 00065 **** toString ); **** 00066 ********************************************************************************************* 00067 ********************************************************************************************/ 00068 namespace casa { 00069 namespace dbus { 00070 00071 std::string asString( const variant &v ); 00072 00073 inline std::string asString( const std::string &s ) { return s; } 00074 00075 template<typename T> 00076 std::string asString( const T &t ) { 00077 std::ostringstream s; 00078 s << asString(t); 00079 return s.str( ); 00080 } 00081 00082 template<typename K, typename V> 00083 std::string asString( const std::pair<K,V> &p ) { 00084 std::ostringstream s; 00085 s << asString(p.first) << ": " << asString(p.second); 00086 return s.str(); 00087 } 00088 } 00089 } 00090 00091 #endif