Skip to content

Commit 8202730

Browse files
authored
Generic record example is added. (#1156)
1 parent 830adf7 commit 8202730

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

examples/serialization/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ add_subdirectory(globalserializer)
2020
add_subdirectory(json)
2121
add_subdirectory(mixed-type-collection)
2222
add_subdirectory(compact)
23+
add_subdirectory(generic-record)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
add_executable(generic-record ./main.cpp)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <hazelcast/client/hazelcast_client.h>
17+
#include <hazelcast/client/serialization/serialization.h>
18+
#include <iomanip>
19+
20+
/**
21+
* This example demonstrates how to use generic_record.
22+
*/
23+
int
24+
main()
25+
{
26+
using namespace hazelcast::client::serialization::generic_record;
27+
auto client = hazelcast::new_client().get();
28+
29+
auto map = client.get_map("generic_record_sample_map").get();
30+
generic_record record = generic_record_builder{ "employee" }
31+
.set_int32("age", 21)
32+
.set_string("name", "John")
33+
.set_int64("id", 11)
34+
.build();
35+
generic_record another_record = generic_record_builder{ "employee" }
36+
.set_int32("age", 19)
37+
.set_string("name", "John")
38+
.set_int64("id", 10)
39+
.build();
40+
41+
auto cloned_record = record.new_builder_with_clone().build();
42+
auto updated_cloned_record =
43+
record.new_builder_with_clone().set_int32("age", 22).build();
44+
45+
// Put into map and wait
46+
map->put(1, record).get();
47+
map->put(2, another_record).get();
48+
map->put(3, cloned_record).get();
49+
map->put(4, updated_cloned_record).get();
50+
51+
std::string variable_names[] = {
52+
"", "record", "another_record", "cloned_record", "updated_cloned_record"
53+
};
54+
55+
for (int i = 1; i < 5; ++i) {
56+
boost::optional<generic_record> record =
57+
map->get<int, generic_record>(i).get();
58+
59+
assert(record.has_value());
60+
61+
std::cout << std::setw(21) << std::left << variable_names[i] << " => "
62+
<< "age: " << record->get_int32("age")
63+
<< ", name:" << record->get_string("name")
64+
<< ", id:" << record->get_int64("id") << std::endl;
65+
}
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)