diff options
author | Marc Laukien <marc@zeroc.com> | 2002-09-22 15:57:38 +0000 |
---|---|---|
committer | Marc Laukien <marc@zeroc.com> | 2002-09-22 15:57:38 +0000 |
commit | dff71be00b07d293b318fede3167b3c48f2270b8 (patch) | |
tree | 35e0236914020cd396dca7675119fb3c435693ac /cpp | |
parent | thread fixes (diff) | |
download | ice-dff71be00b07d293b318fede3167b3c48f2270b8.tar.bz2 ice-dff71be00b07d293b318fede3167b3c48f2270b8.tar.xz ice-dff71be00b07d293b318fede3167b3c48f2270b8.zip |
dos2unix
Diffstat (limited to 'cpp')
32 files changed, 5879 insertions, 5879 deletions
diff --git a/cpp/include/IceUtil/Base64.h b/cpp/include/IceUtil/Base64.h index 3e1dc03e8ee..a4563e4cb8e 100644 --- a/cpp/include/IceUtil/Base64.h +++ b/cpp/include/IceUtil/Base64.h @@ -1,38 +1,38 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_BASE_64_H
-#define ICE_UTIL_BASE_64_H
-
-#include <IceUtil/Config.h>
-#include <string>
-#include <vector>
-
-namespace IceUtil
-{
-
-class ICE_UTIL_API Base64
-{
-public:
-
- static std::string encode(const std::vector<char>&);
- static std::vector<char> decode(const std::string&);
-
-
-private:
-
- static char encode(unsigned char);
- static unsigned char decode(char);
- static bool isBase64(char);
-};
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_BASE_64_H +#define ICE_UTIL_BASE_64_H + +#include <IceUtil/Config.h> +#include <string> +#include <vector> + +namespace IceUtil +{ + +class ICE_UTIL_API Base64 +{ +public: + + static std::string encode(const std::vector<char>&); + static std::vector<char> decode(const std::string&); + + +private: + + static char encode(unsigned char); + static unsigned char decode(char); + static bool isBase64(char); +}; + +} + +#endif diff --git a/cpp/include/IceUtil/Cond.h b/cpp/include/IceUtil/Cond.h index c9ad5036ae3..88647bc4d3d 100644 --- a/cpp/include/IceUtil/Cond.h +++ b/cpp/include/IceUtil/Cond.h @@ -1,334 +1,334 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_COND_H
-#define ICE_UTIL_COND_H
-
-#include <IceUtil/Config.h>
-#include <IceUtil/Time.h>
-#include <IceUtil/ThreadException.h>
-
-#ifdef _WIN32
-//
-// Needed for implementation under WIN32.
-//
-# include <IceUtil/Mutex.h>
-//
-// See member-template note for waitImpl & timedWaitImpl.
-//
-# include <IceUtil/RecMutex.h>
-#endif
-
-namespace IceUtil
-{
-
-//
-// Forward declaration (for friend declarations).
-//
-template <class T> class Monitor;
-class RecMutex;
-class Mutex;
-
-#ifdef _WIN32
-//
-// Needed for implementation.
-//
-class Semaphore
-{
-public:
-
- Semaphore(long = 0);
- ~Semaphore();
-
- void wait() const;
- bool timedWait(const Time&) const;
- void post(int = 1) const;
-
-private:
-
- mutable HANDLE _sem;
-};
-#endif
-
-//
-// Condition variable implementation. Conforms to the same semantics
-// as a POSIX threads condition variable.
-//
-class ICE_UTIL_API Cond : public noncopyable
-{
-public:
-
- Cond();
- ~Cond();
-
- //
- // signal restarts one of the threads that are waiting on the
- // condition variable cond. If no threads are waiting on cond,
- // nothing happens. If several threads are waiting on cond,
- // exactly one is restarted, but it is not specified which.
- //
- void signal();
-
- //
- // broadcast restarts all the threads that are waiting on the
- // condition variable cond. Nothing happens if no threads are
- // waiting on cond.
- //
- void broadcast();
-
- //
- // MSVC doesn't support out-of-class definitions of member
- // templates. See KB Article Q241949 for details.
- //
-
- //
- // wait atomically unlocks the mutex and waits for the condition
- // variable to be signaled. Before returning to the calling thread
- // the mutex is reaquired.
- //
- template <typename Lock> inline void
- wait(const Lock& lock) const
- {
- waitImpl(lock._mutex);
- }
-
- //
- // wait atomically unlocks the mutex and waits for the condition
- // variable to be signaled for up to the given timeout. Before
- // returning to the calling thread the mutex is reaquired. Returns
- // true if the condition variable was signaled, false on a
- // timeout.
- //
- template <typename Lock> inline bool
- timedWait(const Lock& lock, const Time& timeout) const
- {
- return timedWaitImpl(lock._mutex, timeout);
- }
-
-private:
-
- friend class Monitor<Mutex>;
- friend class Monitor<RecMutex>;
-
- //
- // The Monitor implementation uses waitImpl & timedWaitImpl.
- //
-#ifdef _WIN32
-
- //
- // For some reason under WIN32 with VC6 using a member-template
- // for waitImpl & timedWaitImpl results in a link error for
- // RecMutex.
- //
-/*
- template <typename M> void
- waitImpl(const M& mutex) const
- {
- preWait();
-
- typedef typename M::LockState LockState;
-
- LockState state;
- mutex.unlock(state);
-
- try
- {
- dowait(-1);
- mutex.lock(state);
- }
- catch(...)
- {
- mutex.lock(state);
- throw;
- }
- }
- template <typename M> bool
- timedWaitImpl(const M& mutex, const Time& timeout) const
- {
- preWait();
-
- typedef typename M::LockState LockState;
-
- LockState state;
- mutex.unlock(state);
-
- try
- {
- bool rc = dowait(timeout);
- mutex.lock(state);
- return rc;
- }
- catch(...)
- {
- mutex.lock(state);
- throw;
- }
- }
- */
-
- void
- waitImpl(const RecMutex& mutex) const
- {
- preWait();
-
- RecMutex::LockState state;
- mutex.unlock(state);
-
- try
- {
- dowait();
- mutex.lock(state);
- }
- catch(...)
- {
- mutex.lock(state);
- throw;
- }
- }
-
- void
- waitImpl(const Mutex& mutex) const
- {
- preWait();
-
- Mutex::LockState state;
- mutex.unlock(state);
-
- try
- {
- dowait();
- mutex.lock(state);
- }
- catch(...)
- {
- mutex.lock(state);
- throw;
- }
- }
-
- bool
- timedWaitImpl(const RecMutex& mutex, const Time& timeout) const
- {
- preWait();
-
- RecMutex::LockState state;
- mutex.unlock(state);
-
- try
- {
- bool rc = timedDowait(timeout);
- mutex.lock(state);
- return rc;
- }
- catch(...)
- {
- mutex.lock(state);
- throw;
- }
- }
-
- bool
- timedWaitImpl(const Mutex& mutex, const Time& timeout) const
- {
- preWait();
-
- Mutex::LockState state;
- mutex.unlock(state);
-
- try
- {
- bool rc = timedDowait(timeout);
- mutex.lock(state);
- return rc;
- }
- catch(...)
- {
- mutex.lock(state);
- throw;
- }
- }
-
-#else
-
- template <typename M> void waitImpl(const M&) const;
- template <typename M> bool timedWaitImpl(const M&, const Time&) const;
-
-#endif
-
-#ifdef _WIN32
- void wake(bool);
- void preWait() const;
- void postWait(bool) const;
- bool timedDowait(const Time&) const;
- void dowait() const;
-
- Mutex _internal;
- Semaphore _gate;
- Semaphore _queue;
- mutable long _blocked;
- mutable long _unblocked;
- mutable long _toUnblock;
-#else
- mutable pthread_cond_t _cond;
-#endif
-
-};
-
-#ifndef _WIN32
-template <typename M> inline void
-Cond::waitImpl(const M& mutex) const
-{
- typedef typename M::LockState LockState;
-
- LockState state;
- mutex.unlock(state);
- int rc = pthread_cond_wait(&_cond, state.mutex);
- mutex.lock(state);
-
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
-}
-
-template <typename M> inline bool
-Cond::timedWaitImpl(const M& mutex, const Time& timeout) const
-{
- typedef typename M::LockState LockState;
-
- LockState state;
- mutex.unlock(state);
-
- timeval tv = Time::now() + timeout;
- timespec ts;
- ts.tv_sec = tv.tv_sec;
- ts.tv_nsec = tv.tv_usec*1000;
- int rc = pthread_cond_timedwait(&_cond, state.mutex, &ts);
- mutex.lock(state);
-
- if(rc != 0)
- {
- //
- // pthread_cond_timedwait returns ETIMEOUT in the event of a
- // timeout.
- //
- if(rc != ETIMEDOUT)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
- return false;
- }
- return true;
-}
-#endif
-
-} // End namespace IceUtil
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_COND_H +#define ICE_UTIL_COND_H + +#include <IceUtil/Config.h> +#include <IceUtil/Time.h> +#include <IceUtil/ThreadException.h> + +#ifdef _WIN32 +// +// Needed for implementation under WIN32. +// +# include <IceUtil/Mutex.h> +// +// See member-template note for waitImpl & timedWaitImpl. +// +# include <IceUtil/RecMutex.h> +#endif + +namespace IceUtil +{ + +// +// Forward declaration (for friend declarations). +// +template <class T> class Monitor; +class RecMutex; +class Mutex; + +#ifdef _WIN32 +// +// Needed for implementation. +// +class Semaphore +{ +public: + + Semaphore(long = 0); + ~Semaphore(); + + void wait() const; + bool timedWait(const Time&) const; + void post(int = 1) const; + +private: + + mutable HANDLE _sem; +}; +#endif + +// +// Condition variable implementation. Conforms to the same semantics +// as a POSIX threads condition variable. +// +class ICE_UTIL_API Cond : public noncopyable +{ +public: + + Cond(); + ~Cond(); + + // + // signal restarts one of the threads that are waiting on the + // condition variable cond. If no threads are waiting on cond, + // nothing happens. If several threads are waiting on cond, + // exactly one is restarted, but it is not specified which. + // + void signal(); + + // + // broadcast restarts all the threads that are waiting on the + // condition variable cond. Nothing happens if no threads are + // waiting on cond. + // + void broadcast(); + + // + // MSVC doesn't support out-of-class definitions of member + // templates. See KB Article Q241949 for details. + // + + // + // wait atomically unlocks the mutex and waits for the condition + // variable to be signaled. Before returning to the calling thread + // the mutex is reaquired. + // + template <typename Lock> inline void + wait(const Lock& lock) const + { + waitImpl(lock._mutex); + } + + // + // wait atomically unlocks the mutex and waits for the condition + // variable to be signaled for up to the given timeout. Before + // returning to the calling thread the mutex is reaquired. Returns + // true if the condition variable was signaled, false on a + // timeout. + // + template <typename Lock> inline bool + timedWait(const Lock& lock, const Time& timeout) const + { + return timedWaitImpl(lock._mutex, timeout); + } + +private: + + friend class Monitor<Mutex>; + friend class Monitor<RecMutex>; + + // + // The Monitor implementation uses waitImpl & timedWaitImpl. + // +#ifdef _WIN32 + + // + // For some reason under WIN32 with VC6 using a member-template + // for waitImpl & timedWaitImpl results in a link error for + // RecMutex. + // +/* + template <typename M> void + waitImpl(const M& mutex) const + { + preWait(); + + typedef typename M::LockState LockState; + + LockState state; + mutex.unlock(state); + + try + { + dowait(-1); + mutex.lock(state); + } + catch(...) + { + mutex.lock(state); + throw; + } + } + template <typename M> bool + timedWaitImpl(const M& mutex, const Time& timeout) const + { + preWait(); + + typedef typename M::LockState LockState; + + LockState state; + mutex.unlock(state); + + try + { + bool rc = dowait(timeout); + mutex.lock(state); + return rc; + } + catch(...) + { + mutex.lock(state); + throw; + } + } + */ + + void + waitImpl(const RecMutex& mutex) const + { + preWait(); + + RecMutex::LockState state; + mutex.unlock(state); + + try + { + dowait(); + mutex.lock(state); + } + catch(...) + { + mutex.lock(state); + throw; + } + } + + void + waitImpl(const Mutex& mutex) const + { + preWait(); + + Mutex::LockState state; + mutex.unlock(state); + + try + { + dowait(); + mutex.lock(state); + } + catch(...) + { + mutex.lock(state); + throw; + } + } + + bool + timedWaitImpl(const RecMutex& mutex, const Time& timeout) const + { + preWait(); + + RecMutex::LockState state; + mutex.unlock(state); + + try + { + bool rc = timedDowait(timeout); + mutex.lock(state); + return rc; + } + catch(...) + { + mutex.lock(state); + throw; + } + } + + bool + timedWaitImpl(const Mutex& mutex, const Time& timeout) const + { + preWait(); + + Mutex::LockState state; + mutex.unlock(state); + + try + { + bool rc = timedDowait(timeout); + mutex.lock(state); + return rc; + } + catch(...) + { + mutex.lock(state); + throw; + } + } + +#else + + template <typename M> void waitImpl(const M&) const; + template <typename M> bool timedWaitImpl(const M&, const Time&) const; + +#endif + +#ifdef _WIN32 + void wake(bool); + void preWait() const; + void postWait(bool) const; + bool timedDowait(const Time&) const; + void dowait() const; + + Mutex _internal; + Semaphore _gate; + Semaphore _queue; + mutable long _blocked; + mutable long _unblocked; + mutable long _toUnblock; +#else + mutable pthread_cond_t _cond; +#endif + +}; + +#ifndef _WIN32 +template <typename M> inline void +Cond::waitImpl(const M& mutex) const +{ + typedef typename M::LockState LockState; + + LockState state; + mutex.unlock(state); + int rc = pthread_cond_wait(&_cond, state.mutex); + mutex.lock(state); + + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } +} + +template <typename M> inline bool +Cond::timedWaitImpl(const M& mutex, const Time& timeout) const +{ + typedef typename M::LockState LockState; + + LockState state; + mutex.unlock(state); + + timeval tv = Time::now() + timeout; + timespec ts; + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec*1000; + int rc = pthread_cond_timedwait(&_cond, state.mutex, &ts); + mutex.lock(state); + + if(rc != 0) + { + // + // pthread_cond_timedwait returns ETIMEOUT in the event of a + // timeout. + // + if(rc != ETIMEDOUT) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } + return false; + } + return true; +} +#endif + +} // End namespace IceUtil + +#endif diff --git a/cpp/include/IceUtil/Config.h b/cpp/include/IceUtil/Config.h index 81c63eb07a6..d555854f10c 100644 --- a/cpp/include/IceUtil/Config.h +++ b/cpp/include/IceUtil/Config.h @@ -1,144 +1,144 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_CONFIG_H
-#define ICE_UTIL_CONFIG_H
-
-//
-// For STLport. If we compile in debug mode, we want to use the debug
-// STLport library. This is done by setting _STLP_DEBUG before any
-// STLport header files are included.
-//
-# if !defined(NDEBUG) && !defined(_STLP_DEBUG)
-# define _STLP_DEBUG
-# endif
-
-//
-// For STLport. If we compile in debug mode, we want to use the debug
-// memory allocation, so that purify doesn't report bogus memory
-// leaks. This is done by setting _STLP_USE_MALLOC before any STLport
-// header files are included.
-//
-# if !defined(NDEBUG) && !defined(_STLP_USE_MALLOC)
-# define _STLP_USE_MALLOC
-# endif
-
-#if defined(_WIN32)
-
-// Necessary for TryEnterCriticalSection.
-# define _WIN32_WINNT 0x0400
-
-# if !defined(_UNICODE)
-# error "Only unicode libraries can be used with Ice!"
-# endif
-
-# if !defined(_DLL) || !defined(_MT)
-# error "Only multi-threaded DLL libraries can be used with Ice!"
-# endif
-
-# ifdef ICE_UTIL_API_EXPORTS
-# define ICE_UTIL_API __declspec(dllexport)
-# else
-# define ICE_UTIL_API __declspec(dllimport)
-# endif
-
-# include <windows.h>
-
-// '...' : forcing value to bool 'true' or 'false' (performance warning)
-# pragma warning( disable : 4800 )
-// ... identifier was truncated to '255' characters in the debug information
-# pragma warning( disable : 4786 )
-// 'this' : used in base member initializer list
-# pragma warning( disable : 4355 )
-// class ... needs to have dll-interface to be used by clients of class ...
-# pragma warning( disable : 4251 )
-// ... : inherits ... via dominance
-# pragma warning( disable : 4250 )
-// non dll-interface class ... used as base for dll-interface class ...
-# pragma warning( disable : 4275 )
-// ...: decorated name length exceeded, name was truncated
-# pragma warning( disable : 4503 )
-
-# define SIZEOF_WCHAR_T 2
-
-#elif defined(__linux__) && defined(i386)
-
-# define ICE_UTIL_API /**/
-# define HAVE_READLINE
-# define SIZEOF_WCHAR_T 4
-
-#else
-
-# error "unsupported operating system or platform"
-
-#endif
-
-//
-// Some include files we need almost everywhere
-//
-#include <cassert>
-#include <iostream>
-#include <sstream>
-
-#ifndef _WIN32
-# ifndef _REENTRANT
-# define _REENTRANT 1
-# endif
-# include <pthread.h>
-# include <errno.h>
-#endif
-
-//
-// By deriving from this class, other classes are made non-copyable
-//
-namespace IceUtil
-{
-
-//
-// TODO: Constructor and destructor should not be inlined, as they are
-// not performance critical.
-//
-// TODO: Naming conventions?
-//
-class noncopyable
-{
-protected:
-
- noncopyable() { }
- ~noncopyable() { } // May not be virtual! Classes without virtual operations also derive from noncopyable.
-
-private:
-
- noncopyable(const noncopyable&);
- const noncopyable& operator=(const noncopyable&);
-};
-//
-//
-// Some definitions for 64-bit integers
-//
-#if defined(_WIN32)
- typedef __int64 Int64;
- const Int64 Int64Min = -9223372036854775808i64;
- const Int64 Int64Max = 9223372036854775807i64;
-#elif defined(__linux__) && defined(i386)
- typedef long long Int64;
- const Int64 Int64Min = LONGLONG_MIN;
- const Int64 Int64Max = LONGLONG_MAX;
-#endif
-
-}
-
-//
-// The Ice version
-//
-#define ICE_STRING_VERSION "0.0.1"
-#define ICE_INT_VERSION 0x00000001
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_CONFIG_H +#define ICE_UTIL_CONFIG_H + +// +// For STLport. If we compile in debug mode, we want to use the debug +// STLport library. This is done by setting _STLP_DEBUG before any +// STLport header files are included. +// +# if !defined(NDEBUG) && !defined(_STLP_DEBUG) +# define _STLP_DEBUG +# endif + +// +// For STLport. If we compile in debug mode, we want to use the debug +// memory allocation, so that purify doesn't report bogus memory +// leaks. This is done by setting _STLP_USE_MALLOC before any STLport +// header files are included. +// +# if !defined(NDEBUG) && !defined(_STLP_USE_MALLOC) +# define _STLP_USE_MALLOC +# endif + +#if defined(_WIN32) + +// Necessary for TryEnterCriticalSection. +# define _WIN32_WINNT 0x0400 + +# if !defined(_UNICODE) +# error "Only unicode libraries can be used with Ice!" +# endif + +# if !defined(_DLL) || !defined(_MT) +# error "Only multi-threaded DLL libraries can be used with Ice!" +# endif + +# ifdef ICE_UTIL_API_EXPORTS +# define ICE_UTIL_API __declspec(dllexport) +# else +# define ICE_UTIL_API __declspec(dllimport) +# endif + +# include <windows.h> + +// '...' : forcing value to bool 'true' or 'false' (performance warning) +# pragma warning( disable : 4800 ) +// ... identifier was truncated to '255' characters in the debug information +# pragma warning( disable : 4786 ) +// 'this' : used in base member initializer list +# pragma warning( disable : 4355 ) +// class ... needs to have dll-interface to be used by clients of class ... +# pragma warning( disable : 4251 ) +// ... : inherits ... via dominance +# pragma warning( disable : 4250 ) +// non dll-interface class ... used as base for dll-interface class ... +# pragma warning( disable : 4275 ) +// ...: decorated name length exceeded, name was truncated +# pragma warning( disable : 4503 ) + +# define SIZEOF_WCHAR_T 2 + +#elif defined(__linux__) && defined(i386) + +# define ICE_UTIL_API /**/ +# define HAVE_READLINE +# define SIZEOF_WCHAR_T 4 + +#else + +# error "unsupported operating system or platform" + +#endif + +// +// Some include files we need almost everywhere +// +#include <cassert> +#include <iostream> +#include <sstream> + +#ifndef _WIN32 +# ifndef _REENTRANT +# define _REENTRANT 1 +# endif +# include <pthread.h> +# include <errno.h> +#endif + +// +// By deriving from this class, other classes are made non-copyable +// +namespace IceUtil +{ + +// +// TODO: Constructor and destructor should not be inlined, as they are +// not performance critical. +// +// TODO: Naming conventions? +// +class noncopyable +{ +protected: + + noncopyable() { } + ~noncopyable() { } // May not be virtual! Classes without virtual operations also derive from noncopyable. + +private: + + noncopyable(const noncopyable&); + const noncopyable& operator=(const noncopyable&); +}; +// +// +// Some definitions for 64-bit integers +// +#if defined(_WIN32) + typedef __int64 Int64; + const Int64 Int64Min = -9223372036854775808i64; + const Int64 Int64Max = 9223372036854775807i64; +#elif defined(__linux__) && defined(i386) + typedef long long Int64; + const Int64 Int64Min = LONGLONG_MIN; + const Int64 Int64Max = LONGLONG_MAX; +#endif + +} + +// +// The Ice version +// +#define ICE_STRING_VERSION "0.0.1" +#define ICE_INT_VERSION 0x00000001 + +#endif diff --git a/cpp/include/IceUtil/Exception.h b/cpp/include/IceUtil/Exception.h index c694cceea23..ee544ec4138 100644 --- a/cpp/include/IceUtil/Exception.h +++ b/cpp/include/IceUtil/Exception.h @@ -1,53 +1,53 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_EXCEPTION_H
-#define ICE_UTIL_EXCEPTION_H
-
-#include <IceUtil/Config.h>
-
-namespace IceUtil
-{
-
-class ICE_UTIL_API Exception
-{
-public:
-
- Exception();
- Exception(const char*, int);
- virtual ~Exception();
- virtual std::string ice_name() const;
- virtual void ice_print(std::ostream&) const;
- virtual Exception* ice_clone() const;
- virtual void ice_throw() const;
- const char* ice_file() const;
- int ice_line() const;
-
-private:
-
- const char* _file;
- int _line;
-};
-
-ICE_UTIL_API std::ostream& operator<<(std::ostream&, const Exception&);
-
-class ICE_UTIL_API NullHandleException : public Exception
-{
-public:
-
- NullHandleException(const char*, int);
- virtual std::string ice_name() const;
- virtual Exception* ice_clone() const;
- virtual void ice_throw() const;
-};
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_EXCEPTION_H +#define ICE_UTIL_EXCEPTION_H + +#include <IceUtil/Config.h> + +namespace IceUtil +{ + +class ICE_UTIL_API Exception +{ +public: + + Exception(); + Exception(const char*, int); + virtual ~Exception(); + virtual std::string ice_name() const; + virtual void ice_print(std::ostream&) const; + virtual Exception* ice_clone() const; + virtual void ice_throw() const; + const char* ice_file() const; + int ice_line() const; + +private: + + const char* _file; + int _line; +}; + +ICE_UTIL_API std::ostream& operator<<(std::ostream&, const Exception&); + +class ICE_UTIL_API NullHandleException : public Exception +{ +public: + + NullHandleException(const char*, int); + virtual std::string ice_name() const; + virtual Exception* ice_clone() const; + virtual void ice_throw() const; +}; + +} + +#endif diff --git a/cpp/include/IceUtil/Functional.h b/cpp/include/IceUtil/Functional.h index 57b0df17243..bf8b4ceac65 100644 --- a/cpp/include/IceUtil/Functional.h +++ b/cpp/include/IceUtil/Functional.h @@ -1,453 +1,453 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_FUNCTIONAL_H
-#define ICE_UTIL_FUNCTIONAL_H
-
-#include <IceUtil/Handle.h>
-#include <functional>
-
-namespace IceUtilInternal
-{
-
-// ----------------------------------------------------------------------
-// Various function objects that work with handles instead of plain
-// pointers.
-// ----------------------------------------------------------------------
-
-template<class R, class T, class H>
-class MemFun : public std::unary_function<H, R>
-{
- typedef R (T::*MemberFN)(void);
- MemberFN _mfn;
-
-public:
-
- explicit MemFun(MemberFN p) : _mfn(p) { }
- R operator()(H handle) const
- {
- return (handle.get() ->* _mfn)();
- }
-};
-
-template<class R, class T, class H, class A>
-class MemFun1 : public std::binary_function<H, A, R>
-{
- typedef R (T::*MemberFN)(A);
- MemberFN _mfn;
-
-public:
-
- explicit MemFun1(MemberFN p) : _mfn(p) { }
- R operator()(H handle, A arg) const
- {
- return (handle.get() ->* _mfn)(arg);
- }
-};
-
-template<class T, class H>
-class VoidMemFun : public std::unary_function<H, void>
-{
- typedef void (T::*MemberFN)(void);
- MemberFN _mfn;
-
-public:
-
- explicit VoidMemFun(MemberFN p) : _mfn(p) { }
- void operator()(H handle) const
- {
- (handle.get() ->* _mfn)();
- }
-};
-
-template<class T, class H, class A>
-class VoidMemFun1 : public std::binary_function<H, A, void>
-{
- typedef void (T::*MemberFN)(A);
- MemberFN _mfn;
-
-public:
-
- explicit VoidMemFun1(MemberFN p) : _mfn(p) { }
- void operator()(H handle, A arg) const
- {
- (handle.get() ->* _mfn)(arg);
- }
-};
-
-template<class R, class K, class T, class H>
-class SecondMemFun : public std::unary_function<std::pair<K, H>, R>
-{
- typedef R (T::*MemberFN)(void);
- MemberFN _mfn;
-
-public:
-
- explicit SecondMemFun(MemberFN p) : _mfn(p) { }
- R operator()(std::pair<K, H> pair) const
- {
- return (pair.second.get() ->* _mfn)();
- }
-};
-
-template<class R, class K, class T, class H, class A>
-class SecondMemFun1 : public std::binary_function<std::pair<K, H>, A, R>
-{
- typedef R (T::*MemberFN)(A);
- MemberFN _mfn;
-
-public:
-
- explicit SecondMemFun1(MemberFN p) : _mfn(p) { }
- R operator()(std::pair<K, H> pair, A arg) const
- {
- return (pair.second.get() ->* _mfn)(arg);
- }
-};
-
-template<class K, class T, class H>
-class SecondVoidMemFun : public std::unary_function<std::pair<K, H>, void>
-{
- typedef void (T::*MemberFN)(void);
- MemberFN _mfn;
-
-public:
-
- explicit SecondVoidMemFun(MemberFN p) : _mfn(p) { }
- void operator()(std::pair<K, H> pair) const
- {
- (pair.second.get() ->* _mfn)();
- }
-};
-
-template<class K, class T, class H, class A>
-class SecondVoidMemFun1 : public std::binary_function<std::pair<K, H>, A, void>
-{
- typedef void (T::*MemberFN)(A);
- MemberFN _mfn;
-
-public:
-
- explicit SecondVoidMemFun1(MemberFN p) : _mfn(p) { }
- void operator()(std::pair<K, H> pair, A arg) const
- {
- (pair.second.get() ->* _mfn)(arg);
- }
-};
-
-template<class R, class T, class H>
-class ConstMemFun : public std::unary_function<H, R>
-{
- typedef R (T::*MemberFN)(void) const;
- MemberFN _mfn;
-
-public:
-
- explicit ConstMemFun(MemberFN p) : _mfn(p) { }
- R operator()(H handle) const
- {
- return (handle.get() ->* _mfn)();
- }
-};
-
-template<class R, class T, class H, class A>
-class ConstMemFun1 : public std::binary_function<H, A, R>
-{
- typedef R (T::*MemberFN)(A) const;
- MemberFN _mfn;
-
-public:
-
- explicit ConstMemFun1(MemberFN p) : _mfn(p) { }
- R operator()(H handle, A arg) const
- {
- return (handle.get() ->* _mfn)(arg);
- }
-};
-
-template<class T, class H>
-class ConstVoidMemFun : public std::unary_function<H, void>
-{
- typedef void (T::*MemberFN)(void) const;
- MemberFN _mfn;
-
-public:
-
- explicit ConstVoidMemFun(MemberFN p) : _mfn(p) { }
- void operator()(H handle) const
- {
- (handle.get() ->* _mfn)();
- }
-};
-
-template<class T, class H, class A>
-class ConstVoidMemFun1 : public std::binary_function<H, A, void>
-{
- typedef void (T::*MemberFN)(A) const;
- MemberFN _mfn;
-
-public:
-
- explicit ConstVoidMemFun1(MemberFN p) : _mfn(p) { }
- void operator()(H handle, A arg) const
- {
- (handle.get() ->* _mfn)(arg);
- }
-};
-
-template<class R, class K, class T, class H>
-class SecondConstMemFun : public std::unary_function<std::pair<K, H>, R>
-{
- typedef R (T::*MemberFN)(void) const;
- MemberFN _mfn;
-
-public:
-
- explicit SecondConstMemFun(MemberFN p) : _mfn(p) { }
- R operator()(std::pair<K, H> pair) const
- {
- return (pair.second.get() ->* _mfn)();
- }
-};
-
-template<class R, class K, class T, class H, class A>
-class SecondConstMemFun1 : public std::binary_function<std::pair<K, H>, A, R>
-{
- typedef R (T::*MemberFN)(A) const;
- MemberFN _mfn;
-
-public:
-
- explicit SecondConstMemFun1(MemberFN p) : _mfn(p) { }
- R operator()(std::pair<K, H> pair, A arg) const
- {
- return (pair.second.get() ->* _mfn)(arg);
- }
-};
-
-template<class K, class T, class H>
-class SecondConstVoidMemFun : public std::unary_function<std::pair<K, H>, void>
-{
- typedef void (T::*MemberFN)(void) const;
- MemberFN _mfn;
-
-public:
-
- explicit SecondConstVoidMemFun(MemberFN p) : _mfn(p) { }
- void operator()(std::pair<K, H> pair) const
- {
- (pair.second.get() ->* _mfn)();
- }
-};
-
-template<class K, class T, class H, class A>
-class SecondConstVoidMemFun1 : public std::binary_function<std::pair<K, H>, A, void>
-{
- typedef void (T::*MemberFN)(A) const;
- MemberFN _mfn;
-
-public:
-
- explicit SecondConstVoidMemFun1(MemberFN p) : _mfn(p) { }
- void operator()(std::pair<K, H> pair, A arg) const
- {
- (pair.second.get() ->* _mfn)(arg);
- }
-};
-
-}
-
-// ----------------------------------------------------------------------
-// Inline functions that return function objects that work with
-// IceUtil::Handle
-// ----------------------------------------------------------------------
-
-namespace IceUtil
-{
-
-template<class R, class T>
-inline ::IceUtilInternal::MemFun<R, T, Handle<T> >
-memFun(R (T::*p)(void))
-{
- return ::IceUtilInternal::MemFun<R, T, Handle<T> >(p);
-}
-
-template<class R, class T, class A>
-inline ::IceUtilInternal::MemFun1<R, T, Handle<T>, A>
-memFun1(R (T::*p)(A))
-{
- return ::IceUtilInternal::MemFun1<R, T, Handle<T>, A>(p);
-}
-
-template<class T>
-inline ::IceUtilInternal::VoidMemFun<T, Handle<T> >
-voidMemFun(void (T::*p)(void))
-{
- return ::IceUtilInternal::VoidMemFun<T, Handle<T> >(p);
-}
-
-template<class T, class A>
-inline ::IceUtilInternal::VoidMemFun1<T, Handle<T>, A>
-voidMemFun1(void (T::*p)(A))
-{
- return ::IceUtilInternal::VoidMemFun1<T, Handle<T>, A>(p);
-}
-
-template<class R, class K, class T>
-inline ::IceUtilInternal::SecondMemFun<R, K, T, Handle<T> >
-secondMemFun(R (T::*p)(void))
-{
- return ::IceUtilInternal::SecondMemFun<R, K, T, Handle<T> >(p);
-}
-
-template<class R, class K, class T, class A>
-inline ::IceUtilInternal::SecondMemFun1<R, K, T, Handle<T>, A>
-secondMemFun1(R (T::*p)(A))
-{
- return ::IceUtilInternal::SecondMemFun1<R, K, T, Handle<T>, A>(p);
-}
-
-template<class K, class T>
-inline ::IceUtilInternal::SecondVoidMemFun<K, T, Handle<T> >
-secondVoidMemFun(void (T::*p)(void))
-{
- return ::IceUtilInternal::SecondVoidMemFun<K, T, Handle<T> >(p);
-}
-
-template<class K, class T, class A>
-inline ::IceUtilInternal::SecondVoidMemFun1<K, T, Handle<T>, A>
-secondVoidMemFun1(void (T::*p)(A))
-{
- return ::IceUtilInternal::SecondVoidMemFun1<K, T, Handle<T>, A>(p);
-}
-
-template<class R, class T>
-inline ::IceUtilInternal::ConstMemFun<R, T, Handle<T> >
-constMemFun(R (T::*p)(void) const)
-{
- return ::IceUtilInternal::ConstMemFun<R, T, Handle<T> >(p);
-}
-
-template<class R, class T, class A>
-inline ::IceUtilInternal::ConstMemFun1<R, T, Handle<T>, A>
-constMemFun1(R (T::*p)(A) const)
-{
- return ::IceUtilInternal::ConstMemFun1<R, T, Handle<T>, A>(p);
-}
-
-template<class T>
-inline ::IceUtilInternal::ConstVoidMemFun<T, Handle<T> >
-constVoidMemFun(void (T::*p)(void) const)
-{
- return ::IceUtilInternal::ConstVoidMemFun<T, Handle<T> >(p);
-}
-
-template<class T, class A>
-inline ::IceUtilInternal::ConstVoidMemFun1<T, Handle<T>, A>
-constVoidMemFun1(void (T::*p)(A) const)
-{
- return ::IceUtilInternal::ConstVoidMemFun1<T, Handle<T>, A>(p);
-}
-
-template<class R, class K, class T>
-inline ::IceUtilInternal::SecondConstMemFun<R, K, T, Handle<T> >
-secondConstMemFun(R (T::*p)(void) const)
-{
- return ::IceUtilInternal::SecondConstMemFun<R, K, T, Handle<T> >(p);
-}
-
-template<class R, class K, class T, class A>
-inline ::IceUtilInternal::SecondConstMemFun1<R, K, T, Handle<T>, A>
-secondConstMemFun1(R (T::*p)(A) const)
-{
- return ::IceUtilInternal::SecondConstMemFun1<R, K, T, Handle<T>, A>(p);
-}
-
-template<class K, class T>
-inline ::IceUtilInternal::SecondConstVoidMemFun<K, T, Handle<T> >
-secondConstVoidMemFun(void (T::*p)(void) const)
-{
- return ::IceUtilInternal::SecondConstVoidMemFun<K, T, Handle<T> >(p);
-}
-
-template<class K, class T, class A>
-inline ::IceUtilInternal::SecondConstVoidMemFun1<K, T, Handle<T>, A>
-secondConstVoidMemFun1(void (T::*p)(A) const)
-{
- return ::IceUtilInternal::SecondConstVoidMemFun1<K, T, Handle<T>, A>(p);
-}
-
-}
-
-// ----------------------------------------------------------------------
-// Extension for STLport: Special versions for bind1st and bind2nd for
-// operations that do not return anything (i.e., return void). Needed
-// for broken compilers, such as Visual C++
-// ----------------------------------------------------------------------
-
-#ifdef _STLP_BEGIN_NAMESPACE
-
-_STLP_BEGIN_NAMESPACE
-
-template <class _Operation>
-class voidbinder1st :
- public unary_function<typename __BINARY_ARG(_Operation,second_argument_type),
- typename __BINARY_ARG(_Operation,result_type) > {
-protected:
- _Operation _M_op;
- typename _Operation::first_argument_type _M_value;
-public:
- voidbinder1st(const _Operation& __x,
- const typename _Operation::first_argument_type& __y)
- : _M_op(__x), _M_value(__y) {}
- typename _Operation::result_type
- operator()(const typename _Operation::second_argument_type& __x) const {
- _M_op(_M_value, __x);
- }
-};
-
-template <class _Operation, class _Tp>
-inline voidbinder1st<_Operation>
-voidbind1st(const _Operation& __fn, const _Tp& __x)
-{
- typedef typename _Operation::first_argument_type _Arg1_type;
- return voidbinder1st<_Operation>(__fn, _Arg1_type(__x));
-}
-
-template <class _Operation>
-class voidbinder2nd
- : public unary_function<typename __BINARY_ARG(_Operation,first_argument_type),
- typename __BINARY_ARG(_Operation,result_type)> {
-protected:
- _Operation _M_op;
- typename _Operation::second_argument_type value;
-public:
- voidbinder2nd(const _Operation& __x,
- const typename _Operation::second_argument_type& __y)
- : _M_op(__x), value(__y) {}
- typename _Operation::result_type
- operator()(const typename _Operation::first_argument_type& __x) const {
- _M_op(__x, value);
- }
-};
-
-template <class _Operation, class _Tp>
-inline voidbinder2nd<_Operation>
-voidbind2nd(const _Operation& __fn, const _Tp& __x)
-{
- typedef typename _Operation::second_argument_type _Arg2_type;
- return voidbinder2nd<_Operation>(__fn, _Arg2_type(__x));
-}
-
-_STLP_END_NAMESPACE
-
-#endif
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_FUNCTIONAL_H +#define ICE_UTIL_FUNCTIONAL_H + +#include <IceUtil/Handle.h> +#include <functional> + +namespace IceUtilInternal +{ + +// ---------------------------------------------------------------------- +// Various function objects that work with handles instead of plain +// pointers. +// ---------------------------------------------------------------------- + +template<class R, class T, class H> +class MemFun : public std::unary_function<H, R> +{ + typedef R (T::*MemberFN)(void); + MemberFN _mfn; + +public: + + explicit MemFun(MemberFN p) : _mfn(p) { } + R operator()(H handle) const + { + return (handle.get() ->* _mfn)(); + } +}; + +template<class R, class T, class H, class A> +class MemFun1 : public std::binary_function<H, A, R> +{ + typedef R (T::*MemberFN)(A); + MemberFN _mfn; + +public: + + explicit MemFun1(MemberFN p) : _mfn(p) { } + R operator()(H handle, A arg) const + { + return (handle.get() ->* _mfn)(arg); + } +}; + +template<class T, class H> +class VoidMemFun : public std::unary_function<H, void> +{ + typedef void (T::*MemberFN)(void); + MemberFN _mfn; + +public: + + explicit VoidMemFun(MemberFN p) : _mfn(p) { } + void operator()(H handle) const + { + (handle.get() ->* _mfn)(); + } +}; + +template<class T, class H, class A> +class VoidMemFun1 : public std::binary_function<H, A, void> +{ + typedef void (T::*MemberFN)(A); + MemberFN _mfn; + +public: + + explicit VoidMemFun1(MemberFN p) : _mfn(p) { } + void operator()(H handle, A arg) const + { + (handle.get() ->* _mfn)(arg); + } +}; + +template<class R, class K, class T, class H> +class SecondMemFun : public std::unary_function<std::pair<K, H>, R> +{ + typedef R (T::*MemberFN)(void); + MemberFN _mfn; + +public: + + explicit SecondMemFun(MemberFN p) : _mfn(p) { } + R operator()(std::pair<K, H> pair) const + { + return (pair.second.get() ->* _mfn)(); + } +}; + +template<class R, class K, class T, class H, class A> +class SecondMemFun1 : public std::binary_function<std::pair<K, H>, A, R> +{ + typedef R (T::*MemberFN)(A); + MemberFN _mfn; + +public: + + explicit SecondMemFun1(MemberFN p) : _mfn(p) { } + R operator()(std::pair<K, H> pair, A arg) const + { + return (pair.second.get() ->* _mfn)(arg); + } +}; + +template<class K, class T, class H> +class SecondVoidMemFun : public std::unary_function<std::pair<K, H>, void> +{ + typedef void (T::*MemberFN)(void); + MemberFN _mfn; + +public: + + explicit SecondVoidMemFun(MemberFN p) : _mfn(p) { } + void operator()(std::pair<K, H> pair) const + { + (pair.second.get() ->* _mfn)(); + } +}; + +template<class K, class T, class H, class A> +class SecondVoidMemFun1 : public std::binary_function<std::pair<K, H>, A, void> +{ + typedef void (T::*MemberFN)(A); + MemberFN _mfn; + +public: + + explicit SecondVoidMemFun1(MemberFN p) : _mfn(p) { } + void operator()(std::pair<K, H> pair, A arg) const + { + (pair.second.get() ->* _mfn)(arg); + } +}; + +template<class R, class T, class H> +class ConstMemFun : public std::unary_function<H, R> +{ + typedef R (T::*MemberFN)(void) const; + MemberFN _mfn; + +public: + + explicit ConstMemFun(MemberFN p) : _mfn(p) { } + R operator()(H handle) const + { + return (handle.get() ->* _mfn)(); + } +}; + +template<class R, class T, class H, class A> +class ConstMemFun1 : public std::binary_function<H, A, R> +{ + typedef R (T::*MemberFN)(A) const; + MemberFN _mfn; + +public: + + explicit ConstMemFun1(MemberFN p) : _mfn(p) { } + R operator()(H handle, A arg) const + { + return (handle.get() ->* _mfn)(arg); + } +}; + +template<class T, class H> +class ConstVoidMemFun : public std::unary_function<H, void> +{ + typedef void (T::*MemberFN)(void) const; + MemberFN _mfn; + +public: + + explicit ConstVoidMemFun(MemberFN p) : _mfn(p) { } + void operator()(H handle) const + { + (handle.get() ->* _mfn)(); + } +}; + +template<class T, class H, class A> +class ConstVoidMemFun1 : public std::binary_function<H, A, void> +{ + typedef void (T::*MemberFN)(A) const; + MemberFN _mfn; + +public: + + explicit ConstVoidMemFun1(MemberFN p) : _mfn(p) { } + void operator()(H handle, A arg) const + { + (handle.get() ->* _mfn)(arg); + } +}; + +template<class R, class K, class T, class H> +class SecondConstMemFun : public std::unary_function<std::pair<K, H>, R> +{ + typedef R (T::*MemberFN)(void) const; + MemberFN _mfn; + +public: + + explicit SecondConstMemFun(MemberFN p) : _mfn(p) { } + R operator()(std::pair<K, H> pair) const + { + return (pair.second.get() ->* _mfn)(); + } +}; + +template<class R, class K, class T, class H, class A> +class SecondConstMemFun1 : public std::binary_function<std::pair<K, H>, A, R> +{ + typedef R (T::*MemberFN)(A) const; + MemberFN _mfn; + +public: + + explicit SecondConstMemFun1(MemberFN p) : _mfn(p) { } + R operator()(std::pair<K, H> pair, A arg) const + { + return (pair.second.get() ->* _mfn)(arg); + } +}; + +template<class K, class T, class H> +class SecondConstVoidMemFun : public std::unary_function<std::pair<K, H>, void> +{ + typedef void (T::*MemberFN)(void) const; + MemberFN _mfn; + +public: + + explicit SecondConstVoidMemFun(MemberFN p) : _mfn(p) { } + void operator()(std::pair<K, H> pair) const + { + (pair.second.get() ->* _mfn)(); + } +}; + +template<class K, class T, class H, class A> +class SecondConstVoidMemFun1 : public std::binary_function<std::pair<K, H>, A, void> +{ + typedef void (T::*MemberFN)(A) const; + MemberFN _mfn; + +public: + + explicit SecondConstVoidMemFun1(MemberFN p) : _mfn(p) { } + void operator()(std::pair<K, H> pair, A arg) const + { + (pair.second.get() ->* _mfn)(arg); + } +}; + +} + +// ---------------------------------------------------------------------- +// Inline functions that return function objects that work with +// IceUtil::Handle +// ---------------------------------------------------------------------- + +namespace IceUtil +{ + +template<class R, class T> +inline ::IceUtilInternal::MemFun<R, T, Handle<T> > +memFun(R (T::*p)(void)) +{ + return ::IceUtilInternal::MemFun<R, T, Handle<T> >(p); +} + +template<class R, class T, class A> +inline ::IceUtilInternal::MemFun1<R, T, Handle<T>, A> +memFun1(R (T::*p)(A)) +{ + return ::IceUtilInternal::MemFun1<R, T, Handle<T>, A>(p); +} + +template<class T> +inline ::IceUtilInternal::VoidMemFun<T, Handle<T> > +voidMemFun(void (T::*p)(void)) +{ + return ::IceUtilInternal::VoidMemFun<T, Handle<T> >(p); +} + +template<class T, class A> +inline ::IceUtilInternal::VoidMemFun1<T, Handle<T>, A> +voidMemFun1(void (T::*p)(A)) +{ + return ::IceUtilInternal::VoidMemFun1<T, Handle<T>, A>(p); +} + +template<class R, class K, class T> +inline ::IceUtilInternal::SecondMemFun<R, K, T, Handle<T> > +secondMemFun(R (T::*p)(void)) +{ + return ::IceUtilInternal::SecondMemFun<R, K, T, Handle<T> >(p); +} + +template<class R, class K, class T, class A> +inline ::IceUtilInternal::SecondMemFun1<R, K, T, Handle<T>, A> +secondMemFun1(R (T::*p)(A)) +{ + return ::IceUtilInternal::SecondMemFun1<R, K, T, Handle<T>, A>(p); +} + +template<class K, class T> +inline ::IceUtilInternal::SecondVoidMemFun<K, T, Handle<T> > +secondVoidMemFun(void (T::*p)(void)) +{ + return ::IceUtilInternal::SecondVoidMemFun<K, T, Handle<T> >(p); +} + +template<class K, class T, class A> +inline ::IceUtilInternal::SecondVoidMemFun1<K, T, Handle<T>, A> +secondVoidMemFun1(void (T::*p)(A)) +{ + return ::IceUtilInternal::SecondVoidMemFun1<K, T, Handle<T>, A>(p); +} + +template<class R, class T> +inline ::IceUtilInternal::ConstMemFun<R, T, Handle<T> > +constMemFun(R (T::*p)(void) const) +{ + return ::IceUtilInternal::ConstMemFun<R, T, Handle<T> >(p); +} + +template<class R, class T, class A> +inline ::IceUtilInternal::ConstMemFun1<R, T, Handle<T>, A> +constMemFun1(R (T::*p)(A) const) +{ + return ::IceUtilInternal::ConstMemFun1<R, T, Handle<T>, A>(p); +} + +template<class T> +inline ::IceUtilInternal::ConstVoidMemFun<T, Handle<T> > +constVoidMemFun(void (T::*p)(void) const) +{ + return ::IceUtilInternal::ConstVoidMemFun<T, Handle<T> >(p); +} + +template<class T, class A> +inline ::IceUtilInternal::ConstVoidMemFun1<T, Handle<T>, A> +constVoidMemFun1(void (T::*p)(A) const) +{ + return ::IceUtilInternal::ConstVoidMemFun1<T, Handle<T>, A>(p); +} + +template<class R, class K, class T> +inline ::IceUtilInternal::SecondConstMemFun<R, K, T, Handle<T> > +secondConstMemFun(R (T::*p)(void) const) +{ + return ::IceUtilInternal::SecondConstMemFun<R, K, T, Handle<T> >(p); +} + +template<class R, class K, class T, class A> +inline ::IceUtilInternal::SecondConstMemFun1<R, K, T, Handle<T>, A> +secondConstMemFun1(R (T::*p)(A) const) +{ + return ::IceUtilInternal::SecondConstMemFun1<R, K, T, Handle<T>, A>(p); +} + +template<class K, class T> +inline ::IceUtilInternal::SecondConstVoidMemFun<K, T, Handle<T> > +secondConstVoidMemFun(void (T::*p)(void) const) +{ + return ::IceUtilInternal::SecondConstVoidMemFun<K, T, Handle<T> >(p); +} + +template<class K, class T, class A> +inline ::IceUtilInternal::SecondConstVoidMemFun1<K, T, Handle<T>, A> +secondConstVoidMemFun1(void (T::*p)(A) const) +{ + return ::IceUtilInternal::SecondConstVoidMemFun1<K, T, Handle<T>, A>(p); +} + +} + +// ---------------------------------------------------------------------- +// Extension for STLport: Special versions for bind1st and bind2nd for +// operations that do not return anything (i.e., return void). Needed +// for broken compilers, such as Visual C++ +// ---------------------------------------------------------------------- + +#ifdef _STLP_BEGIN_NAMESPACE + +_STLP_BEGIN_NAMESPACE + +template <class _Operation> +class voidbinder1st : + public unary_function<typename __BINARY_ARG(_Operation,second_argument_type), + typename __BINARY_ARG(_Operation,result_type) > { +protected: + _Operation _M_op; + typename _Operation::first_argument_type _M_value; +public: + voidbinder1st(const _Operation& __x, + const typename _Operation::first_argument_type& __y) + : _M_op(__x), _M_value(__y) {} + typename _Operation::result_type + operator()(const typename _Operation::second_argument_type& __x) const { + _M_op(_M_value, __x); + } +}; + +template <class _Operation, class _Tp> +inline voidbinder1st<_Operation> +voidbind1st(const _Operation& __fn, const _Tp& __x) +{ + typedef typename _Operation::first_argument_type _Arg1_type; + return voidbinder1st<_Operation>(__fn, _Arg1_type(__x)); +} + +template <class _Operation> +class voidbinder2nd + : public unary_function<typename __BINARY_ARG(_Operation,first_argument_type), + typename __BINARY_ARG(_Operation,result_type)> { +protected: + _Operation _M_op; + typename _Operation::second_argument_type value; +public: + voidbinder2nd(const _Operation& __x, + const typename _Operation::second_argument_type& __y) + : _M_op(__x), value(__y) {} + typename _Operation::result_type + operator()(const typename _Operation::first_argument_type& __x) const { + _M_op(__x, value); + } +}; + +template <class _Operation, class _Tp> +inline voidbinder2nd<_Operation> +voidbind2nd(const _Operation& __fn, const _Tp& __x) +{ + typedef typename _Operation::second_argument_type _Arg2_type; + return voidbinder2nd<_Operation>(__fn, _Arg2_type(__x)); +} + +_STLP_END_NAMESPACE + +#endif + +#endif diff --git a/cpp/include/IceUtil/Handle.h b/cpp/include/IceUtil/Handle.h index de901a65835..b9b625ac1bd 100644 --- a/cpp/include/IceUtil/Handle.h +++ b/cpp/include/IceUtil/Handle.h @@ -1,231 +1,231 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_HANDLE_H
-#define ICE_UTIL_HANDLE_H
-
-#include <IceUtil/Exception.h>
-#include <algorithm>
-
-//
-// "Handle" or "smart pointer" class for classes derived from
-// IceUtil::Shared or IceUtil::SimpleShared.
-//
-namespace IceUtil
-{
-
-template<typename T>
-class HandleBase
-{
-public:
-
- typedef T element_type;
-
- T* get() const
- {
- return _ptr;
- }
-
- T* operator->() const
- {
- if(!_ptr)
- {
- throw NullHandleException(__FILE__, __LINE__);
- }
-
- return _ptr;
- }
-
- operator bool() const
- {
- return _ptr ? true : false;
- }
-
- void swap(HandleBase& other)
- {
- std::swap(_ptr, other._ptr);
- }
-
- T* _ptr;
-};
-
-template<typename T, typename U>
-inline bool operator==(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
-{
- T* l = lhs.get();
- U* r = rhs.get();
- if(l && r)
- {
- return *l == *r;
- }
- else
- {
- return !l && !r;
- }
-}
-
-template<typename T, typename U>
-inline bool operator!=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
-{
- T* l = lhs.get();
- U* r = rhs.get();
- if(l && r)
- {
- return *l != *r;
- }
- else
- {
- return l || r;
- }
-}
-
-template<typename T, typename U>
-inline bool operator<(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
-{
- T* l = lhs.get();
- U* r = rhs.get();
- if(l && r)
- {
- return *l < *r;
- }
- else
- {
- return !l && r;
- }
-}
-
-template<typename T>
-class Handle : public HandleBase<T>
-{
-public:
-
- Handle(T* p = 0)
- {
- _ptr = p;
-
- if(_ptr)
- {
- _ptr->__incRef();
- }
- }
-
- template<typename Y>
- Handle(const Handle<Y>& r)
- {
- _ptr = r._ptr;
-
- if(_ptr)
- {
- _ptr->__incRef();
- }
- }
-
-#ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here???
- template<>
- Handle(const Handle<T>& r)
-#else
- Handle(const Handle& r)
-#endif
- {
- _ptr = r._ptr;
-
- if(_ptr)
- {
- _ptr->__incRef();
- }
- }
-
- ~Handle()
- {
- if(_ptr)
- {
- _ptr->__decRef();
- }
- }
-
- Handle& operator=(T* p)
- {
- if(_ptr != p)
- {
- if(p)
- {
- p->__incRef();
- }
-
- if(_ptr)
- {
- _ptr->__decRef();
- }
-
- _ptr = p;
- }
- return *this;
- }
-
- template<typename Y>
- Handle& operator=(const Handle<Y>& r)
- {
- if(_ptr != r._ptr)
- {
- if(r._ptr)
- {
- r._ptr->__incRef();
- }
-
- if(_ptr)
- {
- _ptr->__decRef();
- }
-
- _ptr = r._ptr;
- }
- return *this;
- }
-
-#ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here???
- template<>
- Handle& operator=(const Handle<T>& r)
-#else
- Handle& operator=(const Handle& r)
-#endif
- {
- if(_ptr != r._ptr)
- {
- if(r._ptr)
- {
- r._ptr->__incRef();
- }
-
- if(_ptr)
- {
- _ptr->__decRef();
- }
-
- _ptr = r._ptr;
- }
- return *this;
- }
-
- template<class Y>
- static Handle dynamicCast(const HandleBase<Y>& r)
- {
- return Handle(dynamic_cast<T*>(r._ptr));
- }
-
- template<class Y>
- static Handle dynamicCast(Y* p)
- {
- return Handle(dynamic_cast<T*>(p));
- }
-};
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_HANDLE_H +#define ICE_UTIL_HANDLE_H + +#include <IceUtil/Exception.h> +#include <algorithm> + +// +// "Handle" or "smart pointer" class for classes derived from +// IceUtil::Shared or IceUtil::SimpleShared. +// +namespace IceUtil +{ + +template<typename T> +class HandleBase +{ +public: + + typedef T element_type; + + T* get() const + { + return _ptr; + } + + T* operator->() const + { + if(!_ptr) + { + throw NullHandleException(__FILE__, __LINE__); + } + + return _ptr; + } + + operator bool() const + { + return _ptr ? true : false; + } + + void swap(HandleBase& other) + { + std::swap(_ptr, other._ptr); + } + + T* _ptr; +}; + +template<typename T, typename U> +inline bool operator==(const HandleBase<T>& lhs, const HandleBase<U>& rhs) +{ + T* l = lhs.get(); + U* r = rhs.get(); + if(l && r) + { + return *l == *r; + } + else + { + return !l && !r; + } +} + +template<typename T, typename U> +inline bool operator!=(const HandleBase<T>& lhs, const HandleBase<U>& rhs) +{ + T* l = lhs.get(); + U* r = rhs.get(); + if(l && r) + { + return *l != *r; + } + else + { + return l || r; + } +} + +template<typename T, typename U> +inline bool operator<(const HandleBase<T>& lhs, const HandleBase<U>& rhs) +{ + T* l = lhs.get(); + U* r = rhs.get(); + if(l && r) + { + return *l < *r; + } + else + { + return !l && r; + } +} + +template<typename T> +class Handle : public HandleBase<T> +{ +public: + + Handle(T* p = 0) + { + _ptr = p; + + if(_ptr) + { + _ptr->__incRef(); + } + } + + template<typename Y> + Handle(const Handle<Y>& r) + { + _ptr = r._ptr; + + if(_ptr) + { + _ptr->__incRef(); + } + } + +#ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? + template<> + Handle(const Handle<T>& r) +#else + Handle(const Handle& r) +#endif + { + _ptr = r._ptr; + + if(_ptr) + { + _ptr->__incRef(); + } + } + + ~Handle() + { + if(_ptr) + { + _ptr->__decRef(); + } + } + + Handle& operator=(T* p) + { + if(_ptr != p) + { + if(p) + { + p->__incRef(); + } + + if(_ptr) + { + _ptr->__decRef(); + } + + _ptr = p; + } + return *this; + } + + template<typename Y> + Handle& operator=(const Handle<Y>& r) + { + if(_ptr != r._ptr) + { + if(r._ptr) + { + r._ptr->__incRef(); + } + + if(_ptr) + { + _ptr->__decRef(); + } + + _ptr = r._ptr; + } + return *this; + } + +#ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? + template<> + Handle& operator=(const Handle<T>& r) +#else + Handle& operator=(const Handle& r) +#endif + { + if(_ptr != r._ptr) + { + if(r._ptr) + { + r._ptr->__incRef(); + } + + if(_ptr) + { + _ptr->__decRef(); + } + + _ptr = r._ptr; + } + return *this; + } + + template<class Y> + static Handle dynamicCast(const HandleBase<Y>& r) + { + return Handle(dynamic_cast<T*>(r._ptr)); + } + + template<class Y> + static Handle dynamicCast(Y* p) + { + return Handle(dynamic_cast<T*>(p)); + } +}; + +} + +#endif diff --git a/cpp/include/IceUtil/IceUtil.h b/cpp/include/IceUtil/IceUtil.h index 5821455d840..64497d6a393 100644 --- a/cpp/include/IceUtil/IceUtil.h +++ b/cpp/include/IceUtil/IceUtil.h @@ -1,32 +1,32 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_ICE_UTIL_H
-#define ICE_UTIL_ICE_UTIL_H
-
-//
-// This file must include *all* other headers of IceUtil.
-//
-
-#include <IceUtil/Functional.h>
-#include <IceUtil/Shared.h>
-#include <IceUtil/Unicode.h>
-#include <IceUtil/UUID.h>
-#include <IceUtil/Mutex.h>
-#include <IceUtil/RecMutex.h>
-#include <IceUtil/RWRecMutex.h>
-#include <IceUtil/Monitor.h>
-#include <IceUtil/Thread.h>
-#include <IceUtil/Base64.h>
-#include <IceUtil/Time.h>
-#include <IceUtil/InputUtil.h>
-#include <IceUtil/OutputUtil.h>
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_ICE_UTIL_H +#define ICE_UTIL_ICE_UTIL_H + +// +// This file must include *all* other headers of IceUtil. +// + +#include <IceUtil/Functional.h> +#include <IceUtil/Shared.h> +#include <IceUtil/Unicode.h> +#include <IceUtil/UUID.h> +#include <IceUtil/Mutex.h> +#include <IceUtil/RecMutex.h> +#include <IceUtil/RWRecMutex.h> +#include <IceUtil/Monitor.h> +#include <IceUtil/Thread.h> +#include <IceUtil/Base64.h> +#include <IceUtil/Time.h> +#include <IceUtil/InputUtil.h> +#include <IceUtil/OutputUtil.h> + +#endif diff --git a/cpp/include/IceUtil/InputUtil.h b/cpp/include/IceUtil/InputUtil.h index ed95e84bbfe..6de10f0d966 100644 --- a/cpp/include/IceUtil/InputUtil.h +++ b/cpp/include/IceUtil/InputUtil.h @@ -1,65 +1,65 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_INPUT_UTIL_H
-#define ICE_UTIL_INPUT_UTIL_H
-
-#include <IceUtil/Config.h>
-#include <string>
-#include <limits.h>
-
-namespace IceUtil
-{
-
-//
-// strToInt64() is drop-in replacement for UNIX strtoll()
-//
-ICE_UTIL_API Int64 strToInt64(const char*, char**, int);
-
-//
-// stringToInt64 converts a string into a signed 64-bit integer.
-//
-// bool stringToInt64(const std::string& stringToParse, Int64& result, std::string::size_type& pos);
-//
-// Semantics:
-//
-// - Ignore leading and trailing whitespace
-//
-// - If the string starts with '0', parse as octal
-//
-// - If the string starts with "0x" or "0X", parse as hexadecimal
-//
-// - Otherwise, parse as decimal
-//
-// - return value == true indicates a successful conversion and result contains the converted value
-//
-// - if pos == string::npos, there are no trailing non-whitespace characters following the converted string
-//
-// - if pos != string::npos, there are trailing non-whitespace characters following the converted string;
-// pos indicates the first such character
-//
-// - return value == false indicates an unsuccessful conversion:
-//
-// - result == 0 indicates that no digits were available for conversion
-//
-// - result == INT64MIN or result == INT64MAX indicate underflow or overflow.
-//
-// - if pos == string::npos, the string did not contain trailing non-whitespace characters
-//
-// - if pos != string::npos, the string contained trailing non-whitespace characters following the
-// digits and pos indicates the first such character. (Note that all digits up to the first
-// non-digit, non-whitespace character are consumed, regardless of how far into the digit string
-// an overflow is detected.)
-//
-ICE_UTIL_API bool stringToInt64(const std::string&, Int64&, std::string::size_type&);
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_INPUT_UTIL_H +#define ICE_UTIL_INPUT_UTIL_H + +#include <IceUtil/Config.h> +#include <string> +#include <limits.h> + +namespace IceUtil +{ + +// +// strToInt64() is drop-in replacement for UNIX strtoll() +// +ICE_UTIL_API Int64 strToInt64(const char*, char**, int); + +// +// stringToInt64 converts a string into a signed 64-bit integer. +// +// bool stringToInt64(const std::string& stringToParse, Int64& result, std::string::size_type& pos); +// +// Semantics: +// +// - Ignore leading and trailing whitespace +// +// - If the string starts with '0', parse as octal +// +// - If the string starts with "0x" or "0X", parse as hexadecimal +// +// - Otherwise, parse as decimal +// +// - return value == true indicates a successful conversion and result contains the converted value +// +// - if pos == string::npos, there are no trailing non-whitespace characters following the converted string +// +// - if pos != string::npos, there are trailing non-whitespace characters following the converted string; +// pos indicates the first such character +// +// - return value == false indicates an unsuccessful conversion: +// +// - result == 0 indicates that no digits were available for conversion +// +// - result == INT64MIN or result == INT64MAX indicate underflow or overflow. +// +// - if pos == string::npos, the string did not contain trailing non-whitespace characters +// +// - if pos != string::npos, the string contained trailing non-whitespace characters following the +// digits and pos indicates the first such character. (Note that all digits up to the first +// non-digit, non-whitespace character are consumed, regardless of how far into the digit string +// an overflow is detected.) +// +ICE_UTIL_API bool stringToInt64(const std::string&, Int64&, std::string::size_type&); + +} + +#endif diff --git a/cpp/include/IceUtil/Lock.h b/cpp/include/IceUtil/Lock.h index 13f8d5fa2c7..c22d08d3f62 100644 --- a/cpp/include/IceUtil/Lock.h +++ b/cpp/include/IceUtil/Lock.h @@ -1,72 +1,72 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_LOCK_H
-#define ICE_UTIL_LOCK_H
-
-#include <IceUtil/Config.h>
-
-namespace IceUtil
-{
-
-//
-// Forward declarations.
-//
-class Cond;
-
-template <typename T>
-class Lock
-{
-public:
-
- Lock(const T& mutex) :
- _mutex(mutex)
- {
- _mutex.lock();
- }
-
- ~Lock()
- {
- _mutex.unlock();
- }
-
-private:
-
- const T& _mutex;
-
- friend class Cond;
-};
-
-template <typename T>
-class TryLock
-{
-public:
-
- TryLock(const T& mutex) :
- _mutex(mutex)
- {
- _mutex.trylock();
- }
-
- ~TryLock()
- {
- _mutex.unlock();
- }
-
-private:
-
- const T& _mutex;
-
- friend class Cond;
-};
-
-} // End namespace IceUtil
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_LOCK_H +#define ICE_UTIL_LOCK_H + +#include <IceUtil/Config.h> + +namespace IceUtil +{ + +// +// Forward declarations. +// +class Cond; + +template <typename T> +class Lock +{ +public: + + Lock(const T& mutex) : + _mutex(mutex) + { + _mutex.lock(); + } + + ~Lock() + { + _mutex.unlock(); + } + +private: + + const T& _mutex; + + friend class Cond; +}; + +template <typename T> +class TryLock +{ +public: + + TryLock(const T& mutex) : + _mutex(mutex) + { + _mutex.trylock(); + } + + ~TryLock() + { + _mutex.unlock(); + } + +private: + + const T& _mutex; + + friend class Cond; +}; + +} // End namespace IceUtil + +#endif diff --git a/cpp/include/IceUtil/Monitor.h b/cpp/include/IceUtil/Monitor.h index 3fe6e76d943..429b595889b 100644 --- a/cpp/include/IceUtil/Monitor.h +++ b/cpp/include/IceUtil/Monitor.h @@ -1,235 +1,235 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_MONITOR_H
-#define ICE_UTIL_MONITOR_H
-
-#include <IceUtil/Config.h>
-#include <IceUtil/Lock.h>
-#include <IceUtil/Cond.h>
-
-namespace IceUtil
-{
-
-//
-// This monitor implements the Mesa monitor semantics. That is any
-// calls to notify() or notifyAll() are delayed until the monitor is
-// unlocked.
-//
-template <class T>
-class ICE_UTIL_API Monitor
-{
-public:
-
- typedef Lock<Monitor<T> > Lock;
- typedef TryLock<Monitor<T> > TryLock;
-
- Monitor();
- ~Monitor();
-
- //
- // Note that lock/trylock & unlock in general should not be used
- // directly. Instead use Lock & TryLock.
- //
- void lock() const;
- void unlock() const;
- void trylock() const;
-
- void wait() const;
- bool timedWait(const Time&) const;
- void notify();
- void notifyAll();
-
-private:
-
- // noncopyable
- Monitor(const Monitor&);
- void operator=(const Monitor&);
-
- void notifyImpl(int) const;
-
- mutable Cond _cond;
- T _mutex;
- mutable int _nnotify;
-};
-
-} // End namespace IceUtil
-
-//
-// Since this monitor implements the Mesa monitor semantics calls to
-// notify() or notifyAll() are delayed until the monitor is
-// unlocked. This can happen either due to a call to unlock(), or a
-// call to wait(). The _nnotify flag keeps track of the number of
-// pending notification calls. -1 indicates a broadcast, a positive
-// number indicates <n> calls to notify(). The _nnotify flag is reset
-// upon initial acquisition of the monitor lock (either through a call
-// to lock(), or a return from wait().
-//
-
-template <class T> inline
-IceUtil::Monitor<T>::Monitor() :
- _nnotify(0)
-{
-}
-
-template <class T> inline
-IceUtil::Monitor<T>::~Monitor()
-{
-}
-
-template <class T> inline void
-IceUtil::Monitor<T>::lock() const
-{
- if(_mutex.lock())
- {
- //
- // On the first mutex acquisition reset the number pending
- // notifications.
- //
- _nnotify = 0;
- }
-}
-
-template <class T> inline void
-IceUtil::Monitor<T>::unlock() const
-{
- int nnotify = _nnotify;
- if(_mutex.unlock())
- {
- //
- // Perform any pending notifications.
- //
- notifyImpl(nnotify);
- }
-}
-
-template <class T> inline void
-IceUtil::Monitor<T>::trylock() const
-{
- if(_mutex.trylock())
- {
- //
- // On the first mutex acquisition reset the number pending
- // notifications.
- //
- _nnotify = 0;
- }
-}
-
-template <class T> inline void
-IceUtil::Monitor<T>::wait() const
-{
- //
- // Perform any pending notifies
- //
- notifyImpl(_nnotify);
-
- //
- // Wait for a notification
- //
- try
- {
- _cond.waitImpl(_mutex);
- //
- // Reset the nnotify count once wait() returns.
- //
- }
- catch(...)
- {
- _nnotify = 0;
- throw;
- }
- _nnotify = 0;
-}
-
-template <class T> inline bool
-IceUtil::Monitor<T>::timedWait(const Time& timeout) const
-{
- //
- // Perform any pending notifies.
- //
- notifyImpl(_nnotify);
-
- bool rc;
- //
- // Wait for a notification.
- //
- try
- {
- rc = _cond.timedWaitImpl(_mutex, timeout);
-
- //
- // Reset the nnotify count once wait() returns.
- //
- }
- catch(...)
- {
- _nnotify = 0;
- throw;
- }
-
- _nnotify = 0;
- return rc;
-}
-
-template <class T> inline void
-IceUtil::Monitor<T>::notify()
-{
- //
- // Increment the _nnotify flag, unless a broadcast has already
- // been requested.
- //
- if(_nnotify != -1)
- {
- ++_nnotify;
- }
-}
-
-template <class T> inline void
-IceUtil::Monitor<T>::notifyAll()
-{
- //
- // -1 (indicates broadcast)
- //
- _nnotify = -1;
-}
-
-
-template <class T> inline void
-IceUtil::Monitor<T>::notifyImpl(int nnotify) const
-{
- //
- // Zero indicates no notifies.
- //
- if(nnotify != 0)
- {
- //
- // -1 means notifyAll.
- //
- if(nnotify == -1)
- {
- _cond.broadcast();
- return;
- }
- else
- {
- //
- // Otherwise notify n times.
- //
- while(nnotify > 0)
- {
- _cond.signal();
- --nnotify;
- }
- }
- }
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_MONITOR_H +#define ICE_UTIL_MONITOR_H + +#include <IceUtil/Config.h> +#include <IceUtil/Lock.h> +#include <IceUtil/Cond.h> + +namespace IceUtil +{ + +// +// This monitor implements the Mesa monitor semantics. That is any +// calls to notify() or notifyAll() are delayed until the monitor is +// unlocked. +// +template <class T> +class ICE_UTIL_API Monitor +{ +public: + + typedef Lock<Monitor<T> > Lock; + typedef TryLock<Monitor<T> > TryLock; + + Monitor(); + ~Monitor(); + + // + // Note that lock/trylock & unlock in general should not be used + // directly. Instead use Lock & TryLock. + // + void lock() const; + void unlock() const; + void trylock() const; + + void wait() const; + bool timedWait(const Time&) const; + void notify(); + void notifyAll(); + +private: + + // noncopyable + Monitor(const Monitor&); + void operator=(const Monitor&); + + void notifyImpl(int) const; + + mutable Cond _cond; + T _mutex; + mutable int _nnotify; +}; + +} // End namespace IceUtil + +// +// Since this monitor implements the Mesa monitor semantics calls to +// notify() or notifyAll() are delayed until the monitor is +// unlocked. This can happen either due to a call to unlock(), or a +// call to wait(). The _nnotify flag keeps track of the number of +// pending notification calls. -1 indicates a broadcast, a positive +// number indicates <n> calls to notify(). The _nnotify flag is reset +// upon initial acquisition of the monitor lock (either through a call +// to lock(), or a return from wait(). +// + +template <class T> inline +IceUtil::Monitor<T>::Monitor() : + _nnotify(0) +{ +} + +template <class T> inline +IceUtil::Monitor<T>::~Monitor() +{ +} + +template <class T> inline void +IceUtil::Monitor<T>::lock() const +{ + if(_mutex.lock()) + { + // + // On the first mutex acquisition reset the number pending + // notifications. + // + _nnotify = 0; + } +} + +template <class T> inline void +IceUtil::Monitor<T>::unlock() const +{ + int nnotify = _nnotify; + if(_mutex.unlock()) + { + // + // Perform any pending notifications. + // + notifyImpl(nnotify); + } +} + +template <class T> inline void +IceUtil::Monitor<T>::trylock() const +{ + if(_mutex.trylock()) + { + // + // On the first mutex acquisition reset the number pending + // notifications. + // + _nnotify = 0; + } +} + +template <class T> inline void +IceUtil::Monitor<T>::wait() const +{ + // + // Perform any pending notifies + // + notifyImpl(_nnotify); + + // + // Wait for a notification + // + try + { + _cond.waitImpl(_mutex); + // + // Reset the nnotify count once wait() returns. + // + } + catch(...) + { + _nnotify = 0; + throw; + } + _nnotify = 0; +} + +template <class T> inline bool +IceUtil::Monitor<T>::timedWait(const Time& timeout) const +{ + // + // Perform any pending notifies. + // + notifyImpl(_nnotify); + + bool rc; + // + // Wait for a notification. + // + try + { + rc = _cond.timedWaitImpl(_mutex, timeout); + + // + // Reset the nnotify count once wait() returns. + // + } + catch(...) + { + _nnotify = 0; + throw; + } + + _nnotify = 0; + return rc; +} + +template <class T> inline void +IceUtil::Monitor<T>::notify() +{ + // + // Increment the _nnotify flag, unless a broadcast has already + // been requested. + // + if(_nnotify != -1) + { + ++_nnotify; + } +} + +template <class T> inline void +IceUtil::Monitor<T>::notifyAll() +{ + // + // -1 (indicates broadcast) + // + _nnotify = -1; +} + + +template <class T> inline void +IceUtil::Monitor<T>::notifyImpl(int nnotify) const +{ + // + // Zero indicates no notifies. + // + if(nnotify != 0) + { + // + // -1 means notifyAll. + // + if(nnotify == -1) + { + _cond.broadcast(); + return; + } + else + { + // + // Otherwise notify n times. + // + while(nnotify > 0) + { + _cond.signal(); + --nnotify; + } + } + } +} + +#endif diff --git a/cpp/include/IceUtil/Mutex.h b/cpp/include/IceUtil/Mutex.h index e6ce737d4e3..44e8e682365 100644 --- a/cpp/include/IceUtil/Mutex.h +++ b/cpp/include/IceUtil/Mutex.h @@ -1,250 +1,250 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_MUTEX_H
-#define ICE_UTIL_MUTEX_H
-
-#include <IceUtil/Config.h>
-#include <IceUtil/Lock.h>
-#include <IceUtil/ThreadException.h>
-
-namespace IceUtil
-{
-
-//
-// Forward declaration for friend.
-//
-class Cond;
-
-//
-// Simple non-recursive Mutex implementation.
-//
-// Don't use noncopyable otherwise you end up with warnings like this:
-//
-// In file included from Connection.cpp:20:
-// ../../include/Ice/Outgoing.h:88: warning: direct base
-// `IceUtil::noncopyable' inaccessible in `IceInternal::Outgoing' due
-// to ambiguity
-//
-class ICE_UTIL_API Mutex
-{
-public:
-
- //
- // Lock & TryLock typedefs.
- //
- typedef Lock<Mutex> Lock;
- typedef TryLock<Mutex> TryLock;
-
- Mutex();
- ~Mutex();
-
- //
- // Note that lock/trylock & unlock in general should not be used
- // directly. Instead use Lock & TryLock.
- //
-
- //
- // The boolean values are for the Monitor implementation which
- // needs to know whether the Mutex has been locked for the first
- // time, or unlocked for the last time (that is another thread is
- // able to acquire the mutex).
- //
-
- //
- // Return true if the mutex has been locked for the first time.
- //
- bool lock() const;
-
- //
- // Throw ThreadLockedException in the case that the lock call would
- // block (that is the mutex is already owned by some other
- // thread). Returns true if the mutex has been locked for the
- // first time.
- //
- bool trylock() const;
-
- //
- // Returns true if the mutex has been unlocked for the last time
- // (false otherwise).
- //
- bool unlock() const;
-
-private:
-
- // noncopyable
- Mutex(const Mutex&);
- void operator=(const Mutex&);
-
- //
- // LockState and the lock/unlock variations are for use by the
- // Condition variable implementation.
- //
-#ifdef _WIN32
- struct LockState
- {
- };
-#else
- struct LockState
- {
- pthread_mutex_t* mutex;
- };
-#endif
-
- void unlock(LockState&) const;
- void lock(LockState&) const;
-
- friend class Cond;
-
-#ifdef _WIN32
- mutable CRITICAL_SECTION _mutex;
-#else
- mutable pthread_mutex_t _mutex;
-#endif
-};
-
-//
-// For performance reasons the following functions are inlined.
-//
-
-#ifdef _WIN32
-
-inline
-Mutex::Mutex()
-{
- InitializeCriticalSection(&_mutex);
-}
-
-inline
-Mutex::~Mutex()
-{
- DeleteCriticalSection(&_mutex);
-}
-
-inline bool
-Mutex::lock() const
-{
- EnterCriticalSection(&_mutex);
- //
- // If necessary this can be removed and replaced with a _count
- // member (like the UNIX implementation of RecMutex).
- //
- assert(_mutex.RecursionCount == 1);
- return true;
-}
-
-inline bool
-Mutex::trylock() const
-{
- if(!TryEnterCriticalSection(&_mutex))
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- if(_mutex.RecursionCount > 1)
- {
- LeaveCriticalSection(&_mutex);
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- return true;
-}
-
-inline bool
-Mutex::unlock() const
-{
- assert(_mutex.RecursionCount == 1);
- LeaveCriticalSection(&_mutex);
- return true;
-}
-
-inline void
-Mutex::unlock(LockState& state) const
-{
- LeaveCriticalSection(&_mutex);
-}
-
-inline void
-Mutex::lock(LockState&) const
-{
- EnterCriticalSection(&_mutex);
-}
-
-#else
-
-inline
-Mutex::Mutex()
-{
- int rc = pthread_mutex_init(&_mutex, 0);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
-}
-
-inline
-Mutex::~Mutex()
-{
- int rc = 0;
- rc = pthread_mutex_destroy(&_mutex);
- assert(rc == 0);
-}
-
-inline bool
-Mutex::lock() const
-{
- int rc = pthread_mutex_lock(&_mutex);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
- return true;
-}
-
-inline bool
-Mutex::trylock() const
-{
- int rc = pthread_mutex_trylock(&_mutex);
- if(rc != 0)
- {
- if(rc == EBUSY)
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
- return true;
-}
-
-inline bool
-Mutex::unlock() const
-{
- int rc = pthread_mutex_unlock(&_mutex);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
- return true;
-}
-
-inline void
-Mutex::unlock(LockState& state) const
-{
- state.mutex = &_mutex;
-}
-
-inline void
-Mutex::lock(LockState&) const
-{
-}
-
-#endif
-
-} // End namespace IceUtil
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_MUTEX_H +#define ICE_UTIL_MUTEX_H + +#include <IceUtil/Config.h> +#include <IceUtil/Lock.h> +#include <IceUtil/ThreadException.h> + +namespace IceUtil +{ + +// +// Forward declaration for friend. +// +class Cond; + +// +// Simple non-recursive Mutex implementation. +// +// Don't use noncopyable otherwise you end up with warnings like this: +// +// In file included from Connection.cpp:20: +// ../../include/Ice/Outgoing.h:88: warning: direct base +// `IceUtil::noncopyable' inaccessible in `IceInternal::Outgoing' due +// to ambiguity +// +class ICE_UTIL_API Mutex +{ +public: + + // + // Lock & TryLock typedefs. + // + typedef Lock<Mutex> Lock; + typedef TryLock<Mutex> TryLock; + + Mutex(); + ~Mutex(); + + // + // Note that lock/trylock & unlock in general should not be used + // directly. Instead use Lock & TryLock. + // + + // + // The boolean values are for the Monitor implementation which + // needs to know whether the Mutex has been locked for the first + // time, or unlocked for the last time (that is another thread is + // able to acquire the mutex). + // + + // + // Return true if the mutex has been locked for the first time. + // + bool lock() const; + + // + // Throw ThreadLockedException in the case that the lock call would + // block (that is the mutex is already owned by some other + // thread). Returns true if the mutex has been locked for the + // first time. + // + bool trylock() const; + + // + // Returns true if the mutex has been unlocked for the last time + // (false otherwise). + // + bool unlock() const; + +private: + + // noncopyable + Mutex(const Mutex&); + void operator=(const Mutex&); + + // + // LockState and the lock/unlock variations are for use by the + // Condition variable implementation. + // +#ifdef _WIN32 + struct LockState + { + }; +#else + struct LockState + { + pthread_mutex_t* mutex; + }; +#endif + + void unlock(LockState&) const; + void lock(LockState&) const; + + friend class Cond; + +#ifdef _WIN32 + mutable CRITICAL_SECTION _mutex; +#else + mutable pthread_mutex_t _mutex; +#endif +}; + +// +// For performance reasons the following functions are inlined. +// + +#ifdef _WIN32 + +inline +Mutex::Mutex() +{ + InitializeCriticalSection(&_mutex); +} + +inline +Mutex::~Mutex() +{ + DeleteCriticalSection(&_mutex); +} + +inline bool +Mutex::lock() const +{ + EnterCriticalSection(&_mutex); + // + // If necessary this can be removed and replaced with a _count + // member (like the UNIX implementation of RecMutex). + // + assert(_mutex.RecursionCount == 1); + return true; +} + +inline bool +Mutex::trylock() const +{ + if(!TryEnterCriticalSection(&_mutex)) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + if(_mutex.RecursionCount > 1) + { + LeaveCriticalSection(&_mutex); + throw ThreadLockedException(__FILE__, __LINE__); + } + return true; +} + +inline bool +Mutex::unlock() const +{ + assert(_mutex.RecursionCount == 1); + LeaveCriticalSection(&_mutex); + return true; +} + +inline void +Mutex::unlock(LockState& state) const +{ + LeaveCriticalSection(&_mutex); +} + +inline void +Mutex::lock(LockState&) const +{ + EnterCriticalSection(&_mutex); +} + +#else + +inline +Mutex::Mutex() +{ + int rc = pthread_mutex_init(&_mutex, 0); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } +} + +inline +Mutex::~Mutex() +{ + int rc = 0; + rc = pthread_mutex_destroy(&_mutex); + assert(rc == 0); +} + +inline bool +Mutex::lock() const +{ + int rc = pthread_mutex_lock(&_mutex); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } + return true; +} + +inline bool +Mutex::trylock() const +{ + int rc = pthread_mutex_trylock(&_mutex); + if(rc != 0) + { + if(rc == EBUSY) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } + return true; +} + +inline bool +Mutex::unlock() const +{ + int rc = pthread_mutex_unlock(&_mutex); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } + return true; +} + +inline void +Mutex::unlock(LockState& state) const +{ + state.mutex = &_mutex; +} + +inline void +Mutex::lock(LockState&) const +{ +} + +#endif + +} // End namespace IceUtil + +#endif diff --git a/cpp/include/IceUtil/OutputUtil.h b/cpp/include/IceUtil/OutputUtil.h index 8b2ed0996bd..4b0e321b64b 100644 --- a/cpp/include/IceUtil/OutputUtil.h +++ b/cpp/include/IceUtil/OutputUtil.h @@ -1,325 +1,325 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_OUTPUT_UTIL_H
-#define ICE_UTIL_OUTPUT_UTIL_H
-
-#include <IceUtil/Config.h>
-#include <fstream>
-#include <stack>
-
-namespace IceUtil
-{
-
-// ----------------------------------------------------------------------
-// OutputBase
-// ----------------------------------------------------------------------
-
-//
-// Technically it's not necessary to have print() & nl() as virtual
-// since the opeator<< functions are specific to each OutputBase
-// derivative. However, since it's possible to call print() & nl()
-// manually I've decided to leave them as virtual.
-//
-
-class ICE_UTIL_API OutputBase : public ::IceUtil::noncopyable
-{
-public:
-
- OutputBase();
- OutputBase(std::ostream&);
- OutputBase(const char*);
- virtual ~OutputBase();
-
- void setIndent(int); // What is the indent level?
- void setUseTab(bool); // Should we output tabs?
-
- void open(const char*); // Open output stream.
-
- virtual void print(const char*); // Print a string.
-
- void inc(); // Increment indentation level.
- void dec(); // Decrement indentation level.
-
- void useCurrentPosAsIndent(); // Save the current position as indentation.
- void zeroIndent(); // Use zero identation.
- void restoreIndent(); // Restore indentation.
-
- virtual void nl(); // Print newline.
- void sp(); // Print separator.
-
- bool operator!() const; // Check whether the output state is ok.
-
- std::streamsize width() const;
- std::streamsize width(std::streamsize newWidth);
-
- std::ios_base::fmtflags flags() const;
- std::ios_base::fmtflags flags(std::ios_base::fmtflags newFlags);
-
- std::ostream::char_type fill() const;
- std::ostream::char_type fill(std::ostream::char_type newFill);
-
-protected:
-
- std::ofstream _fout;
- std::ostream& _out;
- int _pos;
- int _indent;
- int _indentSize;
- std::stack<int> _indentSave;
- bool _useTab;
- bool _separator;
-};
-
-class ICE_UTIL_API NextLine
-{
-};
-extern ICE_UTIL_API NextLine nl;
-
-class ICE_UTIL_API Separator
-{
-};
-extern ICE_UTIL_API Separator sp;
-
-// ----------------------------------------------------------------------
-// Output
-// ----------------------------------------------------------------------
-
-class ICE_UTIL_API Output : public OutputBase
-{
-public:
-
- Output();
- Output(std::ostream&);
- Output(const char*);
-
- void setBeginBlock(const char *); // what do we use at block starts?
- void setEndBlock(const char *); // what do we use the block end?
-
- void sb(); // Start a block.
- void eb(); // End a block.
-
-private:
-
- std::string _blockStart;
- std::string _blockEnd;
-};
-
-template<typename T>
-Output&
-operator<<(Output& out, const T& val)
-{
- std::ostringstream s;
- s << val;
- out.print(s.str().c_str());
- return out;
-}
-
-template<>
-inline Output&
-operator<<(Output& o, const NextLine&)
-{
- o.nl();
- return o;
-}
-
-template<>
-inline Output&
-operator<<(Output& o, const Separator&)
-{
- o.sp();
- return o;
-}
-
-class ICE_UTIL_API StartBlock
-{
-};
-extern ICE_UTIL_API StartBlock sb;
-
-template<>
-inline Output&
-operator<<(Output& o, const StartBlock&)
-{
- o.sb();
- return o;
-}
-
-class ICE_UTIL_API EndBlock
-{
-};
-extern ICE_UTIL_API EndBlock eb;
-
-template<>
-inline Output&
-operator<<(Output& o, const EndBlock&)
-{
- o.eb();
- return o;
-}
-
-ICE_UTIL_API Output& operator<<(Output&, std::ios_base& (*)(std::ios_base&));
-
-// ----------------------------------------------------------------------
-// XMLOutput
-// ----------------------------------------------------------------------
-
-class ICE_UTIL_API XMLOutput : public OutputBase
-{
-public:
-
- XMLOutput();
- XMLOutput(std::ostream&);
- XMLOutput(const char*);
-
- void setSGML(bool);
- virtual void print(const char*); // Print a string.
-
- virtual void nl(); // Print newline.
-
- void se(const std::string&); // Start an element.
- void ee(); // End an element.
- void attr(const std::string&, const std::string&); // Add an attribute to an element.
-
- void startEscapes();
- void endEscapes();
-
- std::string currentElement() const;
-
-private:
-
- ::std::string escape(const ::std::string&) const;
-
- std::stack<std::string> _elementStack;
-
- bool _se;
- bool _text;
-
- bool _sgml;
- bool _escape;
-};
-
-template<typename T>
-XMLOutput&
-operator<<(XMLOutput& out, const T& val)
-{
- std::ostringstream s;
- s << val;
- out.print(s.str().c_str());
- return out;
-}
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const NextLine&)
-{
- o.nl();
- return o;
-}
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const Separator&)
-{
- o.sp();
- return o;
-}
-
-class ICE_UTIL_API EndElement
-{
-};
-extern ICE_UTIL_API EndElement ee;
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const EndElement&)
-{
- o.ee();
- return o;
-}
-
-class ICE_UTIL_API StartElement
-{
-public:
-
- StartElement(const std::string&);
-
- const std::string& getName() const;
-
-private:
-
- const std::string _name;
-};
-
-typedef StartElement se;
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const StartElement& e)
-{
- o.se(e.getName());
- return o;
-}
-
-class ICE_UTIL_API Attribute
-{
-public:
-
- Attribute(const ::std::string&, const ::std::string&);
-
- const ::std::string& getName() const;
- const ::std::string& getValue() const;
-
-private:
-
- const ::std::string _name;
- const ::std::string _value;
-};
-
-typedef Attribute attr;
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const Attribute& e)
-{
- o.attr(e.getName(), e.getValue());
- return o;
-}
-
-class ICE_UTIL_API StartEscapes
-{
-};
-extern ICE_UTIL_API StartEscapes startEscapes;
-
-class ICE_UTIL_API EndEscapes
-{
-};
-extern ICE_UTIL_API EndEscapes endEscapes;
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const StartEscapes&)
-{
- o.startEscapes();
- return o;
-}
-
-template<>
-inline XMLOutput&
-operator<<(XMLOutput& o, const EndEscapes&)
-{
- o.endEscapes();
- return o;
-}
-
-ICE_UTIL_API XMLOutput& operator<<(XMLOutput&, std::ios_base& (*)(std::ios_base&));
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_OUTPUT_UTIL_H +#define ICE_UTIL_OUTPUT_UTIL_H + +#include <IceUtil/Config.h> +#include <fstream> +#include <stack> + +namespace IceUtil +{ + +// ---------------------------------------------------------------------- +// OutputBase +// ---------------------------------------------------------------------- + +// +// Technically it's not necessary to have print() & nl() as virtual +// since the opeator<< functions are specific to each OutputBase +// derivative. However, since it's possible to call print() & nl() +// manually I've decided to leave them as virtual. +// + +class ICE_UTIL_API OutputBase : public ::IceUtil::noncopyable +{ +public: + + OutputBase(); + OutputBase(std::ostream&); + OutputBase(const char*); + virtual ~OutputBase(); + + void setIndent(int); // What is the indent level? + void setUseTab(bool); // Should we output tabs? + + void open(const char*); // Open output stream. + + virtual void print(const char*); // Print a string. + + void inc(); // Increment indentation level. + void dec(); // Decrement indentation level. + + void useCurrentPosAsIndent(); // Save the current position as indentation. + void zeroIndent(); // Use zero identation. + void restoreIndent(); // Restore indentation. + + virtual void nl(); // Print newline. + void sp(); // Print separator. + + bool operator!() const; // Check whether the output state is ok. + + std::streamsize width() const; + std::streamsize width(std::streamsize newWidth); + + std::ios_base::fmtflags flags() const; + std::ios_base::fmtflags flags(std::ios_base::fmtflags newFlags); + + std::ostream::char_type fill() const; + std::ostream::char_type fill(std::ostream::char_type newFill); + +protected: + + std::ofstream _fout; + std::ostream& _out; + int _pos; + int _indent; + int _indentSize; + std::stack<int> _indentSave; + bool _useTab; + bool _separator; +}; + +class ICE_UTIL_API NextLine +{ +}; +extern ICE_UTIL_API NextLine nl; + +class ICE_UTIL_API Separator +{ +}; +extern ICE_UTIL_API Separator sp; + +// ---------------------------------------------------------------------- +// Output +// ---------------------------------------------------------------------- + +class ICE_UTIL_API Output : public OutputBase +{ +public: + + Output(); + Output(std::ostream&); + Output(const char*); + + void setBeginBlock(const char *); // what do we use at block starts? + void setEndBlock(const char *); // what do we use the block end? + + void sb(); // Start a block. + void eb(); // End a block. + +private: + + std::string _blockStart; + std::string _blockEnd; +}; + +template<typename T> +Output& +operator<<(Output& out, const T& val) +{ + std::ostringstream s; + s << val; + out.print(s.str().c_str()); + return out; +} + +template<> +inline Output& +operator<<(Output& o, const NextLine&) +{ + o.nl(); + return o; +} + +template<> +inline Output& +operator<<(Output& o, const Separator&) +{ + o.sp(); + return o; +} + +class ICE_UTIL_API StartBlock +{ +}; +extern ICE_UTIL_API StartBlock sb; + +template<> +inline Output& +operator<<(Output& o, const StartBlock&) +{ + o.sb(); + return o; +} + +class ICE_UTIL_API EndBlock +{ +}; +extern ICE_UTIL_API EndBlock eb; + +template<> +inline Output& +operator<<(Output& o, const EndBlock&) +{ + o.eb(); + return o; +} + +ICE_UTIL_API Output& operator<<(Output&, std::ios_base& (*)(std::ios_base&)); + +// ---------------------------------------------------------------------- +// XMLOutput +// ---------------------------------------------------------------------- + +class ICE_UTIL_API XMLOutput : public OutputBase +{ +public: + + XMLOutput(); + XMLOutput(std::ostream&); + XMLOutput(const char*); + + void setSGML(bool); + virtual void print(const char*); // Print a string. + + virtual void nl(); // Print newline. + + void se(const std::string&); // Start an element. + void ee(); // End an element. + void attr(const std::string&, const std::string&); // Add an attribute to an element. + + void startEscapes(); + void endEscapes(); + + std::string currentElement() const; + +private: + + ::std::string escape(const ::std::string&) const; + + std::stack<std::string> _elementStack; + + bool _se; + bool _text; + + bool _sgml; + bool _escape; +}; + +template<typename T> +XMLOutput& +operator<<(XMLOutput& out, const T& val) +{ + std::ostringstream s; + s << val; + out.print(s.str().c_str()); + return out; +} + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const NextLine&) +{ + o.nl(); + return o; +} + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const Separator&) +{ + o.sp(); + return o; +} + +class ICE_UTIL_API EndElement +{ +}; +extern ICE_UTIL_API EndElement ee; + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const EndElement&) +{ + o.ee(); + return o; +} + +class ICE_UTIL_API StartElement +{ +public: + + StartElement(const std::string&); + + const std::string& getName() const; + +private: + + const std::string _name; +}; + +typedef StartElement se; + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const StartElement& e) +{ + o.se(e.getName()); + return o; +} + +class ICE_UTIL_API Attribute +{ +public: + + Attribute(const ::std::string&, const ::std::string&); + + const ::std::string& getName() const; + const ::std::string& getValue() const; + +private: + + const ::std::string _name; + const ::std::string _value; +}; + +typedef Attribute attr; + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const Attribute& e) +{ + o.attr(e.getName(), e.getValue()); + return o; +} + +class ICE_UTIL_API StartEscapes +{ +}; +extern ICE_UTIL_API StartEscapes startEscapes; + +class ICE_UTIL_API EndEscapes +{ +}; +extern ICE_UTIL_API EndEscapes endEscapes; + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const StartEscapes&) +{ + o.startEscapes(); + return o; +} + +template<> +inline XMLOutput& +operator<<(XMLOutput& o, const EndEscapes&) +{ + o.endEscapes(); + return o; +} + +ICE_UTIL_API XMLOutput& operator<<(XMLOutput&, std::ios_base& (*)(std::ios_base&)); + +} + +#endif diff --git a/cpp/include/IceUtil/RWRecMutex.h b/cpp/include/IceUtil/RWRecMutex.h index cc09ab4113e..23eba24b62d 100644 --- a/cpp/include/IceUtil/RWRecMutex.h +++ b/cpp/include/IceUtil/RWRecMutex.h @@ -1,256 +1,256 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_RW_REC_MUTEX_H
-#define ICE_UTIL_RW_REC_MUTEX_H
-
-#include <IceUtil/Mutex.h>
-#include <IceUtil/Cond.h>
-#include <IceUtil/Thread.h>
-
-namespace IceUtil
-{
-
-template <typename T>
-class RLock
-{
-public:
-
- RLock(const T& mutex) :
- _mutex(mutex)
- {
- _mutex.readlock();
- }
-
- ~RLock()
- {
- _mutex.unlock();
- }
-
- void
- upgrade()
- {
- _mutex.upgrade();
- }
-
- void
- timedUpgrade(const Time& timeout)
- {
- _mutex.timedUpgrade(timeout);
- }
-
-private:
-
- const T& _mutex;
-};
-
-template <typename T>
-class TryRLock
-{
-public:
-
- TryRLock(const T& mutex) :
- _mutex(mutex)
- {
- _mutex.tryReadlock();
- }
-
- TryRLock(const T& mutex, const Time& timeout) :
- _mutex(mutex)
- {
- _mutex.timedTryReadlock(timeout);
- }
-
- ~TryRLock()
- {
- _mutex.unlock();
- }
-
- void
- upgrade()
- {
- _mutex.upgrade();
- }
-
- void
- timedUpgrade(const Time& timeout)
- {
- _mutex.timedUpgrade(timeout);
- }
-
-private:
-
- const T& _mutex;
-};
-
-template <typename T>
-class WLock
-{
-public:
-
- WLock(const T& mutex) :
- _mutex(mutex)
- {
- _mutex.writelock();
- }
-
- ~WLock()
- {
- _mutex.unlock();
- }
-
-private:
-
- const T& _mutex;
-};
-
-template <typename T>
-class TryWLock
-{
-public:
-
- TryWLock(const T& mutex) :
- _mutex(mutex)
- {
- _mutex.tryWritelock();
- }
-
- TryWLock(const T& mutex, const Time& timeout) :
- _mutex(mutex)
- {
- _mutex.timedTryWritelock(timeout);
- }
-
- ~TryWLock()
- {
- _mutex.unlock();
- }
-
-private:
-
- const T& _mutex;
-};
-
-//
-// Concurrency primitive that allows many readers & one writer access
-// to a data structure. Writers have priority over readers. The
-// structure is not strictly fair in that there is no absolute queue
-// of waiting writers - that is managed by a condition variable.
-//
-// Both Reader & Writer mutexes can be recursively locked. Calling
-// WLock (or TryWLock) while holding a read lock promotes the reader
-// to a writer lock.
-//
-class ICE_UTIL_API RWRecMutex
-{
-public:
-
- //
- // RLock (reader) & WLock (writer) typedefs.
- //
- typedef RLock<RWRecMutex> RLock;
- typedef TryRLock<RWRecMutex> TryRLock;
- typedef WLock<RWRecMutex> WLock;
- typedef TryWLock<RWRecMutex> TryWLock;
-
- RWRecMutex();
- ~RWRecMutex();
-
- //
- // Note that readlock/writelock & unlock in general should not be
- // used directly. Instead use RLock & WLock.
- //
-
- //
- // Acquire a read lock.
- //
- void readlock() const;
-
- //
- // Try to acquire a read lock.
- //
- void tryReadlock() const;
-
- //
- // Try to acquire a read lock for upto the given timeout.
- //
- void timedTryReadlock(const Time&) const;
-
- //
- // Acquire a write lock.
- //
- void writelock() const;
-
- //
- // Acquire a write lock.
- //
- void tryWritelock() const;
-
- //
- // Acquire a write lock for up to the given timeout.
- //
- void timedTryWritelock(const Time&) const;
-
- //
- // Unlock the reader/writer lock.
- //
- void unlock() const;
-
- //
- // Upgrade the read lock to a writer lock. Note that this method
- // can only be called if the reader lock is not held recursively.
- //
- void upgrade() const;
-
- //
- // Upgrade the read lock to a writer lock for up to the given
- // timeout Note that this method can only be called if the reader
- // lock is not held recursively.
- //
- void timedUpgrade(const Time&) const;
-
-private:
-
- // noncopyable
- RWRecMutex(const RWRecMutex&);
- void operator=(const RWRecMutex&);
-
- //
- // Number of readers holding the lock. A positive number indicates
- // readers are active. A negative number means that a writer is
- // active.
- //
- mutable int _count;
-
- //
- // If there is an active writer this is a control for thread.
- //
- mutable ThreadControl _writerControl;
-
- //
- // Number of waiting writers.
- //
- mutable unsigned int _waitingWriters;
-
- //
- // Internal mutex.
- //
- Mutex _mutex;
-
- //
- // Two condition variables for waiting readers & writers.
- //
- mutable Cond _readers;
- mutable Cond _writers;
-};
-
-} // End namespace IceUtil
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_RW_REC_MUTEX_H +#define ICE_UTIL_RW_REC_MUTEX_H + +#include <IceUtil/Mutex.h> +#include <IceUtil/Cond.h> +#include <IceUtil/Thread.h> + +namespace IceUtil +{ + +template <typename T> +class RLock +{ +public: + + RLock(const T& mutex) : + _mutex(mutex) + { + _mutex.readlock(); + } + + ~RLock() + { + _mutex.unlock(); + } + + void + upgrade() + { + _mutex.upgrade(); + } + + void + timedUpgrade(const Time& timeout) + { + _mutex.timedUpgrade(timeout); + } + +private: + + const T& _mutex; +}; + +template <typename T> +class TryRLock +{ +public: + + TryRLock(const T& mutex) : + _mutex(mutex) + { + _mutex.tryReadlock(); + } + + TryRLock(const T& mutex, const Time& timeout) : + _mutex(mutex) + { + _mutex.timedTryReadlock(timeout); + } + + ~TryRLock() + { + _mutex.unlock(); + } + + void + upgrade() + { + _mutex.upgrade(); + } + + void + timedUpgrade(const Time& timeout) + { + _mutex.timedUpgrade(timeout); + } + +private: + + const T& _mutex; +}; + +template <typename T> +class WLock +{ +public: + + WLock(const T& mutex) : + _mutex(mutex) + { + _mutex.writelock(); + } + + ~WLock() + { + _mutex.unlock(); + } + +private: + + const T& _mutex; +}; + +template <typename T> +class TryWLock +{ +public: + + TryWLock(const T& mutex) : + _mutex(mutex) + { + _mutex.tryWritelock(); + } + + TryWLock(const T& mutex, const Time& timeout) : + _mutex(mutex) + { + _mutex.timedTryWritelock(timeout); + } + + ~TryWLock() + { + _mutex.unlock(); + } + +private: + + const T& _mutex; +}; + +// +// Concurrency primitive that allows many readers & one writer access +// to a data structure. Writers have priority over readers. The +// structure is not strictly fair in that there is no absolute queue +// of waiting writers - that is managed by a condition variable. +// +// Both Reader & Writer mutexes can be recursively locked. Calling +// WLock (or TryWLock) while holding a read lock promotes the reader +// to a writer lock. +// +class ICE_UTIL_API RWRecMutex +{ +public: + + // + // RLock (reader) & WLock (writer) typedefs. + // + typedef RLock<RWRecMutex> RLock; + typedef TryRLock<RWRecMutex> TryRLock; + typedef WLock<RWRecMutex> WLock; + typedef TryWLock<RWRecMutex> TryWLock; + + RWRecMutex(); + ~RWRecMutex(); + + // + // Note that readlock/writelock & unlock in general should not be + // used directly. Instead use RLock & WLock. + // + + // + // Acquire a read lock. + // + void readlock() const; + + // + // Try to acquire a read lock. + // + void tryReadlock() const; + + // + // Try to acquire a read lock for upto the given timeout. + // + void timedTryReadlock(const Time&) const; + + // + // Acquire a write lock. + // + void writelock() const; + + // + // Acquire a write lock. + // + void tryWritelock() const; + + // + // Acquire a write lock for up to the given timeout. + // + void timedTryWritelock(const Time&) const; + + // + // Unlock the reader/writer lock. + // + void unlock() const; + + // + // Upgrade the read lock to a writer lock. Note that this method + // can only be called if the reader lock is not held recursively. + // + void upgrade() const; + + // + // Upgrade the read lock to a writer lock for up to the given + // timeout Note that this method can only be called if the reader + // lock is not held recursively. + // + void timedUpgrade(const Time&) const; + +private: + + // noncopyable + RWRecMutex(const RWRecMutex&); + void operator=(const RWRecMutex&); + + // + // Number of readers holding the lock. A positive number indicates + // readers are active. A negative number means that a writer is + // active. + // + mutable int _count; + + // + // If there is an active writer this is a control for thread. + // + mutable ThreadControl _writerControl; + + // + // Number of waiting writers. + // + mutable unsigned int _waitingWriters; + + // + // Internal mutex. + // + Mutex _mutex; + + // + // Two condition variables for waiting readers & writers. + // + mutable Cond _readers; + mutable Cond _writers; +}; + +} // End namespace IceUtil + +#endif diff --git a/cpp/include/IceUtil/RecMutex.h b/cpp/include/IceUtil/RecMutex.h index 8aab8c48a21..eec4dca2796 100644 --- a/cpp/include/IceUtil/RecMutex.h +++ b/cpp/include/IceUtil/RecMutex.h @@ -1,112 +1,112 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_RMUTEX_H
-#define ICE_UTIL_RMUTEX_H
-
-#include <IceUtil/Config.h>
-#include <IceUtil/Lock.h>
-#include <IceUtil/ThreadException.h>
-
-namespace IceUtil
-{
-
-//
-// Forward declarations for friend.
-//
-class Cond;
-
-//
-// Recursive Mutex implementation.
-//
-class ICE_UTIL_API RecMutex
-{
-public:
-
- //
- // Lock & TryLock typedefs.
- //
- typedef Lock<RecMutex> Lock;
- typedef TryLock<RecMutex> TryLock;
-
- RecMutex();
- ~RecMutex();
-
- //
- // Note that lock/trylock & unlock in general should not be used
- // directly. Instead use Lock & TryLock.
- //
-
- //
- // The boolean values are for the Monitor implementation which
- // needs to know whether the Mutex has been locked for the first
- // time, or unlocked for the last time (that is another thread is
- // able to acquire the mutex).
- //
-
- //
- // Return true if the mutex has been locked for the first time.
- //
- bool lock() const;
-
- //
- // Throw ThreadLockedException in the case that the lock call would
- // block (that is the mutex is already owned by some other
- // thread). Returns true if the mutex has been locked for the
- // first time.
- //
- bool trylock() const;
-
- //
- // Returns true if the mutex has been unlocked for the last time
- // (false otherwise).
- //
- bool unlock() const;
-
-private:
-
- // noncopyable
- RecMutex(const RecMutex&);
- void operator=(const RecMutex&);
-
- //
- // LockState and the lock/unlock variations are for use by the
- // Condition variable implementation.
- //
-#ifdef _WIN32
- struct LockState
- {
- int count;
- };
-#else
- struct LockState
- {
- pthread_mutex_t* mutex;
- int count;
- };
-#endif
-
- void unlock(LockState&) const;
- void lock(LockState&) const;
-
- friend class Cond;
-
-#ifdef _WIN32
- mutable CRITICAL_SECTION _mutex;
-#else
- mutable pthread_mutex_t _mutex;
-#endif
-
- mutable int _count;
-};
-
-} // End namespace IceUtil
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_RMUTEX_H +#define ICE_UTIL_RMUTEX_H + +#include <IceUtil/Config.h> +#include <IceUtil/Lock.h> +#include <IceUtil/ThreadException.h> + +namespace IceUtil +{ + +// +// Forward declarations for friend. +// +class Cond; + +// +// Recursive Mutex implementation. +// +class ICE_UTIL_API RecMutex +{ +public: + + // + // Lock & TryLock typedefs. + // + typedef Lock<RecMutex> Lock; + typedef TryLock<RecMutex> TryLock; + + RecMutex(); + ~RecMutex(); + + // + // Note that lock/trylock & unlock in general should not be used + // directly. Instead use Lock & TryLock. + // + + // + // The boolean values are for the Monitor implementation which + // needs to know whether the Mutex has been locked for the first + // time, or unlocked for the last time (that is another thread is + // able to acquire the mutex). + // + + // + // Return true if the mutex has been locked for the first time. + // + bool lock() const; + + // + // Throw ThreadLockedException in the case that the lock call would + // block (that is the mutex is already owned by some other + // thread). Returns true if the mutex has been locked for the + // first time. + // + bool trylock() const; + + // + // Returns true if the mutex has been unlocked for the last time + // (false otherwise). + // + bool unlock() const; + +private: + + // noncopyable + RecMutex(const RecMutex&); + void operator=(const RecMutex&); + + // + // LockState and the lock/unlock variations are for use by the + // Condition variable implementation. + // +#ifdef _WIN32 + struct LockState + { + int count; + }; +#else + struct LockState + { + pthread_mutex_t* mutex; + int count; + }; +#endif + + void unlock(LockState&) const; + void lock(LockState&) const; + + friend class Cond; + +#ifdef _WIN32 + mutable CRITICAL_SECTION _mutex; +#else + mutable pthread_mutex_t _mutex; +#endif + + mutable int _count; +}; + +} // End namespace IceUtil + +#endif diff --git a/cpp/include/IceUtil/Shared.h b/cpp/include/IceUtil/Shared.h index 38c72dd343b..b9cc3c3d265 100644 --- a/cpp/include/IceUtil/Shared.h +++ b/cpp/include/IceUtil/Shared.h @@ -1,364 +1,364 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_SHARED_H
-#define ICE_UTIL_SHARED_H
-
-#include <IceUtil/Config.h>
-
-//
-// The inline assembler causes problems with shared libraries.
-//
-#if defined(__ICC) && !defined(_WIN32)
-# define ICE_USE_MUTEX_SHARED
-#endif
-
-#ifdef ICE_USE_MUTEX_SHARED
-# include <IceUtil/Mutex.h>
-#endif
-
-#if !defined(_WIN32) && !defined(ICE_USE_MUTEX_SHARED)
-
-//
-// Linux only. Unfortunately, asm/atomic.h builds non-SMP safe code
-// with non-SMP kernels. This means that executables compiled with a
-// non-SMP kernel would fail randomly due to concurrency errors with
-// reference counting on SMP hosts. Therefore the relevent pieces of
-// atomic.h are more-or-less duplicated.
-//
-
-/*
- * Make sure gcc doesn't try to be clever and move things around
- * on us. We need to use _exactly_ the address the user gave us,
- * not some alias that contains the same information.
- */
-struct ice_atomic_t
-{
- volatile int counter;
-};
-
-/*
- * ice_atomic_set - set ice_atomic variable
- * @v: pointer of type ice_atomic_t
- * @i: required value
- *
- * Atomically sets the value of @v to @i. Note that the guaranteed
- * useful range of an ice_atomic_t is only 24 bits.
- */
-inline void ice_atomic_set(ice_atomic_t* v, int i)
-{
- v->counter = i;
-}
-
-/*
- * ice_atomic_inc - increment ice_atomic variable
- * @v: pointer of type ice_atomic_t
- *
- * Atomically increments @v by 1. Note that the guaranteed useful
- * range of an ice_atomic_t is only 24 bits.
- *
- * Inlined because this operation is performance critical.
- */
-inline void ice_atomic_inc(ice_atomic_t *v)
-{
- __asm__ __volatile__(
- "lock ; incl %0"
- :"=m" (v->counter)
- :"m" (v->counter));
-}
-
-/**
- * ice_atomic_dec_and_test - decrement and test
- * @v: pointer of type ice_atomic_t
- *
- * Atomically decrements @v by 1 and returns true if the result is 0,
- * or false for all other cases. Note that the guaranteed useful
- * range of an ice_atomic_t is only 24 bits.
- *
- * Inlined because this operation is performance critical.
- */
-inline int ice_atomic_dec_and_test(ice_atomic_t *v)
-{
- unsigned char c;
- __asm__ __volatile__(
- "lock ; decl %0; sete %1"
- :"=m" (v->counter), "=qm" (c)
- :"m" (v->counter) : "memory");
- return c != 0;
-}
-
-/**
- * ice_atomic_exchange_add - same as InterlockedExchangeAdd. This
- * didn't come from atomic.h (the code was derived from similar code
- * in /usr/include/asm/rwsem.h)
- *
- * Inlined because this operation is performance critical.
- */
-inline int ice_atomic_exchange_add(int i, ice_atomic_t* v)
-{
- int tmp = i;
- __asm__ __volatile__(
- "lock ; xadd %0,(%2)"
- :"+r"(tmp), "=m"(v->counter)
- :"r"(v), "m"(v->counter)
- : "memory");
- return tmp + i;
-}
-
-#endif
-
-//
-// Base classes for reference counted types. The IceUtil::Handle
-// template can be used for smart pointers to types derived from these
-// bases.
-//
-// IceUtil::SimpleShared
-// =====================
-//
-// A non thread-safe base class for reference-counted types.
-//
-// IceUtil::Shared
-// ===============
-//
-// A thread-safe base class for reference-counted types.
-//
-namespace IceUtil
-{
-
-//
-// TODO: Not all operations in this class are performance critical,
-// thus not all of them should be inlined.
-//
-
-class SimpleShared : public noncopyable
-{
-public:
-
- SimpleShared() :
- _ref(0),
- _noDelete(false)
- {
- }
-
- virtual ~SimpleShared()
- {
- }
-
- void __incRef()
- {
- assert(_ref >= 0);
- ++_ref;
- }
-
- void __decRef()
- {
- assert(_ref > 0);
- if(--_ref == 0)
- {
- if(!_noDelete)
- {
- delete this;
- }
- }
- }
-
- int __getRef() const
- {
- return _ref;
- }
-
- void __setNoDelete(bool b)
- {
- _noDelete = b;
- }
-
-private:
-
- int _ref;
- bool _noDelete;
-};
-
-class Shared : public noncopyable
-{
-public:
-
- Shared();
- virtual ~Shared();
- void __incRef();
- void __decRef();
- int __getRef() const;
- void __setNoDelete(bool);
-
-private:
-
-#ifdef ICE_USE_MUTEX_SHARED
- int _ref;
- Mutex _mutex;
-#elif defined(_WIN32)
- LONG _ref;
-#else
- ice_atomic_t _ref;
-#endif
- bool _noDelete;
-};
-
-//
-// TODO: Not all operations below are performance critical, thus not
-// all of them should be inlined.
-//
-
-#ifdef ICE_USE_MUTEX_SHARED
-
-inline
-Shared::Shared() :
- _ref(0),
- _noDelete(false)
-{
-}
-
-inline
-Shared::~Shared()
-{
-}
-
-inline void
-Shared::__incRef()
-{
- _mutex.lock();
- assert(_ref >= 0);
- ++_ref;
- _mutex.unlock();
-}
-
-inline void
-Shared::__decRef()
-{
- _mutex.lock();
- bool doDelete = false;
- assert(_ref > 0);
- if(--_ref == 0)
- {
- doDelete = !_noDelete;
- }
- _mutex.unlock();
- if(doDelete)
- {
- delete this;
- }
-}
-
-inline int
-Shared::__getRef() const
-{
- _mutex.lock();
- int ref = _ref;
- _mutex.unlock();
- return ref;
-}
-
-inline void
-Shared::__setNoDelete(bool b)
-{
- _mutex.lock();
- _noDelete = b;
- _mutex.unlock();
-}
-
-#elif defined(_WIN32)
-
-inline
-Shared::Shared() :
- _ref(0),
- _noDelete(false)
-{
-}
-
-inline
-Shared::~Shared()
-{
-}
-
-inline void
-Shared::__incRef()
-{
- assert(InterlockedExchangeAdd(&_ref, 0) >= 0);
- InterlockedIncrement(&_ref);
-}
-
-inline void
-Shared::__decRef()
-{
- assert(InterlockedExchangeAdd(&_ref, 0) > 0);
- if(InterlockedDecrement(&_ref) == 0 && !_noDelete)
- {
- delete this;
- }
-}
-
-inline int
-Shared::__getRef() const
-{
- return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0);
-}
-
-inline void
-Shared::__setNoDelete(bool b)
-{
- _noDelete = b;
-}
-
-#else
-
-inline
-Shared::Shared() :
- _noDelete(false)
-{
- ice_atomic_set(&_ref, 0);
-}
-
-inline
-Shared::~Shared()
-{
-}
-
-inline void
-Shared::__incRef()
-{
- assert(ice_atomic_exchange_add(0, &_ref) >= 0);
- ice_atomic_inc(&_ref);
-}
-
-inline void
-Shared::__decRef()
-{
- assert(ice_atomic_exchange_add(0, &_ref) > 0);
- if(ice_atomic_dec_and_test(&_ref) && !_noDelete)
- {
- delete this;
- }
-}
-
-inline int
-Shared::__getRef() const
-{
- return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref));
-}
-
-inline void
-Shared::__setNoDelete(bool b)
-{
- _noDelete = b;
-}
-
-#endif
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_SHARED_H +#define ICE_UTIL_SHARED_H + +#include <IceUtil/Config.h> + +// +// The inline assembler causes problems with shared libraries. +// +#if defined(__ICC) && !defined(_WIN32) +# define ICE_USE_MUTEX_SHARED +#endif + +#ifdef ICE_USE_MUTEX_SHARED +# include <IceUtil/Mutex.h> +#endif + +#if !defined(_WIN32) && !defined(ICE_USE_MUTEX_SHARED) + +// +// Linux only. Unfortunately, asm/atomic.h builds non-SMP safe code +// with non-SMP kernels. This means that executables compiled with a +// non-SMP kernel would fail randomly due to concurrency errors with +// reference counting on SMP hosts. Therefore the relevent pieces of +// atomic.h are more-or-less duplicated. +// + +/* + * Make sure gcc doesn't try to be clever and move things around + * on us. We need to use _exactly_ the address the user gave us, + * not some alias that contains the same information. + */ +struct ice_atomic_t +{ + volatile int counter; +}; + +/* + * ice_atomic_set - set ice_atomic variable + * @v: pointer of type ice_atomic_t + * @i: required value + * + * Atomically sets the value of @v to @i. Note that the guaranteed + * useful range of an ice_atomic_t is only 24 bits. + */ +inline void ice_atomic_set(ice_atomic_t* v, int i) +{ + v->counter = i; +} + +/* + * ice_atomic_inc - increment ice_atomic variable + * @v: pointer of type ice_atomic_t + * + * Atomically increments @v by 1. Note that the guaranteed useful + * range of an ice_atomic_t is only 24 bits. + * + * Inlined because this operation is performance critical. + */ +inline void ice_atomic_inc(ice_atomic_t *v) +{ + __asm__ __volatile__( + "lock ; incl %0" + :"=m" (v->counter) + :"m" (v->counter)); +} + +/** + * ice_atomic_dec_and_test - decrement and test + * @v: pointer of type ice_atomic_t + * + * Atomically decrements @v by 1 and returns true if the result is 0, + * or false for all other cases. Note that the guaranteed useful + * range of an ice_atomic_t is only 24 bits. + * + * Inlined because this operation is performance critical. + */ +inline int ice_atomic_dec_and_test(ice_atomic_t *v) +{ + unsigned char c; + __asm__ __volatile__( + "lock ; decl %0; sete %1" + :"=m" (v->counter), "=qm" (c) + :"m" (v->counter) : "memory"); + return c != 0; +} + +/** + * ice_atomic_exchange_add - same as InterlockedExchangeAdd. This + * didn't come from atomic.h (the code was derived from similar code + * in /usr/include/asm/rwsem.h) + * + * Inlined because this operation is performance critical. + */ +inline int ice_atomic_exchange_add(int i, ice_atomic_t* v) +{ + int tmp = i; + __asm__ __volatile__( + "lock ; xadd %0,(%2)" + :"+r"(tmp), "=m"(v->counter) + :"r"(v), "m"(v->counter) + : "memory"); + return tmp + i; +} + +#endif + +// +// Base classes for reference counted types. The IceUtil::Handle +// template can be used for smart pointers to types derived from these +// bases. +// +// IceUtil::SimpleShared +// ===================== +// +// A non thread-safe base class for reference-counted types. +// +// IceUtil::Shared +// =============== +// +// A thread-safe base class for reference-counted types. +// +namespace IceUtil +{ + +// +// TODO: Not all operations in this class are performance critical, +// thus not all of them should be inlined. +// + +class SimpleShared : public noncopyable +{ +public: + + SimpleShared() : + _ref(0), + _noDelete(false) + { + } + + virtual ~SimpleShared() + { + } + + void __incRef() + { + assert(_ref >= 0); + ++_ref; + } + + void __decRef() + { + assert(_ref > 0); + if(--_ref == 0) + { + if(!_noDelete) + { + delete this; + } + } + } + + int __getRef() const + { + return _ref; + } + + void __setNoDelete(bool b) + { + _noDelete = b; + } + +private: + + int _ref; + bool _noDelete; +}; + +class Shared : public noncopyable +{ +public: + + Shared(); + virtual ~Shared(); + void __incRef(); + void __decRef(); + int __getRef() const; + void __setNoDelete(bool); + +private: + +#ifdef ICE_USE_MUTEX_SHARED + int _ref; + Mutex _mutex; +#elif defined(_WIN32) + LONG _ref; +#else + ice_atomic_t _ref; +#endif + bool _noDelete; +}; + +// +// TODO: Not all operations below are performance critical, thus not +// all of them should be inlined. +// + +#ifdef ICE_USE_MUTEX_SHARED + +inline +Shared::Shared() : + _ref(0), + _noDelete(false) +{ +} + +inline +Shared::~Shared() +{ +} + +inline void +Shared::__incRef() +{ + _mutex.lock(); + assert(_ref >= 0); + ++_ref; + _mutex.unlock(); +} + +inline void +Shared::__decRef() +{ + _mutex.lock(); + bool doDelete = false; + assert(_ref > 0); + if(--_ref == 0) + { + doDelete = !_noDelete; + } + _mutex.unlock(); + if(doDelete) + { + delete this; + } +} + +inline int +Shared::__getRef() const +{ + _mutex.lock(); + int ref = _ref; + _mutex.unlock(); + return ref; +} + +inline void +Shared::__setNoDelete(bool b) +{ + _mutex.lock(); + _noDelete = b; + _mutex.unlock(); +} + +#elif defined(_WIN32) + +inline +Shared::Shared() : + _ref(0), + _noDelete(false) +{ +} + +inline +Shared::~Shared() +{ +} + +inline void +Shared::__incRef() +{ + assert(InterlockedExchangeAdd(&_ref, 0) >= 0); + InterlockedIncrement(&_ref); +} + +inline void +Shared::__decRef() +{ + assert(InterlockedExchangeAdd(&_ref, 0) > 0); + if(InterlockedDecrement(&_ref) == 0 && !_noDelete) + { + delete this; + } +} + +inline int +Shared::__getRef() const +{ + return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0); +} + +inline void +Shared::__setNoDelete(bool b) +{ + _noDelete = b; +} + +#else + +inline +Shared::Shared() : + _noDelete(false) +{ + ice_atomic_set(&_ref, 0); +} + +inline +Shared::~Shared() +{ +} + +inline void +Shared::__incRef() +{ + assert(ice_atomic_exchange_add(0, &_ref) >= 0); + ice_atomic_inc(&_ref); +} + +inline void +Shared::__decRef() +{ + assert(ice_atomic_exchange_add(0, &_ref) > 0); + if(ice_atomic_dec_and_test(&_ref) && !_noDelete) + { + delete this; + } +} + +inline int +Shared::__getRef() const +{ + return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref)); +} + +inline void +Shared::__setNoDelete(bool b) +{ + _noDelete = b; +} + +#endif + +} + +#endif diff --git a/cpp/include/IceUtil/Thread.h b/cpp/include/IceUtil/Thread.h index 172c411c18c..2d96293bd23 100644 --- a/cpp/include/IceUtil/Thread.h +++ b/cpp/include/IceUtil/Thread.h @@ -1,117 +1,117 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_THREAD_H
-#define ICE_UTIL_THREAD_H
-
-#include <IceUtil/Shared.h>
-#include <IceUtil/Handle.h>
-
-namespace IceUtil
-{
-
-class Time;
-
-#ifdef _WIN32
-struct HandleWrapper : public Shared
-{
- // Inline for performance reasons.
- HandleWrapper(HANDLE h) :
- handle(h)
- {
- }
-
- // Inline for performance reasons.
- virtual ~HandleWrapper()
- {
- if(handle)
- {
- CloseHandle(handle);
- }
- }
-
- HANDLE handle;
-};
-
-typedef Handle<HandleWrapper> HandleWrapperPtr;
-#endif
-
-class ICE_UTIL_API ThreadControl
-{
-public:
-
- ThreadControl();
-
-#ifdef _WIN32
- ThreadControl(const HandleWrapperPtr&, unsigned);
-#else
- ThreadControl(pthread_t);
-#endif
-
- bool operator==(const ThreadControl&) const;
- bool operator!=(const ThreadControl&) const;
- bool operator<(const ThreadControl&) const;
-
- //
- // Wait until the controlled thread terminates. The call has POSIX
- // semantics.
- //
- // At most one thread can wait for the termination of a given
- // thread.C alling join on a thread on which another thread is
- // already waiting for termination results in undefined behaviour.
- //
- void join();
-
- static void sleep(const Time&);
- static void yield();
-
-private:
-
-#ifdef _WIN32
- HandleWrapperPtr _handle;
- unsigned _id;
-#else
- pthread_t _id;
-#endif
-};
-
-class ICE_UTIL_API Thread : virtual public IceUtil::Shared
-{
-public:
-
- Thread();
- virtual ~Thread();
-
- virtual void run() = 0;
-
- ThreadControl start();
-
- ThreadControl getThreadControl();
-
- bool operator==(const Thread&) const;
- bool operator!=(const Thread&) const;
- bool operator<(const Thread&) const;
-
-private:
-
-#ifdef _WIN32
- unsigned _id;
- HandleWrapperPtr _handle;
-#else
- pthread_t _id;
-#endif
-};
-
-typedef Handle<Thread> ThreadPtr;
-
-}
-
-#endif
-
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_THREAD_H +#define ICE_UTIL_THREAD_H + +#include <IceUtil/Shared.h> +#include <IceUtil/Handle.h> + +namespace IceUtil +{ + +class Time; + +#ifdef _WIN32 +struct HandleWrapper : public Shared +{ + // Inline for performance reasons. + HandleWrapper(HANDLE h) : + handle(h) + { + } + + // Inline for performance reasons. + virtual ~HandleWrapper() + { + if(handle) + { + CloseHandle(handle); + } + } + + HANDLE handle; +}; + +typedef Handle<HandleWrapper> HandleWrapperPtr; +#endif + +class ICE_UTIL_API ThreadControl +{ +public: + + ThreadControl(); + +#ifdef _WIN32 + ThreadControl(const HandleWrapperPtr&, unsigned); +#else + ThreadControl(pthread_t); +#endif + + bool operator==(const ThreadControl&) const; + bool operator!=(const ThreadControl&) const; + bool operator<(const ThreadControl&) const; + + // + // Wait until the controlled thread terminates. The call has POSIX + // semantics. + // + // At most one thread can wait for the termination of a given + // thread.C alling join on a thread on which another thread is + // already waiting for termination results in undefined behaviour. + // + void join(); + + static void sleep(const Time&); + static void yield(); + +private: + +#ifdef _WIN32 + HandleWrapperPtr _handle; + unsigned _id; +#else + pthread_t _id; +#endif +}; + +class ICE_UTIL_API Thread : virtual public IceUtil::Shared +{ +public: + + Thread(); + virtual ~Thread(); + + virtual void run() = 0; + + ThreadControl start(); + + ThreadControl getThreadControl(); + + bool operator==(const Thread&) const; + bool operator!=(const Thread&) const; + bool operator<(const Thread&) const; + +private: + +#ifdef _WIN32 + unsigned _id; + HandleWrapperPtr _handle; +#else + pthread_t _id; +#endif +}; + +typedef Handle<Thread> ThreadPtr; + +} + +#endif + diff --git a/cpp/include/IceUtil/ThreadException.h b/cpp/include/IceUtil/ThreadException.h index 9d4cdc9ad6d..5e9b409f556 100644 --- a/cpp/include/IceUtil/ThreadException.h +++ b/cpp/include/IceUtil/ThreadException.h @@ -1,47 +1,47 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_THREAD_EXCEPTION_H
-#define ICE_UTIL_THREAD_EXCEPTION_H
-
-#include <IceUtil/Exception.h>
-
-namespace IceUtil
-{
-
-class ICE_UTIL_API ThreadSyscallException : public Exception
-{
-public:
-
- ThreadSyscallException(const char*, int);
- virtual std::string ice_name() const;
- virtual void ice_print(std::ostream&) const;
- virtual Exception* ice_clone() const;
- virtual void ice_throw() const;
-
-private:
-
- const int _error;
-};
-
-class ICE_UTIL_API ThreadLockedException : public Exception
-{
-public:
-
- ThreadLockedException(const char*, int);
- virtual std::string ice_name() const;
- virtual Exception* ice_clone() const;
- virtual void ice_throw() const;
-};
-
-}
-
-#endif
-
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_THREAD_EXCEPTION_H +#define ICE_UTIL_THREAD_EXCEPTION_H + +#include <IceUtil/Exception.h> + +namespace IceUtil +{ + +class ICE_UTIL_API ThreadSyscallException : public Exception +{ +public: + + ThreadSyscallException(const char*, int); + virtual std::string ice_name() const; + virtual void ice_print(std::ostream&) const; + virtual Exception* ice_clone() const; + virtual void ice_throw() const; + +private: + + const int _error; +}; + +class ICE_UTIL_API ThreadLockedException : public Exception +{ +public: + + ThreadLockedException(const char*, int); + virtual std::string ice_name() const; + virtual Exception* ice_clone() const; + virtual void ice_throw() const; +}; + +} + +#endif + diff --git a/cpp/include/IceUtil/Time.h b/cpp/include/IceUtil/Time.h index 241919dbb8a..ad59dd847b6 100644 --- a/cpp/include/IceUtil/Time.h +++ b/cpp/include/IceUtil/Time.h @@ -1,63 +1,63 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_TIME_H
-#define ICE_UTIL_TIME_H
-
-#include <IceUtil/Config.h>
-
-namespace IceUtil
-{
-
-class ICE_UTIL_API Time
-{
-public:
-
- Time();
-
- // No copy constructor and assignment operator necessary. The
- // automatically generated copy constructor and assignment
- // operator do the right thing.
-
- static Time now();
- static Time seconds(long);
- static Time milliSeconds(long);
-#ifdef _WIN32
- static Time microSeconds(__int64);
-#else
- static Time microSeconds(long long);
-#endif
-
- Time operator-() const;
- Time operator-(const Time&) const;
- Time operator+(const Time&) const;
- Time& operator+=(const Time&);
- Time& operator-=(const Time&);
-
- bool operator<(const Time&) const;
- bool operator<=(const Time&) const;
- bool operator>(const Time&) const;
- bool operator>=(const Time&) const;
- bool operator==(const Time&) const;
- bool operator!=(const Time&) const;
-
- operator timeval() const;
- operator double() const;
-
-private:
-
- Time(Int64);
-
- Int64 _usec;
-};
-
-} // End namespace IceUtil
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_TIME_H +#define ICE_UTIL_TIME_H + +#include <IceUtil/Config.h> + +namespace IceUtil +{ + +class ICE_UTIL_API Time +{ +public: + + Time(); + + // No copy constructor and assignment operator necessary. The + // automatically generated copy constructor and assignment + // operator do the right thing. + + static Time now(); + static Time seconds(long); + static Time milliSeconds(long); +#ifdef _WIN32 + static Time microSeconds(__int64); +#else + static Time microSeconds(long long); +#endif + + Time operator-() const; + Time operator-(const Time&) const; + Time operator+(const Time&) const; + Time& operator+=(const Time&); + Time& operator-=(const Time&); + + bool operator<(const Time&) const; + bool operator<=(const Time&) const; + bool operator>(const Time&) const; + bool operator>=(const Time&) const; + bool operator==(const Time&) const; + bool operator!=(const Time&) const; + + operator timeval() const; + operator double() const; + +private: + + Time(Int64); + + Int64 _usec; +}; + +} // End namespace IceUtil + +#endif diff --git a/cpp/include/IceUtil/UUID.h b/cpp/include/IceUtil/UUID.h index 0c6ee035c08..2de0c61718f 100644 --- a/cpp/include/IceUtil/UUID.h +++ b/cpp/include/IceUtil/UUID.h @@ -1,23 +1,23 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_UUID_H
-#define ICE_UTIL_UUID_H
-
-#include <IceUtil/Config.h>
-
-namespace IceUtil
-{
-
-ICE_UTIL_API std::string generateUUID();
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_UUID_H +#define ICE_UTIL_UUID_H + +#include <IceUtil/Config.h> + +namespace IceUtil +{ + +ICE_UTIL_API std::string generateUUID(); + +} + +#endif diff --git a/cpp/include/IceUtil/Unicode.h b/cpp/include/IceUtil/Unicode.h index 6fb8e6304cd..ca4cd774750 100644 --- a/cpp/include/IceUtil/Unicode.h +++ b/cpp/include/IceUtil/Unicode.h @@ -1,24 +1,24 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#ifndef ICE_UTIL_UNICODE_H
-#define ICE_UTIL_UNICODE_H
-
-#include <IceUtil/Config.h>
-
-namespace IceUtil
-{
-
-ICE_UTIL_API std::string wstringToString(const std::wstring&);
-ICE_UTIL_API std::wstring stringToWstring(const std::string&);
-
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#ifndef ICE_UTIL_UNICODE_H +#define ICE_UTIL_UNICODE_H + +#include <IceUtil/Config.h> + +namespace IceUtil +{ + +ICE_UTIL_API std::string wstringToString(const std::wstring&); +ICE_UTIL_API std::wstring stringToWstring(const std::string&); + +} + +#endif diff --git a/cpp/src/IceUtil/Base64.cpp b/cpp/src/IceUtil/Base64.cpp index 078448c8106..00ef4d55a68 100644 --- a/cpp/src/IceUtil/Base64.cpp +++ b/cpp/src/IceUtil/Base64.cpp @@ -1,271 +1,271 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/Base64.h>
-#include <iostream>
-
-using namespace std;
-
-string
-IceUtil::Base64::encode(const vector<char>& plainSeq)
-{
- string retval;
-
- if(plainSeq.size() == 0)
- {
- return retval;
- }
-
- // Reserve enough space for the returned base64 string
- unsigned long base64Bytes = (((plainSeq.size() * 4) / 3) + 1);
- unsigned long newlineBytes = (((base64Bytes * 2) / 76) + 1);
- unsigned long totalBytes = base64Bytes + newlineBytes;
-
- retval.reserve(totalBytes);
-
- unsigned char by1 = 0;
- unsigned char by2 = 0;
- unsigned char by3 = 0;
- unsigned char by4 = 0;
- unsigned char by5 = 0;
- unsigned char by6 = 0;
- unsigned char by7 = 0;
-
- for(unsigned long i = 0; i < plainSeq.size(); i += 3)
- {
- by1 = plainSeq[i];
- by2 = 0;
- by3 = 0;
-
- if((i + 1) < plainSeq.size())
- {
- by2 = plainSeq[i+1];
- }
-
- if((i + 2) < plainSeq.size())
- {
- by3 = plainSeq[i+2];
- }
-
- by4 = by1 >> 2;
- by5 = ((by1 & 0x3) << 4) | (by2 >> 4);
- by6 = ((by2 & 0xf) << 2) | (by3 >> 6);
- by7 = by3 & 0x3f;
-
- retval += encode(by4);
- retval += encode(by5);
-
- if((i + 1) < plainSeq.size())
- {
- retval += encode(by6);
- }
- else
- {
- retval += "=";
- }
-
- if((i + 2) < plainSeq.size())
- {
- retval += encode(by7);
- }
- else
- {
- retval += "=";
- }
- }
-
- string outString;
- outString.reserve(totalBytes);
- string::iterator iter = retval.begin();
-
- while((retval.end() - iter) > 76)
- {
- copy(iter, iter+76, back_inserter(outString));
- outString += "\r\n";
- iter += 76;
- }
-
- copy(iter, retval.end(), back_inserter(outString));
-
- return outString;
-}
-
-vector<char>
-IceUtil::Base64::decode(const string& str)
-{
- string newStr;
-
- newStr.reserve(str.length());
-
- for(unsigned long j = 0; j < str.length(); j++)
- {
- if(isBase64(str[j]))
- {
- newStr += str[j];
- }
- }
-
- vector<char> retval;
-
- if(newStr.length() == 0)
- {
- return retval;
- }
-
- // Note: This is how we were previously computing the size of the return
- // sequence. The method below is more efficient (and correct).
- // unsigned long lines = str.size() / 78;
- // unsigned long totalBytes = (lines * 76) + (((str.size() - (lines * 78)) * 3) / 4);
-
- // Figure out how long the final sequence is going to be.
- unsigned long totalBytes = (newStr.size() * 3 / 4) + 1;
-
- retval.reserve(totalBytes);
-
- unsigned char by1 = 0;
- unsigned char by2 = 0;
- unsigned char by3 = 0;
- unsigned char by4 = 0;
-
- char c1, c2, c3, c4;
-
- for(unsigned long i = 0; i < newStr.length(); i += 4)
- {
- c1 = 'A';
- c2 = 'A';
- c3 = 'A';
- c4 = 'A';
-
- c1 = newStr[i];
-
- if((i + 1) < newStr.length())
- {
- c2 = newStr[i + 1];
- }
-
- if((i + 2) < newStr.length())
- {
- c3 = newStr[i + 2];
- }
-
- if((i + 3) < newStr.length())
- {
- c4 = newStr[i + 3];
- }
-
- by1 = decode(c1);
- by2 = decode(c2);
- by3 = decode(c3);
- by4 = decode(c4);
-
- retval.push_back((by1 << 2) | (by2 >> 4));
-
- if(c3 != '=')
- {
- retval.push_back(((by2 & 0xf) << 4) | (by3 >> 2));
- }
-
- if(c4 != '=')
- {
- retval.push_back(((by3 & 0x3) << 6) | by4);
- }
- }
-
- return retval;
-}
-
-char
-IceUtil::Base64::encode(unsigned char uc)
-{
- if(uc < 26)
- {
- return 'A' + uc;
- }
-
- if(uc < 52)
- {
- return 'a' + (uc - 26);
- }
-
- if(uc < 62)
- {
- return '0' + (uc - 52);
- }
-
- if(uc == 62)
- {
- return '+';
- }
-
- return '/';
-}
-
-unsigned char
-IceUtil::Base64::decode(char c)
-{
- if(c >= 'A' && c <= 'Z')
- {
- return c - 'A';
- }
-
- if(c >= 'a' && c <= 'z')
- {
- return c - 'a' + 26;
- }
-
- if(c >= '0' && c <= '9')
- {
- return c - '0' + 52;
- }
-
- if(c == '+')
- {
- return 62;
- }
-
- return 63;
-}
-
-
-bool
-IceUtil::Base64::isBase64(char c)
-{
- if(c >= 'A' && c <= 'Z')
- {
- return true;
- }
-
- if(c >= 'a' && c <= 'z')
- {
- return true;
- }
-
- if(c >= '0' && c <= '9')
- {
- return true;
- }
-
- if(c == '+')
- {
- return true;
- }
-
- if(c == '/')
- {
- return true;
- }
-
- if(c == '=')
- {
- return true;
- }
-
- return false;
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/Base64.h> +#include <iostream> + +using namespace std; + +string +IceUtil::Base64::encode(const vector<char>& plainSeq) +{ + string retval; + + if(plainSeq.size() == 0) + { + return retval; + } + + // Reserve enough space for the returned base64 string + unsigned long base64Bytes = (((plainSeq.size() * 4) / 3) + 1); + unsigned long newlineBytes = (((base64Bytes * 2) / 76) + 1); + unsigned long totalBytes = base64Bytes + newlineBytes; + + retval.reserve(totalBytes); + + unsigned char by1 = 0; + unsigned char by2 = 0; + unsigned char by3 = 0; + unsigned char by4 = 0; + unsigned char by5 = 0; + unsigned char by6 = 0; + unsigned char by7 = 0; + + for(unsigned long i = 0; i < plainSeq.size(); i += 3) + { + by1 = plainSeq[i]; + by2 = 0; + by3 = 0; + + if((i + 1) < plainSeq.size()) + { + by2 = plainSeq[i+1]; + } + + if((i + 2) < plainSeq.size()) + { + by3 = plainSeq[i+2]; + } + + by4 = by1 >> 2; + by5 = ((by1 & 0x3) << 4) | (by2 >> 4); + by6 = ((by2 & 0xf) << 2) | (by3 >> 6); + by7 = by3 & 0x3f; + + retval += encode(by4); + retval += encode(by5); + + if((i + 1) < plainSeq.size()) + { + retval += encode(by6); + } + else + { + retval += "="; + } + + if((i + 2) < plainSeq.size()) + { + retval += encode(by7); + } + else + { + retval += "="; + } + } + + string outString; + outString.reserve(totalBytes); + string::iterator iter = retval.begin(); + + while((retval.end() - iter) > 76) + { + copy(iter, iter+76, back_inserter(outString)); + outString += "\r\n"; + iter += 76; + } + + copy(iter, retval.end(), back_inserter(outString)); + + return outString; +} + +vector<char> +IceUtil::Base64::decode(const string& str) +{ + string newStr; + + newStr.reserve(str.length()); + + for(unsigned long j = 0; j < str.length(); j++) + { + if(isBase64(str[j])) + { + newStr += str[j]; + } + } + + vector<char> retval; + + if(newStr.length() == 0) + { + return retval; + } + + // Note: This is how we were previously computing the size of the return + // sequence. The method below is more efficient (and correct). + // unsigned long lines = str.size() / 78; + // unsigned long totalBytes = (lines * 76) + (((str.size() - (lines * 78)) * 3) / 4); + + // Figure out how long the final sequence is going to be. + unsigned long totalBytes = (newStr.size() * 3 / 4) + 1; + + retval.reserve(totalBytes); + + unsigned char by1 = 0; + unsigned char by2 = 0; + unsigned char by3 = 0; + unsigned char by4 = 0; + + char c1, c2, c3, c4; + + for(unsigned long i = 0; i < newStr.length(); i += 4) + { + c1 = 'A'; + c2 = 'A'; + c3 = 'A'; + c4 = 'A'; + + c1 = newStr[i]; + + if((i + 1) < newStr.length()) + { + c2 = newStr[i + 1]; + } + + if((i + 2) < newStr.length()) + { + c3 = newStr[i + 2]; + } + + if((i + 3) < newStr.length()) + { + c4 = newStr[i + 3]; + } + + by1 = decode(c1); + by2 = decode(c2); + by3 = decode(c3); + by4 = decode(c4); + + retval.push_back((by1 << 2) | (by2 >> 4)); + + if(c3 != '=') + { + retval.push_back(((by2 & 0xf) << 4) | (by3 >> 2)); + } + + if(c4 != '=') + { + retval.push_back(((by3 & 0x3) << 6) | by4); + } + } + + return retval; +} + +char +IceUtil::Base64::encode(unsigned char uc) +{ + if(uc < 26) + { + return 'A' + uc; + } + + if(uc < 52) + { + return 'a' + (uc - 26); + } + + if(uc < 62) + { + return '0' + (uc - 52); + } + + if(uc == 62) + { + return '+'; + } + + return '/'; +} + +unsigned char +IceUtil::Base64::decode(char c) +{ + if(c >= 'A' && c <= 'Z') + { + return c - 'A'; + } + + if(c >= 'a' && c <= 'z') + { + return c - 'a' + 26; + } + + if(c >= '0' && c <= '9') + { + return c - '0' + 52; + } + + if(c == '+') + { + return 62; + } + + return 63; +} + + +bool +IceUtil::Base64::isBase64(char c) +{ + if(c >= 'A' && c <= 'Z') + { + return true; + } + + if(c >= 'a' && c <= 'z') + { + return true; + } + + if(c >= '0' && c <= '9') + { + return true; + } + + if(c == '+') + { + return true; + } + + if(c == '/') + { + return true; + } + + if(c == '=') + { + return true; + } + + return false; +} diff --git a/cpp/src/IceUtil/Cond.cpp b/cpp/src/IceUtil/Cond.cpp index cffa290dc4a..551d81c2520 100644 --- a/cpp/src/IceUtil/Cond.cpp +++ b/cpp/src/IceUtil/Cond.cpp @@ -1,233 +1,233 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/Cond.h>
-
-#ifndef _WIN32
-# include <sys/time.h>
-#endif
-
-#ifdef _WIN32
-
-IceUtil::Semaphore::Semaphore(long initial)
-{
- _sem = CreateSemaphore(0, initial, 0x7fffffff, 0);
- if(_sem == INVALID_HANDLE_VALUE)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
-}
-
-IceUtil::Semaphore::~Semaphore()
-{
- CloseHandle(_sem);
-}
-
-void
-IceUtil::Semaphore::wait() const
-{
- int rc = WaitForSingleObject(_sem, INFINITE);
- if(rc != WAIT_OBJECT_0)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
-}
-
-bool
-IceUtil::Semaphore::timedWait(const Time& timeout) const
-{
- timeval tv = timeout;
- long msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
-
- int rc = WaitForSingleObject(_sem, msec);
- if(rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
- return rc != WAIT_TIMEOUT;
-}
-
-void
-IceUtil::Semaphore::post(int count) const
-{
- int rc = ReleaseSemaphore(_sem, count, 0);
- if(rc == 0)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
-}
-
-IceUtil::Cond::Cond() :
- _gate(1),
- _blocked(0),
- _unblocked(0),
- _toUnblock(0)
-{
-}
-
-IceUtil::Cond::~Cond()
-{
-}
-
-void
-IceUtil::Cond::signal()
-{
- wake(false);
-}
-
-void
-IceUtil::Cond::broadcast()
-{
- wake(true);
-}
-
-void
-IceUtil::Cond::wake(bool broadcast)
-{
- //
- // Lock gate & mutex.
- //
- _gate.wait();
- _internal.lock();
-
- if(_unblocked != 0)
- {
- _blocked -= _unblocked;
- _unblocked = 0;
- }
-
- if(_blocked > 0)
- {
- //
- // Unblock some number of waiters.
- //
- _toUnblock = (broadcast) ? _blocked : 1;
- _internal.unlock();
- _queue.post();
- }
- else
- {
- //
- // Otherwise no blocked waiters, release gate & mutex.
- //
- _gate.post();
- _internal.unlock();
- }
-}
-
-void
-IceUtil::Cond::preWait() const
-{
- _gate.wait();
- _blocked++;
- _gate.post();
-}
-
-void
-IceUtil::Cond::postWait(bool timedOut) const
-{
- _internal.lock();
- _unblocked++;
-
- if(_toUnblock != 0)
- {
- bool last = --_toUnblock == 0;
- _internal.unlock();
-
- if(timedOut)
- {
- _queue.wait();
- }
-
- if(last)
- {
- _gate.post();
- }
- else
- {
- _queue.post();
- }
- }
- else
- {
- _internal.unlock();
- }
-}
-
-void
-IceUtil::Cond::dowait() const
-{
- try
- {
- _queue.wait();
- postWait(false);
- }
- catch(...)
- {
- postWait(false);
- throw;
- }
-}
-
-bool
-IceUtil::Cond::timedDowait(const Time& timeout) const
-{
- try
- {
- bool rc = _queue.timedWait(timeout);
- postWait(!rc);
- return rc;
- }
- catch(...)
- {
- postWait(false);
- throw;
- }
-}
-
-#else
-
-IceUtil::Cond::Cond()
-{
- int rc = pthread_cond_init(&_cond, 0);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
-}
-
-IceUtil::Cond::~Cond()
-{
- int rc = 0;
- rc = pthread_cond_destroy(&_cond);
- assert(rc == 0);
-}
-
-void
-IceUtil::Cond::signal()
-{
- int rc = pthread_cond_signal(&_cond);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
-}
-
-void
-IceUtil::Cond::broadcast()
-{
- int rc = pthread_cond_broadcast(&_cond);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/Cond.h> + +#ifndef _WIN32 +# include <sys/time.h> +#endif + +#ifdef _WIN32 + +IceUtil::Semaphore::Semaphore(long initial) +{ + _sem = CreateSemaphore(0, initial, 0x7fffffff, 0); + if(_sem == INVALID_HANDLE_VALUE) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } +} + +IceUtil::Semaphore::~Semaphore() +{ + CloseHandle(_sem); +} + +void +IceUtil::Semaphore::wait() const +{ + int rc = WaitForSingleObject(_sem, INFINITE); + if(rc != WAIT_OBJECT_0) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } +} + +bool +IceUtil::Semaphore::timedWait(const Time& timeout) const +{ + timeval tv = timeout; + long msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); + + int rc = WaitForSingleObject(_sem, msec); + if(rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } + return rc != WAIT_TIMEOUT; +} + +void +IceUtil::Semaphore::post(int count) const +{ + int rc = ReleaseSemaphore(_sem, count, 0); + if(rc == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } +} + +IceUtil::Cond::Cond() : + _gate(1), + _blocked(0), + _unblocked(0), + _toUnblock(0) +{ +} + +IceUtil::Cond::~Cond() +{ +} + +void +IceUtil::Cond::signal() +{ + wake(false); +} + +void +IceUtil::Cond::broadcast() +{ + wake(true); +} + +void +IceUtil::Cond::wake(bool broadcast) +{ + // + // Lock gate & mutex. + // + _gate.wait(); + _internal.lock(); + + if(_unblocked != 0) + { + _blocked -= _unblocked; + _unblocked = 0; + } + + if(_blocked > 0) + { + // + // Unblock some number of waiters. + // + _toUnblock = (broadcast) ? _blocked : 1; + _internal.unlock(); + _queue.post(); + } + else + { + // + // Otherwise no blocked waiters, release gate & mutex. + // + _gate.post(); + _internal.unlock(); + } +} + +void +IceUtil::Cond::preWait() const +{ + _gate.wait(); + _blocked++; + _gate.post(); +} + +void +IceUtil::Cond::postWait(bool timedOut) const +{ + _internal.lock(); + _unblocked++; + + if(_toUnblock != 0) + { + bool last = --_toUnblock == 0; + _internal.unlock(); + + if(timedOut) + { + _queue.wait(); + } + + if(last) + { + _gate.post(); + } + else + { + _queue.post(); + } + } + else + { + _internal.unlock(); + } +} + +void +IceUtil::Cond::dowait() const +{ + try + { + _queue.wait(); + postWait(false); + } + catch(...) + { + postWait(false); + throw; + } +} + +bool +IceUtil::Cond::timedDowait(const Time& timeout) const +{ + try + { + bool rc = _queue.timedWait(timeout); + postWait(!rc); + return rc; + } + catch(...) + { + postWait(false); + throw; + } +} + +#else + +IceUtil::Cond::Cond() +{ + int rc = pthread_cond_init(&_cond, 0); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } +} + +IceUtil::Cond::~Cond() +{ + int rc = 0; + rc = pthread_cond_destroy(&_cond); + assert(rc == 0); +} + +void +IceUtil::Cond::signal() +{ + int rc = pthread_cond_signal(&_cond); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } +} + +void +IceUtil::Cond::broadcast() +{ + int rc = pthread_cond_broadcast(&_cond); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } +} + +#endif diff --git a/cpp/src/IceUtil/Exception.cpp b/cpp/src/IceUtil/Exception.cpp index f239e8ba53b..18c25824027 100644 --- a/cpp/src/IceUtil/Exception.cpp +++ b/cpp/src/IceUtil/Exception.cpp @@ -1,99 +1,99 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/Exception.h>
-
-using namespace std;
-
-IceUtil::Exception::Exception() :
- _file(0),
- _line(0)
-{
-}
-
-IceUtil::Exception::Exception(const char* file, int line) :
- _file(file),
- _line(line)
-{
-}
-
-IceUtil::Exception::~Exception()
-{
-}
-
-string
-IceUtil::Exception::ice_name() const
-{
- return "IceUtil::Exception";
-}
-
-void
-IceUtil::Exception::ice_print(ostream& out) const
-{
- if(_file && _line > 0)
- {
- out << _file << ':' << _line << ": ";
- }
- out << ice_name();
-}
-
-IceUtil::Exception*
-IceUtil::Exception::ice_clone() const
-{
- return new Exception(*this);
-}
-
-void
-IceUtil::Exception::ice_throw() const
-{
- throw *this;
-}
-
-const char*
-IceUtil::Exception::ice_file() const
-{
- return _file;
-}
-
-int
-IceUtil::Exception::ice_line() const
-{
- return _line;
-}
-
-ostream&
-IceUtil::operator<<(ostream& out, const IceUtil::Exception& ex)
-{
- ex.ice_print(out);
- return out;
-}
-
-IceUtil::NullHandleException::NullHandleException(const char* file, int line) :
- Exception(file, line)
-{
-}
-
-string
-IceUtil::NullHandleException::ice_name() const
-{
- return "IceUtil::NullHandleException";
-}
-
-IceUtil::Exception*
-IceUtil::NullHandleException::ice_clone() const
-{
- return new NullHandleException(*this);
-}
-
-void
-IceUtil::NullHandleException::ice_throw() const
-{
- throw *this;
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/Exception.h> + +using namespace std; + +IceUtil::Exception::Exception() : + _file(0), + _line(0) +{ +} + +IceUtil::Exception::Exception(const char* file, int line) : + _file(file), + _line(line) +{ +} + +IceUtil::Exception::~Exception() +{ +} + +string +IceUtil::Exception::ice_name() const +{ + return "IceUtil::Exception"; +} + +void +IceUtil::Exception::ice_print(ostream& out) const +{ + if(_file && _line > 0) + { + out << _file << ':' << _line << ": "; + } + out << ice_name(); +} + +IceUtil::Exception* +IceUtil::Exception::ice_clone() const +{ + return new Exception(*this); +} + +void +IceUtil::Exception::ice_throw() const +{ + throw *this; +} + +const char* +IceUtil::Exception::ice_file() const +{ + return _file; +} + +int +IceUtil::Exception::ice_line() const +{ + return _line; +} + +ostream& +IceUtil::operator<<(ostream& out, const IceUtil::Exception& ex) +{ + ex.ice_print(out); + return out; +} + +IceUtil::NullHandleException::NullHandleException(const char* file, int line) : + Exception(file, line) +{ +} + +string +IceUtil::NullHandleException::ice_name() const +{ + return "IceUtil::NullHandleException"; +} + +IceUtil::Exception* +IceUtil::NullHandleException::ice_clone() const +{ + return new NullHandleException(*this); +} + +void +IceUtil::NullHandleException::ice_throw() const +{ + throw *this; +} diff --git a/cpp/src/IceUtil/InputUtil.cpp b/cpp/src/IceUtil/InputUtil.cpp index 32959cb3fca..322dc195955 100644 --- a/cpp/src/IceUtil/InputUtil.cpp +++ b/cpp/src/IceUtil/InputUtil.cpp @@ -1,184 +1,184 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/InputUtil.h>
-#include <stdlib.h>
-#include <errno.h>
-
-using namespace std;
-
-namespace IceUtil
-{
-
-//
-// strToInt64 emulates strtoll() for Windows
-//
-
-Int64
-strToInt64(const char* s, char** endptr, int base)
-{
-#if defined(_WIN32)
- //
- // Assume nothing will be there to convert for now
- //
- if(endptr)
- {
- *endptr = const_cast<char *>(s);
- }
-
- //
- // Skip leading whitespace
- //
- while(*s && isspace(*s))
- {
- ++s;
- }
-
- //
- // Check for sign
- //
- int sign = 1;
- if(*s == '+')
- {
- ++s;
- }
- else if(*s == '-')
- {
- sign = -1;
- ++s;
- }
-
- //
- // Check that base is valid
- //
- if(base == 0)
- {
- if(*s == '0')
- {
- base = 8;
- if(*++s == 'x' || *s == 'X')
- {
- base = 16;
- ++s;
- }
- }
- else
- {
- base = 10;
- }
- }
- else if(base < 2 || base > 36)
- {
- errno = EINVAL;
- return 0;
- }
-
- //
- // Check that we have something left to parse
- //
- if(*s == '/0')
- {
- return 0;
- }
-
- static const string allDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const string validDigits(allDigits.begin(), allDigits.begin() + base);
-
- //
- // Table to convert ASCII digits/letters into their value (100 for unused slots)
- //
- static const char digitVal[] =
- {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // '0' - '9'
- 100, 100, 100, 100, 100, 100, 100, // punctuation
- 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, // 'A' - 'J'
- 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, // 'K' - 'T'
- 30, 31, 32, 33, 34, 35 // 'U' - 'Z'
- };
-
- Int64 result = 0;
- bool overflow = false;
- while(*s && validDigits.find_first_of(toupper(*s)) != validDigits.npos)
- {
- if(!overflow)
- {
- int digit = digitVal[toupper(*s) - '0'];
- assert(digit != 100);
- if(result < Int64Max / base)
- {
- result *= base;
- result += digit;
- }
- else if((digit <= Int64Max % base) || (sign == -1 && digit == Int64Max % base + 1))
- {
- result *= base;
- result += digit;
- }
- else
- {
- overflow = true;
- result = sign == -1 ? Int64Min : Int64Max;
- }
- }
- ++s;
- }
-
- if(overflow)
- {
- errno = ERANGE;
- }
- else
- {
- result *= sign;
- }
-
- if(endptr)
- {
- *endptr = const_cast<char *>(s);
- }
-
- return result;
-
-#else
- return strtoll(s, endptr, base);
-#endif
-}
-
-bool
-stringToInt64(const string& stringToParse, Int64& result, string::size_type& pos)
-{
- string::const_iterator i = stringToParse.begin();
- while(i != stringToParse.end() && isspace(*i))
- {
- ++i;
- }
- if(i == stringToParse.end()) // String empty or nothing but whitespace
- {
- result = 0;
- pos = string::npos;
- return false;
- }
- string::const_reverse_iterator j = stringToParse.rbegin();
- while(isspace(*j))
- {
- ++j;
- } // j now points at last non-whitespace char
-
- string nonWhite(i, j.base()); // nonWhite has trailing whitespace stripped
-
- errno = 0;
- const char* startp = nonWhite.c_str();
- char* endp;
- result = strToInt64(startp, &endp, 0);
- pos = *endp == '\0' ? string::npos : (i - stringToParse.begin()) + (endp - startp);
- return startp != endp && errno != ERANGE && errno != EINVAL;
-}
-
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/InputUtil.h> +#include <stdlib.h> +#include <errno.h> + +using namespace std; + +namespace IceUtil +{ + +// +// strToInt64 emulates strtoll() for Windows +// + +Int64 +strToInt64(const char* s, char** endptr, int base) +{ +#if defined(_WIN32) + // + // Assume nothing will be there to convert for now + // + if(endptr) + { + *endptr = const_cast<char *>(s); + } + + // + // Skip leading whitespace + // + while(*s && isspace(*s)) + { + ++s; + } + + // + // Check for sign + // + int sign = 1; + if(*s == '+') + { + ++s; + } + else if(*s == '-') + { + sign = -1; + ++s; + } + + // + // Check that base is valid + // + if(base == 0) + { + if(*s == '0') + { + base = 8; + if(*++s == 'x' || *s == 'X') + { + base = 16; + ++s; + } + } + else + { + base = 10; + } + } + else if(base < 2 || base > 36) + { + errno = EINVAL; + return 0; + } + + // + // Check that we have something left to parse + // + if(*s == '/0') + { + return 0; + } + + static const string allDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string validDigits(allDigits.begin(), allDigits.begin() + base); + + // + // Table to convert ASCII digits/letters into their value (100 for unused slots) + // + static const char digitVal[] = + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // '0' - '9' + 100, 100, 100, 100, 100, 100, 100, // punctuation + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, // 'A' - 'J' + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, // 'K' - 'T' + 30, 31, 32, 33, 34, 35 // 'U' - 'Z' + }; + + Int64 result = 0; + bool overflow = false; + while(*s && validDigits.find_first_of(toupper(*s)) != validDigits.npos) + { + if(!overflow) + { + int digit = digitVal[toupper(*s) - '0']; + assert(digit != 100); + if(result < Int64Max / base) + { + result *= base; + result += digit; + } + else if((digit <= Int64Max % base) || (sign == -1 && digit == Int64Max % base + 1)) + { + result *= base; + result += digit; + } + else + { + overflow = true; + result = sign == -1 ? Int64Min : Int64Max; + } + } + ++s; + } + + if(overflow) + { + errno = ERANGE; + } + else + { + result *= sign; + } + + if(endptr) + { + *endptr = const_cast<char *>(s); + } + + return result; + +#else + return strtoll(s, endptr, base); +#endif +} + +bool +stringToInt64(const string& stringToParse, Int64& result, string::size_type& pos) +{ + string::const_iterator i = stringToParse.begin(); + while(i != stringToParse.end() && isspace(*i)) + { + ++i; + } + if(i == stringToParse.end()) // String empty or nothing but whitespace + { + result = 0; + pos = string::npos; + return false; + } + string::const_reverse_iterator j = stringToParse.rbegin(); + while(isspace(*j)) + { + ++j; + } // j now points at last non-whitespace char + + string nonWhite(i, j.base()); // nonWhite has trailing whitespace stripped + + errno = 0; + const char* startp = nonWhite.c_str(); + char* endp; + result = strToInt64(startp, &endp, 0); + pos = *endp == '\0' ? string::npos : (i - stringToParse.begin()) + (endp - startp); + return startp != endp && errno != ERANGE && errno != EINVAL; +} + +} diff --git a/cpp/src/IceUtil/OutputUtil.cpp b/cpp/src/IceUtil/OutputUtil.cpp index ddb7e8d0a42..b29ecd1a9be 100644 --- a/cpp/src/IceUtil/OutputUtil.cpp +++ b/cpp/src/IceUtil/OutputUtil.cpp @@ -1,571 +1,571 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/OutputUtil.h>
-
-using namespace std;
-using namespace IceUtil;
-
-namespace IceUtil
-{
-
-NextLine nl;
-StartBlock sb;
-EndBlock eb;
-Separator sp;
-EndElement ee;
-StartEscapes startEscapes;
-EndEscapes endEscapes;
-
-}
-
-// ----------------------------------------------------------------------
-// OutputBase
-// ----------------------------------------------------------------------
-
-IceUtil::OutputBase::OutputBase() :
- _out(_fout),
- _pos(0),
- _indent(0),
- _indentSize(4),
- _useTab(true),
- _separator(true)
-{
-}
-
-IceUtil::OutputBase::OutputBase(ostream& os) :
- _out(os),
- _pos(0),
- _indent(0),
- _indentSize(4),
- _useTab(true),
- _separator(true)
-{
-}
-
-
-IceUtil::OutputBase::OutputBase(const char* s) :
- _out(_fout),
- _pos(0),
- _indent(0),
- _indentSize(4),
- _useTab(true),
- _separator(true)
-{
- open(s);
-}
-
-IceUtil::OutputBase::~OutputBase()
-{
-}
-
-void
-IceUtil::OutputBase::open(const char* s)
-{
- _fout.open(s);
-}
-
-void
-IceUtil::OutputBase::print(const char* s)
-{
- for(unsigned int i = 0; i < strlen(s); ++i)
- {
- if(s[i] == '\n')
- {
- _pos = 0;
- }
- else
- {
- ++_pos;
- }
- }
-
- _out << s;
-}
-
-void
-IceUtil::OutputBase::inc()
-{
- _indent += _indentSize;
-}
-
-void
-IceUtil::OutputBase::dec()
-{
- assert(_indent >= _indentSize);
- _indent -= _indentSize;
-}
-
-void
-IceUtil::OutputBase::useCurrentPosAsIndent()
-{
- _indentSave.push(_indent);
- _indent = _pos;
-}
-
-void
-IceUtil::OutputBase::zeroIndent()
-{
- _indentSave.push(_indent);
- _indent = 0;
-}
-
-void
-IceUtil::OutputBase::restoreIndent()
-{
- assert(!_indentSave.empty());
- _indent = _indentSave.top();
- _indentSave.pop();
-}
-
-void
-IceUtil::OutputBase::setIndent(int indentSize)
-{
- _indentSize = indentSize;
-}
-
-void
-IceUtil::OutputBase::setUseTab(bool useTab)
-{
- _useTab = useTab;
-}
-
-void
-IceUtil::OutputBase::nl()
-{
- _out << '\n';
- _pos = 0;
- _separator = true;
-
- int indent = _indent;
-
- if(_useTab)
- {
- while(indent >= 8)
- {
- indent -= 8;
- _out << '\t';
- _pos += 8;
- }
- }
- else
- {
- while(indent >= _indentSize)
- {
- indent -= _indentSize;
- _out << " ";
- _pos += _indentSize;
- }
- }
-
- while(indent > 0)
- {
- --indent;
- _out << ' ';
- ++_pos;
- }
-
- _out.flush();
-}
-
-void
-IceUtil::OutputBase::sp()
-{
- if(_separator)
- {
- _out << '\n';
- }
-}
-
-bool
-IceUtil::OutputBase::operator!() const
-{
- return !_out;
-}
-
-streamsize
-IceUtil::OutputBase::width() const
-{
- return _out.width();
-}
-
-streamsize
-IceUtil::OutputBase::width(streamsize newWidth)
-{
- return _out.width(newWidth);
-}
-
-ios_base::fmtflags
-IceUtil::OutputBase::flags() const
-{
- return _out.flags();
-}
-
-ios_base::fmtflags
-IceUtil::OutputBase::flags(ios_base::fmtflags newFlags)
-{
- return _out.flags(newFlags);
-}
-
-ostream::char_type
-IceUtil::OutputBase::fill() const
-{
- return _out.fill();
-}
-
-ostream::char_type
-IceUtil::OutputBase::fill(ostream::char_type newFill)
-{
- return _out.fill(newFill);
-}
-
-// ----------------------------------------------------------------------
-// Output
-// ----------------------------------------------------------------------
-
-IceUtil::Output::Output() :
- OutputBase(),
- _blockStart("{"),
- _blockEnd("}")
-{
-}
-
-IceUtil::Output::Output(ostream& os) :
- OutputBase(os),
- _blockStart("{"),
- _blockEnd("}")
-{
-}
-
-IceUtil::Output::Output(const char* s) :
- OutputBase(s),
- _blockStart("{"),
- _blockEnd("}")
-{
-}
-
-void
-IceUtil::Output::setBeginBlock(const char *bb)
-{
- _blockStart = bb;
-}
-
-void
-IceUtil::Output::setEndBlock(const char *eb)
-{
- _blockEnd = eb;
-}
-
-void
-IceUtil::Output::sb()
-{
- if(_blockStart.length())
- {
- nl();
- _out << _blockStart;
- }
- ++_pos;
- inc();
- _separator = false;
-}
-
-void
-IceUtil::Output::eb()
-{
- dec();
- if(_blockEnd.length())
- {
- nl();
- _out << _blockEnd;
- }
- --_pos;
-}
-
-Output&
-IceUtil::operator<<(Output& out, ios_base& (*val)(ios_base&))
-{
- ostringstream s;
- s << val;
- out.print(s.str().c_str());
- return out;
-}
-
-// ----------------------------------------------------------------------
-// XMLOutput
-// ----------------------------------------------------------------------
-
-IceUtil::XMLOutput::XMLOutput() :
- OutputBase(),
- _se(false),
- _text(false),
- _sgml(false),
- _escape(false)
-{
-}
-
-IceUtil::XMLOutput::XMLOutput(ostream& os) :
- OutputBase(os),
- _se(false),
- _text(false),
- _sgml(false),
- _escape(false)
-{
-}
-
-IceUtil::XMLOutput::XMLOutput(const char* s) :
- OutputBase(s),
- _se(false),
- _text(false),
- _sgml(false),
- _escape(false)
-{
-}
-
-void
-IceUtil::XMLOutput::setSGML(bool sgml)
-{
- _sgml = true;
-}
-
-void
-IceUtil::XMLOutput::print(const char* s)
-{
- if(_se)
- {
- _out << '>';
- _se = false;
- }
- _text = true;
-
- if(_escape)
- {
- string escaped = escape(s);
- OutputBase::print(escaped.c_str());
- }
- else
- {
- OutputBase::print(s);
- }
-}
-
-void
-IceUtil::XMLOutput::nl()
-{
- if(_se)
- {
- _se = false;
- _out << '>';
- }
- OutputBase::nl();
-}
-
-void
-IceUtil::XMLOutput::se(const string& element)
-{
- nl();
-
- //
- // If we're not in SGML mode the output of the '>' character is
- // deferred until either the //end-element (in which case a /> is
- // emitted) or until something //is displayed.
- //
- if(_escape)
- {
- _out << '<' << escape(element);
- }
- else
- {
- _out << '<' << element;
- }
- _se = true;
- _text = false;
-
- string::size_type pos = element.find_first_of(" \t");
- if(pos == string::npos)
- {
- _elementStack.push(element);
- }
- else
- {
- _elementStack.push(element.substr(0, pos));
- }
-
- ++_pos; // TODO: ???
- inc();
- _separator = false;
-}
-
-void
-IceUtil::XMLOutput::ee()
-{
- string element = _elementStack.top();
- _elementStack.pop();
-
- dec();
- if(_se)
- {
- //
- // SGML (docbook) doesn't support <foo/>.
- //
- if(_sgml)
- {
- _out << "></" << element << '>';
- }
- else
- {
- _out << "/>";
- }
- }
- else
- {
- if(!_text)
- {
- nl();
- }
- _out << "</" << element << '>';
- }
- --_pos; // TODO: ???
-
- _se = false;
- _text = false;
-}
-
-void
-IceUtil::XMLOutput::attr(const string& name, const string& value)
-{
- //
- // Precondition: Attributes can only be attached to elements.
- //
- assert(_se);
- _out << " " << name << "=\"" << escape(value) << '"';
-}
-
-void
-IceUtil::XMLOutput::startEscapes()
-{
- _escape = true;
-}
-
-void
-IceUtil::XMLOutput::endEscapes()
-{
- _escape = false;
-}
-
-string
-IceUtil::XMLOutput::currentElement() const
-{
- if(_elementStack.size() > 0)
- {
- return _elementStack.top();
- }
- else
- {
- return string();
- }
-}
-
-string
-IceUtil::XMLOutput::escape(const string& input) const
-{
- string v = input;
-
- //
- // Find out whether there is a reserved character to avoid
- // conversion if not necessary.
- //
- static const string allReserved = "<>'\"&";
- if(v.find_first_of(allReserved) != string::npos)
- {
- //
- // First convert all & to &
- //
- size_t pos = 0;
- while((pos = v.find_first_of('&', pos)) != string::npos)
- {
- v.insert(pos+1, "amp;");
- pos += 4;
- }
-
- //
- // Next convert remaining reserved characters.
- //
- static const string reserved = "<>'\"";
- pos = 0;
- while((pos = v.find_first_of(reserved, pos)) != string::npos)
- {
- string replace;
- switch(v[pos])
- {
- case '>':
- replace = ">";
- break;
-
- case '<':
- replace = "<";
- break;
-
- case '\'':
- replace = "'";
- break;
-
- case '"':
- replace = """;
- break;
-
- default:
- assert(false);
- }
-
- v.erase(pos, 1);
- v.insert(pos, replace);
- pos += replace.size();
- }
- }
- return v;
-}
-
-XMLOutput&
-IceUtil::operator<<(XMLOutput& out, ios_base& (*val)(ios_base&))
-{
- ostringstream s;
- s << val;
- out.print(s.str().c_str());
- return out;
-}
-
-IceUtil::StartElement::StartElement(const string& name) :
- _name(name)
-{
-}
-
-const string&
-IceUtil::StartElement::getName() const
-{
- return _name;
-}
-
-IceUtil::Attribute::Attribute(const string& name, const string& value) :
- _name(name),
- _value(value)
-{
-}
-
-const string&
-IceUtil::Attribute::getName() const
-{
- return _name;
-}
-
-const string&
-IceUtil::Attribute::getValue() const
-{
- return _value;
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/OutputUtil.h> + +using namespace std; +using namespace IceUtil; + +namespace IceUtil +{ + +NextLine nl; +StartBlock sb; +EndBlock eb; +Separator sp; +EndElement ee; +StartEscapes startEscapes; +EndEscapes endEscapes; + +} + +// ---------------------------------------------------------------------- +// OutputBase +// ---------------------------------------------------------------------- + +IceUtil::OutputBase::OutputBase() : + _out(_fout), + _pos(0), + _indent(0), + _indentSize(4), + _useTab(true), + _separator(true) +{ +} + +IceUtil::OutputBase::OutputBase(ostream& os) : + _out(os), + _pos(0), + _indent(0), + _indentSize(4), + _useTab(true), + _separator(true) +{ +} + + +IceUtil::OutputBase::OutputBase(const char* s) : + _out(_fout), + _pos(0), + _indent(0), + _indentSize(4), + _useTab(true), + _separator(true) +{ + open(s); +} + +IceUtil::OutputBase::~OutputBase() +{ +} + +void +IceUtil::OutputBase::open(const char* s) +{ + _fout.open(s); +} + +void +IceUtil::OutputBase::print(const char* s) +{ + for(unsigned int i = 0; i < strlen(s); ++i) + { + if(s[i] == '\n') + { + _pos = 0; + } + else + { + ++_pos; + } + } + + _out << s; +} + +void +IceUtil::OutputBase::inc() +{ + _indent += _indentSize; +} + +void +IceUtil::OutputBase::dec() +{ + assert(_indent >= _indentSize); + _indent -= _indentSize; +} + +void +IceUtil::OutputBase::useCurrentPosAsIndent() +{ + _indentSave.push(_indent); + _indent = _pos; +} + +void +IceUtil::OutputBase::zeroIndent() +{ + _indentSave.push(_indent); + _indent = 0; +} + +void +IceUtil::OutputBase::restoreIndent() +{ + assert(!_indentSave.empty()); + _indent = _indentSave.top(); + _indentSave.pop(); +} + +void +IceUtil::OutputBase::setIndent(int indentSize) +{ + _indentSize = indentSize; +} + +void +IceUtil::OutputBase::setUseTab(bool useTab) +{ + _useTab = useTab; +} + +void +IceUtil::OutputBase::nl() +{ + _out << '\n'; + _pos = 0; + _separator = true; + + int indent = _indent; + + if(_useTab) + { + while(indent >= 8) + { + indent -= 8; + _out << '\t'; + _pos += 8; + } + } + else + { + while(indent >= _indentSize) + { + indent -= _indentSize; + _out << " "; + _pos += _indentSize; + } + } + + while(indent > 0) + { + --indent; + _out << ' '; + ++_pos; + } + + _out.flush(); +} + +void +IceUtil::OutputBase::sp() +{ + if(_separator) + { + _out << '\n'; + } +} + +bool +IceUtil::OutputBase::operator!() const +{ + return !_out; +} + +streamsize +IceUtil::OutputBase::width() const +{ + return _out.width(); +} + +streamsize +IceUtil::OutputBase::width(streamsize newWidth) +{ + return _out.width(newWidth); +} + +ios_base::fmtflags +IceUtil::OutputBase::flags() const +{ + return _out.flags(); +} + +ios_base::fmtflags +IceUtil::OutputBase::flags(ios_base::fmtflags newFlags) +{ + return _out.flags(newFlags); +} + +ostream::char_type +IceUtil::OutputBase::fill() const +{ + return _out.fill(); +} + +ostream::char_type +IceUtil::OutputBase::fill(ostream::char_type newFill) +{ + return _out.fill(newFill); +} + +// ---------------------------------------------------------------------- +// Output +// ---------------------------------------------------------------------- + +IceUtil::Output::Output() : + OutputBase(), + _blockStart("{"), + _blockEnd("}") +{ +} + +IceUtil::Output::Output(ostream& os) : + OutputBase(os), + _blockStart("{"), + _blockEnd("}") +{ +} + +IceUtil::Output::Output(const char* s) : + OutputBase(s), + _blockStart("{"), + _blockEnd("}") +{ +} + +void +IceUtil::Output::setBeginBlock(const char *bb) +{ + _blockStart = bb; +} + +void +IceUtil::Output::setEndBlock(const char *eb) +{ + _blockEnd = eb; +} + +void +IceUtil::Output::sb() +{ + if(_blockStart.length()) + { + nl(); + _out << _blockStart; + } + ++_pos; + inc(); + _separator = false; +} + +void +IceUtil::Output::eb() +{ + dec(); + if(_blockEnd.length()) + { + nl(); + _out << _blockEnd; + } + --_pos; +} + +Output& +IceUtil::operator<<(Output& out, ios_base& (*val)(ios_base&)) +{ + ostringstream s; + s << val; + out.print(s.str().c_str()); + return out; +} + +// ---------------------------------------------------------------------- +// XMLOutput +// ---------------------------------------------------------------------- + +IceUtil::XMLOutput::XMLOutput() : + OutputBase(), + _se(false), + _text(false), + _sgml(false), + _escape(false) +{ +} + +IceUtil::XMLOutput::XMLOutput(ostream& os) : + OutputBase(os), + _se(false), + _text(false), + _sgml(false), + _escape(false) +{ +} + +IceUtil::XMLOutput::XMLOutput(const char* s) : + OutputBase(s), + _se(false), + _text(false), + _sgml(false), + _escape(false) +{ +} + +void +IceUtil::XMLOutput::setSGML(bool sgml) +{ + _sgml = true; +} + +void +IceUtil::XMLOutput::print(const char* s) +{ + if(_se) + { + _out << '>'; + _se = false; + } + _text = true; + + if(_escape) + { + string escaped = escape(s); + OutputBase::print(escaped.c_str()); + } + else + { + OutputBase::print(s); + } +} + +void +IceUtil::XMLOutput::nl() +{ + if(_se) + { + _se = false; + _out << '>'; + } + OutputBase::nl(); +} + +void +IceUtil::XMLOutput::se(const string& element) +{ + nl(); + + // + // If we're not in SGML mode the output of the '>' character is + // deferred until either the //end-element (in which case a /> is + // emitted) or until something //is displayed. + // + if(_escape) + { + _out << '<' << escape(element); + } + else + { + _out << '<' << element; + } + _se = true; + _text = false; + + string::size_type pos = element.find_first_of(" \t"); + if(pos == string::npos) + { + _elementStack.push(element); + } + else + { + _elementStack.push(element.substr(0, pos)); + } + + ++_pos; // TODO: ??? + inc(); + _separator = false; +} + +void +IceUtil::XMLOutput::ee() +{ + string element = _elementStack.top(); + _elementStack.pop(); + + dec(); + if(_se) + { + // + // SGML (docbook) doesn't support <foo/>. + // + if(_sgml) + { + _out << "></" << element << '>'; + } + else + { + _out << "/>"; + } + } + else + { + if(!_text) + { + nl(); + } + _out << "</" << element << '>'; + } + --_pos; // TODO: ??? + + _se = false; + _text = false; +} + +void +IceUtil::XMLOutput::attr(const string& name, const string& value) +{ + // + // Precondition: Attributes can only be attached to elements. + // + assert(_se); + _out << " " << name << "=\"" << escape(value) << '"'; +} + +void +IceUtil::XMLOutput::startEscapes() +{ + _escape = true; +} + +void +IceUtil::XMLOutput::endEscapes() +{ + _escape = false; +} + +string +IceUtil::XMLOutput::currentElement() const +{ + if(_elementStack.size() > 0) + { + return _elementStack.top(); + } + else + { + return string(); + } +} + +string +IceUtil::XMLOutput::escape(const string& input) const +{ + string v = input; + + // + // Find out whether there is a reserved character to avoid + // conversion if not necessary. + // + static const string allReserved = "<>'\"&"; + if(v.find_first_of(allReserved) != string::npos) + { + // + // First convert all & to & + // + size_t pos = 0; + while((pos = v.find_first_of('&', pos)) != string::npos) + { + v.insert(pos+1, "amp;"); + pos += 4; + } + + // + // Next convert remaining reserved characters. + // + static const string reserved = "<>'\""; + pos = 0; + while((pos = v.find_first_of(reserved, pos)) != string::npos) + { + string replace; + switch(v[pos]) + { + case '>': + replace = ">"; + break; + + case '<': + replace = "<"; + break; + + case '\'': + replace = "'"; + break; + + case '"': + replace = """; + break; + + default: + assert(false); + } + + v.erase(pos, 1); + v.insert(pos, replace); + pos += replace.size(); + } + } + return v; +} + +XMLOutput& +IceUtil::operator<<(XMLOutput& out, ios_base& (*val)(ios_base&)) +{ + ostringstream s; + s << val; + out.print(s.str().c_str()); + return out; +} + +IceUtil::StartElement::StartElement(const string& name) : + _name(name) +{ +} + +const string& +IceUtil::StartElement::getName() const +{ + return _name; +} + +IceUtil::Attribute::Attribute(const string& name, const string& value) : + _name(name), + _value(value) +{ +} + +const string& +IceUtil::Attribute::getName() const +{ + return _name; +} + +const string& +IceUtil::Attribute::getValue() const +{ + return _value; +} diff --git a/cpp/src/IceUtil/RWRecMutex.cpp b/cpp/src/IceUtil/RWRecMutex.cpp index ef632a347ec..7cde5079a98 100644 --- a/cpp/src/IceUtil/RWRecMutex.cpp +++ b/cpp/src/IceUtil/RWRecMutex.cpp @@ -1,355 +1,355 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/RWRecMutex.h>
-#include <IceUtil/Exception.h>
-#include <IceUtil/Time.h>
-
-#include <assert.h>
-
-IceUtil::RWRecMutex::RWRecMutex() :
- _count(0),
- _waitingWriters(0)
-{
-}
-
-IceUtil::RWRecMutex::~RWRecMutex()
-{
-}
-
-void
-IceUtil::RWRecMutex::readlock() const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // Wait while a writer holds the lock or while writers are waiting
- // to get the lock.
- //
- while(_count < 0 || _waitingWriters != 0)
- {
- _readers.wait(lock);
- }
- _count++;
-}
-
-void
-IceUtil::RWRecMutex::tryReadlock() const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // Would block if a writer holds the lock or if writers are
- // waiting to get the lock.
- //
- if(_count < 0 || _waitingWriters != 0)
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- _count++;
-}
-
-void
-IceUtil::RWRecMutex::timedTryReadlock(const Time& timeout) const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // Wait while a writer holds the lock or while writers are waiting
- // to get the lock.
- //
- Time end = Time::now() + timeout;
- while(_count < 0 || _waitingWriters != 0)
- {
- Time remainder = end - Time::now();
- if(remainder > Time())
- {
- _readers.timedWait(lock, remainder);
- }
- else
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- }
-
- _count++;
-}
-
-void
-IceUtil::RWRecMutex::writelock() const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // If the mutex is already write locked by this writer then
- // decrement _count, and return.
- //
- if(_count < 0 && _writerControl == ThreadControl())
- {
- --_count;
- return;
- }
-
- //
- // Wait for the lock to become available and increment the number
- // of waiting writers.
- //
- while(_count != 0)
- {
- _waitingWriters++;
- try
- {
- _writers.wait(lock);
- }
- catch(...)
- {
- --_waitingWriters;
- throw;
- }
- _waitingWriters--;
- }
-
- //
- // Got the lock, indicate it's held by a writer.
- //
- _count = -1;
-}
-
-void
-IceUtil::RWRecMutex::tryWritelock() const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // If the mutex is already write locked by this writer then
- // decrement _count, and return.
- //
- if(_count < 0 && _writerControl == ThreadControl())
- {
- --_count;
- return;
- }
-
- //
- // If there are readers or other writers then the call would block.
- //
- if(_count != 0)
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
-
- //
- // Got the lock, indicate it's held by a writer.
- //
- _count = -1;
-}
-
-void
-IceUtil::RWRecMutex::timedTryWritelock(const Time& timeout) const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // If the mutex is already write locked by this writer then
- // decrement _count, and return.
- //
- if(_count < 0 && _writerControl == ThreadControl())
- {
- --_count;
- return;
- }
-
- //
- // Wait for the lock to become available and increment the number
- // of waiting writers.
- //
- Time end = Time::now() + timeout;
- while(_count != 0)
- {
- Time remainder = end - Time::now();
- if(remainder > Time())
- {
- _waitingWriters++;
- try
- {
- _writers.timedWait(lock, remainder);
- }
- catch(...)
- {
- --_waitingWriters;
- throw;
- }
- _waitingWriters--;
- }
- else
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- }
-
- //
- // Got the lock, indicate it's held by a writer.
- //
- _count = -1;
-}
-
-void
-IceUtil::RWRecMutex::unlock() const
-{
- bool ww;
- bool wr;
- {
- Mutex::Lock lock(_mutex);
-
- assert(_count != 0);
-
- //
- // If _count < 0, the calling thread is a writer that holds the
- // lock, so release the lock. Otherwise, _count is guaranteed to
- // be > 0, so the calling thread is a reader releasing the lock.
- //
- if(_count < 0)
- {
- //
- // Writer called unlock
- //
- ++_count;
-
- //
- // If the write lock wasn't totally released we're done.
- //
- if(_count != 0)
- {
- return;
- }
- }
- else
- {
- //
- // Reader called unlock
- //
- --_count;
- }
-
- //
- // Writers are waiting (ww) if _waitingWriters > 0. In that
- // case, it's OK to let another writer into the region once there
- // are no more readers (_count == 0). Otherwise, no writers are
- // waiting but readers may be waiting (wr).
- //
- ww = (_waitingWriters != 0 && _count == 0);
- wr = (_waitingWriters == 0);
- }
-
- //
- // Wake up a waiting writer if there is one. If not, wake up all
- // readers (just in case -- there may be none).
- //
- if(ww)
- {
- //
- // Wake writer
- //
- _writers.signal();
- }
- else if(wr)
- {
- //
- // Wake readers
- //
- _readers.broadcast();
- }
-}
-
-void
-IceUtil::RWRecMutex::upgrade() const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // Reader called unlock
- //
- assert(_count > 0);
- --_count;
-
- //
- // Wait to acquire the write lock.
- //
- while(_count != 0)
- {
- _waitingWriters++;
- try
- {
- _writers.wait(lock);
- }
- catch(...)
- {
- --_waitingWriters;
- throw;
- }
- _waitingWriters--;
-
-
- }
-
- //
- // Got the lock, indicate it's held by a writer.
- //
- _count = -1;
-}
-
-void
-IceUtil::RWRecMutex::timedUpgrade(const Time& timeout) const
-{
- Mutex::Lock lock(_mutex);
-
- //
- // Reader called unlock
- //
- assert(_count > 0);
- --_count;
-
- //
- // Wait to acquire the write lock.
- //
- Time end = Time::now() + timeout;
- while(_count != 0)
- {
- Time remainder = end - Time::now();
- if(remainder > Time())
- {
- _waitingWriters++;
- try
- {
- _writers.timedWait(lock, remainder);
- }
- catch(...)
- {
- --_waitingWriters;
- throw;
- }
- _waitingWriters--;
- }
- else
- {
- //
- // If a timeout occurred then the lock wasn't acquired. Ensure
- // that the _count is increased again before returning.
- //
- ++_count;
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- }
-
- //
- // Got the lock, indicate it's held by a writer.
- //
- _count = -1;
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/RWRecMutex.h> +#include <IceUtil/Exception.h> +#include <IceUtil/Time.h> + +#include <assert.h> + +IceUtil::RWRecMutex::RWRecMutex() : + _count(0), + _waitingWriters(0) +{ +} + +IceUtil::RWRecMutex::~RWRecMutex() +{ +} + +void +IceUtil::RWRecMutex::readlock() const +{ + Mutex::Lock lock(_mutex); + + // + // Wait while a writer holds the lock or while writers are waiting + // to get the lock. + // + while(_count < 0 || _waitingWriters != 0) + { + _readers.wait(lock); + } + _count++; +} + +void +IceUtil::RWRecMutex::tryReadlock() const +{ + Mutex::Lock lock(_mutex); + + // + // Would block if a writer holds the lock or if writers are + // waiting to get the lock. + // + if(_count < 0 || _waitingWriters != 0) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + _count++; +} + +void +IceUtil::RWRecMutex::timedTryReadlock(const Time& timeout) const +{ + Mutex::Lock lock(_mutex); + + // + // Wait while a writer holds the lock or while writers are waiting + // to get the lock. + // + Time end = Time::now() + timeout; + while(_count < 0 || _waitingWriters != 0) + { + Time remainder = end - Time::now(); + if(remainder > Time()) + { + _readers.timedWait(lock, remainder); + } + else + { + throw ThreadLockedException(__FILE__, __LINE__); + } + } + + _count++; +} + +void +IceUtil::RWRecMutex::writelock() const +{ + Mutex::Lock lock(_mutex); + + // + // If the mutex is already write locked by this writer then + // decrement _count, and return. + // + if(_count < 0 && _writerControl == ThreadControl()) + { + --_count; + return; + } + + // + // Wait for the lock to become available and increment the number + // of waiting writers. + // + while(_count != 0) + { + _waitingWriters++; + try + { + _writers.wait(lock); + } + catch(...) + { + --_waitingWriters; + throw; + } + _waitingWriters--; + } + + // + // Got the lock, indicate it's held by a writer. + // + _count = -1; +} + +void +IceUtil::RWRecMutex::tryWritelock() const +{ + Mutex::Lock lock(_mutex); + + // + // If the mutex is already write locked by this writer then + // decrement _count, and return. + // + if(_count < 0 && _writerControl == ThreadControl()) + { + --_count; + return; + } + + // + // If there are readers or other writers then the call would block. + // + if(_count != 0) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + + // + // Got the lock, indicate it's held by a writer. + // + _count = -1; +} + +void +IceUtil::RWRecMutex::timedTryWritelock(const Time& timeout) const +{ + Mutex::Lock lock(_mutex); + + // + // If the mutex is already write locked by this writer then + // decrement _count, and return. + // + if(_count < 0 && _writerControl == ThreadControl()) + { + --_count; + return; + } + + // + // Wait for the lock to become available and increment the number + // of waiting writers. + // + Time end = Time::now() + timeout; + while(_count != 0) + { + Time remainder = end - Time::now(); + if(remainder > Time()) + { + _waitingWriters++; + try + { + _writers.timedWait(lock, remainder); + } + catch(...) + { + --_waitingWriters; + throw; + } + _waitingWriters--; + } + else + { + throw ThreadLockedException(__FILE__, __LINE__); + } + } + + // + // Got the lock, indicate it's held by a writer. + // + _count = -1; +} + +void +IceUtil::RWRecMutex::unlock() const +{ + bool ww; + bool wr; + { + Mutex::Lock lock(_mutex); + + assert(_count != 0); + + // + // If _count < 0, the calling thread is a writer that holds the + // lock, so release the lock. Otherwise, _count is guaranteed to + // be > 0, so the calling thread is a reader releasing the lock. + // + if(_count < 0) + { + // + // Writer called unlock + // + ++_count; + + // + // If the write lock wasn't totally released we're done. + // + if(_count != 0) + { + return; + } + } + else + { + // + // Reader called unlock + // + --_count; + } + + // + // Writers are waiting (ww) if _waitingWriters > 0. In that + // case, it's OK to let another writer into the region once there + // are no more readers (_count == 0). Otherwise, no writers are + // waiting but readers may be waiting (wr). + // + ww = (_waitingWriters != 0 && _count == 0); + wr = (_waitingWriters == 0); + } + + // + // Wake up a waiting writer if there is one. If not, wake up all + // readers (just in case -- there may be none). + // + if(ww) + { + // + // Wake writer + // + _writers.signal(); + } + else if(wr) + { + // + // Wake readers + // + _readers.broadcast(); + } +} + +void +IceUtil::RWRecMutex::upgrade() const +{ + Mutex::Lock lock(_mutex); + + // + // Reader called unlock + // + assert(_count > 0); + --_count; + + // + // Wait to acquire the write lock. + // + while(_count != 0) + { + _waitingWriters++; + try + { + _writers.wait(lock); + } + catch(...) + { + --_waitingWriters; + throw; + } + _waitingWriters--; + + + } + + // + // Got the lock, indicate it's held by a writer. + // + _count = -1; +} + +void +IceUtil::RWRecMutex::timedUpgrade(const Time& timeout) const +{ + Mutex::Lock lock(_mutex); + + // + // Reader called unlock + // + assert(_count > 0); + --_count; + + // + // Wait to acquire the write lock. + // + Time end = Time::now() + timeout; + while(_count != 0) + { + Time remainder = end - Time::now(); + if(remainder > Time()) + { + _waitingWriters++; + try + { + _writers.timedWait(lock, remainder); + } + catch(...) + { + --_waitingWriters; + throw; + } + _waitingWriters--; + } + else + { + // + // If a timeout occurred then the lock wasn't acquired. Ensure + // that the _count is increased again before returning. + // + ++_count; + throw ThreadLockedException(__FILE__, __LINE__); + } + } + + // + // Got the lock, indicate it's held by a writer. + // + _count = -1; +} diff --git a/cpp/src/IceUtil/RecMutex.cpp b/cpp/src/IceUtil/RecMutex.cpp index 2f617b2d51d..0a976db7bf1 100644 --- a/cpp/src/IceUtil/RecMutex.cpp +++ b/cpp/src/IceUtil/RecMutex.cpp @@ -1,168 +1,168 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/RecMutex.h>
-#include <IceUtil/Exception.h>
-
-using namespace std;
-
-#ifdef _WIN32
-
-IceUtil::RecMutex::RecMutex() :
- _count(0)
-{
- InitializeCriticalSection(&_mutex);
-}
-
-IceUtil::RecMutex::~RecMutex()
-{
- assert(_count == 0);
- DeleteCriticalSection(&_mutex);
-}
-
-bool
-IceUtil::RecMutex::lock() const
-{
- EnterCriticalSection(&_mutex);
- if(++_count > 1)
- {
- LeaveCriticalSection(&_mutex);
- return false;
- }
- return true;
-}
-
-bool
-IceUtil::RecMutex::trylock() const
-{
- if(!TryEnterCriticalSection(&_mutex))
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- if(++_count > 1)
- {
- LeaveCriticalSection(&_mutex);
- return false;
- }
- return true;
-}
-
-bool
-IceUtil::RecMutex::unlock() const
-{
- if(--_count == 0)
- {
- LeaveCriticalSection(&_mutex);
- return true;
- }
- return false;
-}
-
-void
-IceUtil::RecMutex::unlock(LockState& state) const
-{
- state.count = _count;
- _count = 0;
- LeaveCriticalSection(&_mutex);
-}
-
-void
-IceUtil::RecMutex::lock(LockState& state) const
-{
- EnterCriticalSection(&_mutex);
- _count = state.count;
-}
-#else
-
-IceUtil::RecMutex::RecMutex() :
- _count(0)
-{
- const pthread_mutexattr_t attr = { PTHREAD_MUTEX_RECURSIVE_NP };
- int rc = pthread_mutex_init(&_mutex, &attr);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
-}
-
-IceUtil::RecMutex::~RecMutex()
-{
- assert(_count == 0);
- int rc = 0;
- rc = pthread_mutex_destroy(&_mutex);
- assert(rc == 0);
-}
-
-bool
-IceUtil::RecMutex::lock() const
-{
- int rc = pthread_mutex_lock(&_mutex);
- if(rc != 0)
- {
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
- if(++_count > 1)
- {
- rc = pthread_mutex_unlock(&_mutex);
- assert(rc == 0);
- return false;
- }
- return true;
-}
-
-bool
-IceUtil::RecMutex::trylock() const
-{
- int rc = pthread_mutex_trylock(&_mutex);
- if(rc != 0)
- {
- if(rc == EBUSY)
- {
- throw ThreadLockedException(__FILE__, __LINE__);
- }
- throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__);
- }
- if(++_count > 1)
- {
- rc = pthread_mutex_unlock(&_mutex);
- assert(rc == 0);
- return false;
- }
- return true;
-}
-
-bool
-IceUtil::RecMutex::unlock() const
-{
- if(--_count == 0)
- {
- int rc = 0; // Prevent warnings when NDEBUG is defined.
- rc = pthread_mutex_unlock(&_mutex);
- assert(rc == 0);
- return true;
- }
- return false;
-}
-
-void
-IceUtil::RecMutex::unlock(LockState& state) const
-{
- state.mutex = &_mutex;
- state.count = _count;
- _count = 0;
-}
-
-void
-IceUtil::RecMutex::lock(LockState& state) const
-{
- _count = state.count;
-}
-
-#endif
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/RecMutex.h> +#include <IceUtil/Exception.h> + +using namespace std; + +#ifdef _WIN32 + +IceUtil::RecMutex::RecMutex() : + _count(0) +{ + InitializeCriticalSection(&_mutex); +} + +IceUtil::RecMutex::~RecMutex() +{ + assert(_count == 0); + DeleteCriticalSection(&_mutex); +} + +bool +IceUtil::RecMutex::lock() const +{ + EnterCriticalSection(&_mutex); + if(++_count > 1) + { + LeaveCriticalSection(&_mutex); + return false; + } + return true; +} + +bool +IceUtil::RecMutex::trylock() const +{ + if(!TryEnterCriticalSection(&_mutex)) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + if(++_count > 1) + { + LeaveCriticalSection(&_mutex); + return false; + } + return true; +} + +bool +IceUtil::RecMutex::unlock() const +{ + if(--_count == 0) + { + LeaveCriticalSection(&_mutex); + return true; + } + return false; +} + +void +IceUtil::RecMutex::unlock(LockState& state) const +{ + state.count = _count; + _count = 0; + LeaveCriticalSection(&_mutex); +} + +void +IceUtil::RecMutex::lock(LockState& state) const +{ + EnterCriticalSection(&_mutex); + _count = state.count; +} +#else + +IceUtil::RecMutex::RecMutex() : + _count(0) +{ + const pthread_mutexattr_t attr = { PTHREAD_MUTEX_RECURSIVE_NP }; + int rc = pthread_mutex_init(&_mutex, &attr); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } +} + +IceUtil::RecMutex::~RecMutex() +{ + assert(_count == 0); + int rc = 0; + rc = pthread_mutex_destroy(&_mutex); + assert(rc == 0); +} + +bool +IceUtil::RecMutex::lock() const +{ + int rc = pthread_mutex_lock(&_mutex); + if(rc != 0) + { + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } + if(++_count > 1) + { + rc = pthread_mutex_unlock(&_mutex); + assert(rc == 0); + return false; + } + return true; +} + +bool +IceUtil::RecMutex::trylock() const +{ + int rc = pthread_mutex_trylock(&_mutex); + if(rc != 0) + { + if(rc == EBUSY) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + throw ThreadSyscallException(strerror(rc), __FILE__, __LINE__); + } + if(++_count > 1) + { + rc = pthread_mutex_unlock(&_mutex); + assert(rc == 0); + return false; + } + return true; +} + +bool +IceUtil::RecMutex::unlock() const +{ + if(--_count == 0) + { + int rc = 0; // Prevent warnings when NDEBUG is defined. + rc = pthread_mutex_unlock(&_mutex); + assert(rc == 0); + return true; + } + return false; +} + +void +IceUtil::RecMutex::unlock(LockState& state) const +{ + state.mutex = &_mutex; + state.count = _count; + _count = 0; +} + +void +IceUtil::RecMutex::lock(LockState& state) const +{ + _count = state.count; +} + +#endif diff --git a/cpp/src/IceUtil/Thread.cpp b/cpp/src/IceUtil/Thread.cpp index 31049c5cae0..37bf99a95f3 100644 --- a/cpp/src/IceUtil/Thread.cpp +++ b/cpp/src/IceUtil/Thread.cpp @@ -1,319 +1,319 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/Thread.h>
-#include <IceUtil/Time.h>
-#include <IceUtil/ThreadException.h>
-
-using namespace std;
-
-#ifdef _WIN32
-
-IceUtil::ThreadControl::ThreadControl() :
- _handle(new HandleWrapper(0)),
- _id(GetCurrentThreadId())
-{
- HANDLE proc = GetCurrentProcess();
- HANDLE current = GetCurrentThread();
- int rc = DuplicateHandle(proc, current, proc, &_handle->handle, SYNCHRONIZE, TRUE, 0);
- if(rc == 0)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
-}
-
-IceUtil::ThreadControl::ThreadControl(const HandleWrapperPtr& handle, unsigned id) :
- _handle(handle),
- _id(id)
-{
-}
-
-bool
-IceUtil::ThreadControl::operator==(const ThreadControl& rhs) const
-{
- return _id == rhs._id;
-}
-
-bool
-IceUtil::ThreadControl::operator!=(const ThreadControl& rhs) const
-{
- return _id != rhs._id;
-}
-
-bool
-IceUtil::ThreadControl::operator<(const ThreadControl& rhs) const
-{
- return _id != rhs._id;
-}
-
-void
-IceUtil::ThreadControl::join()
-{
- if(_handle->handle)
- {
- int rc = WaitForSingleObject(_handle->handle, INFINITE);
- if(rc != WAIT_OBJECT_0)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
- }
-}
-
-void
-IceUtil::ThreadControl::sleep(const Time& timeout)
-{
- timeval tv = timeout;
- long msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
- Sleep(msec);
-}
-
-void
-IceUtil::ThreadControl::yield()
-{
- //
- // A value of zero causes the thread to relinquish the remainder
- // of its time slice to any other thread of equal priority that is
- // ready to run.
- //
- Sleep(0);
-}
-
-IceUtil::Thread::Thread() :
- _id(0),
- _handle(new HandleWrapper(0))
-{
-}
-
-IceUtil::Thread::~Thread()
-{
-}
-
-static void*
-startHook(void* arg)
-{
- try
- {
- IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg);
-
- //
- // Ensure that the thread doesn't go away until run() has
- // completed.
- //
- IceUtil::ThreadPtr thread = rawThread;
-
- //
- // See the comment in IceUtil::Thread::start() for details.
- //
- rawThread->__decRef();
- thread->run();
- }
- catch(const IceUtil::Exception& e)
- {
- cerr << "IceUtil::Thread::run(): uncaught exception: ";
- cerr << e << endl;
- }
- return 0;
-}
-
-#include <process.h>
-
-IceUtil::ThreadControl
-IceUtil::Thread::start()
-{
- //
- // It's necessary to increment the reference count since
- // pthread_create won't necessarily call the thread function until
- // later. If the user does (new MyThread)->start() then the thread
- // object could be deleted before the thread object takes
- // ownership. It's also necessary to increment the reference count
- // prior to calling pthread_create since the thread itself calls
- // __decRef().
- //
- __incRef();
-
- _handle->handle = (HANDLE)_beginthreadex(0, 0, (unsigned (__stdcall*)(void*))startHook, (LPVOID)this, 0, &_id);
- if(_handle->handle == 0)
- {
- __decRef();
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
-
- return ThreadControl(_handle, _id);
-}
-
-IceUtil::ThreadControl
-IceUtil::Thread::getThreadControl()
-{
- return ThreadControl(_handle, _id);
-}
-
-bool
-IceUtil::Thread::operator==(const Thread& rhs) const
-{
- return _id == rhs._id;
-}
-
-bool
-IceUtil::Thread::operator!=(const Thread& rhs) const
-{
- return _id != rhs._id;
-}
-
-bool
-IceUtil::Thread::operator<(const Thread& rhs) const
-{
- return _id < rhs._id;
-}
-
-#else
-
-IceUtil::ThreadControl::ThreadControl(pthread_t id) :
- _id(id)
-{
-}
-
-IceUtil::ThreadControl::ThreadControl() :
- _id(pthread_self())
-{
-}
-
-bool
-IceUtil::ThreadControl::operator==(const ThreadControl& rhs) const
-{
- return pthread_equal(_id, rhs._id);
-}
-
-bool
-IceUtil::ThreadControl::operator!=(const ThreadControl& rhs) const
-{
- return !pthread_equal(_id, rhs._id);
-}
-
-bool
-IceUtil::ThreadControl::operator<(const ThreadControl& rhs) const
-{
- // NOTE: Linux specific
- return _id < rhs._id;
-}
-
-void
-IceUtil::ThreadControl::join()
-{
- if(_id)
- {
- void* ignore = 0;
- int rc = pthread_join(_id, &ignore);
- if(rc != 0)
- {
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
- }
-}
-
-void
-IceUtil::ThreadControl::sleep(const Time& timeout)
-{
- struct timeval tv = timeout;
- struct timespec ts;
- ts.tv_sec = tv.tv_sec;
- ts.tv_nsec = tv.tv_usec * 1000L;
- nanosleep(&ts, 0);
-}
-
-void
-IceUtil::ThreadControl::yield()
-{
- sched_yield();
-}
-
-IceUtil::Thread::Thread()
-{
-}
-
-IceUtil::Thread::~Thread()
-{
-}
-
-static void*
-startHook(void* arg)
-{
- try
- {
- IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg);
-
- //
- // Ensure that the thread doesn't go away until run() has
- // completed.
- //
- IceUtil::ThreadPtr thread = rawThread;
-
- //
- // See the comment in IceUtil::Thread::start() for details.
- //
- rawThread->__decRef();
- thread->run();
- }
- catch(const IceUtil::Exception& e)
- {
- cerr << "IceUtil::Thread::run(): uncaught exception: ";
- cerr << e << endl;
- }
- return 0;
-}
-
-IceUtil::ThreadControl
-IceUtil::Thread::start()
-{
- //
- // It's necessary to increment the reference count since
- // pthread_create won't necessarily call the thread function until
- // later. If the user does (new MyThread)->start() then the thread
- // object could be deleted before the thread object takes
- // ownership. It's also necessary to increment the reference count
- // prior to calling pthread_create since the thread itself calls
- // __decRef().
- //
- __incRef();
- int rc = pthread_create(&_id, 0, startHook, this);
- if(rc != 0)
- {
- __decRef();
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
- return ThreadControl(_id);
-}
-
-IceUtil::ThreadControl
-IceUtil::Thread::getThreadControl()
-{
- return ThreadControl(_id);
-}
-
-bool
-IceUtil::Thread::operator==(const Thread& rhs) const
-{
- return pthread_equal(_id, rhs._id);
-}
-
-bool
-IceUtil::Thread::operator!=(const Thread& rhs) const
-{
- return !pthread_equal(_id, rhs._id);
-}
-
-bool
-IceUtil::Thread::operator<(const Thread& rhs) const
-{
- // NOTE: Linux specific
- return _id < rhs._id;
-}
-
-#endif
-
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/Thread.h> +#include <IceUtil/Time.h> +#include <IceUtil/ThreadException.h> + +using namespace std; + +#ifdef _WIN32 + +IceUtil::ThreadControl::ThreadControl() : + _handle(new HandleWrapper(0)), + _id(GetCurrentThreadId()) +{ + HANDLE proc = GetCurrentProcess(); + HANDLE current = GetCurrentThread(); + int rc = DuplicateHandle(proc, current, proc, &_handle->handle, SYNCHRONIZE, TRUE, 0); + if(rc == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } +} + +IceUtil::ThreadControl::ThreadControl(const HandleWrapperPtr& handle, unsigned id) : + _handle(handle), + _id(id) +{ +} + +bool +IceUtil::ThreadControl::operator==(const ThreadControl& rhs) const +{ + return _id == rhs._id; +} + +bool +IceUtil::ThreadControl::operator!=(const ThreadControl& rhs) const +{ + return _id != rhs._id; +} + +bool +IceUtil::ThreadControl::operator<(const ThreadControl& rhs) const +{ + return _id != rhs._id; +} + +void +IceUtil::ThreadControl::join() +{ + if(_handle->handle) + { + int rc = WaitForSingleObject(_handle->handle, INFINITE); + if(rc != WAIT_OBJECT_0) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } + } +} + +void +IceUtil::ThreadControl::sleep(const Time& timeout) +{ + timeval tv = timeout; + long msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); + Sleep(msec); +} + +void +IceUtil::ThreadControl::yield() +{ + // + // A value of zero causes the thread to relinquish the remainder + // of its time slice to any other thread of equal priority that is + // ready to run. + // + Sleep(0); +} + +IceUtil::Thread::Thread() : + _id(0), + _handle(new HandleWrapper(0)) +{ +} + +IceUtil::Thread::~Thread() +{ +} + +static void* +startHook(void* arg) +{ + try + { + IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg); + + // + // Ensure that the thread doesn't go away until run() has + // completed. + // + IceUtil::ThreadPtr thread = rawThread; + + // + // See the comment in IceUtil::Thread::start() for details. + // + rawThread->__decRef(); + thread->run(); + } + catch(const IceUtil::Exception& e) + { + cerr << "IceUtil::Thread::run(): uncaught exception: "; + cerr << e << endl; + } + return 0; +} + +#include <process.h> + +IceUtil::ThreadControl +IceUtil::Thread::start() +{ + // + // It's necessary to increment the reference count since + // pthread_create won't necessarily call the thread function until + // later. If the user does (new MyThread)->start() then the thread + // object could be deleted before the thread object takes + // ownership. It's also necessary to increment the reference count + // prior to calling pthread_create since the thread itself calls + // __decRef(). + // + __incRef(); + + _handle->handle = (HANDLE)_beginthreadex(0, 0, (unsigned (__stdcall*)(void*))startHook, (LPVOID)this, 0, &_id); + if(_handle->handle == 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__); + } + + return ThreadControl(_handle, _id); +} + +IceUtil::ThreadControl +IceUtil::Thread::getThreadControl() +{ + return ThreadControl(_handle, _id); +} + +bool +IceUtil::Thread::operator==(const Thread& rhs) const +{ + return _id == rhs._id; +} + +bool +IceUtil::Thread::operator!=(const Thread& rhs) const +{ + return _id != rhs._id; +} + +bool +IceUtil::Thread::operator<(const Thread& rhs) const +{ + return _id < rhs._id; +} + +#else + +IceUtil::ThreadControl::ThreadControl(pthread_t id) : + _id(id) +{ +} + +IceUtil::ThreadControl::ThreadControl() : + _id(pthread_self()) +{ +} + +bool +IceUtil::ThreadControl::operator==(const ThreadControl& rhs) const +{ + return pthread_equal(_id, rhs._id); +} + +bool +IceUtil::ThreadControl::operator!=(const ThreadControl& rhs) const +{ + return !pthread_equal(_id, rhs._id); +} + +bool +IceUtil::ThreadControl::operator<(const ThreadControl& rhs) const +{ + // NOTE: Linux specific + return _id < rhs._id; +} + +void +IceUtil::ThreadControl::join() +{ + if(_id) + { + void* ignore = 0; + int rc = pthread_join(_id, &ignore); + if(rc != 0) + { + throw ThreadSyscallException(__FILE__, __LINE__); + } + } +} + +void +IceUtil::ThreadControl::sleep(const Time& timeout) +{ + struct timeval tv = timeout; + struct timespec ts; + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec * 1000L; + nanosleep(&ts, 0); +} + +void +IceUtil::ThreadControl::yield() +{ + sched_yield(); +} + +IceUtil::Thread::Thread() +{ +} + +IceUtil::Thread::~Thread() +{ +} + +static void* +startHook(void* arg) +{ + try + { + IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg); + + // + // Ensure that the thread doesn't go away until run() has + // completed. + // + IceUtil::ThreadPtr thread = rawThread; + + // + // See the comment in IceUtil::Thread::start() for details. + // + rawThread->__decRef(); + thread->run(); + } + catch(const IceUtil::Exception& e) + { + cerr << "IceUtil::Thread::run(): uncaught exception: "; + cerr << e << endl; + } + return 0; +} + +IceUtil::ThreadControl +IceUtil::Thread::start() +{ + // + // It's necessary to increment the reference count since + // pthread_create won't necessarily call the thread function until + // later. If the user does (new MyThread)->start() then the thread + // object could be deleted before the thread object takes + // ownership. It's also necessary to increment the reference count + // prior to calling pthread_create since the thread itself calls + // __decRef(). + // + __incRef(); + int rc = pthread_create(&_id, 0, startHook, this); + if(rc != 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__); + } + return ThreadControl(_id); +} + +IceUtil::ThreadControl +IceUtil::Thread::getThreadControl() +{ + return ThreadControl(_id); +} + +bool +IceUtil::Thread::operator==(const Thread& rhs) const +{ + return pthread_equal(_id, rhs._id); +} + +bool +IceUtil::Thread::operator!=(const Thread& rhs) const +{ + return !pthread_equal(_id, rhs._id); +} + +bool +IceUtil::Thread::operator<(const Thread& rhs) const +{ + // NOTE: Linux specific + return _id < rhs._id; +} + +#endif + diff --git a/cpp/src/IceUtil/ThreadException.cpp b/cpp/src/IceUtil/ThreadException.cpp index f0e9acea3b9..88c3413c9fd 100644 --- a/cpp/src/IceUtil/ThreadException.cpp +++ b/cpp/src/IceUtil/ThreadException.cpp @@ -1,100 +1,100 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/ThreadException.h>
-
-using namespace std;
-
-IceUtil::ThreadSyscallException::ThreadSyscallException(const char* file, int line) :
- Exception(file, line),
-#ifdef _WIN32
- _error(GetLastError())
-#else
- _error(errno)
-#endif
-{
-}
-
-string
-IceUtil::ThreadSyscallException::ice_name() const
-{
- return "IceUtil::ThreadSyscallException";
-}
-
-void
-IceUtil::ThreadSyscallException::ice_print(ostream& os) const
-{
- Exception::ice_print(os);
- if(_error != 0)
- {
- os << ":\nthread syscall exception: ";
-#ifdef _WIN32
- LPVOID lpMsgBuf = 0;
- DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- _error,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR)&lpMsgBuf,
- 0,
- NULL);
-
- if(ok)
- {
- LPCTSTR msg = (LPCTSTR)lpMsgBuf;
- assert(msg && strlen(msg) > 0);
- os << msg;
- LocalFree(lpMsgBuf);
- }
- else
- {
- os << "unknown thread error";
- }
-#else
- os << strerror(_error);
-#endif
- }
-}
-
-IceUtil::Exception*
-IceUtil::ThreadSyscallException::ice_clone() const
-{
- return new ThreadSyscallException(*this);
-}
-
-void
-IceUtil::ThreadSyscallException::ice_throw() const
-{
- throw *this;
-}
-
-IceUtil::ThreadLockedException::ThreadLockedException(const char* file, int line) :
- Exception(file, line)
-{
-}
-
-string
-IceUtil::ThreadLockedException::ice_name() const
-{
- return "IceUtil::ThreadLockedException";
-}
-
-IceUtil::Exception*
-IceUtil::ThreadLockedException::ice_clone() const
-{
- return new ThreadLockedException(*this);
-}
-
-void
-IceUtil::ThreadLockedException::ice_throw() const
-{
- throw *this;
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/ThreadException.h> + +using namespace std; + +IceUtil::ThreadSyscallException::ThreadSyscallException(const char* file, int line) : + Exception(file, line), +#ifdef _WIN32 + _error(GetLastError()) +#else + _error(errno) +#endif +{ +} + +string +IceUtil::ThreadSyscallException::ice_name() const +{ + return "IceUtil::ThreadSyscallException"; +} + +void +IceUtil::ThreadSyscallException::ice_print(ostream& os) const +{ + Exception::ice_print(os); + if(_error != 0) + { + os << ":\nthread syscall exception: "; +#ifdef _WIN32 + LPVOID lpMsgBuf = 0; + DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + _error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, + 0, + NULL); + + if(ok) + { + LPCTSTR msg = (LPCTSTR)lpMsgBuf; + assert(msg && strlen(msg) > 0); + os << msg; + LocalFree(lpMsgBuf); + } + else + { + os << "unknown thread error"; + } +#else + os << strerror(_error); +#endif + } +} + +IceUtil::Exception* +IceUtil::ThreadSyscallException::ice_clone() const +{ + return new ThreadSyscallException(*this); +} + +void +IceUtil::ThreadSyscallException::ice_throw() const +{ + throw *this; +} + +IceUtil::ThreadLockedException::ThreadLockedException(const char* file, int line) : + Exception(file, line) +{ +} + +string +IceUtil::ThreadLockedException::ice_name() const +{ + return "IceUtil::ThreadLockedException"; +} + +IceUtil::Exception* +IceUtil::ThreadLockedException::ice_clone() const +{ + return new ThreadLockedException(*this); +} + +void +IceUtil::ThreadLockedException::ice_throw() const +{ + throw *this; +} diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp index 886edc4b6e8..6774ec97736 100644 --- a/cpp/src/IceUtil/Time.cpp +++ b/cpp/src/IceUtil/Time.cpp @@ -1,142 +1,142 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/Time.h>
-
-#ifdef _WIN32
-# include <sys/timeb.h>
-#else
-# include <sys/time.h>
-#endif
-
-using namespace IceUtil;
-
-Time::Time() :
- _usec(0)
-{
-}
-
-Time
-IceUtil::Time::now()
-{
-#ifdef WIN32
- struct _timeb tb;
- _ftime(&tb);
- return Time(tb.time * static_cast<Int64>(1000000) + tb.millitm * static_cast<Int64>(1000));
-#else
- struct timeval tv;
- gettimeofday(&tv, 0);
- return Time(tv.tv_sec * static_cast<Int64>(1000000) + tv.tv_usec);
-#endif
-}
-
-Time
-IceUtil::Time::seconds(long t)
-{
- return Time(t * static_cast<Int64>(1000000));
-}
-
-Time
-IceUtil::Time::milliSeconds(long t)
-{
- return Time(t * static_cast<Int64>(1000));
-}
-
-Time
-IceUtil::Time::microSeconds(Int64 t)
-{
- return Time(t);
-}
-
-Time
-IceUtil::Time::operator-() const
-{
- return Time(-_usec);
-}
-
-Time
-IceUtil::Time::operator-(const Time& rhs) const
-{
- return Time(_usec - rhs._usec);
-}
-
-Time
-IceUtil::Time::operator+(const Time& rhs) const
-{
- return Time(_usec + rhs._usec);
-}
-
-Time&
-IceUtil::Time::operator+=(const Time& rhs)
-{
- _usec += rhs._usec;
- return *this;
-}
-
-Time&
-IceUtil::Time::operator-=(const Time& rhs)
-{
- _usec -= rhs._usec;
- return *this;
-}
-
-bool
-IceUtil::Time::operator<(const Time& rhs) const
-{
- return _usec < rhs._usec;
-}
-
-bool
-IceUtil::Time::operator<=(const Time& rhs) const
-{
- return _usec <= rhs._usec;
-}
-
-bool
-IceUtil::Time::operator>(const Time& rhs) const
-{
- return _usec > rhs._usec;
-}
-
-bool
-IceUtil::Time::operator>=(const Time& rhs) const
-{
- return _usec >= rhs._usec;
-}
-
-bool
-IceUtil::Time::operator==(const Time& rhs) const
-{
- return _usec == rhs._usec;
-}
-
-bool
-IceUtil::Time::operator!=(const Time& rhs) const
-{
- return _usec != rhs._usec;
-}
-
-IceUtil::Time::operator timeval() const
-{
- timeval tv;
- tv.tv_sec = static_cast<long>(_usec / 1000000);
- tv.tv_usec = static_cast<long>(_usec % 1000000);
- return tv;
-}
-
-IceUtil::Time::operator double() const
-{
- return _usec / 1000000.0L;
-}
-
-Time::Time(Int64 usec) :
- _usec(usec)
-{
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/Time.h> + +#ifdef _WIN32 +# include <sys/timeb.h> +#else +# include <sys/time.h> +#endif + +using namespace IceUtil; + +Time::Time() : + _usec(0) +{ +} + +Time +IceUtil::Time::now() +{ +#ifdef WIN32 + struct _timeb tb; + _ftime(&tb); + return Time(tb.time * static_cast<Int64>(1000000) + tb.millitm * static_cast<Int64>(1000)); +#else + struct timeval tv; + gettimeofday(&tv, 0); + return Time(tv.tv_sec * static_cast<Int64>(1000000) + tv.tv_usec); +#endif +} + +Time +IceUtil::Time::seconds(long t) +{ + return Time(t * static_cast<Int64>(1000000)); +} + +Time +IceUtil::Time::milliSeconds(long t) +{ + return Time(t * static_cast<Int64>(1000)); +} + +Time +IceUtil::Time::microSeconds(Int64 t) +{ + return Time(t); +} + +Time +IceUtil::Time::operator-() const +{ + return Time(-_usec); +} + +Time +IceUtil::Time::operator-(const Time& rhs) const +{ + return Time(_usec - rhs._usec); +} + +Time +IceUtil::Time::operator+(const Time& rhs) const +{ + return Time(_usec + rhs._usec); +} + +Time& +IceUtil::Time::operator+=(const Time& rhs) +{ + _usec += rhs._usec; + return *this; +} + +Time& +IceUtil::Time::operator-=(const Time& rhs) +{ + _usec -= rhs._usec; + return *this; +} + +bool +IceUtil::Time::operator<(const Time& rhs) const +{ + return _usec < rhs._usec; +} + +bool +IceUtil::Time::operator<=(const Time& rhs) const +{ + return _usec <= rhs._usec; +} + +bool +IceUtil::Time::operator>(const Time& rhs) const +{ + return _usec > rhs._usec; +} + +bool +IceUtil::Time::operator>=(const Time& rhs) const +{ + return _usec >= rhs._usec; +} + +bool +IceUtil::Time::operator==(const Time& rhs) const +{ + return _usec == rhs._usec; +} + +bool +IceUtil::Time::operator!=(const Time& rhs) const +{ + return _usec != rhs._usec; +} + +IceUtil::Time::operator timeval() const +{ + timeval tv; + tv.tv_sec = static_cast<long>(_usec / 1000000); + tv.tv_usec = static_cast<long>(_usec % 1000000); + return tv; +} + +IceUtil::Time::operator double() const +{ + return _usec / 1000000.0L; +} + +Time::Time(Int64 usec) : + _usec(usec) +{ +} diff --git a/cpp/src/IceUtil/UUID.cpp b/cpp/src/IceUtil/UUID.cpp index 21bab48af46..95ddb2a1d38 100644 --- a/cpp/src/IceUtil/UUID.cpp +++ b/cpp/src/IceUtil/UUID.cpp @@ -1,50 +1,50 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/UUID.h>
-
-#ifdef _WIN32
-# include <rpc.h>
-#else
-extern "C" // uuid/uuid.h seems to miss extern "C" declarations.
-{
-# include <uuid/uuid.h>
-}
-#endif
-
-using namespace std;
-
-string
-IceUtil::generateUUID()
-{
-#ifdef _WIN32
-
- UUID uuid;
- UuidCreate(&uuid);
-
- unsigned char* str;
- UuidToString(&uuid, &str);
-
- string result(reinterpret_cast<char*>(str));
- RpcStringFree(&str);
- return result;
-
-#else
-
- uuid_t uuid;
- uuid_generate(uuid);
-
- char str[37];
- uuid_unparse(uuid, str);
-
- return str;
-
-#endif
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/UUID.h> + +#ifdef _WIN32 +# include <rpc.h> +#else +extern "C" // uuid/uuid.h seems to miss extern "C" declarations. +{ +# include <uuid/uuid.h> +} +#endif + +using namespace std; + +string +IceUtil::generateUUID() +{ +#ifdef _WIN32 + + UUID uuid; + UuidCreate(&uuid); + + unsigned char* str; + UuidToString(&uuid, &str); + + string result(reinterpret_cast<char*>(str)); + RpcStringFree(&str); + return result; + +#else + + uuid_t uuid; + uuid_generate(uuid); + + char str[37]; + uuid_unparse(uuid, str); + + return str; + +#endif +} diff --git a/cpp/src/IceUtil/Unicode.cpp b/cpp/src/IceUtil/Unicode.cpp index d440bb00fe5..2997a6e4dc7 100644 --- a/cpp/src/IceUtil/Unicode.cpp +++ b/cpp/src/IceUtil/Unicode.cpp @@ -1,149 +1,149 @@ -// **********************************************************************
-//
-// Copyright (c) 2001
-// Mutable Realms, Inc.
-// Huntsville, AL, USA
-//
-// All Rights Reserved
-//
-// **********************************************************************
-
-#include <IceUtil/Unicode.h>
-#include <algorithm>
-
-using namespace std;
-
-string
-IceUtil::wstringToString(const wstring& str)
-{
- string result;
- result.reserve(str.length() * 2);
-
- for(unsigned int i = 0; i < str.length(); ++i)
- {
- wchar_t wc;
- wc = str[i];
-
- if(wc < 0x80)
- {
- result += static_cast<char>(wc);
- }
- else if(wc < 0x800)
- {
- result += 0xc0 | (wc>>6);
- result += 0x80 | (wc & 0x3f);
- }
- else if(wc < 0x10000)
- {
- result += 0xe0 | (wc>>12);
- result += 0x80 | ((wc>>6) & 0x3f);
- result += 0x80 | (wc & 0x3f);
- }
- else if(wc < 0x10FFFF)
- {
- result += 0xf0 | (wc>>18);
- result += 0x80 | ((wc>>12) & 0x3f);
- result += 0x80 | ((wc>>6) & 0x3f);
- result += 0x80 | (wc & 0x3f);
- }
- else
- {
- return result; // Error, not encodable.
- }
- }
-
- return result;
-}
-
-wstring
-IceUtil::stringToWstring(const string& str)
-{
- wstring result;
- result.reserve(str.length());
-
- unsigned int len;
- for(unsigned int i = 0; i < str.length(); i += len)
- {
- unsigned char c = str[i];
- wchar_t wc;
- int minval;
-
- if(c < 0x80)
- {
- wc = c;
- len = 1;
- minval = 0;
- }
- else if(c < 0xc0) // Lead byte must not be 10xxxxxx
- {
- return result; // Error, not encodable.
- }
- else if(c < 0xe0) // 110xxxxx
- {
- wc = c & 0x1f;
- len = 2;
- minval = 0x80;
- }
- else if(c < 0xf0) // 1110xxxx
- {
- wc = c & 0xf;
- len = 3;
- minval = 0x800;
- }
-#if SIZEOF_WCHAR_T >= 4
- else if(c < 0xf8) // 11110xxx
- {
- wc = c & 7;
- len = 4;
- minval = 0x10000;
- }
- else if(c < 0xfc) // 111110xx
- {
- // Length 5 and 6 is declared invalid in Unicode 3.1 and ISO 10646:2001.
- wc = c & 3;
- len = 5;
- minval = 0x110000;
- }
- else if(c < 0xfe) // 1111110x
- {
- // Length 5 and 6 is declared invalid in Unicode 3.1 and ISO 10646:2001.
- wc = c & 1;
- len = 6;
- minval = 0x4000000;
- }
-#endif
- else
- {
- return result; // Error, not encodable.
- }
-
- if(i + len - 1 < str.length())
- {
- for(unsigned int j = 1; j < len; ++j)
- {
- if((str[i + j] & 0xc0) != 0x80) // All other bytes must be 10xxxxxx
- {
- return result; // Error, not encodable.
- }
-
- wc <<= 6;
- wc |= str[i + j] & 0x3f;
- }
-
- if(wc < minval)
- {
- return result; // Error, non-shortest form.
- }
- else
- {
- result += wc;
- }
- }
- else
- {
- return result; // Error, not encodable.
- }
- }
-
- return result;
-}
+// ********************************************************************** +// +// Copyright (c) 2001 +// Mutable Realms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <IceUtil/Unicode.h> +#include <algorithm> + +using namespace std; + +string +IceUtil::wstringToString(const wstring& str) +{ + string result; + result.reserve(str.length() * 2); + + for(unsigned int i = 0; i < str.length(); ++i) + { + wchar_t wc; + wc = str[i]; + + if(wc < 0x80) + { + result += static_cast<char>(wc); + } + else if(wc < 0x800) + { + result += 0xc0 | (wc>>6); + result += 0x80 | (wc & 0x3f); + } + else if(wc < 0x10000) + { + result += 0xe0 | (wc>>12); + result += 0x80 | ((wc>>6) & 0x3f); + result += 0x80 | (wc & 0x3f); + } + else if(wc < 0x10FFFF) + { + result += 0xf0 | (wc>>18); + result += 0x80 | ((wc>>12) & 0x3f); + result += 0x80 | ((wc>>6) & 0x3f); + result += 0x80 | (wc & 0x3f); + } + else + { + return result; // Error, not encodable. + } + } + + return result; +} + +wstring +IceUtil::stringToWstring(const string& str) +{ + wstring result; + result.reserve(str.length()); + + unsigned int len; + for(unsigned int i = 0; i < str.length(); i += len) + { + unsigned char c = str[i]; + wchar_t wc; + int minval; + + if(c < 0x80) + { + wc = c; + len = 1; + minval = 0; + } + else if(c < 0xc0) // Lead byte must not be 10xxxxxx + { + return result; // Error, not encodable. + } + else if(c < 0xe0) // 110xxxxx + { + wc = c & 0x1f; + len = 2; + minval = 0x80; + } + else if(c < 0xf0) // 1110xxxx + { + wc = c & 0xf; + len = 3; + minval = 0x800; + } +#if SIZEOF_WCHAR_T >= 4 + else if(c < 0xf8) // 11110xxx + { + wc = c & 7; + len = 4; + minval = 0x10000; + } + else if(c < 0xfc) // 111110xx + { + // Length 5 and 6 is declared invalid in Unicode 3.1 and ISO 10646:2001. + wc = c & 3; + len = 5; + minval = 0x110000; + } + else if(c < 0xfe) // 1111110x + { + // Length 5 and 6 is declared invalid in Unicode 3.1 and ISO 10646:2001. + wc = c & 1; + len = 6; + minval = 0x4000000; + } +#endif + else + { + return result; // Error, not encodable. + } + + if(i + len - 1 < str.length()) + { + for(unsigned int j = 1; j < len; ++j) + { + if((str[i + j] & 0xc0) != 0x80) // All other bytes must be 10xxxxxx + { + return result; // Error, not encodable. + } + + wc <<= 6; + wc |= str[i + j] & 0x3f; + } + + if(wc < minval) + { + return result; // Error, non-shortest form. + } + else + { + result += wc; + } + } + else + { + return result; // Error, not encodable. + } + } + + return result; +} |