名前空間
変種
操作

std::ios_base::register_callback

From cppreference.com
< cpp‎ | io‎ | ios base
 
 
 
 
void register_callback( event_callback function, int index );

imbue(), std::basic_ios::copyfmt(), および ~ios_base() によって呼び出される、ユーザー定義関数を登録します。登録された各コールバックは、イベントタイプ(event 型の値)が最初の引数として渡され、呼び出し元を区別するために使用できます。毎回呼び出されます。

コールバックは、登録の逆順に呼び出されます(つまり、register_callback() はコールバックスタックにコールバックペアをプッシュします)。コールバック関数内から新しいコールバックを追加するために register_callback() が呼び出された場合、新しいコールバックは次のイベントで初めて呼び出されます。

ユーザー定義コールバック関数は例外をスローすることは許可されません。

目次

[編集] パラメータ

関数 - event_callback 型の関数ポインタとして提供される、イベント時に呼び出される関数。
index - 関数に渡されるカスタムパラメータ。

[編集] 戻り値

(なし)

[編集] 注釈

登録されたコールバックは、解除することはできません。ストリームオブジェクトの存続期間中は、その一部として保持されます。コールバックの動作を変更する必要がある場合は、iword() または pword() を介して制御できます。

同じ関数が複数回登録された場合、複数回呼び出されます。

コールバックと共に格納される整数値は、通常、xalloc() から取得したインデックスです。

[編集]

カスタム出力演算子によって使用される、ロケール依存のキャッシュ値を更新するために register_callback を使用する方法を示します。

#include <functional>
#include <iostream>
#include <locale>
 
// Cached locale-specific message and its hash
typedef std::pair<std::string, std::size_t> cache_t;
 
// Populate the cached message and its hash from the locale
void update_cache(cache_t& cache, std::locale loc)
{
    auto& fct = std::use_facet< std::messages<char> >(loc);
    std::messages_base::catalog cat = fct.open("sed", loc);
    cache.first = cat < 0 ? "" : fct.get(cat, 0, 0, "Memory exhausted");
    cache.second = std::hash<std::string>()(cache.first);
}
 
// Update the cache if the locale changed
void true_callback(std::ios_base::event evt, std::ios_base& str, int idx)
{
    if (evt == std::ios_base::imbue_event) 
    {
        cache_t* ptr = static_cast<cache_t*>(str.pword(idx));
        update_cache(*ptr, str.getloc());
    }
}
 
// Registers the cache in pword() and sets up the callback
struct CacheSetup
{
    CacheSetup(std::ostream& os, std::ios_base::event_callback f, cache_t* cache)
    {
        int index = std::ostream::xalloc();
        os.pword(index) = cache; // Store pointer to cache in the stream
        os.register_callback(f, index); // Store callback and the index to the pointer
        update_cache(*cache, os.getloc()); // Initialize cache
    };
};
 
// Some custom class 
struct S {};
// Some custom class's operator<< that needs fast access to hashed message
std::ostream& operator<<(std::ostream& os, const S&)
{
    static cache_t cache;
    static CacheSetup setup(os, true_callback, &cache);
    return os << cache.first << " : " << cache.second;
}
 
int main()
{
    std::locale loc("en_US.utf8");
 
    S s;
    std::cout.imbue(loc);
    std::cout << s << '\n';
 
    std::cout.imbue(std::locale(loc, new std::messages_byname<char>("de_DE.utf8")));
    std::cout << s << '\n';
 
    std::cout.imbue(std::locale(loc, new std::messages_byname<char>("ja_JP.utf8")));
    std::cout << s << '\n';
 
    std::cout.imbue(std::locale(loc, new std::messages_byname<char>("ru_RU.utf8")));
    std::cout << s << '\n';
}

出力

Memory exhausted : 2,295,079,096
Speicher erschöpft : 3,139,423,551
メモリーが足りません : 3,837,351,114
Память исчерпана : 3,742,732,851
English 日本語 中文(简体) 中文(繁體)