commit e68707766bf0e7cd870629e304deff389d54dee7 parent f65818528b9b219a4bee755bd540771dcf938f62 Author: Christos Margiolis <christos@margiolis.net> Date: Sun, 24 May 2020 20:30:35 +0300 inlined trivial getters/setters Diffstat:
26 files changed, 104 insertions(+), 204 deletions(-)
diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/obj/app.o b/assignment-2.4-inheritance/obj/app.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/appsystem.o b/assignment-2.4-inheritance/obj/appsystem.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/game.o b/assignment-2.4-inheritance/obj/game.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/manufacturer.o b/assignment-2.4-inheritance/obj/manufacturer.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/office.o b/assignment-2.4-inheritance/obj/office.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/review.o b/assignment-2.4-inheritance/obj/review.o Binary files differ. diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -52,62 +52,8 @@ App::convsn(const char *serialnum) } void -App::addrev(Review *rev) -{ - reviews.push_back(rev); -} - -void -App::addrevs(const std::vector<Review *> revs) -{ - reviews.insert(reviews.end(), revs.begin(), revs.end()); -} - -const std::string& -App::get_name() const -{ - return name; -} - -const std::string& -App::get_os() const -{ - return os; -} - -const Manufacturer -App::get_manf() const -{ - return Manufacturer(*manf); -} - -void App::set_serialnum(const char *serialnum) { if (this->serialnum != nullptr) delete[] this->serialnum; this->serialnum = convsn(serialnum); } - -void -App::set_name(const std::string& name) -{ - this->name = name; -} - -void -App::set_os(const std::string& os) -{ - this->os = os; -} - -void -App::set_manf(Manufacturer *manf) -{ - this->manf = manf; -} - -void -App::set_price(int price) -{ - this->price = price; -} diff --git a/assignment-2.4-inheritance/src/app.h b/assignment-2.4-inheritance/src/app.h @@ -28,21 +28,22 @@ class App App(const App& app); virtual ~App(); - void addrev(Review *rev); - void addrevs(const std::vector<Review *> revs); + void addrev(Review *rev) {reviews.push_back(rev);} + void addrevs(const std::vector<Review *> revs) + {reviews.insert(reviews.end(), revs.begin(), revs.end());} - const std::string& get_name() const; - const std::string& get_os() const; - const Manufacturer get_manf() const; - constexpr const char *get_serialnum() const {return serialnum;} - constexpr const std::vector<Review *>& get_revs() const {return reviews;} - constexpr int get_price() const {return price;} + inline const std::string& get_name() const {return name;} + inline const std::string& get_os() const {return os;} + inline const Manufacturer get_manf() const {return Manufacturer(*manf);} + inline constexpr const char *get_serialnum() const {return serialnum;} + inline constexpr const std::vector<Review *>& get_revs() const {return reviews;} + inline constexpr int get_price() const {return price;} void set_serialnum(const char *serialnum); - void set_name(const std::string& name); - void set_os(const std::string& os); - void set_manf(Manufacturer *manf); - void set_price(int price); + inline void set_name(const std::string& name) {this->name = name;} + inline void set_os(const std::string& os) {this->os = os;} + inline void set_manf(Manufacturer *manf) {this->manf = manf;} + inline void set_price(int price) {this->price = price;} virtual void print(std::ostream& stream) const = 0; diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -37,8 +37,8 @@ class AppSystem void removebad (Manufacturer *man); void removebad (const char *manfname); - constexpr const std::vector<App *>& get_apps() const {return apps;} - constexpr const std::vector<Manufacturer *>& get_manfs() const {return manfs;} + inline constexpr const std::vector<App *>& get_apps() const {return apps;} + inline constexpr const std::vector<Manufacturer *>& get_manfs() const {return manfs;} const std::vector<Office *> get_freeapps() const; const std::vector<Game *> get_goodgames() const; @@ -60,7 +60,7 @@ AppSystem::parse(std::ifstream& f) { try { - if constexpr (std::is_same<T, Manufacturer>::value) + if constexpr (std::is_same_v<T, Manufacturer>) { std::string sn, name, email; std::getline(f, sn, ','); @@ -69,7 +69,7 @@ AppSystem::parse(std::ifstream& f) if (f.eof()) return true; manfs.push_back(new Manufacturer(sn.c_str(), name.c_str(), email)); } - else if constexpr (std::is_same<T, Office>::value) + else if constexpr (std::is_same_v<T, Office>) { std::string sn, name, os, manf, price, skip1, skip2; std::getline(f, sn, ','); @@ -95,7 +95,7 @@ AppSystem::parse(std::ifstream& f) } } } - else if constexpr (std::is_same<T, Game>::value) + else if constexpr (std::is_same_v<T, Game>) { std::string sn, name, os, manf, price, genre, online; std::string skip; @@ -151,11 +151,11 @@ AppSystem::import_data(const char *fpath) std::getline(f, skip); while (f.good()) { - if constexpr (std::is_same<T, Manufacturer>::value) + if constexpr (std::is_same_v<T, Manufacturer>) { if (!parse<Manufacturer>(f)) break; } - else if constexpr (std::is_same<T, App>::value) + else if constexpr (std::is_same_v<T, App>) { std::string type; std::getline(f, type, ','); @@ -164,7 +164,7 @@ AppSystem::import_data(const char *fpath) if (type == "Office") if (!parse<Office>(f)) break; } - else if constexpr (std::is_same<T, Review>::value) + else if constexpr (std::is_same_v<T, Review>) { std::string appname, stars, username, comment; std::getline(f, appname, ','); @@ -201,7 +201,7 @@ AppSystem::export_data(const char *fpath) f.open(fpath); std::cout << "Exporting data to \'" << fpath << "\'." << std::endl; - if constexpr (std::is_same<T, Manufacturer>::value) + if constexpr (std::is_same_v<T, Manufacturer>) { f << "SN,Name,Email\n"; for (const auto& manf : manfs) @@ -209,7 +209,7 @@ AppSystem::export_data(const char *fpath) manf->get_name() << ',' << manf->get_email() << std::endl; } - else if constexpr (std::is_same<T, App>::value) + else if constexpr (std::is_same_v<T, App>) { f << "Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions\n"; for (const auto& app : apps) @@ -233,7 +233,7 @@ AppSystem::export_data(const char *fpath) } } } - else if constexpr (std::is_same<T, Review>::value) + else if constexpr (std::is_same_v<T, Review>) { f << "AppName,Stars,Username,Comment\n"; for (const auto& app : apps) diff --git a/assignment-2.4-inheritance/src/game.cpp b/assignment-2.4-inheritance/src/game.cpp @@ -16,24 +16,6 @@ Game::Game(const Game& g) :App(g.serialnum, g.name, g.os, g.manf, g.price), genre(g.genre), online(g.online) {} -const std::string -Game::get_genre() const -{ - return genre; -} - -void -Game::set_genre(const std::string& genre) -{ - this->genre = genre; -} - -void -Game::set_online(bool online) -{ - this->online = online; -} - void Game::print(std::ostream& stream) const { diff --git a/assignment-2.4-inheritance/src/game.h b/assignment-2.4-inheritance/src/game.h @@ -16,11 +16,10 @@ class Game: public App const std::string& genre, bool online); Game(const Game& g); - const std::string get_genre() const; - constexpr bool get_online() const {return online;} - - void set_genre(const std::string& genre); - void set_online(bool online); + inline const std::string get_genre() const {return genre;} + inline constexpr bool get_online() const {return online;} + inline void set_genre(const std::string& genre) {this->genre = genre;} + inline void set_online(bool online) {this->online = online;} virtual void print(std::ostream& stream) const override; }; diff --git a/assignment-2.4-inheritance/src/manufacturer.cpp b/assignment-2.4-inheritance/src/manufacturer.cpp @@ -36,12 +36,6 @@ Manufacturer::convstr(const char *str) return tmp; } -const std::string& -Manufacturer::get_email() const -{ - return email; -} - void Manufacturer::set_serialnum(const char *serialnum) { @@ -55,9 +49,3 @@ Manufacturer::set_name(const char *name) if (this->name != nullptr) delete[] name; this->name = convstr(name); } - -void -Manufacturer::set_email(const std::string& email) -{ - this->email = email; -} diff --git a/assignment-2.4-inheritance/src/manufacturer.h b/assignment-2.4-inheritance/src/manufacturer.h @@ -19,13 +19,13 @@ class Manufacturer Manufacturer(const Manufacturer& m); ~Manufacturer(); - const std::string& get_email() const; - constexpr const char *get_serialnum() const {return serialnum;} - constexpr const char *get_name() const {return name;} + inline const std::string& get_email() const {return email;} + inline constexpr const char *get_serialnum() const {return serialnum;} + inline constexpr const char *get_name() const {return name;} void set_serialnum(const char *serialnum); - void set_name(const char *name); - void set_email(const std::string& email); + void set_name(const char *name); + inline void set_email(const std::string& email) {this->email = email;} private: char *convstr(const char *str); diff --git a/assignment-2.4-inheritance/src/office.cpp b/assignment-2.4-inheritance/src/office.cpp @@ -16,12 +16,6 @@ Office::~Office() if (!extensions.empty()) extensions.clear(); } -const std::vector<std::string> -Office::get_exts() const -{ - return extensions; -} - void Office::set_exts(const std::vector<std::string>& extensions) { diff --git a/assignment-2.4-inheritance/src/office.h b/assignment-2.4-inheritance/src/office.h @@ -16,7 +16,7 @@ class Office: public App Office(const Office& o); ~Office(); - const std::vector<std::string> get_exts() const; + const std::vector<std::string> get_exts() const {return extensions;} void set_exts(const std::vector<std::string>& extensions); virtual void print(std::ostream& stream) const override; }; diff --git a/assignment-2.4-inheritance/src/review.cpp b/assignment-2.4-inheritance/src/review.cpp @@ -12,33 +12,3 @@ Review::Review(int stars, const std::string& username, const std::string& commen Review::Review(const Review& r) :stars(r.stars), username(r.username), comment(r.comment) {} - -const std::string& -Review::get_username() const -{ - return username; -} - -const std::string& -Review::get_comment() const -{ - return comment; -} - -void -Review::set_stars(int stars) -{ - this->stars = stars; -} - -void -Review::set_username(const std::string& username) -{ - this->username = username; -} - -void -Review::set_comment(const std::string& comment) -{ - this->comment = comment; -} diff --git a/assignment-2.4-inheritance/src/review.h b/assignment-2.4-inheritance/src/review.h @@ -13,16 +13,19 @@ class Review public: Review(); - Review(int stars, const std::string& username, const std::string& comment); + Review(int stars, const std::string& username, + const std::string& comment); Review(const Review& r); - const std::string& get_username() const; - const std::string& get_comment() const; - constexpr int get_stars() const {return stars;} + inline const std::string& get_username() const {return username;} + inline const std::string& get_comment() const {return comment;} + inline constexpr int get_stars() const {return stars;} - void set_stars(int stars); - void set_username(const std::string& username); - void set_comment(const std::string& comment); + inline void set_stars(int stars) {this->stars = stars;} + inline void set_username(const std::string& username) + {this->username = username;} + inline void set_comment(const std::string& comment) + {this->comment = comment;} }; #endif /* REVIEW_H */ diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/datahandler.o b/assignment-2.5-spreadsheets/obj/datahandler.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/src/course.h b/assignment-2.5-spreadsheets/src/course.h @@ -3,12 +3,18 @@ #include "xstring.h" -struct Course +class Course { + private: lab::xstring code; lab::xstring name; + + public: Course(const lab::xstring& code, const lab::xstring& name) :code(code), name(name) {} + + inline constexpr const lab::xstring& get_code() const {return code;} + inline constexpr const lab::xstring& get_name() const {return name;} }; #endif /* COURSE_H */ diff --git a/assignment-2.5-spreadsheets/src/datahandler.cpp b/assignment-2.5-spreadsheets/src/datahandler.cpp @@ -7,8 +7,11 @@ DataHandler::~DataHandler() { dealloc<Course>(courses); dealloc<Student>(studs); - eqvs.clear(); - errs.clear(); + if (!grds.empty()) grds.clear(); + if (!data.empty()) data.clear(); + if (!eqvs.empty()) eqvs.clear(); + if (!errs.empty()) errs.clear(); + if (!missing.empty()) missing.clear(); } bool @@ -121,17 +124,17 @@ DataHandler::miss(lab::xstring id, lab::xstring code, float grade) { bool found = false; for (const auto& grd : grds) - if (grd.first->code == eqvs[code]) + if (grd.first->get_code() == eqvs[code]) found = true; if (!found) { missing.push_back(id + ";" + - studs[id]->lname + ";" + - studs[id]->fname + ";" + - courses[eqvs[code]]->code + ";" + - courses[eqvs[code]]->name + ";" + + studs[id]->get_lname() + ";" + + studs[id]->get_fname() + ";" + + courses[eqvs[code]]->get_code() + ";" + + courses[eqvs[code]]->get_name() + ";" + eqvs[code] + ";" + - courses[code]->name + ";" + + courses[code]->get_name() + ";" + lab::to_xstr<float>("%.1f", grade)); misscount++; } diff --git a/assignment-2.5-spreadsheets/src/datahandler.h b/assignment-2.5-spreadsheets/src/datahandler.h @@ -12,7 +12,7 @@ #include "student.h" #include "xstring.h" -typedef std::map<lab::xstring, lab::xstring> equivalencies; +using equivalencies = std::map<lab::xstring, lab::xstring>; class DataHandler { @@ -70,7 +70,7 @@ DataHandler::import_data(const char *fpath) lab::getline(f, skip); while (f.good()) { - if constexpr (std::is_same<T, Course>::value) + if constexpr (std::is_same_v<T, Course>) { lab::xstring code, name; lab::getline(f, code, ';'); @@ -78,7 +78,7 @@ DataHandler::import_data(const char *fpath) if (f.eof()) break; courses.insert(std::make_pair(code, new Course(code, name))); } - else if constexpr (std::is_same<T, Student>::value) + else if constexpr (std::is_same_v<T, Student>) { lab::xstring id, lname, fname; lab::getline(f, id, ';'); @@ -87,7 +87,7 @@ DataHandler::import_data(const char *fpath) if (f.eof()) break; studs.insert(std::make_pair(id, new Student(id, lname, fname))); } - else if constexpr (std::is_same<T, equivalencies>::value) + else if constexpr (std::is_same_v<T, equivalencies>) { lab::xstring oldcurr, newcurr; lab::getline(f, oldcurr, ';'); diff --git a/assignment-2.5-spreadsheets/src/student.h b/assignment-2.5-spreadsheets/src/student.h @@ -3,16 +3,22 @@ #include "xstring.h" -struct Student +class Student { + private: lab::xstring id; lab::xstring lname; lab::xstring fname; + public: Student(const lab::xstring& id, const lab::xstring& lname, const lab::xstring& fname) :id(id), lname(lname), fname(fname) {} + + inline constexpr const lab::xstring& get_id() const {return id;} + inline constexpr const lab::xstring& get_lname() const {return lname;} + inline constexpr const lab::xstring& get_fname() const {return fname;} }; #endif /* STUDENT_H */ diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -27,29 +27,29 @@ class xstring xstring& operator+= (const xstring& s); xstring& operator+= (const char *s); xstring& operator+= (char c); - constexpr bool operator== (const xstring& s) const + inline constexpr bool operator== (const xstring& s) const {return std::strcmp(this->str, s.str) == 0;} - constexpr bool operator== (const char *s) const + inline constexpr bool operator== (const char *s) const {return std::strcmp(this->str, s) == 0;} - constexpr bool operator!= (const xstring& s) const + inline constexpr bool operator!= (const xstring& s) const {return std::strcmp(this->str, s.str) != 0;} - constexpr bool operator!= (const char *s) const + inline constexpr bool operator!= (const char *s) const {return std::strcmp(this->str, s) != 0;} - constexpr bool operator< (const xstring& s) const + inline constexpr bool operator< (const xstring& s) const {return std::strcmp(this->str, s.str) < 0;} - constexpr bool operator< (const char *s) const + inline constexpr bool operator< (const char *s) const {return std::strcmp(this->str, s) < 0;} - constexpr bool operator<= (const xstring& s) const + inline constexpr bool operator<= (const xstring& s) const {return std::strcmp(this->str, s.str) <= 0;} - constexpr bool operator<= (const char *s) const + inline constexpr bool operator<= (const char *s) const {return std::strcmp(this->str, s) <= 0;} - constexpr bool operator> (const xstring& s) const + inline constexpr bool operator> (const xstring& s) const {return std::strcmp(this->str, s.str) > 0;} - constexpr bool operator> (const char *s) const + inline constexpr bool operator> (const char *s) const {return std::strcmp(this->str, s) > 0;} - constexpr bool operator>= (const xstring& s) const + inline constexpr bool operator>= (const xstring& s) const {return std::strcmp(this->str, s.str) >= 0;} - constexpr bool operator>= (const char *s) const + inline constexpr bool operator>= (const char *s) const {return std::strcmp(this->str, s) >= 0;} char& operator[] (std::size_t i) const; friend std::ostream& operator<< (std::ostream& stream, const xstring& s); @@ -66,16 +66,18 @@ class xstring void clear(); bool find(const xstring& s) const; bool find(const char *s) const; - constexpr char *cstr() const {return str;} - constexpr char& front() const {return str[0];} - constexpr char& back() const {return (!this->empty()) ? str[len-1] : str[0];}; - constexpr bool empty() const {return len == 0;} - constexpr std::size_t length() const {return std::strlen(str);} + inline constexpr char *cstr() const {return str;} + inline constexpr char& front() const {return str[0];} + inline constexpr char& back() const + {return (!this->empty()) ? str[len-1] : str[0];}; + inline constexpr bool empty() const {return len == 0;} + inline constexpr std::size_t length() const {return std::strlen(str);} private: void resize(std::size_t n); char *conv(const char *s) const; - constexpr bool strempty(const char *s) const {return std::strlen(s) == 0;} + inline constexpr bool strempty(const char *s) const + {return std::strlen(s) == 0;} }; std::istream& getline(std::istream& stream, xstring& s, char delim = '\n'); @@ -102,17 +104,17 @@ to_xstr(const char *fs, T val) template<typename T> constexpr const char * getformat() { - if constexpr (std::is_same<T, short>::value) return "%hi"; - if constexpr (std::is_same<T, int>::value) return "%d"; - if constexpr (std::is_same<T, long>::value) return "%ld"; - if constexpr (std::is_same<T, long long>::value) return "%lld"; - if constexpr (std::is_same<T, unsigned short>::value) return "%hu"; - if constexpr (std::is_same<T, unsigned int>::value) return "%u"; - if constexpr (std::is_same<T, unsigned long>::value) return "%lu"; - if constexpr (std::is_same<T, unsigned long>::value) return "%llu"; - if constexpr (std::is_same<T, float>::value) return "%f"; - if constexpr (std::is_same<T, double>::value) return "%f"; - if constexpr (std::is_same<T, long double>::value) return "%Lf"; + if constexpr (std::is_same_v<T, short>) return "%hi"; + if constexpr (std::is_same_v<T, int>) return "%d"; + if constexpr (std::is_same_v<T, long>) return "%ld"; + if constexpr (std::is_same_v<T, long long>) return "%lld"; + if constexpr (std::is_same_v<T, unsigned short>) return "%hu"; + if constexpr (std::is_same_v<T, unsigned int>) return "%u"; + if constexpr (std::is_same_v<T, unsigned long>) return "%lu"; + if constexpr (std::is_same_v<T, unsigned long>) return "%llu"; + if constexpr (std::is_same_v<T, float>) return "%f"; + if constexpr (std::is_same_v<T, double>) return "%f"; + if constexpr (std::is_same_v<T, long double>) return "%Lf"; } }