Merge branch 'next' of ssh://git.code.sf.net/p/flightgear/simgear into next

This commit is contained in:
gallaert
2017-08-25 20:47:36 +01:00
4 changed files with 40 additions and 24 deletions
@@ -46,8 +46,20 @@ static unique_ptr<EmbeddedResourceManager> staticInstance;
// * EmbeddedResourceManager::Impl * // * EmbeddedResourceManager::Impl *
// *************************************************************************** // ***************************************************************************
EmbeddedResourceManager::Impl::Impl() EmbeddedResourceManager::Impl::Impl()
: dirty(true)
{ } { }
void
EmbeddedResourceManager::Impl::rehash()
{
// Update the list of resource pools to search when looking up a resource.
// This allows to optimize resource lookup: no need to parse, split and hash
// the same locale string every time to find the corresponding resource
// pools.
poolSearchList = listOfResourcePoolsToSearch(selectedLocale);
dirty = false;
}
string string
EmbeddedResourceManager::Impl::getLocale() const EmbeddedResourceManager::Impl::getLocale() const
{ {
@@ -59,11 +71,7 @@ EmbeddedResourceManager::Impl::selectLocale(const std::string& locale)
{ {
string previousLocale = std::move(selectedLocale); string previousLocale = std::move(selectedLocale);
selectedLocale = locale; selectedLocale = locale;
// Update the list of resource pools to search when looking up a resource. dirty = true;
// This allows to optimize resource lookup: no need to parse, split and hash
// the same locale string every time to find the corresponding resource
// pools.
poolSearchList = listOfResourcePoolsToSearch(selectedLocale);
return previousLocale; return previousLocale;
} }
@@ -157,6 +165,8 @@ EmbeddedResourceManager::Impl::addResource(
"Virtual path already in use for " + localeDescr + "Virtual path already in use for " + localeDescr +
" in the EmbeddedResourceManager: '" + virtualPath + "'"); " in the EmbeddedResourceManager: '" + virtualPath + "'");
} }
dirty = true;
} }
// *************************************************************************** // ***************************************************************************
@@ -203,10 +213,10 @@ EmbeddedResourceManager::addResource(
shared_ptr<const AbstractEmbeddedResource> shared_ptr<const AbstractEmbeddedResource>
EmbeddedResourceManager::getResourceOrNullPtr(const string& virtualPath) const EmbeddedResourceManager::getResourceOrNullPtr(const string& virtualPath) const
{ {
// Failure would indicate that either no resource has been added, or if (p->dirty) {
// selectLocale() hasn't been called. Remember that selectLocale() must be p->rehash(); // update p->poolSearchList
// called after all resources have been added. }
assert(!p->poolSearchList.empty());
// Use the selected locale // Use the selected locale
return p->lookupResourceInPools(virtualPath, p->poolSearchList); return p->lookupResourceInPools(virtualPath, p->poolSearchList);
} }
@@ -216,8 +226,8 @@ EmbeddedResourceManager::getResourceOrNullPtr(const string& virtualPath,
const string& locale) const const string& locale) const
{ {
// In this overload, we don't use the cached list of pools // In this overload, we don't use the cached list of pools
// (p->poolSearchList), therefore this can be used to find a resource for // (p->poolSearchList), therefore there is no need to check the 'dirty' flag
// any locale without any need to call selectLocale(). // or to rehash().
return p->lookupResourceInPools(virtualPath, return p->lookupResourceInPools(virtualPath,
p->listOfResourcePoolsToSearch(locale)); p->listOfResourcePoolsToSearch(locale));
} }
@@ -61,14 +61,12 @@ public:
// Select the locale for which resources will be returned in the future, for // Select the locale for which resources will be returned in the future, for
// the getResourceOrNullPtr(), getResource(), getString(), getStreambuf() // the getResourceOrNullPtr(), getResource(), getString(), getStreambuf()
// and getIStream() overloads that don't have a 'locale' parameter. // and getIStream() overloads that don't have a 'locale' parameter.
// Return the previously-selected locale. // May be called several times. Return the previously-selected locale.
// //
// This method *must* be called after all resources have been added. It can // If you just want to fetch one or two resources in a particular “locale”
// be called several times (typically, when the user changes the language // (language), it is simpler to use an overload of one of the
// for the whole application). If you just want to fetch one or two // getResourceOrNullPtr(), getResource(), ..., getIStream() methods that has
// resources in a particular “locale” (language), it is simpler to use an // a 'locale' parameter.
// overload of one of the getResourceOrNullPtr(), getResource(), ...,
// getIStream() methods that has a 'locale' parameter.
std::string selectLocale(const std::string& locale); std::string selectLocale(const std::string& locale);
// Add a resource for the specified locale to the embedded resource manager. // Add a resource for the specified locale to the embedded resource manager.
@@ -68,6 +68,10 @@ public:
const std::string& virtualPath, const std::string& virtualPath,
const std::vector< std::shared_ptr<ResourcePool> >& poolSearchList); const std::vector< std::shared_ptr<ResourcePool> >& poolSearchList);
// Recompute p->poolSearchList. This method is automatically called whenever
// needed (lazily), so it doesn't need to be part of the public interface.
void rehash();
// Implement the corresponding EmbeddedResourceManager public methods // Implement the corresponding EmbeddedResourceManager public methods
std::string getLocale() const; std::string getLocale() const;
std::string selectLocale(const std::string& locale); std::string selectLocale(const std::string& locale);
@@ -78,11 +82,15 @@ public:
const std::string& locale); const std::string& locale);
std::string selectedLocale; std::string selectedLocale;
// After each call to selectLocale(), we update this member to contain // Each call to rehash() updates this member to contain precisely the
// precisely the (ordered) list of pools to search for a resource in the // (ordered) list of pools to search for a resource in the selected
// selected “locale”. This allows relatively cheap resource lookups, // “locale”. This allows relatively cheap resource lookups, assuming the
// assuming the desired “locale” doesn't change all the time. // desired “locale” doesn't change all the time.
std::vector< std::shared_ptr<ResourcePool> > poolSearchList; std::vector< std::shared_ptr<ResourcePool> > poolSearchList;
// Indicate whether 'poolSearchList' must be updated (i.e., resources have
// been added or the selected locale was changed without rehash() being
// called afterwards).
bool dirty;
// Maps each “locale name” to the corresponding resource pool. // Maps each “locale name” to the corresponding resource pool.
std::unordered_map< std::string, std::unordered_map< std::string,
@@ -158,8 +158,8 @@ void initResources()
new RawEmbeddedResource(res2frArray, sizeof(res2frArray) - 1)); new RawEmbeddedResource(res2frArray, sizeof(res2frArray) - 1));
resMgr->addResource("/path/to/resource2", std::move(res2fr), "fr"); resMgr->addResource("/path/to/resource2", std::move(res2fr), "fr");
// This method must be called after all resources have been added. Here, we // Explicitly select the default locale (typically, English). This is for
// select the default locale (typically, English). // clarity, but isn't required.
resMgr->selectLocale(""); resMgr->selectLocale("");
} }