C++ Struct,Reference and Instancing the right way? - c++

I'm creating a replay system Basically it stores user inputs as:
std::list<ReplayBuffer>
which is a:
struct ReplayBuffer
{
std::bitset<10> flags;
float frameTime;
uint64_t frameID;
};
now I've created two more structs, ReplayFile (the file information), and ReplayFileWriter(Write the file information into a file of course)
struct ReplayFile
{
ReplayFile(std::list<ReplayBuffer>* _buffers) {
time_t t = time(0);
date = ctime(&t);
_version = 1;
buffers = *_buffers;
}
uint16_t _version;
char* date;
std::list<ReplayBuffer> buffers;
};
struct ReplayFileWriter
{
std::ofstream file;
ReplayFileWriter(ReplayFile *data) {
file.open("replay.cdr", std::ofstream::out);
file << data;
file.close();
}
};
What I want to do is, type this:
ReplayFileWriter(new ReplayFile(&buffers));
so I pass to ReplayFile the adress of the buffers (list) and to ReplayFileWriter the adress a instance of ReplayFile) how I'm supposed to do this (I'm new to cpp, watched some videos into pointer, but when using as parameters is hard to find the right way to place "* and &"

What you are probably looking for is something like this:
struct ReplayBuffer { /* as in your code */ };
struct ReplayFile {
explicit ReplayFile(const std::list<ReplayBuffer>& _buffers)
: _version(1), buffers(_buffers) {
time_t t = time(nullptr);
date = ctime(&t);
}
uint16_t _version;
std::string date;
std::list<ReplayBuffer> buffers;
};
ostream& operator<<(ostream& os, const ReplayFile& data) {
/* Write contents of `data` to `os` here.
Implementation left as an exercise for the reader. */
return os;
}
void WriteReplayFile(const ReplayFile& data) {
file.open("replay.cdr", std::ofstream::out);
file << data;
file.close();
}
int main() {
std::list<ReplayBuffer> buffers = ...; // initialized somehow.
WriteReplayFile(ReplayFile(buffers));
}
The interesting part is in operator<<. That's where you get to define your file format.

Related

de-serialize ASCII to struct

I have come up with the following structure to declare various formats if messages that are to be received from the network:
#include <stdint.h>
#include <iostream>
#include <string.h>
template<int T>
struct uint
{
static uint<T> create(uint64_t value)
{
uint<T> r = {value};
return r;
}
uint(uint64_t value)
{
v = value;
}
uint()
{}
uint<T>& operator =(uint64_t value)
{
v = value;
return *this;
}
operator uint64_t() const
{
return (uint64_t)v;
}
unsigned long long v:T;
}__attribute__((packed));
example:
typedef uint<5> second_t;
suppose one of the message formats (which are auto-generated via some process) is like this:
struct seconds
{
char _type;
second_t _second;
} __attribute__((packed));
Now suppose I would like to populate an instance of the above messahe using a string:
int main()
{
seconds ii;
const char *i = "123456";
// memset, memcpy,sprintf... ??? what to use here?
std::cout << ii._type << " " << ii._second << std::endl;
}
Given a stream 123456, I expect the instance of the seconds (ii) structure to have char ii._type = '1' and integer ii._second = 23456. But I dont know how to do that. Do you have a clue how i can do that? and do you have any suggestion how to improve the basic structure?
thanks
You have a number of easier and more reliable options available that require almost no work.
check out google protocol buffers (platform independent message serialisation and deserialisation): https://developers.google.com/protocol-buffers/
or boost::serialization - (probably faster, but not platform-independant) http://www.boost.org/doc/libs/1_58_0/libs/serialization/doc/index.html

C++ Structure with unknown data types

