From b1628c03657b6da1231be0b5b61236e9ec7d4be1 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 19 Oct 2017 04:40:41 -0700 Subject: [PATCH] Create and get SourceGroups by StringView --- include/AL/alure2.h | 12 ++++++++++-- src/context.cpp | 24 +++++++++++++----------- src/context.h | 4 ++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/include/AL/alure2.h b/include/AL/alure2.h index c42536e..5800f44 100644 --- a/include/AL/alure2.h +++ b/include/AL/alure2.h @@ -251,6 +251,8 @@ public: } bool operator==(BasicStringView rhs) const noexcept { return compare(rhs) == 0; } + bool operator!=(BasicStringView rhs) const noexcept + { return compare(rhs) != 0; } }; using StringView = BasicStringView; @@ -273,8 +275,14 @@ template inline bool operator==(const BasicString &lhs, BasicStringView rhs) { return BasicStringView(lhs) == rhs; } template +inline bool operator!=(const BasicString &lhs, BasicStringView rhs) +{ return BasicStringView(lhs) != rhs; } +template inline bool operator==(const typename BasicString::value_type *lhs, BasicStringView rhs) { return BasicStringView(lhs) == rhs; } +template +inline bool operator!=(const typename BasicString::value_type *lhs, BasicStringView rhs) +{ return BasicStringView(lhs) != rhs; } // Inline operator to write out a StringView to an ostream template @@ -795,8 +803,8 @@ public: Effect createEffect(); - SourceGroup createSourceGroup(String name); - SourceGroup getSourceGroup(const String &name); + SourceGroup createSourceGroup(StringView name); + SourceGroup getSourceGroup(StringView name); /** Sets the doppler factor to apply to all source calculations. */ void setDopplerFactor(ALfloat factor); diff --git a/src/context.cpp b/src/context.cpp index 9ef04e6..6be7c55 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -1247,23 +1247,25 @@ Effect ContextImpl::createEffect() } -SourceGroup ContextImpl::createSourceGroup(String name) +SourceGroup ContextImpl::createSourceGroup(StringView name) { - auto iter = std::lower_bound(mSourceGroups.begin(), mSourceGroups.end(), name, - [](const UniquePtr &lhs, const String &rhs) -> bool - { return lhs->getName() < rhs; } + auto hasher = std::hash(); + auto iter = std::lower_bound(mSourceGroups.begin(), mSourceGroups.end(), hasher(name), + [hasher](const UniquePtr &lhs, size_t rhs) -> bool + { return hasher(lhs->getName()) < rhs; } ); if(iter != mSourceGroups.end() && (*iter)->getName() == name) throw std::runtime_error("Duplicate source group name"); - iter = mSourceGroups.insert(iter, MakeUnique(this, std::move(name))); + iter = mSourceGroups.insert(iter, MakeUnique(this, String(name))); return SourceGroup(iter->get()); } -SourceGroup ContextImpl::getSourceGroup(const String &name) +SourceGroup ContextImpl::getSourceGroup(StringView name) { - auto iter = std::lower_bound(mSourceGroups.begin(), mSourceGroups.end(), name, - [](const UniquePtr &lhs, const String &rhs) -> bool - { return lhs->getName() < rhs; } + auto hasher = std::hash(); + auto iter = std::lower_bound(mSourceGroups.begin(), mSourceGroups.end(), hasher(name), + [hasher](const UniquePtr &lhs, size_t rhs) -> bool + { return hasher(lhs->getName()) < rhs; } ); if(iter == mSourceGroups.end() || (*iter)->getName() != name) throw std::runtime_error("Source group not found"); @@ -1365,8 +1367,8 @@ DECL_THUNK1(void, Context, removeBuffer,, Buffer) DECL_THUNK0(Source, Context, createSource,) DECL_THUNK0(AuxiliaryEffectSlot, Context, createAuxiliaryEffectSlot,) DECL_THUNK0(Effect, Context, createEffect,) -DECL_THUNK1(SourceGroup, Context, createSourceGroup,, String) -DECL_THUNK1(SourceGroup, Context, getSourceGroup,, const String&) +DECL_THUNK1(SourceGroup, Context, createSourceGroup,, StringView) +DECL_THUNK1(SourceGroup, Context, getSourceGroup,, StringView) DECL_THUNK1(void, Context, setDopplerFactor,, ALfloat) DECL_THUNK1(void, Context, setSpeedOfSound,, ALfloat) DECL_THUNK1(void, Context, setDistanceModel,, DistanceModel) diff --git a/src/context.h b/src/context.h index 020eeed..6e15372 100644 --- a/src/context.h +++ b/src/context.h @@ -294,8 +294,8 @@ public: Effect createEffect(); - SourceGroup createSourceGroup(String name); - SourceGroup getSourceGroup(const String &name); + SourceGroup createSourceGroup(StringView name); + SourceGroup getSourceGroup(StringView name); void setDopplerFactor(ALfloat factor); -- 2.11.4.GIT