casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
io.h
Go to the documentation of this file.
1 //# IO.h: IO utilities for dbus values
2 //# Copyright (C) 2013
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASADBUS_IO_H_
29 #define CASADBUS_IO_H_
30 
31 #include <sstream>
32 #include <iterator>
33 
34 #include <casadbus/types/variant.h>
35 
36 /********************************************************************************************
37 *********************************************************************************************
38 **** The first attempt at generalized std-c++ was based upon "copying" elements of data ****
39 **** structures to an output stream, as with: ****
40 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ****
41 **** std::ostream_iterator<std::pair<std::string,dbus::variant> > out( cout, ", " ); ****
42 **** std::copy( details.begin( ), details.end( ), out ); ****
43 **** std::cout << std::endl; ****
44 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ****
45 **** This proved to be unsuccessful (and infact this example does not work) because ****
46 **** there is no way to provide a shift operator for non-standard types which will allow ****
47 **** std::pair<K,V> to be output. For example, defining a shift operator for this ****
48 **** example like: ****
49 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ****
50 **** ostream &operator<<( ostream &out, const std::pair<std::string, ****
51 **** casa::dbus::variant> &p ); ****
52 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ****
53 **** ONLY works if this operator is added to the "std" namespace. Instead, these ****
54 **** functions for converting arbitrary types to strings are inteded to be used with the ****
55 **** std::transform(...) function, like: ****
56 **** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ****
57 **** std::string toStdString( const std::pair<size_t,size_t> & data) { ****
58 **** std::ostringstream str; ****
59 **** str << data.first << ", " << data.second; ****
60 **** return str.str(); ****
61 **** } ****
62 **** ****
63 **** std::transform( a_map.begin(), a_map.end(), ****
64 **** std::ostream_iterator<std::string>( std::cout, "\n" ), ****
65 **** toString ); ****
66 *********************************************************************************************
67 ********************************************************************************************/
68 namespace casa {
69  namespace dbus {
70 
71  std::string asString( const variant &v );
72 
73  inline std::string asString( const std::string &s ) { return s; }
74 
75  template<typename T>
76  std::string asString( const T &t ) {
77  std::ostringstream s;
78  s << asString(t);
79  return s.str( );
80  }
81 
82  template<typename K, typename V>
83  std::string asString( const std::pair<K,V> &p ) {
84  std::ostringstream s;
85  s << asString(p.first) << ": " << asString(p.second);
86  return s.str();
87  }
88  }
89 }
90 
91 #endif
std::string asString(const variant &v)