Add helper function to print BT_Value variants and update main function for better array handling
This commit is contained in:
@@ -416,6 +416,27 @@ inline std::vector<BT_FreeListEntry> decodeFreeList(const std::vector<uint8_t>&
|
|||||||
return freeList;
|
return freeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper to print BT_Value variant
|
||||||
|
inline std::string printBTValue(const BT_Value& v) {
|
||||||
|
if (std::holds_alternative<int>(v)) return std::to_string(std::get<int>(v));
|
||||||
|
if (std::holds_alternative<double>(v)) return std::to_string(std::get<double>(v));
|
||||||
|
if (std::holds_alternative<std::string>(v)) return '"' + std::get<std::string>(v) + '"';
|
||||||
|
if (std::holds_alternative<std::vector<int>>(v)) {
|
||||||
|
const auto& arr = std::get<std::vector<int>>(v);
|
||||||
|
std::ostringstream oss; oss << "[";
|
||||||
|
for (size_t i = 0; i < arr.size(); ++i) { if (i) oss << ", "; oss << arr[i]; }
|
||||||
|
oss << "]"; return oss.str();
|
||||||
|
}
|
||||||
|
if (std::holds_alternative<std::vector<double>>(v)) {
|
||||||
|
const auto& arr = std::get<std::vector<double>>(v);
|
||||||
|
std::ostringstream oss; oss << "[";
|
||||||
|
for (size_t i = 0; i < arr.size(); ++i) { if (i) oss << ", "; oss << arr[i]; }
|
||||||
|
oss << "]"; return oss.str();
|
||||||
|
}
|
||||||
|
if (std::holds_alternative<BT_UniformArray>(v)) return std::get<BT_UniformArray>(v).to_string(true);
|
||||||
|
return "<unknown>";
|
||||||
|
}
|
||||||
|
|
||||||
// --- Main function for testing ---
|
// --- Main function for testing ---
|
||||||
#ifdef BINARY_TABLE_MAIN
|
#ifdef BINARY_TABLE_MAIN
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -423,12 +444,9 @@ inline std::vector<BT_FreeListEntry> decodeFreeList(const std::vector<uint8_t>&
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
const std::string filename = "example.bin";
|
const std::string filename = "example.bin";
|
||||||
// Remove file if it exists
|
|
||||||
std::remove(filename.c_str());
|
std::remove(filename.c_str());
|
||||||
// Create file
|
|
||||||
std::ofstream(filename).close();
|
std::ofstream(filename).close();
|
||||||
BinaryTable table(filename);
|
BinaryTable table(filename);
|
||||||
// No explicit initialise needed, but could be added if desired
|
|
||||||
|
|
||||||
std::cout << "File dump:" << std::endl;
|
std::cout << "File dump:" << std::endl;
|
||||||
{
|
{
|
||||||
@@ -438,32 +456,40 @@ int main() {
|
|||||||
std::cout << "File size: " << data.size() << " bytes\n" << std::endl;
|
std::cout << "File size: " << data.size() << " bytes\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert arrays
|
|
||||||
table.set("int_array", std::vector<int>{6, 3, 9, 2, 5});
|
table.set("int_array", std::vector<int>{6, 3, 9, 2, 5});
|
||||||
table.set("float_array", std::vector<double>{1.5, 2.5, 3.5});
|
table.set("float_array", std::vector<double>{1.5, 2.5, 3.5});
|
||||||
table.set("empty", std::vector<int>{});
|
table.set("empty", std::vector<int>{});
|
||||||
|
|
||||||
// Modify arrays
|
// Modify arrays
|
||||||
BT_UniformArray intArr = std::get<BT_UniformArray>(table.get("int_array"));
|
auto v1 = table.get("int_array");
|
||||||
BT_UniformArray floatArr = std::get<BT_UniformArray>(table.get("float_array"));
|
auto v2 = table.get("float_array");
|
||||||
intArr.set(0, 1);
|
if (std::holds_alternative<BT_UniformArray>(v1)) {
|
||||||
floatArr.set(1, 4.5);
|
BT_UniformArray intArr = std::get<BT_UniformArray>(v1);
|
||||||
|
intArr.set(0, 1);
|
||||||
std::cout << "int_array pointer: " << intArr._pointer.to_string() << std::endl;
|
intArr.add(10);
|
||||||
std::cout << "float_array pointer: " << floatArr._pointer.to_string() << std::endl;
|
intArr.addAll({420, 69, 1337, 1738});
|
||||||
|
std::cout << "int_array pointer: " << intArr._pointer.to_string() << std::endl;
|
||||||
intArr.add(10);
|
std::cout << "Readback1: " << intArr.to_string(true) << std::endl;
|
||||||
floatArr.add(5.5);
|
} else {
|
||||||
intArr.addAll({420, 69, 1337, 1738});
|
std::cout << "int_array is not a BT_UniformArray!\n";
|
||||||
floatArr.addAll({6.5, 7.5, 8.5});
|
}
|
||||||
|
if (std::holds_alternative<BT_UniformArray>(v2)) {
|
||||||
auto readback1 = intArr.to_string(true);
|
BT_UniformArray floatArr = std::get<BT_UniformArray>(v2);
|
||||||
auto readback2 = floatArr.to_string(true);
|
floatArr.set(1, 4.5);
|
||||||
BT_UniformArray emptyArr = std::get<BT_UniformArray>(table.get("empty"));
|
floatArr.add(5.5);
|
||||||
auto readback3 = emptyArr.to_string(true);
|
floatArr.addAll({6.5, 7.5, 8.5});
|
||||||
std::cout << "Readback1: " << readback1 << std::endl;
|
std::cout << "float_array pointer: " << floatArr._pointer.to_string() << std::endl;
|
||||||
std::cout << "Readback2: " << readback2 << std::endl;
|
std::cout << "Readback2: " << floatArr.to_string(true) << std::endl;
|
||||||
std::cout << "Readback3: " << readback3 << std::endl;
|
} else {
|
||||||
|
std::cout << "float_array is not a BT_UniformArray!\n";
|
||||||
|
}
|
||||||
|
auto v3 = table.get("empty");
|
||||||
|
if (std::holds_alternative<BT_UniformArray>(v3)) {
|
||||||
|
BT_UniformArray emptyArr = std::get<BT_UniformArray>(v3);
|
||||||
|
std::cout << "Readback3: " << emptyArr.to_string(true) << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "empty is not a BT_UniformArray!\n";
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << "\nFile dump:" << std::endl;
|
std::cout << "\nFile dump:" << std::endl;
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user