C++ MFC Serializre(CArchive &ar) with CArray<class*, class*> - c++

I have a program,draws shapes: Lozenge, square, rectangle, line, circle... it's same Paint in Microsoft.
My problems are save and load file with Serialize(CArchive &), but i cannot save and load file when use CArray. How i can do that:
class BaseShape : public CObject
{
DECLARE_SERIAL(BaseShape)
public:
CPoint topLeft, bottomRight;
COLORREF m_clrBack;
EShapeType m_ShapeType; //enum type of shape
public:
BaseShape(void); //empty method
BaseShape (CPoint , CPoint, COLORREF, EShapeType);
~BaseShape(void);
virtual void DrawShape (CDC*); //empty method
void Serialize(CArchive& ar);
};
Implement Serialize(CArchive& ar) of BaseShape class:
IMPLEMENT_SERIAL(BaseShape, CObject, 1)
void BaseShape::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << topLeft << bottomRight << m_clrBack << m_ShapeType;
}
else
{
int temp_shape;
ar >> topLeft >> bottomRight >> m_clrBack >> temp_shape;
m_ShapeType = (EShapeType)temp_shape;
}
}
Square class and Lozenge class are derived by BaseShape:
class CSquare : public BaseShape
{
public:
CSquare(void);
CSquare (CPoint , CPoint, COLORREF, EShapeType);
~CSquare(void);
void DrawShape(CDC*);
};
In MFC Document class, i have:
//declare properties
CArray<BaseShape*, BaseShape*> m_arrShape;
COLORREF m_clrBack;
EShapeType m_ShapeType;
//implement method
void CdemoDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
int i;
ar << m_arrShape.GetSize();
for (i = 0; i <m_arrShape.GetSize(); i++)
ar << m_arrShape[i];
}
else
{
int ncount, i;
ar >> ncount;
m_arrShape.RemoveAll();
for (i = 0; i < ncount; i++)
{
BaseShape* pShape = new BaseShape();
ar >> pShape;
m_arrShape.Add(pShape);
}
above my code, When i click open file, the shapes is not showed, which was drew before, although my code are not error, I save data file uncertain successfuly. I dont understand how the code lines of "isloading()" function working. Is there another way to do it ? This is my source code for all my project: http://www.mediafire.com/download/jy23ct28bgqybdc/demo.rar

The reason is simple: You can not create a BaseShape object and expect that it get specialized when it is loaded.
The trick is that ar << saves the complete object type and saves the Name of the class in the stream to. All you need is to load the stream into an object pointer again. The CArchive code will try to find a matching object class creates it and loads it.
Please read the MSDN about CObject serializing... se also the Scribble samples and others.
if (ar.IsStoring())
{
int i;
ar << m_arrShape.GetSize();
for (i = 0; i <m_arrShape.GetSize(); i++)
ar << m_arrShape[i];
}
else
{
int ncount, i;
ar >> ncount;
m_arrShape.RemoveAll();
for (i = 0; i < ncount; i++)
{
BaseShape *pNewShape;
ar >> pNewShape;
m_arrShape.Add(pNewShape);
}
}
PS: If you provide sample code, it should match the code in your question.

Related

Is this member variable used to register subclasses in this protype design pattern? If yes, how?

I am studying this piece of code in the book "Design Patterns explained simply".
It is the implementation of proytpe design pattern by the author Alexander Shvets.
Although I thikn I have undrstand most of it, but the static variable _landSatImage have confued me for several days.
By the comment of the author, it seems to register the subclass.
But how _landSatImage is used to register the subclasses?
Anyone can give me some clue? Thanks :)
#include <iostream>
#include <vector>
using namespace std;
//--------------------- 1 Image
enum imageType
{
LSAT, SPOT
};
class Image
{
public:
virtual void draw() = 0;
static Image *findAndClone(imageType);
protected:
virtual imageType returnType() = 0;
virtual Image *clone() = 0;
// As each subclass of Image is declared, it registers its prototype
static void addPrototype(Image *image)
{
_prototypes[_nextSlot++] = image;
}
private:
static Image *_prototypes[10];
static int _nextSlot;
};
Image *Image::_prototypes[];
int Image::_nextSlot;
// Client calls this public static member function
// when it needs an instance of an Image subclass
Image *Image::findAndClone(imageType type)
{
for (int i = 0; i < _nextSlot; i++)
if (_prototypes[i]->returnType() == type)
return _prototypes[i]->clone();
}
//----------------------- 2 LandSatImage
class LandSatImage: public Image
{
public:
imageType returnType()
{
return LSAT;
}
void draw()
{
cout << "LandSatImage::draw " << _id << endl;
}
// When clone() is called, call the one-argument ctor with a dummy arg
Image *clone()
{
return new LandSatImage(1);
}
protected:
// This is only called from clone()
LandSatImage(int dummy)
{
_id = _count++;
}
private:
// Mechanism for initializing an Image subclass -
// this causes the default ctor to be called,
// which registers the subclass's prototype
static LandSatImage _landSatImage;
// This is only called
// when the private static data member
// is inited
LandSatImage()
{
addPrototype(this);
}
// Nominal "state" per instance mechanism
int _id;
static int _count;
};
// Register the subclass's prototype
LandSatImage LandSatImage::_landSatImage;
// Initialize the "state" per instance mechanism
int LandSatImage::_count = 1;
//------------------------------ 3 SpotImage
class SpotImage: public Image
{
public:
imageType returnType()
{
return SPOT;
}
void draw()
{
cout << "SpotImage::draw " << _id << endl;
}
Image *clone()
{
return new SpotImage(1); //
}
protected:
SpotImage(int dummy)
{
_id = _count++;
}
private:
SpotImage()
{
addPrototype(this);
}
static SpotImage _spotImage;
int _id;
static int _count;
};
SpotImage SpotImage::_spotImage;
int SpotImage::_count = 1;
//----------------------------------------- main
// Simulated stream of creation requests
const int NUM_IMAGES = 8;
imageType input[NUM_IMAGES] =
{
LSAT, LSAT, LSAT, SPOT, LSAT, SPOT, SPOT, LSAT
};
int main()
{
Image *images[NUM_IMAGES];
// Given an image type, find the right prototype, and return a clone
for (int i = 0; i < NUM_IMAGES; i++)
images[i] = Image::findAndClone(input[i]);
// Demonstrate that correct image objects have been cloned
for (int i = 0; i < NUM_IMAGES; i++)
images[i]->draw();
// Free the dynamic memory
for (int i = 0; i < NUM_IMAGES; i++)
delete images[i];
}

C++ Boot serialization cause a segmentation fault

I have those two classes and I'm trying to deserialize them using boost
class WorldItem
{
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar &foreground;
ar &background;
ar &breakLevel;
ar &breakTime;
ar &water;
ar &fire;
ar &glue;
ar &red;
ar &green;
ar &blue;
}
public:
int foreground = 0;
int background = 0;
int breakLevel = 0;
long long int breakTime = 0;
bool water = false;
bool fire = false;
bool glue = false;
bool red = false;
bool green = false;
bool blue = false;
};
class Worlds
{
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar &width;
ar &height;
ar &name;
for (int i = 0; i < 100 * 60; i++)
{
ar &items[i];
}
ar &owner;
ar &weather;
ar &isPublic;
ar &isNuked;
}
public:
int width;
int height;
string name;
WorldItem *items;
string owner = "";
int weather = 0;
bool isPublic = false;
bool isNuked = false;
};
Here I'm creating a world in this way
Worlds generateWorld(string name, int width, int height)
{
Worlds world;
world.name = name;
world.width = width;
world.height = height;
world.items = new WorldItem[100 * 60];
}
and here im using this function to serialize the world
std::stringstream serialize_world(Worlds world)
{
std::stringstream str;
{
boost::archive::binary_oarchive oa(str);
oa << world;
}
return str;
}
So the serialize_world function is working without issues and i am inserting it value to mysql longblob.
But now when I'm trying to get the blob from MySql and deserialize it back by using this function
Worlds deserialize(std::string world)
{
Worlds wld;
std::istream *blobdata = WORLD_DATA(world);
{
boost::archive::binary_iarchive ia(*blobdata);
ia >> wld;
}
return wld;
}
I'm getting a Segmentation fault (core dumped) I don't know what's wrong.
Thanks.
Looks like you never return a value from generateWorld.
My compiler warns about this. Try enabling your compiler's diagnostics. I usually have -Wall -Wextra -pedantic enabled
Also in deserialize you never initialize items to anything. That is going to lead to UB.
This, too, could be diagnosed by most compilers (-fsanitize=address,undefined helps, although it makes compilation and runtime slow). There's also external tools like Valgrind that do these
Finally, I have no idea what is going on with blobdata, so I'm going to ignore that, but it too looks wrong.
Don't use raw new/delete
See also e.g. https://www.quora.com/Why-are-the-%E2%80%98new%E2%80%99-and-%E2%80%98delete%E2%80%99-keywords-considered-bad-in-modern-C++
Just use std::array then and be happy:
Live On Coliru
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <array>
#include <iostream>
#include <sstream>
class WorldItem {
private:
friend class boost::serialization::access;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar& foreground& background& breakLevel& breakTime& water& fire& glue&
red& green& blue;
}
public:
int foreground = 0;
int background = 0;
int breakLevel = 0;
long long int breakTime = 0;
bool water = false;
bool fire = false;
bool glue = false;
bool red = false;
bool green = false;
bool blue = false;
auto operator<=>(WorldItem const&) const = default;
};
class Worlds
{
private:
friend class boost::serialization::access;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar& width& height& name& items& owner& weather& isPublic& isNuked;
}
public:
int width;
int height;
std::string name;
std::array<WorldItem, 100 * 60> items;
std::string owner = "";
int weather = 0;
bool isPublic = false;
bool isNuked = false;
auto operator<=>(Worlds const&) const = default;
};
//So here im creating a world in this way
Worlds generateWorld(std::string name, int width, int height) {
Worlds world;
world.name = name;
world.width = width;
world.height = height;
return world;
}
std::string serialize_world(Worlds const& world) {
std::stringstream str;
{
boost::archive::binary_oarchive oa(str);
oa << world;
}
return str.str();
}
Worlds deserialize(std::string world) {
Worlds wld;
std::istringstream blobdata(world);
{
boost::archive::binary_iarchive ia(blobdata);
ia >> wld;
}
return wld;
}
int main() {
Worlds w = generateWorld("test", 6, 6);
Worlds clone = deserialize(serialize_world(w));
std::cout << "Worlds equal? " << std::boolalpha << (w == clone) << "\n";
}
Prints
Worlds equal? true

