A framework for saving and loading objects on disc
Feb 23, 2016 at 1:20pm UTC
I have created a framework for persisting objects on c++ Need to implement with binary files as well. I am presenting here the code from my header file. Also the main body is provided here. Some feedback would definitely be appreciated here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#include <iostream>
#include <cstring>
#include <typeinfo>
#include <fstream>
static const std::string FNAME = "Filename" ;
template <class ObjectType> class ObjectStorage {
protected :
ObjectType obj;
std::string filename;
//Helper Functions
virtual std::string get_object_heading() {
return typeid (obj).name();
}
public :
ObjectStorage(ObjectType obj) {
this ->obj = obj;
filename = FNAME + "-" + typeid (obj).name()+".txt" ;
}
ObjectStorage(const ObjectStorage<ObjectType> &object) {
this ->obj = object.GetObj();
this ->filename = object.GetFilename();
}
virtual ~ObjectStorage() {
}
//Accessors
ObjectType GetObj() const {return obj;}
std::string GetFilename() const {return filename;}
//Mutators
void EditFilename(std::string new_name) {
filename = new_name;
}
//Framework APIs
virtual ObjectType ReadFromFile() {
/* Read input from file. User shall override >> operator for reading from file */
std::fstream f;
f.open(filename.c_str(),std::fstream::in);
filename>>&obj;
f.close();
return obj;
}
virtual void WriteToFile() {
/* User shall provide his/her implementation for printing his/her object obj */
std::ofstream f;
f.open(filename.c_str(), std::ios::app);
f<<"----------STORAGE FOR OBJECT TYPE: " <<get_object_heading()<<"------------\n" ;
f<<&obj;
f<<"\n-------------------------------------------------------------------------\n" ;
f.close();
}
};
template <class ObjectType> class ObjectStorage<ObjectType *> {
protected :
ObjectType *obj;
std::string filename;
//Helper Functions
virtual std::string get_object_heading() {
return typeid (obj).name();
}
public :
ObjectStorage(ObjectType *obj) {
this ->obj = obj;
filename = FNAME + "-" + typeid (obj).name()+".txt" ;
}
ObjectStorage(const ObjectStorage<ObjectType> &object) {
this ->obj = new ObjectType(*(object.GetObj()));
this ->filename = object.GetFilename();
}
virtual ~ObjectStorage() {
delete obj;
}
//Accessors
ObjectType *GetObj() const {return obj;}
std::string GetFilename() const {return filename;}
//Mutators
void EditFilename(std::string new_name) {
filename = new_name;
}
//Framework APIs
virtual ObjectType *ReadFromFile(std::string filename, ObjectType *obj) {
/* Read input from file. User shall override >> operator for reading from file */
std::fstream f;
f.open(filename.c_str(),std::fstream::in);
filename>>obj;
f.close();
return obj;
}
virtual void WriteToFile() {
/* User shall provide his/her implementation for printing his/her object obj */
std::ofstream f;
f.open(filename.c_str(), std::ios::app);
f<<"----------STORAGE FOR OBJECT TYPE: " <<get_object_heading()<<"------------\n" ;
f<<obj;
f<<"\n-------------------------------------------------------------------------\n" ;
f.close();
}
};
std::ostream& operator <<(std::ostream &os, const int *obj) {
os<<"1\n" ;
os<<*obj<<"\n" ;
return os;
}
int main()
{
int *a = new int (10);
int b = 20;
//ObjectStorage<int*> storage(a);
//storage.WriteToFile();
return 0;
}
Feb 23, 2016 at 2:00pm UTC
Last edited on Feb 23, 2016 at 2:01pm UTC
Feb 29, 2016 at 1:51pm UTC
Thanks for the input. Can you suggest some guidelines to design such a framework. Am new to these things.
Feb 29, 2016 at 2:01pm UTC
Don't. Just use what exists already.
Topic archived. No new replies allowed.