📄 message_queue_sequence.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: acestl/collections/message_queue_sequence.hpp
*
* Purpose: Sequence class for adapting ACE_Message_Queue to an STL sequence.
*
* Created: 15th September 2004
* Updated: 2nd June 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2004-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file acestl/collections/message_queue_sequence.hpp
*
* \brief [C++ only] Definition of the acestl::message_queue_sequence
* collection class template
* (\ref group__library__collections "Collections" Library).
*/
#ifndef ACESTL_INCL_ACESTL_COLLECTIONS_HPP_MESSAGE_QUEUE_SEQUENCE
#define ACESTL_INCL_ACESTL_COLLECTIONS_HPP_MESSAGE_QUEUE_SEQUENCE
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define ACESTL_VER_ACESTL_COLLECTIONS_HPP_MESSAGE_QUEUE_SEQUENCE_MAJOR 2
# define ACESTL_VER_ACESTL_COLLECTIONS_HPP_MESSAGE_QUEUE_SEQUENCE_MINOR 1
# define ACESTL_VER_ACESTL_COLLECTIONS_HPP_MESSAGE_QUEUE_SEQUENCE_REVISION 7
# define ACESTL_VER_ACESTL_COLLECTIONS_HPP_MESSAGE_QUEUE_SEQUENCE_EDIT 54
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef ACESTL_INCL_ACESTL_HPP_ACESTL
# include <acestl/acestl.hpp>
#endif /* !ACESTL_INCL_ACESTL_HPP_ACESTL */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
# include <stlsoft/util/std/iterator_helper.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
#ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
# include <stlsoft/collections/util/collections.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
#ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
# include <stlsoft/conversion/sap_cast.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST */
#ifndef STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_BOUNDED
# include <stlsoft/algorithms/bounded.hpp> // for stlsoft::copy_n()
#endif /* !STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_BOUNDED */
#if ACESTL_ACE_VERSION >= 0x00050004
// This stinks, but appears to be necessary in ACE versions later than 5.3
# include <ace/Condition_Thread_Mutex.h>
# include <ace/Guard_T.h>
# include <ace/Null_Condition.h>
# include <ace/Null_Mutex.h>
# include <ace/Thread_Mutex.h>
#endif /* ACESTL_ACE_VERSION */
#include <ace/Message_Queue_T.h>
#include <algorithm> // for std::copy
#ifdef STLSOFT_UNITTEST
# include <acestl/memory/message_block_functions.hpp>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _ACESTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::acestl */
namespace acestl
{
# else
/* Define stlsoft::acestl_project */
namespace stlsoft
{
namespace acestl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_ACESTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/** An \ref group__pattern__instance_adaptor "instance adaptor" that
* adapts an instance of \c ACE_Message_Queue to provide an STL
* input-sequence interface.
*
* \ingroup group__library__collections
*
* It is used as follows:
\code
using acestl::message_queue_sequence;
using stlsoft::auto_buffer;
ACE_Message_Queue<ACE_NULL_SYNCH> &mq = . . .
// Adapt the message queue into the sequence
message_queue_sequence<ACE_NULL_SYNCH> mqs(mq);
// Allocate a contiguous buffer, using stlsoft::auto_buffer
auto_buffer<char> buff(mqs.size());
// Block copy into the buffer
std::copy(&buff[0], &buff[0] + (buff.size()), mqs.begin());
\endcode
*
* \note Although this class provides <i>Input Iterator</i>s, it
* uses customisations of \c std algorithms to effect very favourable
* performance, as described in chapter 31 of
* \ref section__publishing__books__xstlv1.
*/
// [[synesis:class:collection: acestl::message_queue_sequence<T<S>>]]
template <ACE_SYNCH_DECL>
class message_queue_sequence
: public stlsoft_ns_qual(stl_collection_tag)
{
/// \name Types
/// @{
public:
/// The value type
typedef char value_type;
/// The sequence type
typedef ACE_Message_Queue<ACE_SYNCH_USE> sequence_type;
/// The current parameterisation of the type
typedef message_queue_sequence<ACE_SYNCH_USE> class_type;
/// The size type
typedef ss_size_t size_type;
/** Iterator type for the message_queue_sequence class template
*
* \note Although this iterator class is an <i>Input Iterator</i>, it
* uses customisations of \c std algorithms to effect very favourable
* performance, as described in chapter 31 of <b>Extended STL,
* volume 1</b> (published by Addison-Wesley, June 2007).
*/
class iterator
: public stlsoft_ns_qual(iterator_base)<acestl_ns_qual_std(input_iterator_tag)
, char
, ss_ptrdiff_t
, char*
, char&
>
{
private:
friend class message_queue_sequence<ACE_SYNCH_USE>;
typedef ACE_Message_Queue_Iterator<ACE_SYNCH_USE> mq_iterator_type;
private:
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
struct shared_handle
{
public:
typedef shared_handle class_type;
// Members
public:
mq_iterator_type m_mqi;
ACE_Message_Block *m_entry;
as_size_t m_entryLength;
as_size_t m_entryIndex;
private:
ss_sint32_t m_refCount;
/// Construction
public:
ss_explicit_k shared_handle(sequence_type &mq)
: m_mqi(mq)
, m_entry(NULL)
, m_entryLength(0)
, m_entryIndex(0)
, m_refCount(1)
{
if(m_mqi.next(m_entry))
{
for(;;)
{
if(0 != (m_entryLength = m_entry->length()))
{
break;
}
else if(NULL == (m_entry = nextEntry()))
{
break;
}
}
}
}
# if defined(STLSOFT_CF_COMPILER_WARNS_NO_PUBLIC_DTOR)
protected:
# else /* ? STLSOFT_CF_COMPILER_WARNS_NO_PUBLIC_DTOR */
private:
# endif /* STLSOFT_CF_COMPILER_WARNS_NO_PUBLIC_DTOR */
~shared_handle() stlsoft_throw_0()
{
ACESTL_MESSAGE_ASSERT("Shared search handle being destroyed with outstanding references!", 0 == m_refCount);
}
/// Accessors
public:
ss_bool_t is_end_point() const
{
return m_entryIndex == m_entryLength;
}
char ¤t()
{
ACESTL_ASSERT(NULL != m_entry);
ACESTL_ASSERT(m_entryIndex != m_entryLength);
return m_entryIndex[m_entry->rd_ptr()];
}
char current() const
{
ACESTL_ASSERT(NULL != m_entry);
ACESTL_ASSERT(m_entryIndex != m_entryLength);
return m_entryIndex[m_entry->rd_ptr()];
}
ss_bool_t advance()
{
ACESTL_MESSAGE_ASSERT("Invalid index", m_entryIndex < m_entryLength);
if(++m_entryIndex == m_entryLength)
{
m_entryIndex = 0;
for(;;)
{
if(NULL == (m_entry = nextEntry()))
{
return false;
}
else if(0 != (m_entryLength = m_entry->length()))
{
break;
}
}
}
return true;
}
/// Operations
public:
ss_sint32_t AddRef()
{
return ++m_refCount;
}
ss_sint32_t Release()
{
ss_sint32_t rc = --m_refCount;
if(0 == rc)
{
delete this;
}
return rc;
}
void fast_copy(char const* f, char const* l, size_type n)
{
ACESTL_ASSERT(0 != n);
ACESTL_ASSERT(f != l);
if(0 != n)
{
size_type n1 = m_entryLength - m_entryIndex;
if(n <= n1)
{
// Terminal case
::memcpy(&m_entryIndex[m_entry->rd_ptr()], f, n);
ACESTL_ASSERT(n <= m_entryLength - m_entryIndex);
m_entryIndex += n;
ACESTL_ASSERT(m_entryIndex <= m_entryLength);
}
else
{
// Recursive case
::memcpy(&m_entryIndex[m_entry->rd_ptr()], f, n1);
f += n1;
m_entry = nextEntry();
ACESTL_MESSAGE_ASSERT("Attempt to walk off end of iterator's range", NULL != m_entry);
m_entryIndex = 0;
m_entryLength = m_entry->length();
fast_copy(f, l, n - n1);
}
}
}
void fast_copy(class_type const* l, char* o)
{
size_type n1 = m_entryLength - m_entryIndex;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -