MCPcopy Create free account
hub / github.com/apache/fory / main

Function main

examples/cpp/hello_row/main.cc:59–257  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

57};
58
59int main() {
60 std::cout << "=== Fory C++ Row Format Example ===" << std::endl << std::endl;
61
62 // ============================================================================
63 // Example 1: Manual Row Writing with Schema
64 // ============================================================================
65 std::cout << "--- Example 1: Manual Row Writing ---" << std::endl;
66 {
67 using namespace fory::row;
68
69 // Define schema with fields
70 auto name_field = field("name", utf8());
71 auto age_field = field("age", int32());
72 auto score_field = field("score", float32());
73 auto tags_field = field("tags", list(utf8()));
74
75 std::vector<FieldPtr> fields = {name_field, age_field, score_field,
76 tags_field};
77 auto row_schema = schema(fields);
78
79 // Create a row writer
80 RowWriter writer(row_schema);
81 writer.reset();
82
83 // write primitive fields
84 writer.write_string(0, "Alice");
85 writer.write(1, static_cast<int32_t>(25));
86 writer.write(2, 95.5f);
87
88 // write array field
89 writer.set_not_null_at(3);
90 int array_start = writer.cursor();
91 auto list_type = std::dynamic_pointer_cast<ListType>(utf8());
92 ArrayWriter array_writer(list(utf8()), &writer);
93 array_writer.reset(3);
94 array_writer.write_string(0, "developer");
95 array_writer.write_string(1, "team-lead");
96 array_writer.write_string(2, "mentor");
97 writer.set_offset_and_size(3, array_start, writer.cursor() - array_start);
98
99 // Convert to row and read back
100 auto row = writer.to_row();
101
102 std::cout << "Written row:" << std::endl;
103 std::cout << " name: " << row->get_string(0) << std::endl;
104 std::cout << " age: " << row->get_int32(1) << std::endl;
105 std::cout << " score: " << row->get_float(2) << std::endl;
106
107 auto tags_array = row->get_array(3);
108 std::cout << " tags: [";
109 for (int i = 0; i < tags_array->num_elements(); ++i) {
110 std::cout << tags_array->get_string(i);
111 if (i < tags_array->num_elements() - 1)
112 std::cout << ", ";
113 }
114 std::cout << "]" << std::endl;
115
116 // Print full row string representation

Callers

nothing calls this directly

Calls 15

utf8Function · 0.85
schemaFunction · 0.85
cursorMethod · 0.80
get_stringMethod · 0.80
field_namesMethod · 0.80
get_writerMethod · 0.80
copy_to_array_dataMethod · 0.80
resetMethod · 0.65
writeMethod · 0.65
encodeMethod · 0.65
fieldFunction · 0.50
int32Function · 0.50

Tested by

no test coverage detected