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 <iostream>
00050 
00051 //ignore this for now
00052 class CustomDataType{
00053 public:
00054   CustomDataType():_theInt(0), _theString(""){}
00055   CustomDataType(int theInt, std::string theString):_theInt(theInt), _theString(theString){}
00056 
00057   void setInt(int theInt){
00058     _theInt = theInt;
00059   }
00060 
00061   void setString(std::string theString){
00062     _theString = theString;
00063   }
00064 
00065   int getInt() const{
00066     return _theInt;
00067   }
00068 
00069   std::string getString() const{
00070     return _theString;
00071   }
00072 
00073   bool operator==(const CustomDataType &other) const{
00074     return _theInt == other._theInt && _theString == other._theString;
00075   }
00076 
00077 private:
00078   int _theInt;
00079   std::string _theString;
00080 };
00081 
00082 std::ostream& operator<<(std::ostream &out, const CustomDataType &object){
00083   out << object.getInt() << " " << object.getString();
00084   return out;
00085 }
00086 
00087 std::istream& operator>>(std::istream &in, CustomDataType &object){
00088   int theInt;
00089   std::string theString;
00090   in >> theInt;
00091   in >> theString;
00092   object.setInt(theInt);
00093   object.setString(theString);
00094   return in;
00095 }
00096 
00105 int main(int argc, char* argv[])
00106 {
00107   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00108 
00109   Teuchos::ParameterList My_List;
00110 
00111   //Basic data types
00112   My_List.set("int", 1);
00113   My_List.set("unsigned int", (unsigned int)1);
00114   My_List.set("short int", (short)1);
00115   My_List.set("unsigned short int", (unsigned short)1);
00116   My_List.set("long int", (long)1);
00117   My_List.set("unsigned long int", (unsigned long)1);
00118   #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00119   My_List.set("long long int", (long long)1);
00120   My_List.set("unsigned long long int", (unsigned long long)1);
00121   #endif
00122   My_List.set("float", (float)4.3);
00123   My_List.set("double", (double)4.3);
00124   My_List.set("string", "hello");
00125   My_List.set("char", 'c');
00126   My_List.set("bool", true);
00127 
00128   //Teuchos::Array are also supported for the following types
00129   Teuchos::Array<int> intArray = Teuchos::tuple<int>(1);
00130   My_List.set("array int", intArray );
00131   Teuchos::Array<unsigned int> uintArray = Teuchos::tuple<unsigned int>(1);
00132   My_List.set("array unsigned int", uintArray);
00133   Teuchos::Array<short> shortArray = Teuchos::tuple<short>(1);
00134   My_List.set("array short int", shortArray);
00135   Teuchos::Array<unsigned short> ushortArray = Teuchos::tuple<unsigned short>(1);
00136   My_List.set("array unsigned short int", ushortArray);
00137   Teuchos::Array<long> longArray = Teuchos::tuple<long>(1);
00138   My_List.set("array long int", longArray);
00139   Teuchos::Array<unsigned long> ulongArray = Teuchos::tuple<unsigned long>(1);
00140   My_List.set("array unsigned long int", ulongArray);
00141   #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00142   Teuchos::Array<long long> longlongArray = Teuchos::tuple<long long>(1);
00143   My_List.set("array long long int", longlongArray);
00144   Teuchos::Array<unsigned long long> ulonglongArray = Teuchos::tuple<unsigned long long>(1);
00145   My_List.set("array unsigned long long int", ulonglongArray);
00146   #endif
00147   Teuchos::Array<float> floatArray = Teuchos::tuple<float>(4.3);
00148   My_List.set("array float", floatArray);
00149   Teuchos::Array<double> doubleArray = Teuchos::tuple<double>(4.3);
00150   My_List.set("array double", doubleArray);
00151   Teuchos::Array<std::string> stringArray = Teuchos::tuple<std::string>("hello");
00152   My_List.set("array string", stringArray);
00153 
00154   //Now for the custom data type. First, lets put one in the parameter list.
00155   CustomDataType sampleCustom(3, "hello");
00156 
00157   My_List.set("custom data", sampleCustom);
00158   
00159   //Now before we write this out to xml, we have to make sure we have a converter 
00160   //for our cusomt data type. Since our custom datatype overrides the operator<< and
00161   //operator>> we can just use and instance of the StandardTemplatedParameterConverter.
00162   //We'll do this using the convience macro. Look at the source code for the macro
00163   //to see everything that's actually goiing on. It's in Teuchos_ParameterEntryXMLConverterDB.hpp .
00164 
00165   TEUCHOS_ADD_TYPE_CONVERTER(CustomDataType);
00166 
00167   //Now we'll write it out to xml.
00168   Teuchos::writeParameterListToXmlFile(My_List, "xml_data_types_test_list.xml");
00169   //Then read it in to a new list.
00170 
00171   Teuchos::writeParameterListToXmlOStream(
00172     My_List,
00173     std::cout);
00174 
00175  
00176   Teuchos::RCP<Teuchos::ParameterList> readIn = Teuchos::getParametersFromXmlFile("xml_data_types_test_list.xml");
00177 
00178   std::cout << *readIn;
00179   //If we compare them, we'll see they're equal
00180   if(*readIn == My_List){
00181     std::cout << "Huzzah!\n";
00182   }
00183   else{
00184     std::cerr << "Uh oh...";
00185     return -1;
00186   }
00187 
00194   return 0;
00195 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines