Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // opus.ru Homework Lecture No.1
- // Creating and invoking class and method
- // compile on wandbox :: g++ prog.cc -Wall -Wextra -std=gnu++17
- //
- #include <iostream>
- #include <map>
- #include <string>
- #include <tuple>
- #define BOX_LOG
- enum class Tag {
- main,
- db,
- othermethod,
- othermethod2
- };
- class Logger
- {
- Tag t;
- public:
- // variables common to the class
- std::string brand;
- float cost;
- // creates logger instance
- #if defined(BOX_LOG)
- static Logger& Instance(Tag t) {
- static std::map<Tag, Logger> instance;
- auto i = instance.find(t);
- if (i == instance.end()) {
- bool b;
- std::tie(i, b) = instance.emplace(std::make_pair(t, Logger{t}));
- }
- return i->second;
- }
- #else
- static Logger& Instance() {
- static Logger instance;
- return instance;
- }
- #endif
- // function is information
- void info(const std::string &message) const {
- std::cout << "info: [" << int(t) << "] " << message << std::endl;
- }
- // function is warning
- void warning(const std::string &message) const {
- std::cout << "warning: [" << int(t) << "] " << message << std::endl;
- }
- // default constructor
- Logger() = default;
- // Parametrized constructor
- Logger(std::string brand, float cost);
- // add the class to the output print operator
- friend std::ostream& operator << (std::ostream& os, const Logger& log) {
- return os << "[brand:\"" << log.brand << "\" cost:" << log.cost << ']';
- }
- // default destructor
- ~Logger() = default;
- #ifndef BOX_LOG
- Logger(const Logger& root) = delete;
- Logger& operator=(const Logger&) = delete;
- Logger(Logger&& root) = delete;
- Logger& operator=(Logger&&) = delete;
- #endif
- private:
- Logger(Tag t_) : t(t_) {
- }
- };
- int main() {
- // Create a logger object with values
- // and print them
- //
- Logger cheese;
- cheese.brand = "jarlsberg";
- cheese.cost = 3.33;
- std::cout << "data_set = " << cheese<< '\n';
- cheese.brand = "edam";
- cheese.cost = 9.41;
- std::cout << "data_set = " << cheese<< '\n';
- // Create a logger object and invoke each method
- //
- #if defined(BOX_LOG)
- Logger::Instance(Tag::main).info("info No.1");
- Logger::Instance(Tag::db).warning("warning No.1");
- Logger& logger = Logger::Instance(Tag::othermethod);
- Logger& logger3 = Logger::Instance(Tag::othermethod2);
- #else
- Logger::Instance().info("info No.1");
- Logger::Instance().warning("warning No.1");
- Logger& logger = Logger::Instance();
- Logger& logger3 = Logger::Instance();
- #endif
- logger.info("info No.2");
- logger.warning("warning No.2");
- // start a second instance of the logger which is a copy and has same Tag
- //
- Logger& logger2 = logger;
- logger2.info("info no.3");
- logger2.warning("warning no.3");
- // write to log which is another instance and different tag with (BOX_LOG)
- //
- logger3.warning("warning no.3");
- // Delete the class objects and instances we used
- //
- (&logger)->~Logger();
- (&logger2)->~Logger();
- (&logger3)->~Logger();
- (&cheese)->~Logger();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement