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

View File

@@ -46,8 +46,20 @@ static unique_ptr<EmbeddedResourceManager> staticInstance;
// * EmbeddedResourceManager::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
EmbeddedResourceManager::Impl::getLocale() const
{
@@ -59,11 +71,7 @@ EmbeddedResourceManager::Impl::selectLocale(const std::string& locale)
{
string previousLocale = std::move(selectedLocale);
selectedLocale = locale;
// 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 = true;
return previousLocale;
}
@@ -157,6 +165,8 @@ EmbeddedResourceManager::Impl::addResource(
"Virtual path already in use for " + localeDescr +
" in the EmbeddedResourceManager: '" + virtualPath + "'");
}
dirty = true;
}
// ***************************************************************************
@@ -203,10 +213,10 @@ EmbeddedResourceManager::addResource(
shared_ptr<const AbstractEmbeddedResource>
EmbeddedResourceManager::getResourceOrNullPtr(const string& virtualPath) const
{
// Failure would indicate that either no resource has been added, or
// selectLocale() hasn't been called. Remember that selectLocale() must be
// called after all resources have been added.
assert(!p->poolSearchList.empty());
if (p->dirty) {
p->rehash(); // update p->poolSearchList
}
// Use the selected locale
return p->lookupResourceInPools(virtualPath, p->poolSearchList);
}
@@ -216,8 +226,8 @@ EmbeddedResourceManager::getResourceOrNullPtr(const string& virtualPath,
const string& locale) const
{
// In this overload, we don't use the cached list of pools
// (p->poolSearchList), therefore this can be used to find a resource for
// any locale without any need to call selectLocale().
// (p->poolSearchList), therefore there is no need to check the 'dirty' flag
// or to rehash().
return p->lookupResourceInPools(virtualPath,
p->listOfResourcePoolsToSearch(locale));
}

View File

@@ -61,14 +61,12 @@ public:
// Select the locale for which resources will be returned in the future, for
// the getResourceOrNullPtr(), getResource(), getString(), getStreambuf()
// 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
// be called several times (typically, when the user changes the language
// for the whole application). If you just want to fetch one or two
// resources in a particular “locale” (language), it is simpler to use an
// overload of one of the getResourceOrNullPtr(), getResource(), ...,
// getIStream() methods that has a 'locale' parameter.
// If you just want to fetch one or two resources in a particular “locale”
// (language), it is simpler to use an overload of one of the
// getResourceOrNullPtr(), getResource(), ..., getIStream() methods that has
// a 'locale' parameter.
std::string selectLocale(const std::string& locale);
// Add a resource for the specified locale to the embedded resource manager.

View File

@@ -68,6 +68,10 @@ public:
const std::string& virtualPath,
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
std::string getLocale() const;
std::string selectLocale(const std::string& locale);
@@ -78,11 +82,15 @@ public:
const std::string& locale);
std::string selectedLocale;
// After each call to selectLocale(), we update this member to contain
// precisely the (ordered) list of pools to search for a resource in the
// selected “locale”. This allows relatively cheap resource lookups,
// assuming the desired “locale” doesn't change all the time.
// Each call to rehash() updates this member to contain precisely the
// (ordered) list of pools to search for a resource in the selected
// “locale”. This allows relatively cheap resource lookups, assuming the
// desired “locale” doesn't change all the time.
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.
std::unordered_map< std::string,

View File

@@ -158,8 +158,8 @@ void initResources()
new RawEmbeddedResource(res2frArray, sizeof(res2frArray) - 1));
resMgr->addResource("/path/to/resource2", std::move(res2fr), "fr");
// This method must be called after all resources have been added. Here, we
// select the default locale (typically, English).
// Explicitly select the default locale (typically, English). This is for
// clarity, but isn't required.
resMgr->selectLocale("");
}