#include <string>
#include "str_to_num.h"
#include "msg.h"
#include "opus_exceptions.h"

//////////////////////////////////////////////////////////////////////////
//
// Method: Oresource::get_value
//
// Purpose:
//    Returns the value associated with the provided key.
//
// Description:
//
//    Allows the user to access the keyword values in the Oresource object.
//    This is a template function that allows the user to use any type 
//    variables to access entries in the Oresource object.
//
// Returns:
//    none
//
// Exceptions thrown:
//    Opus_exceptions::No_entry  - if no value is found for that key
//    Opus_exceptions::Not_ready - if file not loaded
//    Opus_exceptions::Bad_val   - value cannot be expressed as desired type.
//
// Example:
//     Oresource path2(file_name);
//     std::string c("MINBLOCKS");
//     int    i;
//     std::string test;
//     path2.get_value(c,i);
//     path2.get_value(c,test);
//     if (i != atoi(test.c_str())  cout << "Error" << endl;
//
// Modification History:
//
// Date      OPR      Who         Reason
// --------- -------- ----------- ----------------------------------------
// 05/27/98           WMiller     Initial code
// 06/01/99  37797    Heller      Made into template function
// 04/25/06  55700    Sontag      Give std:: to naked endl
//////////////////////////////////////////////////////////////////////////
   
   template <class Result> 
   void Oresource::get_value(const std::string &key, Result &r) const
   {
	std::string entry;			// Value as a string
	entry = lookup_value(key);	// Lookup value in Oresource object
	try { 
	  Str_to_num cv(entry);
	  cv.convert(r);	// Convert value to required type
	}
        catch (...) {
          Msg m;
            m << sev(Msg::E) << type(Msg::BADVAL)   <<
            "Failed to convert" << key << " value, "<<
             entry << " to desired type " << std::endl << endm;
          throw Bad_val<std::string>(key);
	}
   }


   template <class Result> 
   void Oresource::get_value(const char *key, Result &r) const
   {
     if (key==NULL) {
       Msg m;
       std::string s("blank");
       m << sev(Msg::D) << type(Msg::MISSING) << "Blank keyword passed "
        << endm;
       throw No_entry<std::string>(s);
     }
     std::string keyword(key);
     get_value(keyword,r);
   }