My program reads user's input and create easy "table". User on the start specifies data types of columns and the number of rows.
User's input:
create table
add attribute string Name
add attribute int Age
rows 3
I need prepare a structure from user's input now. I have something like this:
CTable
{
unsigned attributesCnt;
string * attributesNames;
void ** attributes;
};
So, from the user's input, the progam does these steps:
CTable myTable;
myTable.attributesCnt = 2; // string "Name", int "Age"
myTable.attributesNames = new string[2];
myTable.attributesNames[0] = "Name";
myTable.attributesNames[1] = "Age";
attributes = new void[2]; // 2 attributes
attributes[0] = (void*) new string[3]; // there will be 3 rows
attributes[1] = (void*) new int[3];
I need remember that "attributes[0]" is string and "attributes[1]" is int too.
Is this "right" way?
I would like use only standard libraries.
What you are looking for is a tagged union also called a variant. It allows you to store multiple data types at the same location just like a regular union but includes an additional but separate data member that indicates it's type. The C++ Standard Library does not include variants but they are easy enough to implement.
Once you have a variant you can apply it to your example like below.
myTable.attributesNames[0] = "Name";
myTable.attributesNames[1] = "Age";
// I recommend using std::vector here instead of using new/delete yourself
attributes = new Variant*[2]; // 2 attributes
attributes[0] = new Variant("player name");
attributes[1] = new Variant(player_age);
The following example shows how the variant might be implemented.
struct Variant
{
enum Type
{
INT,
STRINGPTR
};
Type type_;
union
{
int int_;
const char* stringptr_;
} data_;
explicit Variant(int data) : type_(INT)
{
data_.int_ = data;
}
explicit Variant(const char *data) : type_(STRINGPTR)
{
data_.stringptr_ = data;
}
Type getType() const { return type_; }
int getIntValue() const
{
if(type_ != INT)
throw std::runtime_error("Variant is not an int");
return data_.int_;
}
const char *getStringPtr() const
{
if(type_ != STRINGPTR)
throw std::runtime_error("Variane is not a string");
return data_.stringptr_;
}
};
int main()
{
Variant intval(1);
Variant stringval("hello");
std::cout << intval.getIntValue() << std::endl;
std::cout << stringval.getStringPtr() << std::endl;
}

Insert an array of tables into one table SQLite C/C++

I made my own database format, and it sadly required too much memory and the size of it got horrendous and upkeep was horrible.
So I'm looking for a way to store an array of a struct that's in an object into a table.
I'm guessing I need to use a blob, but all other options are welcome. An easy way to implement a blob would be helpful as well.
I've attached my saving code and related structures(Updated from my horrible post earlier)
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <vector>
#include "sqlite3.h"
using namespace std;
struct PriceEntry{
float cardPrice;
string PriceDate;
int Edition;
int Rarity;
};
struct cardEntry{
string cardName;
long pesize;
long gsize;
vector<PriceEntry> cardPrices;
float vThreshold;
int fav;
};
vector<cardEntry> Cards;
void FillCards(){
int i=0;
int j=0;
char z[32]={0};
for(j=0;j<3;j++){
cardEntry tmpStruct;
sprintf(z, "Card Name: %d" , i);
tmpStruct.cardName=z;
tmpStruct.vThreshold=1.00;
tmpStruct.gsize=0;
tmpStruct.fav=1;
for(i=0;i<3;i++){
PriceEntry ss;
ss.cardPrice=i+1;
ss.Edition=i;
ss.Rarity=i-1;
sprintf(z,"This is struct %d", i);
ss.PriceDate=z;
tmpStruct.cardPrices.push_back(ss);
}
tmpStruct.pesize=tmpStruct.cardPrices.size();
Cards.push_back(tmpStruct);
}
}
int SaveCards(){
// Create an int variable for storing the return code for each call
int retval;
int CardCounter=0;
int PriceEntries=0;
char tmpQuery[256]={0};
int q_cnt = 5,q_size = 256;
sqlite3_stmt *stmt;
sqlite3 *handle;
retval = sqlite3_open("sampledb.sqlite3",&handle);
if(retval)
{
printf("Database connection failed\n");
return -1;
}
printf("Connection successful\n");
//char create_table[100] = "CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)";
char create_table[] = "CREATE TABLE IF NOT EXISTS Cards (CardName TEXT, PriceNum NUMERIC, Threshold NUMERIC, Fav NUMERIC);";
retval = sqlite3_exec(handle,create_table,0,0,0);
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(handle) );
for(CardCounter=0;CardCounter<Cards.size();CardCounter++){
char Query[512]={0};
for(PriceEntries=0;PriceEntries<Cards[CardCounter].cardPrices.size();PriceEntries++){
//Here is where I need to find out the process of storing the vector of PriceEntry for Cards then I can modify this loop to process the data
}
sprintf(Query,"INSERT INTO Cards VALUES('%s', %d, %f, %d)",
Cards[CardCounter].cardName.c_str(),
Cards[CardCounter].pesize,
Cards[CardCounter].vThreshold,
Cards[CardCounter].fav); //My insert command
retval = sqlite3_exec(handle,Query,0,0,0);
if(retval){
printf( "Could not prepare statement: %s\n", sqlite3_errmsg(handle) );
}
}
// Insert first row and second row
sqlite3_close(handle);
return 0;
}
I tried googling but my results didn't suffice.
You have two types here: Cards and PriceEntries. And for each Card there can be many PriceEntries.
You can store Cards in one table, one Card per row. But you're puzzled about how to store the PriceEntries, right?
What you'd normally do here is have a second table for PriceEntries, keyed off a unique column (or columns) of the Cards table. I guess the CardName is unique to each card? Let's go with that. So your PriceEntry table would have a column CardName, followed by columns of PriceEntry information. You'll have a row for each PriceEntry, even if there are duplicates in the CardName column.
The PriceEntry table might look like:
CardName | Some PE value | Some other PE value
Ace | 1 | 1
Ace | 1 | 5
2 | 2 | 3
and so on. So when you want to find the array of PriceEntries for a card, you'd do
select * from PriceEntry where CardName = 'Ace'
And from the example data above you'd get back 2 rows, which you could shove into an array (if you wanted to).
No need for BLOBs!
This is a simple serialization and deserialization system. The class PriceEntry has been extended with serialization support (very simply). Now all you have to do is serialize a PriceEntry (or a set of them) to binary data and store it in a blob column. Later on, you get the blob data and from that deserialize a new PriceEntry with the same values. An example of how it is used is given at the bottom. Enjoy.
#include <iostream>
#include <vector>
#include <string>
#include <cstring> // for memcpy
using std::vector;
using std::string;
// deserialization archive
struct iarchive
{
explicit iarchive(vector<unsigned char> data)
: _data(data)
, _cursor(0)
{}
void read(float& v) { read_var(v); }
void read(int& v) { read_var(v); }
void read(size_t& v) { read_var(v); }
void read(string& v) { read_string(v); }
vector<unsigned char> data() { return _data; }
private:
template <typename T>
void read_var(T& v)
{
// todo: check that the cursor will not be past-the-end after the operation
// read the binary data
std::memcpy(reinterpret_cast<void*>(&v), reinterpret_cast<const void*>(&_data[_cursor]), sizeof(T));
// advance the cursor
_cursor += sizeof(T);
}
inline
void
read_string(string& v)
{
// get the array size
size_t sz;
read_var(sz);
// get alignment padding
size_t padding = sz % 4;
if (padding == 1) padding = 3;
else if (padding == 3) padding = 1;
// todo: check that the cursor will not be past-the-end after the operation
// resize the string
v.resize(sz);
// read the binary data
std::memcpy(reinterpret_cast<void*>(&v[0]), reinterpret_cast<const void*>(&_data[_cursor]), sz);
// advance the cursor
_cursor += sz + padding;
}
vector<unsigned char> _data; // archive data
size_t _cursor; // current position in the data
};
// serialization archive
struct oarchive
{
void write(float v) { write_var(v); }
void write(int v) { write_var(v); }
void write(size_t v) { write_var(v); }
void write(const string& v) { write_string(v); }
vector<unsigned char> data() { return _data; }
private:
template <typename T>
void write_var(const T& v)
{
// record the current data size
size_t s(_data.size());
// enlarge the data
_data.resize(s + sizeof(T));
// store the binary data
std::memcpy(reinterpret_cast<void*>(&_data[s]), reinterpret_cast<const void*>(&v), sizeof(T));
}
void write_string(const string& v)
{
// write the string size
write(v.size());
// get alignment padding
size_t padding = v.size() % 4;
if (padding == 1) padding = 3;
else if (padding == 3) padding = 1;
// record the data size
size_t s(_data.size());
// enlarge the data
_data.resize(s + v.size() + padding);
// store the binary data
std::memcpy(reinterpret_cast<void*>(&_data[s]), reinterpret_cast<const void*>(&v[0]), v.size());
}
vector<unsigned char> _data; /// archive data
};
struct PriceEntry
{
PriceEntry()
{}
PriceEntry(iarchive& in) // <<< deserialization support
{
in.read(cardPrice);
in.read(PriceDate);
in.read(Edition);
in.read(Rarity);
}
void save(oarchive& out) const // <<< serialization support
{
out.write(cardPrice);
out.write(PriceDate);
out.write(Edition);
out.write(Rarity);
}
float cardPrice;
string PriceDate;
int Edition;
int Rarity;
};
int main()
{
// create a PriceEntry
PriceEntry x;
x.cardPrice = 1;
x.PriceDate = "hi";
x.Edition = 3;
x.Rarity = 0;
// serialize it
oarchive out;
x.save(out);
// create a deserializer archive, from serialized data
iarchive in(out.data());
// deserialize a PriceEntry
PriceEntry y(in);
std::cout << y.cardPrice << std::endl;
std::cout << y.PriceDate << std::endl;
std::cout << y.Edition << std::endl;
std::cout << y.Rarity << std::endl;
}

How to use boost::iostreams filter directly

How can I use boost::iostreams::gzip_decompressor to decompress a sequence of boost::asio::streambuf's?
Here is what I had in mind (non-working pseudo code):
struct Foo {
public:
void sinkData(boost::asio::streambuf & buf) {
z.write(boost::asio::buffer_cast<const char*>(buf.data()), buf.size());
while(write());
}
void flush() {
z.flush();
while(write());
}
private:
bool write() {
char buf[1024];
size_t s = z.read(buf, 1024);
std::cout << std::string(buf, s) << std::endl;
return
}
gzip_decompressor z;
}
The sinkData and the flush functions can not be changed. sinkdata() is called a number of time, followed by a single call to flush().
Is boost::iostreams::gzip_decompressor the best library for this purpose, if so how to do it? Otherwise, what could else be suggested?

