diff options
author | Chris Cleeland <chris.cleeland@gmail.com> | 1997-12-31 22:37:40 +0000 |
---|---|---|
committer | Chris Cleeland <chris.cleeland@gmail.com> | 1997-12-31 22:37:40 +0000 |
commit | cfa8353c086d114acd8bd8bdff3ed3dba8e200b1 (patch) | |
tree | e15b6719d24d79df2127b9928a9d888f95851366 | |
parent | 07e05a5237293863f8e4712fd7a353495a342094 (diff) | |
download | ATCD-cfa8353c086d114acd8bd8bdff3ed3dba8e200b1.tar.gz |
* ace/Env_Value_T.*: Added a new template (ACE_Env_Value) which is
a typesafe encapsulation of environment variables with the ability
to provide a value to be used as a default if the variable isn't
set. An example of its use can be seen in the corresponding test,
tests/Env_Value_Test.cpp.
* tests/Env_Value_Test.cpp: Added test for the new template added
above.
* tests/Makefile: Added new Env_Value_Test test to the Makefile.
-rw-r--r-- | ChangeLog-98a | 17 | ||||
-rw-r--r-- | ace/Env_Value_T.cpp | 12 | ||||
-rw-r--r-- | ace/Env_Value_T.h | 85 | ||||
-rw-r--r-- | ace/Env_Value_T.i | 91 | ||||
-rw-r--r-- | ace/OS.h | 8 | ||||
-rw-r--r-- | ace/OS.i | 9 | ||||
-rw-r--r-- | tests/Env_Value_Test.cpp | 87 | ||||
-rw-r--r-- | tests/Makefile | 3 |
8 files changed, 307 insertions, 5 deletions
diff --git a/ChangeLog-98a b/ChangeLog-98a index 00d2da38f9b..b09c68946ea 100644 --- a/ChangeLog-98a +++ b/ChangeLog-98a @@ -1,3 +1,20 @@ +Wed Dec 31 16:25:16 1997 Chris Cleeland <cleeland@cs.wustl.edu> + + * ace/Env_Value_T.*: Added a new template (ACE_Env_Value) which is + a typesafe encapsulation of environment variables with the ability + to provide a value to be used as a default if the variable isn't + set. An example of its use can be seen in the corresponding test, + tests/Env_Value_Test.cpp. Thanks to Carlos O'Ryan for giving me + the seed that started this. + + * tests/Env_Value_Test.cpp: Added test for the new template added + above. + + * tests/Makefile: Added new Env_Value_Test test to the Makefile. + + * ace/OS.{h,i} (strtod): Added new function for converting strings + to doubles. + Wed Dec 31 14:56:06 1997 Douglas C. Schmidt <schmidt@flamenco.cs.wustl.edu> * examples/Reactor/Dgram/: Enhanced the examples to make sure they diff --git a/ace/Env_Value_T.cpp b/ace/Env_Value_T.cpp new file mode 100644 index 00000000000..25a2efed4d1 --- /dev/null +++ b/ace/Env_Value_T.cpp @@ -0,0 +1,12 @@ +// $Id$ + +#if ! defined (__ACE_INLINE__) +#include "ace/Env_Value_T.i" +#endif /* __ACE_INLINE__ */ + +#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) +template void ACE_Convert(const char*, char*&); +#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA) +#pragma instantiate void ACE_Convert (const char*, char*&) +#endif + diff --git a/ace/Env_Value_T.h b/ace/Env_Value_T.h new file mode 100644 index 00000000000..dbce975361e --- /dev/null +++ b/ace/Env_Value_T.h @@ -0,0 +1,85 @@ +// This may look like C, but it's really -*- C++ -*- +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// ACE +// +// = DESCRIPTION +// Template to encapsulate getting a value from an environment variable +// and using a supplied default value if not in the environment. +// +// = AUTHOR +// Chris Cleeland (derived from work by Carlos O'Ryan) +// +// ============================================================================ + +#if !defined (ACE_ENV_VALUE_T_H) +# define ACE_ENV_VALUE_T_H + +template <class T> +class ACE_Env_Value +// +// = TITLE +// Enviroment Variable Value +// +// = DESCRIPTION +// Reads a variable from the user enviroment, providing a default +// value. +// +// = AUTHOR +// Chris Cleeland, Carlos O'Ryan +// +{ +public: + ACE_Env_Value (void); + // Default constructor which isn't bound to a specific environment + // variable name or a default value. Before being useful it must + // <open()>ed. + + ACE_Env_Value (const char* varname, + const T& vardefault); + // Constructor which calls <open()>. + + ~ACE_Env_Value (void); + // Destroy the value. + + operator const T (void) const; + // Returns the value as type T. + + void open (const char* varname, const T& defval); + // The constructor, read <varname> from the enviroment, using + // <vardefault> as its value if it is not defined. + + const char* varname (void) const; + // Returns the name of the variable being tracked. + +private: + ACE_UNIMPLEMENTED_FUNC(ACE_Env_Value (const ACE_Env_Value&)) + ACE_UNIMPLEMENTED_FUNC(ACE_Env_Value& operator= (const ACE_Env_Value&)) + // Disallow copying and assignment. + +private: + void fetch_value (void); + + const char* varname_; + T value_; +}; + +template <class T> void ACE_Convert (const char* s, T& t); +// Function to convert a string <s> into type <T>. + +#if defined (__ACE_INLINE__) +#include "ace/Env_Value_T.i" +#endif /* __ACE_INLINE__ */ + +#if defined (ACE_TEMPLATES_REQUIRE_SOURCE) +#include "ace/Env_Value_T.cpp" +#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ + +#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) +#pragma implementation ("Env_Value_T.cpp") +#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ + +#endif diff --git a/ace/Env_Value_T.i b/ace/Env_Value_T.i new file mode 100644 index 00000000000..0770f86c79c --- /dev/null +++ b/ace/Env_Value_T.i @@ -0,0 +1,91 @@ +// $Id$ + +template <class T> ACE_INLINE +ACE_Env_Value<T>::operator const T (void) const +{ + return value_; +} + +template <class T> ACE_INLINE +ACE_Env_Value<T>::ACE_Env_Value (void) + : varname_ (0) +{} + +template <class T> ACE_INLINE +ACE_Env_Value<T>::ACE_Env_Value (const char* varname, const T& defval) + : varname_ (varname), value_(defval) +{ + this->fetch_value (); +} + + +template <class T> ACE_INLINE void +ACE_Env_Value<T>::open (const char* varname, const T& defval) +{ + varname_ = varname; + value_ = defval; + this->fetch_value (); +} + +template <class T> ACE_INLINE void +ACE_Env_Value<T>::fetch_value () +{ + const char* env = ACE_OS::getenv (varname_); + if (env != 0) + ACE_Convert (env, value_); +} + + +template <class T> ACE_INLINE +ACE_Env_Value<T>::~ACE_Env_Value (void) +{ +} + + +// Default calls a CTOR on type T of the form 'T::T(const char*)', but +// users can feel free to create their own specialized conversion +// functions if necessary, as shown below. Note that for 'char*' the +// default is used because a simple cast will be performed and no +// conversion will be necessary. + +template <class T> ACE_INLINE void +ACE_Convert (const char* s, T& t) +{ + t = T(s); +} + +ACE_INLINE void +ACE_Convert (const char* s, char*& v) +{ + v = (char*)s; +} + +ACE_INLINE void +ACE_Convert (const char* s, short& si) +{ + si = ACE_OS::strtol (s, 0, 10); +} + +ACE_INLINE void +ACE_Convert (const char* s, long& l) +{ + l = ACE_OS::strtol (s, 0, 10); +} + +ACE_INLINE void +ACE_Convert (const char* s, int& i) +{ + i = ACE_OS::strtol (s, 0, 10); +} + +ACE_INLINE void +ACE_Convert (const char* s, unsigned long& ul) +{ + ul = ACE_OS::strtoul (s, 0, 10); +} + +ACE_INLINE void +ACE_Convert (const char* s, double& d) +{ + d = ACE_OS::strtod (s, 0); +} @@ -4533,9 +4533,11 @@ public: static long strtol (const char *s, char **ptr, int base); - static u_long strtoul(const char *s, - char **ptr, - int base); + static u_long strtoul (const char *s, + char **ptr, + int base); + static double strtod (const char *s, + char **endptr); #if !defined (ACE_HAS_WCHAR_TYPEDEFS_CHAR) // = These go here since they are needed for TAO. @@ -1259,10 +1259,17 @@ ACE_OS::strtol (const char *s, char **ptr, int base) ACE_INLINE unsigned long ACE_OS::strtoul (const char *s, char **ptr, int base) { - // ACE_TRACE ("ACE_OS::strtol"); + // ACE_TRACE ("ACE_OS::strtoul"); return ::strtoul (s, ptr, base); } +ACE_INLINE double +ACE_OS::strtod (const char *s, char **endptr) +{ + // ACE_TRACE ("ACE_OS::strtod"); + return ::strtod (s, endptr); +} + ACE_INLINE long ACE_OS::sysconf (int name) { diff --git a/tests/Env_Value_Test.cpp b/tests/Env_Value_Test.cpp new file mode 100644 index 00000000000..13b51ec65df --- /dev/null +++ b/tests/Env_Value_Test.cpp @@ -0,0 +1,87 @@ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// tests +// +// = DESCRIPTION +// This is a simple test to make sure the Env_Value template is +// working correctly. +// +// = AUTHOR +// Chris Cleeland +// +// ============================================================================ + +#include "test_config.h" +#include "ace/OS.h" +#include "ace/Process.h" +#include "ace/Env_Value_T.h" + +int +main (int argc, char *argv[], char* environ[]) +{ + if (argc == 1) + { + // No arguments means we're the initial test + ACE_Process_Options options (1); + options.setenv (environ); + + char* const* envargv; + + options.command_line ("Env_Value_Test run_as_test"); + + options.setenv("TEST_VALUE_POSITIVE", "%s", "10.2"); + options.setenv("TEST_VALUE_NEGATIVE", "%s", "-10.2"); + + ACE_Process p; + ACE_ASSERT (p.spawn (options) != -1); + p.wait (); + } + else + { + // In this case we're the child + ACE_START_TEST ("Env_Value"); + +#define TEST_THIS(type,varname,defval,expval) \ + do { \ + ACE_Env_Value<type> val(varname, (defval));\ + ACE_ASSERT (val == (expval)); \ + } while (0) + + ; + + TEST_THIS (int, "TEST_VALUE_POSITIVE", 4, 10); + TEST_THIS (double, "TEST_VALUE_POSITIVE", -1.0, 10.2); + TEST_THIS (unsigned long, "TEST_VALUE_POSITIVE", 0, 10); + TEST_THIS (short, "TEST_VALUE_POSITIVE", 0, 10); + + TEST_THIS (int, "TEST_VALUE_NEGATIVE", 4, -10); + TEST_THIS (double, "TEST_VALUE_NEGATIVE", -1.0, -10.2); + TEST_THIS (unsigned long, "TEST_VALUE_NEGATIVE", 0, -10); + TEST_THIS (short, "TEST_VALUE_NEGATIVE", 0, -10); + + char* defstr = "Sarah Cleeland is Two!"; + ACE_Env_Value<char*> sval("This_Shouldnt_Be_Set_Hopefully", defstr); + ACE_ASSERT (ACE_OS::strcmp (sval, defstr) == 0); + + ACE_END_TEST; + } + + return 0; +} + +#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) +template class ACE_Env_Value<int>; +template class ACE_Env_Value<double>; +template class ACE_Env_Value<unsigned long>; +template class ACE_Env_Value<short>; +template class ACE_Env_Value<char*>; +#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA) +#pragma instantiate ACE_Env_Value<int> +#pragma instantiate ACE_Env_Value<double> +#pragma instantiate ACE_Env_Value<unsigned long> +#pragma instantiate ACE_Env_Value<short> +#pragma instantiate ACE_Env_Value<char*> +#endif diff --git a/tests/Makefile b/tests/Makefile index 56a8b1fc9a3..3a508f65f13 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -9,7 +9,8 @@ # Local macros #---------------------------------------------------------------------------- -BIN = Atomic_Op_Test \ +BIN = Env_Value_Test \ + Atomic_Op_Test \ Barrier_Test \ Basic_Types_Test \ Buffer_Stream_Test \ |