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_XMLParameterListHelpers.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<unsigned short int>("my unsigned short int", 1);
00123   myPL.set<long int>("my long int", 1);
00124   myPL.set<unsigned long int>("my unsigned long int", 1);
00125 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00126   myPL.set<long long int>("my long long int", 1);
00127   myPL.set<unsigned long long int>("my unsigned long long int", 1);
00128 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00129   myPL.set<float>("my float", 4.3);
00130   myPL.set<double>("my double", 4.3);
00131   myPL.set("my string", "hello");
00132   myPL.set("my char", 'c');
00133   myPL.set("my bool", true);
00134 
00135   // Array are also supported for the following types
00136   myPL.set<Array<int> >("my array int", tuple<int>(1, 2));
00137   myPL.set<Array<unsigned int> >("my array unsigned int",
00138     tuple<unsigned int>(1));
00139   myPL.set<Array<short int> > ("my array short int",
00140     tuple<short int>(1, 2));
00141   myPL.set<Array<unsigned short int> > ("my array unsigned short int",
00142     tuple<unsigned short int>(1, 2));
00143   myPL.set<Array<long int> >("my array long int",
00144     tuple<long int>(1, 2));
00145   myPL.set<Array<unsigned long int> >("my array unsigned long int",
00146     tuple<unsigned long int>(1, 2));
00147 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00148   myPL.set<Array<long long int> >("my array long long int",
00149     tuple<long long int>(1, 2));
00150   myPL.set<Array<unsigned long long int> >("my array unsigned long long int",
00151     tuple<unsigned long long>(1, 2));
00152 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00153   myPL.set<Array<float> >("my array float", tuple<float>(1,1, 2.2));
00154   myPL.set<Array<double> >("my array double", tuple<double>(1,1, 2.2));
00155   myPL.set<Array<std::string> >("my array string",
00156     tuple<std::string>("hello", "world"));
00157 
00158   //Now for the custom data type. First, lets put one in the parameter list.
00159   CustomDataType sampleCustom(3, "hello");
00160 
00161   myPL.set("my custom data", sampleCustom);
00162   
00163   //Now before we write this out to xml, we have to make sure we have a
00164   //converter for our cusomt data type. Since our custom datatype overrides
00165   //the operator<< and operator>> we can just use and instance of the
00166   //StandardTemplatedParameterConverter.  We'll do this using the convience
00167   //macro. Look at the source code for the macro to see everything that's
00168   //actually goiing on. It's in Teuchos_ParameterEntryXMLConverterDB.hpp.
00169 
00170   TEUCHOS_ADD_TYPE_CONVERTER(CustomDataType);
00171 
00172   //Now we'll write it out to xml.
00173   Teuchos::writeParameterListToXmlFile(myPL, "xml_data_types_test_list.xml");
00174   //Then read it in to a new list.
00175 
00176   Teuchos::writeParameterListToXmlOStream(
00177     myPL,
00178     std::cout);
00179  
00180   const RCP<ParameterList> readPL = 
00181     Teuchos::getParametersFromXmlFile("xml_data_types_test_list.xml");
00182 
00183   std::cout << *readPL;
00184 
00185   //If we compare them, we'll see they're equal
00186   if(*readPL == myPL){
00187     std::cout << "Huzzah!\n";
00188   }
00189   else{
00190     std::cerr << "Uh oh...";
00191     return -1;
00192   }
00193 
00194   // Read the parameters in one at a time
00195   const int myInt = readPL->get<int>("my int");
00196   std::cout << "myInt = " << myInt << "\n";
00197   const float myFloat = readPL->get<float>("my float");
00198   std::cout << "myFloat = " << myFloat << "\n";
00199   const double myDouble = readPL->get<double>("my double");
00200   std::cout << "myDouble = " << myDouble << "\n";
00201   const std::string myString = readPL->get<std::string>("my string");
00202   std::cout << "myString = " << myString << "\n";
00203   const char myChar = readPL->get<char>("my char");
00204   std::cout << "myChar = " << myChar << "\n";
00205   const bool myBool = readPL->get<bool>("my bool");
00206   std::cout << "myBool = " << myBool << "\n";
00207   const Array<int> myIntArray = readPL->get<Array<int> >("my array int");
00208   std::cout << "myIntArray = " << myIntArray << "\n";
00209   const Array<float> myFloatArray = readPL->get<Array<float> >("my array float");
00210   std::cout << "myFloatArray = " << myFloatArray << "\n";
00211   const Array<double> myDoubleArray = readPL->get<Array<double> >("my array double");
00212   std::cout << "myDoubleArray = " << myDoubleArray << "\n";
00213   const Array<std::string> myStringArray = readPL->get<Array<std::string> >("my array string");
00214   std::cout << "myStringArray = " << myStringArray << "\n";
00215 
00222   return 0;
00223 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines