Env_Value_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Env_Value_T.h
00006  *
00007  *  Env_Value_T.h,v 4.32 2006/03/01 09:47:33 jwillemsen Exp
00008  *
00009  *  Template to encapsulate getting a value from an environment variable
00010  *  and using a supplied default value if not in the environment.
00011  *
00012  *
00013  *  @author Chris Cleeland (derived from work by Carlos O'Ryan)
00014  */
00015 //=============================================================================
00016 
00017 #ifndef ACE_ENV_VALUE_T_H
00018 #define ACE_ENV_VALUE_T_H
00019 
00020 #include /**/ "ace/pre.h"
00021 
00022 #include "ace/config-all.h"
00023 #include "ace/Global_Macros.h"
00024 #include "ace/OS_NS_stdlib.h"
00025 
00026 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00027 # pragma once
00028 #endif /* ACE_LACKS_PRAGMA_ONCE */
00029 
00030 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00031 
00032 /**
00033  * @class ACE_Env_Value
00034  *
00035  * @brief Environment Variable Value
00036  *
00037  * Reads a variable from the user environment, providing a default
00038  * value.
00039  */
00040 template <class T>
00041 class ACE_Env_Value
00042 {
00043 public:
00044   /**
00045    * Default constructor which isn't bound to a specific environment
00046    * variable name or a default value.  Before being useful it must
00047    * <open>'d.
00048    */
00049   ACE_Env_Value (void);
00050 
00051   /// Constructor that calls <open>.
00052   ACE_Env_Value (const ACE_TCHAR *varname,
00053                  const T &vardefault);
00054 
00055   /// Destroy the value.
00056   ~ACE_Env_Value (void);
00057 
00058   /// Returns the value as type T.
00059   operator T (void);
00060 
00061   /// The constructor, read @a varname from the environment, using
00062   /// @a defval as its value if it is not defined.
00063   void open (const ACE_TCHAR *varname, const T &defval);
00064 
00065   /// Returns the name of the variable being tracked.
00066   const ACE_TCHAR *varname (void) const;
00067 
00068 private:
00069   /// Disallow copying and assignment.
00070   ACE_UNIMPLEMENTED_FUNC (ACE_Env_Value(const ACE_Env_Value<T> &))
00071   ACE_UNIMPLEMENTED_FUNC (ACE_Env_Value<T> operator=(const ACE_Env_Value<T> &))
00072 
00073   void fetch_value (void);
00074 
00075   const ACE_TCHAR *varname_;
00076   T value_;
00077 };
00078 
00079 /// Function to convert a string @a s into type @c T.
00080 template <class T> void ACE_Convert (const ACE_TCHAR *s, T &t);
00081 
00082 ACE_END_VERSIONED_NAMESPACE_DECL
00083 
00084 #if defined (__ACE_INLINE__)
00085 #include "ace/Env_Value_T.inl"
00086 #endif /* __ACE_INLINE__ */
00087 
00088 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00089 #include "ace/Env_Value_T.cpp"
00090 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00091 
00092 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00093 
00094 template <> inline void
00095 ACE_Convert (const ACE_TCHAR *s, ACE_TCHAR *&v)
00096 {
00097   v = (ACE_TCHAR *) s;
00098 }
00099 
00100 template <> inline void
00101 ACE_Convert (const ACE_TCHAR *s, const ACE_TCHAR *&v)
00102 {
00103   v = (const ACE_TCHAR *) s;
00104 }
00105 
00106 template <> inline void
00107 ACE_Convert (const ACE_TCHAR *s, short &si)
00108 {
00109   si = static_cast<short> (ACE_OS::strtol (s, 0, 10));
00110 }
00111 
00112 template <> inline void
00113 ACE_Convert (const ACE_TCHAR *s, u_short &us)
00114 {
00115   us = static_cast <u_short> (ACE_OS::strtol (s, 0, 10));
00116 }
00117 
00118 template <> inline void
00119 ACE_Convert (const ACE_TCHAR *s, u_int &i)
00120 {
00121   i = static_cast<u_int> (ACE_OS::strtol (s, 0, 10));
00122 }
00123 
00124 template <> inline void
00125 ACE_Convert (const ACE_TCHAR *s, long &l)
00126 {
00127   l = ACE_OS::strtol (s, 0, 10);
00128 }
00129 
00130 template <> inline void
00131 ACE_Convert (const ACE_TCHAR *s, int &i)
00132 {
00133   i = static_cast<int> (ACE_OS::strtol (s, 0, 10));
00134 }
00135 
00136 template <> inline void
00137 ACE_Convert (const ACE_TCHAR *s, u_long &ul)
00138 {
00139   ul = ACE_OS::strtoul (s, 0, 10);
00140 }
00141 
00142 template <> inline void
00143 ACE_Convert (const ACE_TCHAR *s, double &d)
00144 {
00145   d = ACE_OS::strtod (s, 0);
00146 }
00147 
00148 // Default calls a CTOR on type T of the form 'T::T(const char*)', but
00149 // users can feel free to create their own specialized conversion
00150 // functions if necessary, as shown above.  Note that for 'char*' the
00151 // default is used because a simple cast will be performed and no
00152 // conversion will be necessary.
00153 template <class T> inline void
00154 ACE_Convert (const ACE_TCHAR *s, T &t)
00155 {
00156   t = T (s);
00157 }
00158 
00159 ACE_END_VERSIONED_NAMESPACE_DECL
00160 
00161 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00162 #pragma implementation ("Env_Value_T.cpp")
00163 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00164 
00165 #include /**/ "ace/post.h"
00166 #endif /* ACE_ENV_VALUE_T_H */

Generated on Thu Nov 9 09:41:50 2006 for ACE by doxygen 1.3.6