Teuchos Package Browser (Single Doxygen Collection) Version of the Day
xml_data_types.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_ParameterList.hpp"
00045 #include "Teuchos_Array.hpp"  
00046 #include "Teuchos_Version.hpp"
00047 #include "Teuchos_ParameterEntryXMLConverterDB.hpp"
00048 #include "Teuchos_XMLParameterListCoreHelpers.hpp"
00049 #include "Teuchos_as.hpp"
00050 #include <iostream>
00051 
00052 //ignore this for now
00053 class CustomDataType{
00054 public:
00055   CustomDataType():_theInt(0), _theString(""){}
00056   CustomDataType(int theInt, std::string theString):_theInt(theInt), _theString(theString){}
00057 
00058   void setInt(int theInt){
00059     _theInt = theInt;
00060   }
00061 
00062   void setString(std::string theString){
00063     _theString = theString;
00064   }
00065 
00066   int getInt() const{
00067     return _theInt;
00068   }
00069 
00070   std::string getString() const{
00071     return _theString;
00072   }
00073 
00074   bool operator==(const CustomDataType &other) const{
00075     return _theInt == other._theInt && _theString == other._theString;
00076   }
00077 
00078 private:
00079   int _theInt;
00080   std::string _theString;
00081 };
00082 
00083 std::ostream& operator<<(std::ostream &out, const CustomDataType &object){
00084   out << object.getInt() << " " << object.getString();
00085   return out;
00086 }
00087 
00088 std::istream& operator>>(std::istream &in, CustomDataType &object){
00089   int theInt;
00090   std::string theString;
00091   in >> theInt;
00092   in >> theString;
00093   object.setInt(theInt);
00094   object.setString(theString);
00095   return in;
00096 }
00097 
00106 int main(int argc, char* argv[])
00107 {
00108 
00109   using Teuchos::tuple;
00110   using Teuchos::Array;
00111   using Teuchos::RCP;
00112   using Teuchos::ParameterList;
00113 
00114   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00115 
00116   ParameterList myPL;
00117 
00118   //Basic data types
00119   myPL.set<int>("my int", 1);
00120   myPL.set<unsigned int>("my unsigned int", 1);
00121   myPL.set<short int>("my short int", 1);
00122   myPL.set<short>("my short", 1);
00123   myPL.set<unsigned short int>("my unsigned short int", 1);
00124   myPL.set<unsigned short>("my unsigned short", 1);
00125   myPL.set<long int>("my long int", 1);
00126   myPL.set<long>("my long", 1);
00127   myPL.set<unsigned long int>("my unsigned long int", 1);
00128   myPL.set<unsigned long>("my unsigned long", 1);
00129 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00130   myPL.set<long long int>("my long long int", 1);
00131   myPL.set<long long>("my long long", 1);
00132   myPL.set<unsigned long long int>("my unsigned long long int", 1);
00133   myPL.set<unsigned long long>("my unsigned long long", 1);
00134 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00135   myPL.set<float>("my float", 4.3);
00136   myPL.set<double>("my double", 4.3);
00137   myPL.set("my string", "hello");
00138   myPL.set("my char", 'c');
00139   myPL.set("my bool", true);
00140 
00141   // Array are also supported for the following types
00142   myPL.set<Array<int> >("my array int", tuple<int>(1, 2));
00143   myPL.set<Array<unsigned int> >("my array unsigned int",
00144     tuple<unsigned int>(1));
00145   myPL.set<Array<short int> > ("my array short int",
00146     tuple<short int>(1, 2));
00147   myPL.set<Array<unsigned short int> > ("my array unsigned short int",
00148     tuple<unsigned short int>(1, 2));
00149   myPL.set<Array<long int> >("my array long int",
00150     tuple<long int>(1, 2));
00151   myPL.set<Array<unsigned long int> >("my array unsigned long int",
00152     tuple<unsigned long int>(1, 2));
00153 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00154   myPL.set<Array<long long int> >("my array long long int",
00155     tuple<long long int>(1, 2));
00156   myPL.set<Array<unsigned long long int> >("my array unsigned long long int",
00157     tuple<unsigned long long>(1, 2));
00158 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00159   myPL.set<Array<float> >("my array float", tuple<float>(1,1, 2.2));
00160   myPL.set<Array<double> >("my array double", tuple<double>(1,1, 2.2));
00161   myPL.set<Array<std::string> >("my array string",
00162     tuple<std::string>("hello", "world"));
00163 
00164   //Now for the custom data type. First, lets put one in the parameter list.
00165   CustomDataType sampleCustom(3, "hello");
00166 
00167   myPL.set("my custom data", sampleCustom);
00168   
00169   //Now before we write this out to xml, we have to make sure we have a
00170   //converter for our cusomt data type. Since our custom datatype overrides
00171   //the operator<< and operator>> we can just use and instance of the
00172   //StandardTemplatedParameterConverter.  We'll do this using the convience
00173   //macro. Look at the source code for the macro to see everything that's
00174   //actually goiing on. It's in Teuchos_ParameterEntryXMLConverterDB.hpp.
00175 
00176   TEUCHOS_ADD_TYPE_CONVERTER(CustomDataType);
00177 
00178   //Now we'll write it out to xml.
00179   Teuchos::writeParameterListToXmlFile(myPL, "xml_data_types_test_list.xml");
00180   //Then read it in to a new list.
00181 
00182   Teuchos::writeParameterListToXmlOStream(
00183     myPL,
00184     std::cout);
00185  
00186   const RCP<ParameterList> readPL = 
00187     Teuchos::getParametersFromXmlFile("xml_data_types_test_list.xml");
00188 
00189   std::cout << *readPL;
00190 
00191   //If we compare them, we'll see they're equal
00192   if(*readPL == myPL){
00193     std::cout << "Huzzah!\n";
00194   }
00195   else{
00196     std::cerr << "Uh oh...";
00197     return -1;
00198   }
00199 
00200   // Read the parameters in one at a time
00201   const int myInt = readPL->get<int>("my int");
00202   std::cout << "myInt = " << myInt << "\n";
00203   const unsigned int myUnsignedInt = readPL->get<unsigned int>("my unsigned int");
00204   std::cout << "myUnsignedInt = " << myUnsignedInt << "\n";
00205   const short int myShortInt = readPL->get<short int>("my short int");
00206   std::cout << "myShortInt = " << myShortInt << "\n";
00207   const short int myShort = readPL->get<short>("my short");
00208   std::cout << "myShort = " << myShort << "\n";
00209   const unsigned short int myUnsignedShortInt = readPL->get<unsigned short int>("my unsigned short int");
00210   std::cout << "myUnsignedShortInt = " << myUnsignedShortInt << "\n";
00211   const unsigned short int myUnsignedShort = readPL->get<unsigned short>("my unsigned short");
00212   std::cout << "myUnsignedShort = " << myUnsignedShort << "\n";
00213   const long int myLongInt = readPL->get<long int>("my long int");
00214   std::cout << "myLongInt = " << myLongInt << "\n";
00215   const long int myLong = readPL->get<long>("my long");
00216   std::cout << "myLong = " << myLong << "\n";
00217   const unsigned long int myUnsignedLongInt = readPL->get<unsigned long int>("my unsigned long int");
00218   std::cout << "myUnsignedLongInt = " << myUnsignedLongInt << "\n";
00219   const unsigned long myUnsignedLong = readPL->get<unsigned long>("my unsigned long");
00220   std::cout << "myUnsignedLong = " << myUnsignedLong << "\n";
00221 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00222   const long long int myLongLongInt = readPL->get<long long int>("my long long int");
00223   std::cout << "myLongLongInt = " << myLongLongInt << "\n";
00224   const long long int myLongLong = readPL->get<long long>("my long long");
00225   std::cout << "myLongLong = " << myLongLong << "\n";
00226   const unsigned long long int myUnsignedLongLongInt = readPL->get<unsigned long long int>("my unsigned long long int");
00227   std::cout << "myUnsignedLongLongInt = " << myUnsignedLongLongInt << "\n";
00228   const unsigned long long myUnsignedLongLong = readPL->get<unsigned long long>("my unsigned long long");
00229   std::cout << "myUnsignedLongLong = " << myUnsignedLongLong << "\n";
00230 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00231   const float myFloat = readPL->get<float>("my float");
00232   std::cout << "myFloat = " << myFloat << "\n";
00233   const double myDouble = readPL->get<double>("my double");
00234   std::cout << "myDouble = " << myDouble << "\n";
00235   const std::string myString = readPL->get<std::string>("my string");
00236   std::cout << "myString = " << myString << "\n";
00237   const char myChar = readPL->get<char>("my char");
00238   std::cout << "myChar = " << myChar << "\n";
00239   const bool myBool = readPL->get<bool>("my bool");
00240   std::cout << "myBool = " << myBool << "\n";
00241   const Array<int> myIntArray = readPL->get<Array<int> >("my array int");
00242   std::cout << "myIntArray = " << myIntArray << "\n";
00243   const Array<float> myFloatArray = readPL->get<Array<float> >("my array float");
00244   std::cout << "myFloatArray = " << myFloatArray << "\n";
00245   const Array<double> myDoubleArray = readPL->get<Array<double> >("my array double");
00246   std::cout << "myDoubleArray = " << myDoubleArray << "\n";
00247   const Array<std::string> myStringArray = readPL->get<Array<std::string> >("my array string");
00248   std::cout << "myStringArray = " << myStringArray << "\n";
00249 
00256   return 0;
00257 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines