[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / DLL.cpp
blobcad251100323b097d9c2c082e8d1ce5cbce28ca3
1 // $Id: DLL.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/DLL.h"
5 #include "ace/Log_Msg.h"
6 #include "ace/ACE.h"
7 #include "ace/DLL_Manager.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_dlfcn.h"
10 #include "ace/OS_NS_Thread.h"
12 #include <algorithm>
14 ACE_RCSID(ace, DLL, "$Id: DLL.cpp 80826 2008-03-04 14:51:23Z wotte $")
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
18 // Default constructor. Also, by default, the object will be closed
19 // before it is destroyed.
21 ACE_DLL::ACE_DLL (bool close_handle_on_destruction)
22 : open_mode_ (0),
23 dll_name_ (0),
24 close_handle_on_destruction_ (close_handle_on_destruction),
25 dll_handle_ (0),
26 error_ (0)
28 ACE_TRACE ("ACE_DLL::ACE_DLL (int)");
31 ACE_DLL::ACE_DLL (const ACE_DLL &rhs)
32 : open_mode_ (0),
33 dll_name_ (0),
34 close_handle_on_destruction_ (false),
35 dll_handle_ (0),
36 error_ (0)
38 ACE_TRACE ("ACE_DLL::ACE_DLL (const ACE_DLL &)");
40 if (rhs.dll_name_
41 // This will automatically up the refcount.
42 && this->open (rhs.dll_name_,
43 rhs.open_mode_,
44 rhs.close_handle_on_destruction_) != 0
45 && ACE::debug ())
46 ACE_ERROR ((LM_ERROR,
47 ACE_TEXT ("ACE_DLL::copy_ctor: error: %s\n"),
48 this->error ()));
51 // Assignment operator
53 ACE_DLL &
54 ACE_DLL::operator= (const ACE_DLL &rhs)
56 ACE_TRACE ("ACE_DLL::operator= (const ACE_DLL &)");
58 ACE_DLL tmp (rhs);
60 std::swap (this->open_mode_, tmp.open_mode_);
61 std::swap (this->dll_name_, tmp.dll_name_);
62 std::swap (this->close_handle_on_destruction_,
63 tmp.close_handle_on_destruction_);
64 std::swap (this->dll_handle_, tmp.dll_handle_);
65 std::swap (this->error_, tmp.error_);
67 return *this;
71 // If the library name and the opening mode are specified than on
72 // object creation the library is implicitly opened.
74 ACE_DLL::ACE_DLL (const ACE_TCHAR *dll_name,
75 int open_mode,
76 bool close_handle_on_destruction)
77 : open_mode_ (open_mode),
78 dll_name_ (0),
79 close_handle_on_destruction_ (close_handle_on_destruction),
80 dll_handle_ (0),
81 error_ (0)
83 ACE_TRACE ("ACE_DLL::ACE_DLL");
85 if (this->open (dll_name, this->open_mode_, close_handle_on_destruction) != 0
86 && ACE::debug ())
87 ACE_ERROR ((LM_ERROR,
88 ACE_TEXT ("ACE_DLL::open: error calling open: %s\n"),
89 this->error ()));
92 // The library is closed before the class gets destroyed depending on
93 // the close_handle_on_destruction value specified which is stored in
94 // close_handle_on_destruction_.
96 ACE_DLL::~ACE_DLL (void)
98 ACE_TRACE ("ACE_DLL::~ACE_DLL");
100 this->close ();
102 // Normally delete()d in ACE_DLL::close(). However, that may not
103 // occur if full ACE_DLL initialization is interrupted due to errors
104 // (e.g. attempting to open a DSO/DLL that does not exist). Make
105 // sure this->dll_name_ is deallocated.
106 delete [] this->dll_name_;
109 // This method opens the library based on the mode specified using the
110 // ACE_SHLIB_HANDLE which is obtained on making the ACE_OS::dlopen call.
111 // The default mode is:
112 // RTLD_LAZY Only references to data symbols are relocate when the
113 // object is first loaded.
114 // The other modes include:
115 // RTLD_NOW All necessary relocations are performed when the
116 // object is first loaded.
117 // RTLD_GLOBAL The object symbols are made available for the
118 // relocation processing of any other object.
121 ACE_DLL::open (const ACE_TCHAR *dll_filename,
122 int open_mode,
123 bool close_handle_on_destruction)
125 ACE_TRACE ("ACE_DLL::open");
127 return open_i (dll_filename, open_mode, close_handle_on_destruction);
131 ACE_DLL::open_i (const ACE_TCHAR *dll_filename,
132 int open_mode,
133 bool close_handle_on_destruction,
134 ACE_SHLIB_HANDLE handle)
136 ACE_TRACE ("ACE_DLL::open_i");
138 this->error_ = 0;
140 if (!dll_filename)
142 if (ACE::debug ())
143 ACE_ERROR ((LM_ERROR,
144 ACE_TEXT ("ACE_DLL::open_i: dll_name is %s\n"),
145 this->dll_name_ == 0 ? ACE_TEXT ("(null)")
146 : this->dll_name_));
147 return -1;
150 if (this->dll_handle_)
152 // If we have a good handle and its the same name, just return.
153 if (ACE_OS::strcmp (this->dll_name_, dll_filename) == 0)
154 return 0;
155 else
156 this->close ();
159 if (!this->dll_name_)
160 this->dll_name_ = ACE::strnew (dll_filename);
162 this->open_mode_ = open_mode;
163 this->close_handle_on_destruction_ = close_handle_on_destruction;
165 this->dll_handle_ = ACE_DLL_Manager::instance()->open_dll (this->dll_name_,
166 this->open_mode_,
167 handle);
169 if (!this->dll_handle_)
170 this->error_ = 1;
172 return this->error_ ? -1 : 0;
175 // The symbol refernce of the name specified is obtained.
177 void *
178 ACE_DLL::symbol (const ACE_TCHAR *sym_name, int ignore_errors)
180 ACE_TRACE ("ACE_DLL::symbol");
182 this->error_ = 0;
184 void *sym = 0;
185 if (this->dll_handle_)
186 sym = this->dll_handle_->symbol (sym_name, ignore_errors);
188 if (!sym)
189 this->error_ = 1;
191 return sym;
194 // The library is closed using the ACE_SHLIB_HANDLE object, i.e., the
195 // shared object is now disassociated form the current process.
198 ACE_DLL::close (void)
200 ACE_TRACE ("ACE_DLL::close");
202 int retval = 0;
204 if (this->dll_handle_
205 && this->close_handle_on_destruction_
206 && this->dll_name_
207 && (retval = ACE_DLL_Manager::instance ()->close_dll (this->dll_name_)) != 0)
208 this->error_ = 1;
210 // Even if close_dll() failed, go ahead and cleanup.
211 this->dll_handle_ = 0;
212 delete [] this->dll_name_;
213 this->dll_name_ = 0;
214 this->close_handle_on_destruction_ = false;
216 return retval;
219 // This method is used return the last error of a library operation.
221 ACE_TCHAR *
222 ACE_DLL::error (void) const
224 ACE_TRACE ("ACE_DLL::error");
225 if (this->error_)
227 return ACE_OS::dlerror ();
230 return 0;
233 // Return the handle to the user either temporarily or forever, thus
234 // orphaning it. If 0 means the user wants the handle forever and if 1
235 // means the user temporarily wants to take the handle.
237 ACE_SHLIB_HANDLE
238 ACE_DLL::get_handle (int become_owner) const
240 ACE_TRACE ("ACE_DLL::get_handle");
242 ACE_SHLIB_HANDLE handle = ACE_SHLIB_INVALID_HANDLE;
244 if (this->dll_handle_)
245 handle = this->dll_handle_->get_handle (become_owner);
247 return handle;
250 // Set the handle for the DLL. By default, the object will be closed
251 // before it is destroyed.
254 ACE_DLL::set_handle (ACE_SHLIB_HANDLE handle,
255 bool close_handle_on_destruction)
257 ACE_TRACE ("ACE_DLL::set_handle");
259 // Create a unique name. Note that this name is only quaranteed
260 // to be unique for the life of this object.
261 ACE_TCHAR temp[ACE_UNIQUE_NAME_LEN];
262 ACE_OS::unique_name (this, temp, ACE_UNIQUE_NAME_LEN);
264 return this->open_i (temp, 1, close_handle_on_destruction, handle);
267 ACE_END_VERSIONED_NAMESPACE_DECL