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 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Array.hpp"
12 #include "Teuchos_Version.hpp"
15 #include "Teuchos_as.hpp"
17 #include <iostream>
18 
19 //ignore this for now
21  public:
23  CustomDataType(int theInt, std::string theString):_theInt(theInt), _theString(theString){}
24 
25  void setInt(int theInt){
26  _theInt = theInt;
27  }
28 
29  void setString(std::string theString){
30  _theString = theString;
31  }
32 
33  int getInt() const{
34  return _theInt;
35  }
36 
37  std::string getString() const{
38  return _theString;
39  }
40 
41  bool operator==(const CustomDataType &other) const{
42  return _theInt == other._theInt && _theString == other._theString;
43  }
44 
45  private:
46  int _theInt;
47  std::string _theString;
48 };
49 
50 std::ostream& operator<<(std::ostream &out, const CustomDataType &object){
51  out << object.getInt() << " " << object.getString();
52  return out;
53 }
54 
55 std::istream& operator>>(std::istream &in, CustomDataType &object){
56  int theInt;
57  std::string theString;
58  in >> theInt;
59  in >> theString;
60  object.setInt(theInt);
61  object.setString(theString);
62  return in;
63 }
64 
73 int main(int argc, char* argv[])
74 {
75 
76  using Teuchos::tuple;
77  using Teuchos::Array;
78  using Teuchos::RCP;
80 
81  bool success = false;
82  bool verbose = true;
83  try {
84  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
85 
86  ParameterList myPL;
87 
88  //Basic data types
89  myPL.set<int>("my int", 1);
90  myPL.set<unsigned int>("my unsigned int", 1);
91  myPL.set<short int>("my short int", 1);
92  myPL.set<short>("my short", 1);
93  myPL.set<unsigned short int>("my unsigned short int", 1);
94  myPL.set<unsigned short>("my unsigned short", 1);
95  myPL.set<long int>("my long int", 1);
96  myPL.set<long>("my long", 1);
97  myPL.set<unsigned long int>("my unsigned long int", 1);
98  myPL.set<unsigned long>("my unsigned long", 1);
99  myPL.set<long long int>("my long long int", 1);
100  myPL.set<long long>("my long long", 1);
101  myPL.set<unsigned long long int>("my unsigned long long int", 1);
102  myPL.set<unsigned long long>("my unsigned long long", 1);
103  myPL.set<float>("my float", 4.3);
104  myPL.set<double>("my double", 4.3);
105  myPL.set("my string", "hello");
106  myPL.set("my char", 'c');
107  myPL.set("my bool", true);
108 
109  // Array are also supported for the following types
110  myPL.set<Array<int> >("my array int", tuple<int>(1, 2));
111  myPL.set<Array<unsigned int> >("my array unsigned int",
112  tuple<unsigned int>(1));
113  myPL.set<Array<short int> > ("my array short int",
114  tuple<short int>(1, 2));
115  myPL.set<Array<unsigned short int> > ("my array unsigned short int",
116  tuple<unsigned short int>(1, 2));
117  myPL.set<Array<long int> >("my array long int",
118  tuple<long int>(1, 2));
119  myPL.set<Array<unsigned long int> >("my array unsigned long int",
120  tuple<unsigned long int>(1, 2));
121  myPL.set<Array<long long int> >("my array long long int",
122  tuple<long long int>(1, 2));
123  myPL.set<Array<unsigned long long int> >("my array unsigned long long int",
124  tuple<unsigned long long>(1, 2));
125  myPL.set<Array<float> >("my array float", tuple<float>(1,1, 2.2));
126  myPL.set<Array<double> >("my array double", tuple<double>(1,1, 2.2));
127  myPL.set<Array<std::string> >("my array string",
128  tuple<std::string>("hello", "world"));
129 
130  //Now for the custom data type. First, lets put one in the parameter list.
131  CustomDataType sampleCustom(3, "hello");
132 
133  myPL.set("my custom data", sampleCustom);
134 
135  //Now before we write this out to xml, we have to make sure we have a
136  //converter for our cusomt data type. Since our custom datatype overrides
137  //the operator<< and operator>> we can just use and instance of the
138  //StandardTemplatedParameterConverter. We'll do this using the convience
139  //macro. Look at the source code for the macro to see everything that's
140  //actually goiing on. It's in Teuchos_ParameterEntryXMLConverterDB.hpp.
141 
143 
144  //Now we'll write it out to xml.
145  Teuchos::writeParameterListToXmlFile(myPL, "xml_data_types_test_list.xml");
146  //Then read it in to a new list.
147 
148  Teuchos::writeParameterListToXmlOStream(
149  myPL,
150  std::cout);
151 
152  const RCP<ParameterList> readPL =
153  Teuchos::getParametersFromXmlFile("xml_data_types_test_list.xml");
154 
155  std::cout << *readPL;
156 
157  //If we compare them, we'll see they're equal
158  if(*readPL == myPL)
159  std::cout << "Huzzah!\n";
160  else
161  throw "Uh oh...";
162 
163  // Read the parameters in one at a time
164  const int myInt = readPL->get<int>("my int");
165  std::cout << "myInt = " << myInt << "\n";
166  const unsigned int myUnsignedInt = readPL->get<unsigned int>("my unsigned int");
167  std::cout << "myUnsignedInt = " << myUnsignedInt << "\n";
168  const short int myShortInt = readPL->get<short int>("my short int");
169  std::cout << "myShortInt = " << myShortInt << "\n";
170  const short int myShort = readPL->get<short>("my short");
171  std::cout << "myShort = " << myShort << "\n";
172  const unsigned short int myUnsignedShortInt = readPL->get<unsigned short int>("my unsigned short int");
173  std::cout << "myUnsignedShortInt = " << myUnsignedShortInt << "\n";
174  const unsigned short int myUnsignedShort = readPL->get<unsigned short>("my unsigned short");
175  std::cout << "myUnsignedShort = " << myUnsignedShort << "\n";
176  const long int myLongInt = readPL->get<long int>("my long int");
177  std::cout << "myLongInt = " << myLongInt << "\n";
178  const long int myLong = readPL->get<long>("my long");
179  std::cout << "myLong = " << myLong << "\n";
180  const unsigned long int myUnsignedLongInt = readPL->get<unsigned long int>("my unsigned long int");
181  std::cout << "myUnsignedLongInt = " << myUnsignedLongInt << "\n";
182  const unsigned long myUnsignedLong = readPL->get<unsigned long>("my unsigned long");
183  std::cout << "myUnsignedLong = " << myUnsignedLong << "\n";
184  const long long int myLongLongInt = readPL->get<long long int>("my long long int");
185  std::cout << "myLongLongInt = " << myLongLongInt << "\n";
186  const long long int myLongLong = readPL->get<long long>("my long long");
187  std::cout << "myLongLong = " << myLongLong << "\n";
188  const unsigned long long int myUnsignedLongLongInt = readPL->get<unsigned long long int>("my unsigned long long int");
189  std::cout << "myUnsignedLongLongInt = " << myUnsignedLongLongInt << "\n";
190  const unsigned long long myUnsignedLongLong = readPL->get<unsigned long long>("my unsigned long long");
191  std::cout << "myUnsignedLongLong = " << myUnsignedLongLong << "\n";
192  const float myFloat = readPL->get<float>("my float");
193  std::cout << "myFloat = " << myFloat << "\n";
194  const double myDouble = readPL->get<double>("my double");
195  std::cout << "myDouble = " << myDouble << "\n";
196  const std::string myString = readPL->get<std::string>("my string");
197  std::cout << "myString = " << myString << "\n";
198  const char myChar = readPL->get<char>("my char");
199  std::cout << "myChar = " << myChar << "\n";
200  const bool myBool = readPL->get<bool>("my bool");
201  std::cout << "myBool = " << myBool << "\n";
202  const Array<int> myIntArray = readPL->get<Array<int> >("my array int");
203  std::cout << "myIntArray = " << myIntArray << "\n";
204  const Array<float> myFloatArray = readPL->get<Array<float> >("my array float");
205  std::cout << "myFloatArray = " << myFloatArray << "\n";
206  const Array<double> myDoubleArray = readPL->get<Array<double> >("my array double");
207  std::cout << "myDoubleArray = " << myDoubleArray << "\n";
208  const Array<std::string> myStringArray = readPL->get<Array<std::string> >("my array string");
209  std::cout << "myStringArray = " << myStringArray << "\n";
210 
211  success = true;
218  }
219  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
220  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
221 }
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...