Examples › Database Examples
database_simple
C++17 or laterDataReel 6.4.3Portable C++ (tested on Linux)
Legacy: examples_database/simple/testprog.cpp · Modern: examples_modern/database/simple/modern.cpp
Modern equivalent of the simple gxDatabase demo. The C++ standard library has no database file, so the gxDatabase class stays: its file format is what DataReel 6.x and 7.x programs read. What changes is the code around it:
- The gxDatabase object lives on the stack (its destructor closes the file), not behind new/delete.
- Errors become exceptions from one small function, so the "if (CheckError(f) != 0) { delete f; return 1; }" after every call goes.
- The record written to disk is a struct whose fixed layout is checked at compile time with static_assert and std::is_trivially_copyable (C++17 _v traits): raw writes of a struct with a std::string or a pointer in it are compile errors, not corrupt files.
- std::array<char, N> holds the fixed-length name, std::string_view fills it without strcpy() overruns, and std::filesystem removes the file at the end.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| gxString / UString | std::string, std::string_view | C++98 / C++17 | string_view looks at text without copying it. |
| new / delete | automatic objects, containers, std::unique_ptr / std::make_unique | C++11 / C++14 | Ownership is in the type, and release() shows a hand-over. |
| strcpy(), strncpy(), memmove() into char arrays | std::string; std::array<char, N> + string_view::copy() | C++11 / C++17 | |
| gxDatabase | keep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>) | C++17 | The standard library has no database file; the file format is DataReel's. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_database/simple/testprog.cpp 166 lines
#include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include <iostream> #include <iomanip> using namespace std; // Use unqualified names for Standard C++ library #else // Use the old iostream library by default #include <iostream.h> #include <iomanip.h> #endif // __USE_ANSI_CPP__ #include <string.h> #include "gxdbase.h" #include "gxfloat.h" #include "gxdstats.h" #include "ustring.h" const int name_length = 16; class DatabaseObject { public: DatabaseObject() { name[0] = 0; oid = (gxINT32)0, cid = (gxFLOAT64)0; } DatabaseObject(const char *s, long i, double d); public: void DisplayObject(); public: // Platform independent data members char name[name_length]; // Fixed string type gxINT32 oid; // Integer type gxFLOAT64 cid; // Floating point type }; DatabaseObject::DatabaseObject(const char *s, long i, double d) { for(int j = 0; j < name_length; j++) name[j] = 0; // Clear the name string strcpy(name, s); oid = i; cid = d; } void DatabaseObject::DisplayObject() { cout << "Database object name: " << name << "\n"; cout << "Database object OID: " << oid << "\n"; cout.setf(ios::showpoint | ios::fixed); cout.precision(3); cout << "Database object CID: " << cid << "\n"; } void PausePrg() { cout << "\n"; cout << "Press enter to continue..." << "\n"; cin.get(); } int main(int argv, char **argc) { const char *fname = "simple.gxd"; // File name of this database char rev_letter = gxDatabaseRevisionLetter; // Set the default rev letter if(argv == 2) { // Set a specified revision letter rev_letter = *argc[1]; if(rev_letter == '0') rev_letter = '\0'; // Valid rev letters are: // Rev 0 // Rev 'A' or 'a' // Rev 'B' or 'b' // Rev 'C' or 'c' // Rev 'D' or 'd' // Rev 'E' or 'e' // NOTE: The gxDatabase class will set invalid revision letters // to the version set by the gxDatabaseRevisionLetter constant. } // Using pointer semantics to prevent copying gxDatabase *f = new gxDatabase; if(!f) { cout << "Could not allocate memory for file pointer" << "\n"; return 1; } cout << "\n"; cout << "Simple demo of the file manager class." << "\n"; cout << "Creating new file..." << "\n"; f->Create(fname, (FAU_t)0, rev_letter); if(CheckError(f) != 0) { delete f; return 1; } // Echo the file version and revision letter EchoDatabaseVersion(f); PausePrg(); DatabaseStats(f); PausePrg(); cout << "Adding object to database file..." << "\n"; cout << "Size of object = " << sizeof(DatabaseObject) << "\n"; // Allocate a block to store the object FAU_t addr = f->Alloc(sizeof(DatabaseObject)); if(CheckError(f) != 0) { delete f; return 1; } // Construct the datbase object DatabaseObject ob("Mouse", (long)addr, 5000.101); // Write the objects data f->Write(&ob, sizeof(DatabaseObject)); UString intbuf; if(CheckError(f) != 0) { delete f; return 1; } intbuf << clear << (FAU_t)(addr - f->BlockHeaderSize()); cout << "Block Address: " << intbuf.c_str() << "\n"; intbuf << clear << (FAU_t)addr; cout << "Writing object to FAU: " << intbuf.c_str() << "\n"; PausePrg(); BlockStats(f, addr); PausePrg(); DatabaseObject buf; cout << "Reading the first object from the database file..." << "\n"; addr = f->FindFirstBlock(); // Read the objects data if(addr) { f->Read(&buf, sizeof(buf), (FAU_t)(addr+f->BlockHeaderSize())); if(CheckError(f) != 0) { delete f; return 1; } buf.DisplayObject(); } PausePrg(); DatabaseStats(f); PausePrg(); cout << "Exiting..." << "\n"; // Close the file and check for any errors f->Close(); if(CheckError(f) != 0) { delete f; return 1; } // Delete the object delete f; // The destructor call will close the file if it is open return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++17)
examples_modern/database/simple/modern.cpp 79 lines
#include <array> #include <cstdint> #include <filesystem> #include <iomanip> #include <iostream> #include <stdexcept> #include <string> #include <string_view> #include <type_traits> #include "gxdbase.h" struct DatabaseObject { // Exactly what is on disk std::array<char, 16> name{}; // Fixed length, zero filled std::int32_t oid = 0; std::int32_t pad = 0; // Explicit padding: no garbage bytes double cid = 0; DatabaseObject() = default; DatabaseObject(std::string_view s, std::int32_t o, double c) : oid(o), cid(c) { s.copy(name.data(), name.size() - 1); // Always leaves a terminator } }; static_assert(std::is_trivially_copyable_v<DatabaseObject>, "objects written as raw bytes must be trivially copyable"); static_assert(sizeof(DatabaseObject) == 32, "on-disk size must not change"); void check(gxDatabase &f) // CheckError() as an exception { if(f.GetDatabaseError() != gxDBASE_NO_ERROR) throw std::runtime_error(f.DatabaseExceptionMessage()); } int main(int argc, char **argv) { const std::filesystem::path fname = "modern_simple.gxd"; char rev_letter = argc == 2 ? argv[1][0] : gxDatabaseRevisionLetter; if(rev_letter == '0') rev_letter = '\0'; try { gxDatabase f; // No new/delete std::cout << "Creating " << fname << "\n"; f.Create(fname.string().c_str(), FAU_t(0), rev_letter); check(f); DatabaseObject ob("Mouse", 0, 5000.101); FAU_t addr = f.Alloc(sizeof ob); check(f); ob.oid = std::int32_t(addr); f.Write(&ob, sizeof ob); check(f); std::cout << "Wrote object at file address " << addr << " (block at " << addr - FAU_t(f.BlockHeaderSize()) << ")\n"; DatabaseObject buf; FAU_t first = f.FindFirstBlock(); if(first) { f.Read(&buf, sizeof buf, first + FAU_t(f.BlockHeaderSize())); check(f); std::cout << "Database object name: " << buf.name.data() << "\n" << "Database object OID: " << buf.oid << "\n" << "Database object CID: " << std::fixed << std::setprecision(3) << buf.cid << "\n"; } std::cout << "Total blocks: " << f.TotalBlocks() << "\n"; f.Close(); check(f); } catch(const std::exception &e) { std::cout << "Database error: " << e.what() << "\n"; return 1; } std::filesystem::remove(fname); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17 cmake --build build-modern --target database_simple ./build-modern/database_simple
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Creating "modern_simple.gxd" Wrote object at file address 128 (block at 96) Database object name: Mouse Database object OID: 128 Database object CID: 5000.101 Total blocks: 1