Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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 "Teuchos_StandardCatchMacros.hpp"
00051 #include <iostream>
00052 
00053 //ignore this for now
00054 class CustomDataType{
00055   public:
00056     CustomDataType():_theInt(0), _theString(""){}
00057     CustomDataType(int theInt, std::string theString):_theInt(theInt), _theString(theString){}
00058 
00059     void setInt(int theInt){
00060       _theInt = theInt;
00061     }
00062 
00063     void setString(std::string theString){
00064       _theString = theString;
00065     }
00066 
00067     int getInt() const{
00068       return _theInt;
00069     }
00070 
00071     std::string getString() const{
00072       return _theString;
00073     }
00074 
00075     bool operator==(const CustomDataType &other) const{
00076       return _theInt == other._theInt && _theString == other._theString;
00077     }
00078 
00079   private:
00080     int _theInt;
00081     std::string _theString;
00082 };
00083 
00084 std::ostream& operator<<(std::ostream &out, const CustomDataType &object){
00085   out << object.getInt() << " " << object.getString();
00086   return out;
00087 }
00088 
00089 std::istream& operator>>(std::istream &in, CustomDataType &object){
00090   int theInt;
00091   std::string theString;
00092   in >> theInt;
00093   in >> theString;
00094   object.setInt(theInt);
00095   object.setString(theString);
00096   return in;
00097 }
00098 
00107 int main(int argc, char* argv[])
00108 {
00109 
00110   using Teuchos::tuple;
00111   using Teuchos::Array;
00112   using Teuchos::RCP;
00113   using Teuchos::ParameterList;
00114 
00115   bool success = false;
00116   bool verbose = true;
00117   try {
00118     std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00119 
00120     ParameterList myPL;
00121 
00122     //Basic data types
00123     myPL.set<int>("my int", 1);
00124     myPL.set<unsigned int>("my unsigned int", 1);
00125     myPL.set<short int>("my short int", 1);
00126     myPL.set<short>("my short", 1);
00127     myPL.set<unsigned short int>("my unsigned short int", 1);
00128     myPL.set<unsigned short>("my unsigned short", 1);
00129     myPL.set<long int>("my long int", 1);
00130     myPL.set<long>("my long", 1);
00131     myPL.set<unsigned long int>("my unsigned long int", 1);
00132     myPL.set<unsigned long>("my unsigned long", 1);
00133 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00134     myPL.set<long long int>("my long long int", 1);
00135     myPL.set<long long>("my long long", 1);
00136     myPL.set<unsigned long long int>("my unsigned long long int", 1);
00137     myPL.set<unsigned long long>("my unsigned long long", 1);
00138 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00139     myPL.set<float>("my float", 4.3);
00140     myPL.set<double>("my double", 4.3);
00141     myPL.set("my string", "hello");
00142     myPL.set("my char", 'c');
00143     myPL.set("my bool", true);
00144 
00145     // Array are also supported for the following types
00146     myPL.set<Array<int> >("my array int", tuple<int>(1, 2));
00147     myPL.set<Array<unsigned int> >("my array unsigned int",
00148         tuple<unsigned int>(1));
00149     myPL.set<Array<short int> > ("my array short int",
00150         tuple<short int>(1, 2));
00151     myPL.set<Array<unsigned short int> > ("my array unsigned short int",
00152         tuple<unsigned short int>(1, 2));
00153     myPL.set<Array<long int> >("my array long int",
00154         tuple<long int>(1, 2));
00155     myPL.set<Array<unsigned long int> >("my array unsigned long int",
00156         tuple<unsigned long int>(1, 2));
00157 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00158     myPL.set<Array<long long int> >("my array long long int",
00159         tuple<long long int>(1, 2));
00160     myPL.set<Array<unsigned long long int> >("my array unsigned long long int",
00161         tuple<unsigned long long>(1, 2));
00162 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00163     myPL.set<Array<float> >("my array float", tuple<float>(1,1, 2.2));
00164     myPL.set<Array<double> >("my array double", tuple<double>(1,1, 2.2));
00165     myPL.set<Array<std::string> >("my array string",
00166         tuple<std::string>("hello", "world"));
00167 
00168     //Now for the custom data type. First, lets put one in the parameter list.
00169     CustomDataType sampleCustom(3, "hello");
00170 
00171     myPL.set("my custom data", sampleCustom);
00172 
00173     //Now before we write this out to xml, we have to make sure we have a
00174     //converter for our cusomt data type. Since our custom datatype overrides
00175     //the operator<< and operator>> we can just use and instance of the
00176     //StandardTemplatedParameterConverter.  We'll do this using the convience
00177     //macro. Look at the source code for the macro to see everything that's
00178     //actually goiing on. It's in Teuchos_ParameterEntryXMLConverterDB.hpp.
00179 
00180     TEUCHOS_ADD_TYPE_CONVERTER(CustomDataType);
00181 
00182     //Now we'll write it out to xml.
00183     Teuchos::writeParameterListToXmlFile(myPL, "xml_data_types_test_list.xml");
00184     //Then read it in to a new list.
00185 
00186     Teuchos::writeParameterListToXmlOStream(
00187         myPL,
00188         std::cout);
00189 
00190     const RCP<ParameterList> readPL =
00191       Teuchos::getParametersFromXmlFile("xml_data_types_test_list.xml");
00192 
00193     std::cout << *readPL;
00194 
00195     //If we compare them, we'll see they're equal
00196     if(*readPL == myPL)
00197       std::cout << "Huzzah!\n";
00198     else
00199       throw "Uh oh...";
00200 
00201     // Read the parameters in one at a time
00202     const int myInt = readPL->get<int>("my int");
00203     std::cout << "myInt = " << myInt << "\n";
00204     const unsigned int myUnsignedInt = readPL->get<unsigned int>("my unsigned int");
00205     std::cout << "myUnsignedInt = " << myUnsignedInt << "\n";
00206     const short int myShortInt = readPL->get<short int>("my short int");
00207     std::cout << "myShortInt = " << myShortInt << "\n";
00208     const short int myShort = readPL->get<short>("my short");
00209     std::cout << "myShort = " << myShort << "\n";
00210     const unsigned short int myUnsignedShortInt = readPL->get<unsigned short int>("my unsigned short int");
00211     std::cout << "myUnsignedShortInt = " << myUnsignedShortInt << "\n";
00212     const unsigned short int myUnsignedShort = readPL->get<unsigned short>("my unsigned short");
00213     std::cout << "myUnsignedShort = " << myUnsignedShort << "\n";
00214     const long int myLongInt = readPL->get<long int>("my long int");
00215     std::cout << "myLongInt = " << myLongInt << "\n";
00216     const long int myLong = readPL->get<long>("my long");
00217     std::cout << "myLong = " << myLong << "\n";
00218     const unsigned long int myUnsignedLongInt = readPL->get<unsigned long int>("my unsigned long int");
00219     std::cout << "myUnsignedLongInt = " << myUnsignedLongInt << "\n";
00220     const unsigned long myUnsignedLong = readPL->get<unsigned long>("my unsigned long");
00221     std::cout << "myUnsignedLong = " << myUnsignedLong << "\n";
00222 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00223     const long long int myLongLongInt = readPL->get<long long int>("my long long int");
00224     std::cout << "myLongLongInt = " << myLongLongInt << "\n";
00225     const long long int myLongLong = readPL->get<long long>("my long long");
00226     std::cout << "myLongLong = " << myLongLong << "\n";
00227     const unsigned long long int myUnsignedLongLongInt = readPL->get<unsigned long long int>("my unsigned long long int");
00228     std::cout << "myUnsignedLongLongInt = " << myUnsignedLongLongInt << "\n";
00229     const unsigned long long myUnsignedLongLong = readPL->get<unsigned long long>("my unsigned long long");
00230     std::cout << "myUnsignedLongLong = " << myUnsignedLongLong << "\n";
00231 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00232     const float myFloat = readPL->get<float>("my float");
00233     std::cout << "myFloat = " << myFloat << "\n";
00234     const double myDouble = readPL->get<double>("my double");
00235     std::cout << "myDouble = " << myDouble << "\n";
00236     const std::string myString = readPL->get<std::string>("my string");
00237     std::cout << "myString = " << myString << "\n";
00238     const char myChar = readPL->get<char>("my char");
00239     std::cout << "myChar = " << myChar << "\n";
00240     const bool myBool = readPL->get<bool>("my bool");
00241     std::cout << "myBool = " << myBool << "\n";
00242     const Array<int> myIntArray = readPL->get<Array<int> >("my array int");
00243     std::cout << "myIntArray = " << myIntArray << "\n";
00244     const Array<float> myFloatArray = readPL->get<Array<float> >("my array float");
00245     std::cout << "myFloatArray = " << myFloatArray << "\n";
00246     const Array<double> myDoubleArray = readPL->get<Array<double> >("my array double");
00247     std::cout << "myDoubleArray = " << myDoubleArray << "\n";
00248     const Array<std::string> myStringArray = readPL->get<Array<std::string> >("my array string");
00249     std::cout << "myStringArray = " << myStringArray << "\n";
00250 
00251     success = true;
00258   }
00259   TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
00260   return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
00261 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines