Skip to content

Commit

Permalink
ImGuiIntegration: Add conversion between ImStrv and StringView
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Amiard committed Jun 24, 2022
1 parent 5a6aa83 commit 5588c06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Magnum/ImGuiIntegration/Integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,27 @@ Example usage:
#include "Magnum/ImGuiIntegration/visibility.h" /* defines IMGUI_API */

#include <imgui.h>
#include <Corrade/Containers/StringView.h>
#include <Magnum/Types.h>
#include <Magnum/Math/Vector.h>

/* Don't list (useless) Magnum and Math namespaces without anything else */
/* Don't list (useless) Corrade and Magnum and Math namespaces without anything else */
#ifndef DOXYGEN_GENERATING_OUTPUT

/* Currently present only in the features/string_view branch of ImGui */
#ifdef IMGUI_HAS_IMSTR
namespace Corrade { namespace Containers { namespace Implementation {

template<> struct StringViewConverter<const char, ImStrv> {
static StringView from(const ImStrv& other) {
return StringView{other.Begin, other.length()};
}
static ImStrv to(StringView other) { return ImStrv{other.begin(), other.end()}; }
};

}}}
#endif

namespace Magnum { namespace Math { namespace Implementation {

/* ImVec2 */
Expand Down
16 changes: 15 additions & 1 deletion src/Magnum/ImGuiIntegration/Test/IntegrationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,33 @@ namespace Magnum { namespace ImGuiIntegration { namespace Test { namespace {
struct IntegrationTest: TestSuite::Tester {
explicit IntegrationTest();

void stringView();
void vector2();
void vector4();
void color();
void colorLiterals();
};

IntegrationTest::IntegrationTest() {
addTests({&IntegrationTest::vector2,
addTests({&IntegrationTest::stringView,
&IntegrationTest::vector2,
&IntegrationTest::vector4,
&IntegrationTest::color,
&IntegrationTest::colorLiterals});
}

void IntegrationTest::stringView() {
#ifdef IMGUI_HAS_IMSTR
ImStrv imStrv{"Hello World!"};
StringView stringView("Hello World!");

CORRADE_COMPARE(StringView(imVec2), vec2);

ImStrv c(stringView);
CORRADE_COMPARE(c, stringView);
#endif
}

void IntegrationTest::vector2() {
ImVec2 imVec2{1.1f, 1.2f};
Vector2 vec2(1.1f, 1.2f);
Expand Down

0 comments on commit 5588c06

Please sign in to comment.