OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
strmresid.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3//
4/// \file
5/// Private helper for reading resource identifiers from persistent streams.
6//----------------------------------------------------------------------------
7
8#ifndef OWL_STRMRESID_H
9#define OWL_STRMRESID_H
10
11#include <owl/private/defs.h>
12#if defined(BI_HAS_PRAGMA_ONCE)
13# pragma once
14#endif
15
16#include <owl/defs.h>
17#include <owl/wsyscls.h>
18#include <memory>
19#include <set>
20
21#if defined(BI_MULTI_THREAD_RTL)
22#include <owl/thread.h>
23#endif
24
25namespace owl {
26
27//
28// Takes ownership of a resource identifier just extracted from a persistent stream, and returns
29// an equivalent identifier that does not need to be deallocated.
30//
31// The persistent stream extraction operator for TResId allocates a new string when the identifier
32// is a string, leaving the caller responsible for deallocating it. Many members that store
33// resource identifiers do not own the string (e.g. TWindow::Attr.AccelTable, TMenuDescr::Id),
34// so here the string is moved into a process-lifetime pool instead, in which equal strings are
35// shared. Hence, repeated reads of the same identifiers do not use any more memory. See bug #156.
36//
38{
39 if (!id.IsString()) return id;
40 const auto s = std::unique_ptr<tchar[]>{const_cast<tchar*>(id.GetString())};
41 static std::set<tstring> pool;
42#if defined(BI_MULTI_THREAD_RTL)
43 static TCriticalSection section;
44 TCriticalSection::TLock lock{section};
45#endif
46 return TResId{pool.insert(tstring{s.get()}).first->c_str()};
47}
48
49} // OWL namespace
50
51#endif // OWL_STRMRESID_H
This nested class handles locking and unlocking critical sections.
Definition thread.h:326
Lightweight intra-process thread synchronization.
Definition thread.h:308
Object Windows Library (OWLNext Core).
Definition animctrl.h:22
char tchar
Definition defs.h:77
std::string tstring
Definition defs.h:79
auto InternStreamedResId_(TResId id) -> TResId
Definition strmresid.h:37
General definitions used by all ObjectWindows programs.
Classes for window system structure and type encapsulation.