Implementing MFC Serialazation for base and derived classes

I have two classes: Product and derived Juice. I need to implement MFC Serialazation for these classes.
class Product : CObject
{
protected:
DECLARE_SERIAL(Product) //IMPLEMENT_SERIAL(Product, CObject, 0) in .cpp
CString name;
int expiring;
double price;
public:
Product();
~Product();
virtual void input_data();
virtual void print_data();
virtual void Serialize(CArchive& archive)
{
CObject::Serialize(archive);
if (archive.IsStoring())
archive << name << expiring << price;
else
archive >> name >> expiring >> price;
};
};
class Juice :
public Product
{
private:
double volume;
CString taste;
public:
Juice();
~Juice();
void input_data() override;
void print_data() override;
void Serialize(CArchive& archive) override
{
Product::Serialize(archive);
if (archive.IsStoring())
archive << volume << taste;
else
archive >> volume >> taste;
}
};
To store objects of the classes I have Stock class which has container of Product class pointers.
class Stock
{
private:
vector<shared_ptr<Product>> stock;
public:
Stock();
~Stock();
void Add(shared_ptr<Product> p);
void Print();
bool Save(string fname);
bool Load(string fname);
void Clear();
};
In Save and Load methods I'm trying to implement serialazation (according to discussion in this topic C++ MFC Serialization).
bool Stock::Save(string fname)
{
CFile out;
if (!out.Open(fname.c_str(), CFile::modeWrite | CFile::modeCreate))
return false;
CArchive ar(&out, CArchive::store);
ar.WriteCount(stock.size());
for (auto it = stock.begin(); it != stock.end(); ++it)
{
(*it)->Serialize(ar);
}
ar.Close();
out.Close();
return true;
}
bool Stock::Load(string fname)
{
CFile in;
if (!in.Open(fname.c_str(), CFile::modeRead))
return false;
CArchive ar(&in, CArchive::load);
int cnt = ar.ReadCount();
for (int i = 0; i < cnt; i++)
{
auto p = make_shared<Product>();
p->Serialize(ar);
stock.push_back(p);
}
ar.Close();
in.Close();
return true;
}
Now I got a problem.
While reading objects from file Juice objects are read like Product (without volume ant taste fields). The reading of the object after Juice starts with the rest of Juice information, so I got CArchiveException in Serialaize method of Product.
If I use only Product objects to add to Stock everything works fine. What are my mistakes and what should I do to implement MFC serialization correctly?
Stock::Save needs to change to:
for (auto it = stock.begin(); it != stock.end(); ++it)
{
ar << (*it).get();
}
And Stock::Load needs to change to:
for (int i = 0; i < cnt; i++)
{
Product* obj = nullptr;
ar >> obj;
stock.emplace_back(obj);
}
When you use ar << obj, it saves type information with the object so it can be retrieved properly when you load it. Calling Serialize directly won't save the type data.
For reference, here's what the MFC serialization code looks like inside of CObArray (basically what you'd use instead of a vector if you stuck to MFC only)
if (ar.IsStoring())
{
ar.WriteCount(m_nSize);
for (INT_PTR i = 0; i < m_nSize; i++)
ar << m_pData[i];
}
else
{
DWORD_PTR nOldSize = ar.ReadCount();
SetSize(nOldSize);
for (INT_PTR i = 0; i < m_nSize; i++)
ar >> m_pData[i];
}