C++ Reading Objects from File Error

fstream file;
Patient Obj("XXX",'M',"XXX");
file.open("Patients.dat",ios::in|ios::out|ios::app);
file.seekg(ios::end);
file.write((char*)&Obj,sizeof(Obj));
file.seekg(ios::beg);
Patient x;
file.read((char*)&x,sizeof(x));
x.printallInfo();
file.close();
I'm writing objects to files using this code but when i reading data VC++ 6 Crashes and thows a exception 'Access violation' .(Writing is successful)
Entire Code
#include <iostream>
#include<fstream>
#include <iomanip.h>
#include "Patient.cpp"
using namespace std;
int main(){
fstream file;
Patient Obj("XXX",'M',"XXX");
file.open("Patients.dat",ios::in|ios::out|ios::app);
file.seekg(ios::end);
file.write((char*)&Obj,sizeof(Obj));
file.seekg(ios::beg);
Patient x;
file.read((char*)&x,sizeof(x));
file.close();
return 0;
}
That seems like a brittle and non-portable way to marshal classes. One thing that could be happening with the way you do this is that you aren't making a deep copy of the data you're serializing. for instance, if one of the members of your Patient class is a std::string, a bare pointer is written to the file, but no string data is written. Worse, when you read that back in, the pointer points... somewhere...
A better way to deal with this issue is to actually implement a class specific method that knows exactly how to serialize and unserialize each member.
I'm not a C++ guru. Onething it doesn't seem correct here is that Object x in your code is not initialized.
Here's how you can read and write strings:
void writestring(std::ostream & out, const std::string & s)
{
std::size_t size = s.size();
out.write((char*)&size,sizeof(size));
out << s;
}
std::string readstring(std::istream & in)
{
std::size_t size;
in.read((char*)&size,sizeof(size));
char* buf = new char[size+1];
in.read(buf,size);
buf[size] = 0;
std::string s(buf);
delete [] buf;
return s;
}
If patient has pointers (e.g. to strings as I think it does based on its constructor) then your saving saves just the pointers, not values they point to. So loading initializes pointers to places in memory which might well be deleted or moved.
ok, here is the code I could not add to the comment below
class Patient : public Person{
.....
bool savePerson(fstream& stream) const
{
// you should do to Person the same thing I did for Patient
return true;
}
bool saveMedicalDetails(fstream& stream) const
{
for(int i=0;i<5;i++)
{
stream<<mD[i].number<<endl;
// we suppose here that the strings cannot contain 'end-of-line'
// otherwise you should save before any data of a string
// the number of characters in that string, like
// stream<<mD[i].doctors_name.size()<<" "<<mD[i].doctors_name<<endl;
stream<<mD[i].doctors_name<<endl;
stream<<mD[i].diognosis<<endl;
stream<<mD[i].medicine<<endl;
stream<<mD[i].date<<endl;
}
return stream;
}
bool savePaymentDetails(fstream& stream)const
{
stream<<pD.admisson<<endl;
stream<<pD.hospital_charges<<endl;
stream<<pD.doctor_charges<<endl;
return stream;
}
bool save(fstream& stream) const
{
return savePerson(stream) ||
saveMedicalDetails(stream) ||
savePaymentDetails(stream);
}
bool loadPerson(fstream& stream)
{
// you should do to Person the same thing I did for Patient
return true;
}
bool loadMedicalDetails(fstream& stream)
{
for(int i=0;i<5;i++)
{
stream>>mD[i].number;
// we suppose here that the strings cannot contain 'end-of-line'
// otherwise you should load before any data of a string
// the number of characters in that string, like
// int size;
// stream>>size;
// char *buffer=new char[size+1];
// stream.read(buffer,size);
// *(buffer+size)=0;
// mD[i].doctors=buffer;
// delete [] buffer;
getline(stream,mD[i].doctors);
getline(stream,mD[i].diognosis);
getline(stream,mD[i].medicine);
getline(stream,mD[i].date);
}
return stream;
}
bool loadPaymentDetails(fstream& stream)
{
stream>>pD.admisson;
stream>>pD.hospital_charges;
stream>>pD.doctor_charges;
return stream;
}
bool load(fstream& stream) const
{
return savePerson(stream) ||
saveMedicalDetails(stream) ||
savePaymentDetails(stream);
}
};
I figured it out using char arrays instead of strings will solve this problem , thanks all for your great help !