Project

General

Profile

Storing WStandardItemModel in the database

Added by Mark Travis almost 2 years ago

I'm trying to architect the best way to handle the need for my application to persist data across sessions.

I've currently got it set up to read in a file using a shared_ptr to WStandardItemModel. I pass the model to another object method that extracts the data and loads it into the structure I need for computations.

I'm also using the shared_ptr to the WStandardItemModel that loaded the file as a data source to Wt::WCartesianChart and Wt::WTableView.

My data model is easy to store and extract. It's just a Struct inside another object that is controlled by a Unique_ptr. When I want to store it, I just extract the data and store it in a sqlite3 blob.

However, I'm not sure how to do that with a WStandardItemModel as it's wrapped in a shared_ptr. Is there a way to store it in sqlite3 as a blob while it's still wrapped in a shared_ptr? The reason I'm asking is that I'd like to store metadata and shadow tables in the WStandardItemModel for reference by other objects. It would be nice to store it "as is" along side the struct I mentioned earlier in the same sqlite3 blob field. (I unwrap it with any_cast)

Is there a way to do this without confusing the smart pointers? Or can it even be done at all?

I'm going to go ahead and try, but if it's not successful, I'm hoping someone will offer some advice in the meantime.


Replies (3)

RE: Storing WStandardItemModel in the database - Added by Mark Travis almost 2 years ago

I just realized I asked this question last year.

Not possible.

However, how should I store a cpp17::any in sqlite3? I tried to convert the struct I've populated to a cpp17::any, then to a std::vector so that I could store it as a binary "blob" in sqlite3.

I tried the following, but I got a "bad any cast" exception.

std::vector<unsigned char> modelToDBO = cpp17::any_cast<std::vector<unsigned char>>(cpp17::any(structOfData));

RE: Storing WStandardItemModel in the database - Added by Mark Travis almost 2 years ago

Arg, hit submit too soon.

I succeeded in getting the blob into the database.

             // serialize TheModel structure so that it can be added to the sqlite3 database
             auto modelToDBO = reinterpret_cast<unsigned char *>(&theModel_);
             std::vector<unsigned char> buffer(modelToDBO, modelToDBO + sizeof theModel_);

I then pass "buffer" to the database transaction to create the record and it works!

Now the question is, unpacking it. I get a std::vector<unsigned char> back from the database. I THINK I need it in "unsigned char *" format so that I can cast it back to the Struct. Do I use blobType() for that?

RE: Storing WStandardItemModel in the database - Added by Mark Travis almost 2 years ago

I'm researching the use of the boost::serialization and archive libraries instead of reinterpret_cast. Any reason to think it would not work for WStandardItemModel?

    (1-3/3)