Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xml_data_types.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "Teuchos_Array.hpp"
46 #include "Teuchos_Version.hpp"
49 #include "Teuchos_as.hpp"
51 #include <iostream>
52 
53 //ignore this for now
55  public:
57  CustomDataType(int theInt, std::string theString):_theInt(theInt), _theString(theString){}
58 
59  void setInt(int theInt){
60  _theInt = theInt;
61  }
62 
63  void setString(std::string theString){
64  _theString = theString;
65  }
66 
67  int getInt() const{
68  return _theInt;
69  }
70 
71  std::string getString() const{
72  return _theString;
73  }
74 
75  bool operator==(const CustomDataType &other) const{
76  return _theInt == other._theInt && _theString == other._theString;
77  }
78 
79  private:
80  int _theInt;
81  std::string _theString;
82 };
83 
84 std::ostream& operator<<(std::ostream &out, const CustomDataType &object){
85  out << object.getInt() << " " << object.getString();
86  return out;
87 }
88 
89 std::istream& operator>>(std::istream &in, CustomDataType &object){
90  int theInt;
91  std::string theString;
92  in >> theInt;
93  in >> theString;
94  object.setInt(theInt);
95  object.setString(theString);
96  return in;
97 }
98 
107 int main(int argc, char* argv[])
108 {
109 
110  using Teuchos::tuple;
111  using Teuchos::Array;
112  using Teuchos::RCP;
114 
115  bool success = false;
116  bool verbose = true;
117  try {
118  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
119 
120  ParameterList myPL;
121 
122  //Basic data types
123  myPL.set<int>("my int", 1);
124  myPL.set<unsigned int>("my unsigned int", 1);
125  myPL.set<short int>("my short int", 1);
126  myPL.set<short>("my short", 1);
127  myPL.set<unsigned short int>("my unsigned short int", 1);
128  myPL.set<unsigned short>("my unsigned short", 1);
129  myPL.set<long int>("my long int", 1);
130  myPL.set<long>("my long", 1);
131  myPL.set<unsigned long int>("my unsigned long int", 1);
132  myPL.set<unsigned long>("my unsigned long", 1);
133  myPL.set<long long int>("my long long int", 1);
134  myPL.set<long long>("my long long", 1);
135  myPL.set<unsigned long long int>("my unsigned long long int", 1);
136  myPL.set<unsigned long long>("my unsigned long long", 1);
137  myPL.set<float>("my float", 4.3);
138  myPL.set<double>("my double", 4.3);
139  myPL.set("my string", "hello");
140  myPL.set("my char", 'c');
141  myPL.set("my bool", true);
142 
143  // Array are also supported for the following types
144  myPL.set<Array<int> >("my array int", tuple<int>(1, 2));
145  myPL.set<Array<unsigned int> >("my array unsigned int",
146  tuple<unsigned int>(1));
147  myPL.set<Array<short int> > ("my array short int",
148  tuple<short int>(1, 2));
149  myPL.set<Array<unsigned short int> > ("my array unsigned short int",
150  tuple<unsigned short int>(1, 2));
151  myPL.set<Array<long int> >("my array long int",
152  tuple<long int>(1, 2));
153  myPL.set<Array<unsigned long int> >("my array unsigned long int",
154  tuple<unsigned long int>(1, 2));
155  myPL.set<Array<long long int> >("my array long long int",
156  tuple<long long int>(1, 2));
157  myPL.set<Array<unsigned long long int> >("my array unsigned long long int",
158  tuple<unsigned long long>(1, 2));
159  myPL.set<Array<float> >("my array float", tuple<float>(1,1, 2.2));
160  myPL.set<Array<double> >("my array double", tuple<double>(1,1, 2.2));
161  myPL.set<Array<std::string> >("my array string",
162  tuple<std::string>("hello", "world"));
163 
164  //Now for the custom data type. First, lets put one in the parameter list.
165  CustomDataType sampleCustom(3, "hello");
166 
167  myPL.set("my custom data", sampleCustom);
168 
169  //Now before we write this out to xml, we have to make sure we have a
170  //converter for our cusomt data type. Since our custom datatype overrides
171  //the operator<< and operator>> we can just use and instance of the
172  //StandardTemplatedParameterConverter. We'll do this using the convience
173  //macro. Look at the source code for the macro to see everything that's
174  //actually goiing on. It's in Teuchos_ParameterEntryXMLConverterDB.hpp.
175 
177 
178  //Now we'll write it out to xml.
179  Teuchos::writeParameterListToXmlFile(myPL, "xml_data_types_test_list.xml");
180  //Then read it in to a new list.
181 
182  Teuchos::writeParameterListToXmlOStream(
183  myPL,
184  std::cout);
185 
186  const RCP<ParameterList> readPL =
187  Teuchos::getParametersFromXmlFile("xml_data_types_test_list.xml");
188 
189  std::cout << *readPL;
190 
191  //If we compare them, we'll see they're equal
192  if(*readPL == myPL)
193  std::cout << "Huzzah!\n";
194  else
195  throw "Uh oh...";
196 
197  // Read the parameters in one at a time
198  const int myInt = readPL->get<int>("my int");
199  std::cout << "myInt = " << myInt << "\n";
200  const unsigned int myUnsignedInt = readPL->get<unsigned int>("my unsigned int");
201  std::cout << "myUnsignedInt = " << myUnsignedInt << "\n";
202  const short int myShortInt = readPL->get<short int>("my short int");
203  std::cout << "myShortInt = " << myShortInt << "\n";
204  const short int myShort = readPL->get<short>("my short");
205  std::cout << "myShort = " << myShort << "\n";
206  const unsigned short int myUnsignedShortInt = readPL->get<unsigned short int>("my unsigned short int");
207  std::cout << "myUnsignedShortInt = " << myUnsignedShortInt << "\n";
208  const unsigned short int myUnsignedShort = readPL->get<unsigned short>("my unsigned short");
209  std::cout << "myUnsignedShort = " << myUnsignedShort << "\n";
210  const long int myLongInt = readPL->get<long int>("my long int");
211  std::cout << "myLongInt = " << myLongInt << "\n";
212  const long int myLong = readPL->get<long>("my long");
213  std::cout << "myLong = " << myLong << "\n";
214  const unsigned long int myUnsignedLongInt = readPL->get<unsigned long int>("my unsigned long int");
215  std::cout << "myUnsignedLongInt = " << myUnsignedLongInt << "\n";
216  const unsigned long myUnsignedLong = readPL->get<unsigned long>("my unsigned long");
217  std::cout << "myUnsignedLong = " << myUnsignedLong << "\n";
218  const long long int myLongLongInt = readPL->get<long long int>("my long long int");
219  std::cout << "myLongLongInt = " << myLongLongInt << "\n";
220  const long long int myLongLong = readPL->get<long long>("my long long");
221  std::cout << "myLongLong = " << myLongLong << "\n";
222  const unsigned long long int myUnsignedLongLongInt = readPL->get<unsigned long long int>("my unsigned long long int");
223  std::cout << "myUnsignedLongLongInt = " << myUnsignedLongLongInt << "\n";
224  const unsigned long long myUnsignedLongLong = readPL->get<unsigned long long>("my unsigned long long");
225  std::cout << "myUnsignedLongLong = " << myUnsignedLongLong << "\n";
226  const float myFloat = readPL->get<float>("my float");
227  std::cout << "myFloat = " << myFloat << "\n";
228  const double myDouble = readPL->get<double>("my double");
229  std::cout << "myDouble = " << myDouble << "\n";
230  const std::string myString = readPL->get<std::string>("my string");
231  std::cout << "myString = " << myString << "\n";
232  const char myChar = readPL->get<char>("my char");
233  std::cout << "myChar = " << myChar << "\n";
234  const bool myBool = readPL->get<bool>("my bool");
235  std::cout << "myBool = " << myBool << "\n";
236  const Array<int> myIntArray = readPL->get<Array<int> >("my array int");
237  std::cout << "myIntArray = " << myIntArray << "\n";
238  const Array<float> myFloatArray = readPL->get<Array<float> >("my array float");
239  std::cout << "myFloatArray = " << myFloatArray << "\n";
240  const Array<double> myDoubleArray = readPL->get<Array<double> >("my array double");
241  std::cout << "myDoubleArray = " << myDoubleArray << "\n";
242  const Array<std::string> myStringArray = readPL->get<Array<std::string> >("my array string");
243  std::cout << "myStringArray = " << myStringArray << "\n";
244 
245  success = true;
252  }
253  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
254  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
255 }
CustomDataType(int theInt, std::string theString)
std::istringstream & operator>>(std::istringstream &in, Array< T > &array)
A wrapper around the fromStringToArray function which allows the operator&gt;&gt; to be used on Arrays...
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
Templated Parameter List class.
std::string _theString
std::string getString() const
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
bool operator==(const CustomDataType &other) const
std::string Teuchos_Version()
A list of parameters of arbitrary type.
int main(int argc, char *argv[])
#define TEUCHOS_ADD_TYPE_CONVERTER(T)
void setInt(int theInt)
void setString(std::string theString)
Templated array class derived from the STL std::vector.
Smart reference counting pointer class for automatic garbage collection.
int getInt() const
Definition of Teuchos::as, for conversions between types.
std::ostream & operator<<(std::ostream &os, const any &rhs)
Writes &quot;any&quot; input rhs to the output stream os.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...