📄 agents.h
字号:
_Right._M_index = _Index;
_Right._M_size = _Size;
}
}
private:
//
// Initialize the array
//
void _Init()
{
_M_array = NULL;
_M_index = 0;
_M_size = 0;
}
//
// Grow the array to the given size. The old elements are copied over.
//
void _Grow(size_t _NewSize)
{
_ASSERTE(_NewSize > _M_size);
_Type * _Array = new _Type[_NewSize];
if (_M_array != NULL)
{
// Copy over the elememts
for (size_t _I = 0; _I < _M_size; _I++)
{
_Array[_I] = _M_array[_I];
}
delete [] _M_array;
}
_M_array = _Array;
_M_size = _NewSize;
}
// Private data members
// Array of elements
_Type * _M_array;
// Index where the next element should be inserted
size_t _M_index;
// Capacity of the array.
size_t _M_size;
static const int _S_growthFactor = 2;
};
} // namespace details
//**************************************************************************
// Public Namespace:
//
// Anything in the Concurrency namespace is intended for direct client consumption.
//
//**************************************************************************
//
// Forward declarations:
//
template<class _Type> class ISource;
template<class _Type> class ITarget;
//**************************************************************************
// Network link registry
//**************************************************************************
// Forward declaration for use in the iterator
template<class _Block> class network_link_registry;
/// <summary>
/// Const iterator for network link registry. Message blocks should use
/// the link_registry::iterator type for iteration.
/// </summary>
/// <typeparam name="_Block">
/// The network block type
/// </typeparam>
/**/
template<class _Block>
class _Network_link_iterator
{
public:
typedef _Network_link_iterator<_Block> _Myt;
typedef network_link_registry<_Block> _MyContainer;
// Element type
typedef _Block* _EType;
// Const iterator - iterator shall not be used to modify the links
typedef _EType const& const_reference;
typedef _EType const* const_pointer;
/// <summary>
/// Construct iterator
/// </summary>
/**/
_Network_link_iterator(_MyContainer * _PNetwork_link, size_t _Index) : _M_pNetwork_link(_PNetwork_link), _M_index(_Index), _M_value(NULL)
{
_M_pNetwork_link->_Next_index(_M_index);
}
/// <summary>
/// Copy construct an iterator
/// </summary>
/**/
_Network_link_iterator(_Myt const& _Right)
{
_M_pNetwork_link = _Right._M_pNetwork_link;
_M_index = _Right._M_index;
}
/// <summary>
/// Copy assign an iterator
/// </summary>
/**/
_Myt const& operator=(_Myt const& _Right)
{
_M_pNetwork_link = _Right._M_pNetwork_link;
_M_index = _Right._M_index;
return *this;
}
/// <summary>
/// Returns the object pointed to by the iterator
/// </summary>
/// <returns>
/// Reference to the object pointed to by the iterator
/// </returns>
/**/
const_reference operator*()
{
_M_value = _M_pNetwork_link->_Get_element(_M_index);
return _M_value;
}
/// <summary>
/// Returns a pointer to the class object
/// </summary>
/// <returns>
/// Returns a pointer to the class object
/// </returns>
/**/
const_pointer operator->() const
{
return (&**this);
}
/// <summary>
/// Pre-increment the iterator to point to the next element
/// </summary>
/// <returns>
/// Reference to the object pointer to by the iterator after
/// incrementing it
/// </returns>
/**/
_Myt& operator++()
{
++_M_index;
_M_pNetwork_link->_Next_index(_M_index);
return (*this);
}
/// <summary>
/// Post-increment the iterator to point to the next element
/// </summary>
/// <returns>
/// Reference to the object pointer to by the iterator before
/// incrementing it
/// </returns>
/**/
_Myt operator++(int)
{
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
private:
// Pointer to the underlying container (network link registry)
_MyContainer * _M_pNetwork_link;
// Current index
size_t _M_index;
// Current value
_EType _M_value;
};
/// <summary>
/// The <c>network_link_registry</c> abstract base class manages the links between source
/// and target blocks.
/// </summary>
/// <typeparam name="_Block">
/// The block data type being stored in the <c>network_link_registry</c>.
/// </typeparam>
/// <remarks>
/// The <c>network link registry</c> is not safe for concurrent access.
/// </remarks>
/// <seealso cref="single_link_registry Class"/>
/// <seealso cref="multi_link_registry Class"/>
/**/
template<class _Block>
class network_link_registry
{
public:
/// <summary>
/// A type that represents the block type stored in the <c>network_link_registry</c> object.
/// </summary>
/**/
typedef typename _Block type;
/// <summary>
/// A type that represents an element pointer stored in the <c>network_link_registry</c> object.
/// </summary>
/**/
typedef _Block * _EType;
/// <summary>
/// A type that provides a reference to a <c>const</c> element stored in a
/// <c>network_link_registry</c> object for reading and performing const operations.
/// </summary>
/**/
typedef _EType const& const_reference;
/// <summary>
/// A type that provides a pointer to a <c>const</c> element in a
/// <c>network_link_registry</c> object.
/// </summary>
/**/
typedef _EType const* const_pointer;
// Make the iterators friends so that they can access some of the
// private routines such as _Get_element.
/**/
friend class _Network_link_iterator<_Block>;
/// <summary>
/// A type that provides an iterator that can read or modify any element in a
/// <c>network_link_registry</c> object.
/// </summary>
/**/
typedef _Network_link_iterator<_Block> iterator;
/// <summary>
/// When overridden in a derived class, adds a link to the <c>network_link_registry</c>
/// object.
/// </summary>
/// <param name="_Link">
/// A pointer to a block to be added.
/// </param>
/**/
virtual void add(_EType _Link) = 0;
/// <summary>
/// When overridden in a derived class, removes a specified block from the
/// <c>network_link_registry</c> object.
/// </summary>
/// <param name="_Link">
/// A pointer to a block to be removed, if found.
/// </param>
/// <returns>
/// <c>true</c> if the link was found and removed, <c>false</c> otherwise.
/// </returns>
/**/
virtual bool remove(_EType _Link) = 0;
/// <summary>
/// When overridden in a derived class, searches the <c>network_link_registry</c> object
/// for a specified block.
/// </summary>
/// <param name="_Link">
/// A pointer to a block that is being searched for in the <c>network_link_registry</c>
/// object.
/// </param>
/// <returns>
/// <c>true</c> if the block was found, <c>false</c> otherwise.
/// </returns>
/**/
virtual bool contains(_EType _Link) = 0;
/// <summary>
/// When overridden in a derived class, returns the number of items in the
/// <c>network_link_registry</c> object.
/// </summary>
/// <returns>
/// The number of items in the <c>network_link_registry</c> object.
/// </returns>
/**/
virtual size_t count() = 0;
/// <summary>
/// When overridden in a derived class, returns an iterator to the first element in the
/// <c>network_link_registry</c> object.
/// </summary>
/// <remarks>
/// The end state of the iterator is indicated by a <c>NULL</c> link.
/// </remarks>
/// <returns>
/// An iterator addressing the first element in the <c>network_link_registry</c> object.
/// </returns>
/**/
virtual iterator begin() = 0;
protected:
/// <summary>
/// Skips empty slots and updates the index to the next
/// non-empty slot. This is called by the iterator.
/// </summary>
/// <param name="_Index">
/// A reference to the index that is to be updated.
/// </param>
/**/
virtual void _Next_index(size_t& _Index) = 0;
/// <summary>
/// Retrieves the element at the given index. If the index is out of bounds,
/// <c>NULL</c> is returned. Users need to use the iterator to access the links.
/// </summary>
/// <param name="_Index">
/// Index of the link to be retrieved.
/// </param>
/// <returns>
/// The element in the registry at the index specified by the <paramref name="_Index"/> parameter.
/// </returns>
/**/
virtual _EType _Get_element(size_t _Index) const = 0;
};
/// <summary>
/// The <c>single_link_registry</c> object is a <c>network_link_registry</c> that manages
/// only a single source or target block.
/// </summary>
/// <typeparam name="_Block">
/// The block data type being stored in the <c>single_link_registry</c> object.
/// </typeparam>
/// <seealso cref="multi_link_registry Class"/>
/**/
template<class _Block>
class single_link_registry : public network_link_registry<_Block>
{
public:
/// <summary>
/// Constructs a <c>single_link_registry</c> object.
/// </summary>
/**/
single_link_registry() : _M_connectedLink(NULL)
{
}
/// <summary>
/// Destroys the <c>single_link_registry</c> object.
/// </summary>
/// <remarks>
/// The method throws an <see cref="invalid_operation Class">invalid_operation</see> exception if
/// it is called before the link is removed.
/// </remarks>
/**/
virtual ~single_link_registry()
{
// It is an error to delete link registry with links
// still present
if (count() != 0)
{
throw invalid_operation("Deleting link registry before removing all the links");
}
}
/// <summary>
/// Adds a link to the <c>single_link_registry</c> object.
/// </summary>
/// <param name="_Link">
/// A pointer to a block to be added.
/// </param>
/// <remarks>
/// The method throws an <see cref="invalid_link_target Class">invalid_link_target</see> exception
/// if there is already a link in this registry.
/// </remarks>
/**/
virtual void add(_EType _Link)
{
if (_Link == NULL)
{
return;
}
// Only one link can be added.
if (_M_connectedLink != NULL)
{
throw invalid_link_target("_Link");
}
_M_connectedLink = _Link;
}
/// <summary>
/// Removes a link from the <c>single_link_registry</c> object.
/// </summary>
/// <param name="_Link">
/// A pointer to a block to be removed, if found.
/// </param>
/// <returns>
/// <c>true</c> if the link was found and removed, <c>false</c> otherwise.
/// </returns>
/**/
virtual bool remove(_EType _Link)
{
if ((_Link != NULL) && (_M_connectedLink == _Link))
{
_M_connectedLink = NULL;
return true;
}
return false;
}
/// <summary>
/// Searches the <c>single_link_registry</c> object for a specified block.
/// </summary>
/// <param name="_Link">
/// A pointer to a block that is to be searched for in the <c>single_link_registry</c> object.
/// </param>
/// <returns>
/// <c>true</c> if the link was found, <c>false</c> otherwise.
/// </returns>
/**/
virtual bool contains(_EType _Link)
{
return ((_Link != NULL) && (_M_connectedLink == _Link));
}
/// <summary>
/// Counts the number of items in the <c>single_link_registry</c> object.
/// </summary>
/// <returns>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -