File: move.cpp

package info (click to toggle)
liblsl 1.16.2b1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,724 kB
  • sloc: cpp: 12,515; ansic: 666; python: 28; sh: 25; makefile: 18
file content (74 lines) | stat: -rw-r--r-- 2,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <catch2/catch.hpp>
#include <lsl_cpp.h>
#include <thread>

// clazy:excludeall=non-pod-global-static

namespace {

TEST_CASE("move C++ API types", "[move][basic]") {
	lsl::stream_info info("movetest", "test", 1, lsl::IRREGULAR_RATE, lsl::cf_int32);
	lsl::stream_outlet outlet(info);
	lsl::continuous_resolver resolver("name", "movetest");
	auto found_stream_info = lsl::resolve_stream("name", "movetest", 1, 2.0);
	REQUIRE(found_stream_info.size() == 1);
	lsl::stream_inlet inlet(found_stream_info[0]);

	inlet.open_stream(2);
	outlet.wait_for_consumers(2);

	int32_t data = 1;

	{
		// move the outlet via the move constructor
		lsl::stream_outlet outlet2(std::move(outlet));
		CHECK(outlet2.have_consumers());
		outlet2.push_sample(&data);
		// Move outlet2 back into outlet via the copy constructor
		outlet = std::move(outlet2);
		data++;
		outlet.push_sample(&data);
		// End of scope, destructor for outlet2 is called
		// Since the stream_outlet is alive in outlet, it's not deconstructed
	}

	{
		// move the outlet via the move constructor
		lsl::stream_inlet inlet2(std::move(inlet));
		REQUIRE(inlet2.get_channel_count() == 1);
		inlet2.pull_sample(&data, 1);
		CHECK(data == 1);
		// Move inlet2 back into inlet via the copy constructor
		inlet = std::move(inlet2);
		inlet.pull_sample(&data, 1);
		CHECK(data == 2);
		// End of scope, destructor for outlet2 is called
		// Since the stream_outlet is alive in outlet, it's not deconstructed
	}

	{
		lsl::continuous_resolver resolver2(std::move(resolver));
		resolver2.results();
		resolver = std::move(resolver2);
		resolver.results();
	}

	{
		lsl::stream_outlet outlet3(std::move(outlet));
		lsl::stream_inlet inlet3(std::move(inlet));
		// End of scope, destructors for inlet3 and outlet3 are called
	}

	// Since the outlet has been destructed in the previous block, it shouldn't
	// be there any more
	found_stream_info = lsl::resolve_stream("name", "movetest", 1, 2.0);
	REQUIRE(found_stream_info.empty());

	// stream_info copies are cheap, for independent copies clone() has to be used
	lsl::stream_info copy1(info), copy2(info), clone(info.clone());
	copy1.desc().append_child("Dummy");
	REQUIRE(copy2.desc().first_child().name() == std::string("Dummy"));
	REQUIRE(clone.desc().first_child().empty());
}

} // namespace