Skip to content

Commit

Permalink
Merge pull request #49 from forderud/ref-const-sample
Browse files Browse the repository at this point in the history
WinrtServer: Extend sample with "ref" & "ref const" samples
  • Loading branch information
Jøger Hansegård authored Jan 10, 2022
2 parents f1a961d + e26c7f5 commit 7716b1b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion TutorialsAndTests/Tests/WinrtServerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,19 @@ TEST(WinrtServerTests, RequireThat_Buffer_ReturnsCorrectValues)

EXPECT_EQ(buffer.size(), 8u);
EXPECT_EQ(buffer[0], 1);
}
}

TEST(WinrtServerTests, RequireThat_Buffer_CanBeSetAndFilled)
{
init_apartment(winrt::apartment_type::single_threaded);

winrt::WinrtServer::Programmer programmer; // will trigger WinrtServer.dll loading

std::vector<uint8_t> content = { 8, 7, 6, 5, 4, 3, 2, 1 };
programmer.SetBuffer(content);

std::vector<uint8_t> copy(8, 0);
programmer.FillBuffer(copy);

EXPECT_EQ(content, copy);
}
12 changes: 12 additions & 0 deletions WinrtServer/Programmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ namespace winrt::WinrtServer::implementation
// return a copy
return com_array<uint8_t>{ m_buffer.begin(), m_buffer.end() };
}

void Programmer::SetBuffer(const array_view<uint8_t> buffer) {
m_buffer.resize(buffer.size());

for (unsigned int i = 0; i < buffer.size(); ++i)
m_buffer[i] = buffer[i];
}

void Programmer::FillBuffer(array_view<uint8_t> buffer) {
for (unsigned int i = 0; (i < buffer.size()) && (i < m_buffer.size()); ++i)
buffer[i] = m_buffer[i];
}
}
4 changes: 4 additions & 0 deletions WinrtServer/Programmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace winrt::WinrtServer::implementation
Favorites GetFavorites();
com_array<uint8_t> Buffer();

void SetBuffer(const array_view<uint8_t> buffer);

void FillBuffer(array_view<uint8_t> buffer);

private:
int m_motivation = 0;
std::vector<uint8_t> m_buffer;
Expand Down
6 changes: 6 additions & 0 deletions WinrtServer/Programmer.idl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ namespace WinrtServer
Pos3 Add(Pos3 a, Pos3 b);

UInt8[] Buffer{ get; };

// set read-only buffer (no copying of argument)
void SetBuffer(ref const UInt8[] buffer);

// fill caller-provided buffer with values (no copying of argument)
void FillBuffer(ref UInt8[] buffer);
}

runtimeclass Programmer : [default] IProgrammer
Expand Down

0 comments on commit 7716b1b

Please sign in to comment.