std::ios_ba...er_callback(3)
π¦ std::ios_base::register_callback π¦
okay so bestie, this is the std::ios_base::register_callback page from the C++ Standard Libary, and the manual name/header up top is the truncated std::ios_ba...er_callback(3) β yes, we're folding that in right here instead of copying the line. π
π NAME π
std::ios_base::register_callback β std::ios_base::register_callback. Yep, that's the whole NAME section, bestie: the function name twice, and honestly? main character energy. π
πΈ Synopsis πΈ
void register_callback( event_callback function, int index );
Registers a user-defined function which will be called by imbue(), std::basic_ios::copyfmt() and ~ios_base(). Every registered callback is called every time: the event type (a value of type event) is passed as its first argument, and may be used to distinguish between the callers.
The callbacks are called in the reverse order of registration (in other words, register_callback() pushes a callback pair on the callback stack). If register_callback() is called from within a callback function to add a new callback, the new callback is only called on the next event.
lowkey important, bestie: the user-defined callback function is not allowed to throw exceptions. Like, do not do it. π«
π Parameters π
functionβ the function which will be called on event, supplied as a function pointer of typeevent_callbackindexβ custom parameter which will be passed to the function
π Return value π
none β literally nothing, bestie. That's the whole return value. π«Ά
β¨ Notes β¨
- Once registered, a callback cannot be deregistered: it remains a part of the stream object for the rest of its lifetime. If the behavior of a callback needs to change, it may be controlled through
iword()orpword(). - If the same function is registered multiple times, it is called multiple times.
- The integer value that is stored together with the callback is typically an index obtained from
xalloc().
π₯ Example π₯
Demonstrates the use of register_callback to update locale-dependent cached values that are used by a custom output operator.
The page says // Run this code, so here it is, bestie:
#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';
}
π Output π
and the output is exactly this, no cap:
Memory exhausted : 2,295,079,096
Speicher erschΓΆpft : 3,139,423,551
γ‘γ’γͺγΌγθΆ³γγΎγγ : 3,837,351,114
ΠΠ°ΠΌΡΡΡ ΠΈΡΡΠ΅ΡΠΏΠ°Π½Π° : 3,742,732,851
mwah π β source/version: http://cppreference.com, date: 2024.06.10, and the manual name is std::ios_ba...er_callback(3). periodt. π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms