Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Safe hooks 安全鉤子

Features of the safe mode 安全模式的特點
Configuring safe-mode assertions 配置安全模式的斷言

Boost.Intrusive hooks can be configured to operate in safe-link mode. The safe mode is activated by default, but it can be also explicitly activated:
Boost.Intrusive 鉤子可以被配置為以安全鏈接模式操作。安全模式缺省是激活的,不過也可以明確激活:

//Configuring the safe mode explicitly 顯式配置安全模式
class Foo : public list_base_hook< link_mode<safe_link> > {};

With the safe mode the user can detect if the object is actually inserted in a container without any external reference. Let's review the basic features of the safe mode:
通過使用安全模式,用戶可以檢測實際插入到容器中的對象是否沒有任何外部引用。我們來複習一下安全模式的基本特點:

  • Hook's constructor puts the hook in a well-known default state.
    鉤子的構造函數將鉤子置於一個可知的缺省狀態。
  • Hook's destructor checks if the hook is in the well-known default state. If not, an assertion is raised.
    鉤子的析構函數檢查鉤子是否處於缺省狀態。如否,則引發斷言。
  • Every time an object is inserted in the intrusive container, the container checks if the hook is in the well-known default state. If not, an assertion is raised.
    每當有對象被插入到介入式容器中時,容器檢查其鉤子是否處於缺省狀態。如否,則引發斷言。
  • Every time an object is being erased from the intrusive container, the container puts the erased object in the well-known default state.
    每當從介入式容器移除對像時,容器將被移除對像重置回缺省狀態。

With these features, without any external reference the user can know if the object has been inserted in a container by calling the is_linked() member function. If the object is not actually inserted in a container, the hook is in the default state, and if it is inserted in a container, the hook is not in the default state.
有了這些特性,無須任何外部引用,用戶可以通過調用 is_linked() 成員函數就知道對象是否已被插入到某個容器中。如果該對像未被實際插入到容器中,則其鉤子處於缺省狀態,如果它已被插入到容器中,則其鉤子就處於非缺省狀態。

By default, all safe-mode assertions raised by Boost-Intrusive hooks and containers in are implemented using BOOST_ASSERT, which can be configured by the user. See http://www.boost.org/libs/utility/assert.html for more information about BOOST_ASSERT.
缺省情況下,所有由 Boost-Intrusive 鉤子和容器引發的安全模式斷言都是用 BOOST_ASSERT 實現的,可以由用戶配置。有關 BOOST_ASSERT 的更多信息請見 http://www.boost.org/libs/utility/assert.html

BOOST_ASSERT is globally configured, so the user might want to redefine intrusive safe-mode assertions without modifying the global BOOST_ASSERT. This can be achieved redefining the following macros:
BOOST_ASSERT 是全局配置的,因此用戶可能會想不修改全局的 BOOST_ASSERT 而重新定義介入式安全模式的斷言。這可以通過重定義以下宏來實現:

  • BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT: This assertion will be used in insertion functions of the intrusive containers to check that the hook of the value to be inserted is default constructed.
    BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT: 該斷言在介入式容器的插入函數中被用於檢查插入值的鉤子是否缺省構造。
  • BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT: This assertion will be used in hooks' destructors to check that the hook is in a default state.
    BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT: 該斷言在鉤子的析構函數中被用於檢查鉤子是否處於缺省狀態。

If any of these macros is not redefined, the assertion will default to BOOST_ASSERT. If BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT or BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT is defined and the programmer needs to include a file to configure that assertion, it can define BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT_INCLUDE or BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT_INCLUDE with the name of the file to include:
如果這兩個宏的任何一個未重定義,則斷言缺省為 BOOST_ASSERT。如果定義了 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERTBOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT 且程序員需要包含一個文件來對斷言進行配置,則可以定義帶有該文件名的 BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT_INCLUDEBOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT_INCLUDE 來包含:

#define BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT          MYASSERT
#define BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT_INCLUDE <myassert.h>

PrevUpHomeNext