How to make a loop that instanciates classes and sort them in a address list?

Consider 2 variables (number of polygons and its coordinates) :
int numberPoly= 2;
float polycoord[18]= {0,50,0,0,0,0,50,0,0,0,50,0,50,50,0,50,0,0};
, a Model class (that is intended to store polygon classes to a list) :
class model{
public:
model();
void affect(int id, int address){
polyclasses[id]=address;
}
private:
string name;
vector<int> polyclasses;
};
, a Polygon class (that I have to sort in Model's polyclasses list) :
class polygone {
public:
polygone();
void affect(int id, int coord){
ABC[id]=coord;
}
private:
int id;
float ABC[9]={0.0};
};
I wanted to code a function (cf. "builder") that instanciate n Polygon classes and sort them (with their memory addresses like an id) in an array ("polyclasses" from Model class). So, I don't arrive. Here is a bit of my builder function not acomplished :
void builder(){
int from = 0; int subfrom = 0;
for(int i=0; i < numberPoly - 1; i++){
from = 0; subfrom = 0;
polygone poly();
!!! need to put that instance in Model's polygon list !!!
...
for(int j=from; j < (polycoord.size())-1; j++){
poly.affect(subfrom, polycoord[j]) ...
subfrom++;
}
from += 3;
}
}
This is for my first c++ project. I'm coding a light 2d engine.
You need to store pointer of instances in your vector and allocate your objects with new keyword. At destructor of your model yo uwill need to deletethe object to avoid a memory leak.
// Model.h
// Class name should begin with uppercase by convention
class Model{
public:
Model();
~Model();
void builder();
// Implementation should go in cpp file
void affect(int id, int address);
private:
// Having a m_ prefix on private variable is useful to make your code more readable so a reader can easily know if a variable is private or not
string m_name;
vector<Polygon*> m_polyclasses;
};
// Polygone.h
class Polygone {
public:
Polygone();
// Don't forget destructor
~Polygone();
// Implementation should go in cpp file
void affect(int id, int address);
private:
int m_id;
// Use std::array in C++ and give meaningful name to your variable
// float m_ABC[9]={0.0}; is replaced by :
std::array<float, 9> m_coordinates;
};
// Model.cpp
void Model::builder() {
int from = 0; int subfrom = 0;
for(int i=0; i < numberPoly - 1; i++){
from = 0; subfrom = 0;
Polygone * poly = new Polygone();
// A pointer of poly is now stored in Model
this->polyclasses.push_back(poly);
// Your polygone object should initialized in the constructor or in a member function of the class Polygone.
for(int j=from; j < (polycoord.size())-1; j++){
poly->affect(subfrom, polycoord[j]) ...
subfrom++;
}
from += 3;
}
}
Model::~Model() {
for(auto p: this->polyclasses) {
// Avoid memory leak
delete p;
}
this->polyclasses.clear();
}
You can also store a std::unique_ptr instead of a plain pointer. In that case you don't need to delete.

C++ Boost deserialization what(): input stream error

I'm trying to make a serialization of an object and then to deserialize it. Even though it seems that everything I wrote is ok I'm still getting an error during the deserialization.
int main(){
MapAttributes mapAtt(MAP_SIZE,MAP_SIZE);
initMapFromImage("map1.png",&mapAtt);
int prevxMax = mapAtt.prevxMax ;
int prevyMax = mapAtt.prevyMax ;
int prevxMin = mapAtt.prevxMin ;
int prevyMin = mapAtt.prevyMin ;
PixelCoords pose = PixelCoords(mapAtt.robotPose.dx, mapAtt.robotPose.dy);
PixelCoords target = PixelCoords( 2048+250, 2048+250 );
PartitionGraphNodes partitionGraph(&mapAtt);
PartitionGraphNodes partitionGraph2(&mapAtt);
partitionGraph.createIncrementalPartition(pose,target);
if (partitionGraph.nodes.size()!=0){
saveSerializedObject<PartitionGraphNodes(partitionGraph,"partition_graph_marshall");
std::cout << "size= " << partitionGraph.nodes.size() << "\n";
restoreSerializedObject<PartitionGraphNodes>(partitionGraph2,"partition_graph_marshall");
}
else{
std::cout << "RUN FOR YOUR LIVES!!!\n";
}
return 0;
This is my code where I'm trying to get my objects serialized and deserialized.
Here is the function for the serialization and the deserialization:
template<class T>
void saveSerializedObject(const T &s, const char * filename){
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
template<class T>
void restoreSerializedObject(T &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
if (ifs.good()) {
std::cout << "Coming!!!\n";
boost::archive::text_iarchive ia(ifs);
ia >> s;
} else {
// throw an error or something
assert(false);
}
}
Finally I have wrote the code for the serialization exactly as the Boost documentation suggests, but I still get an error: an instance of 'boost::archive::archive_exception' what(): input stream error. Can you help me with that?
EDIT:
As it is clear enough, initially I'm trying to serialize an object of class PartitionGraphNodes. So this is how an PartitionGraphNodes object is serialized:
#include <boost/serialization/base_object.hpp>
class PartitionGraphNodes: public NodesVector{
private:
std::vector<int> targetNeighbors; //!<Vector holding target neighbors
std::vector<int> robotNeighbors; //!<Vector holding robot neighbors
std::vector<PixelCoords> uniformPartition;
public:
PartitionGraphNodes(MapAttributes* mapAttr);
void createIncrementalPartition(PixelCoords startPos, PixelCoords target);
int insertNodeInPartition(Node currNode);
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
std::cout << "In partition_serial" << "\n";
ar & boost::serialization::base_object<NodesVector>(*this);
}
};
#include <boost/serialization/vector.hpp>
class NodesVector{
protected:
//~ std::vector<Voronode> nodes;
public:
NodesVector(void){};
std::vector<Node> nodes;
MapAttributes* mapAttributes;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
std::cout << "In nodesVector_serial" << "\n";
ar & nodes;
}
};
#include <boost/serialization/vector.hpp>
class Node{
public:
PixelCoords p; //!< The coordinates of the node
bool visited; //!< True if the node has been visited
unsigned int ID; //!< The node's ID
int parent;
std::vector<PixelCoords> neigh; //!< Vector of the neighbors of the node (coords)
std::vector<unsigned int> neighID; //!< Vector of the neighbors' IDs
std::vector<float> dist; //!< Vector of the distances of the neighbors
Weight w; //!< The node's weight
std::vector<PixelCoords> path;
Node(PixelCoords a) {p=a;}
Node(PixelCoords a,unsigned int IDt) {p=a; ID=IDt;}
Node(void){}
void makeNeighbor(Node &a);
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
std::cout << "In nodes_serial" << "\n";
ar & p;
ar & visited;
ar & ID;
ar & parent;
ar & neigh;
ar & neighID;
ar & dist;
ar & w;
}
};
#endif
I think you need
BOOST_CLASS_EXPORT();
lines.
See for instance the answers to this question: Where to put BOOST_CLASS_EXPORT for boost::serialization?