summaryrefslogtreecommitdiff
path: root/ACE/ace/Env_Value_T.h
blob: 6e97e3602d76a30a488988150f64c92ee7dd1f98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Env_Value_T.h
 *
 *  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)
 */
//=============================================================================

#ifndef ACE_ENV_VALUE_T_H
#define ACE_ENV_VALUE_T_H

#include /**/ "ace/pre.h"

#include /**/ "ace/config-all.h"
#include "ace/Global_Macros.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/Copy_Disabled.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

/**
 * @class ACE_Env_Value
 *
 * @brief Environment Variable Value
 *
 * Reads a variable from the user environment, providing a default
 * value.
 */
template <class T>
class ACE_Env_Value : private ACE_Copy_Disabled
{
public:
  /**
   * Default constructor which isn't bound to a specific environment
   * variable name or a default value.  Before being useful it must
   * open()'d.
   */
  ACE_Env_Value ();

  /// Constructor that calls open().
  ACE_Env_Value (const ACE_TCHAR *varname, const T &vardefault);

  /// Destroy the value.
  ~ACE_Env_Value ();

  /// Returns the value as type T.
  operator T ();

  /// The constructor, read @a varname from the environment, using
  /// @a defval as its value if it is not defined.
  void open (const ACE_TCHAR *varname, const T &defval);

  /// Returns the name of the variable being tracked.
  const ACE_TCHAR *varname () const;

private:
  void fetch_value ();

  const ACE_TCHAR *varname_;
  T value_;
};

/// Function to convert a string @a s into type @c T.
template <class T> void ACE_Convert (const ACE_TCHAR *s, T &t);

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "ace/Env_Value_T.inl"
#endif /* __ACE_INLINE__ */

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Env_Value_T.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

template <> inline void
ACE_Convert (const ACE_TCHAR *s, ACE_TCHAR *&v)
{
  v = (ACE_TCHAR *) s;
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, const ACE_TCHAR *&v)
{
  v = (const ACE_TCHAR *) s;
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, short &si)
{
  si = static_cast<short> (ACE_OS::strtol (s, 0, 10));
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, u_short &us)
{
  us = static_cast <u_short> (ACE_OS::strtol (s, 0, 10));
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, u_int &i)
{
  i = static_cast<u_int> (ACE_OS::strtol (s, 0, 10));
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, long &l)
{
  l = ACE_OS::strtol (s, 0, 10);
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, int &i)
{
  i = static_cast<int> (ACE_OS::strtol (s, 0, 10));
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, u_long &ul)
{
  ul = ACE_OS::strtoul (s, 0, 10);
}

template <> inline void
ACE_Convert (const ACE_TCHAR *s, double &d)
{
  d = ACE_OS::strtod (s, 0);
}

// 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 above.  Note that for 'char*' the
// default is used because a simple cast will be performed and no
// conversion will be necessary.
template <class T> inline void
ACE_Convert (const ACE_TCHAR *s, T &t)
{
  t = T (s);
}

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Env_Value_T.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#include /**/ "ace/post.h"
#endif /* ACE_ENV_VALUE_T_H */