Advertisement
aircampro

simple public class example

Jun 17th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. // opus.ru Homework Lecture No.1
  2. // Creating and invoking class and method
  3. // compile on wandbox :: g++ prog.cc -Wall -Wextra -std=gnu++17
  4. //
  5. #include <iostream>
  6. #include <map>
  7. #include <string>
  8. #include <tuple>
  9.  
  10. #define BOX_LOG
  11.  
  12. enum class Tag {
  13.     main,
  14.     db,
  15.     othermethod,
  16.     othermethod2
  17. };
  18.  
  19. class Logger
  20. {
  21.     Tag t;
  22. public:
  23.     // variables common to the class
  24.     std::string brand;
  25.     float       cost;
  26.    
  27.     // creates logger instance
  28. #if defined(BOX_LOG)
  29.     static Logger& Instance(Tag t) {
  30.         static std::map<Tag, Logger> instance;
  31.         auto i = instance.find(t);
  32.         if (i == instance.end()) {
  33.             bool b;
  34.             std::tie(i, b) = instance.emplace(std::make_pair(t, Logger{t}));
  35.         }
  36.         return i->second;
  37.     }
  38. #else
  39.     static Logger& Instance() {
  40.         static Logger instance;
  41.         return instance;
  42.     }
  43. #endif
  44.    
  45.     // function is information
  46.     void info(const std::string &message) const {
  47.         std::cout << "info: [" << int(t) << "] " << message << std::endl;
  48.     }
  49.  
  50.     // function is warning
  51.     void warning(const std::string &message) const {
  52.         std::cout << "warning: [" << int(t) << "] " << message << std::endl;
  53.     }
  54.  
  55.     // default constructor
  56.     Logger() = default;
  57.    
  58.     // Parametrized constructor
  59.     Logger(std::string brand, float cost);
  60.    
  61.     // add the class to the output print operator
  62.     friend std::ostream& operator << (std::ostream& os, const Logger& log) {
  63.         return os << "[brand:\"" << log.brand << "\" cost:" << log.cost << ']';
  64.     }
  65.    
  66.     // default destructor
  67.     ~Logger() = default;
  68. #ifndef BOX_LOG
  69.     Logger(const Logger& root) = delete;
  70.     Logger& operator=(const Logger&) = delete;
  71.     Logger(Logger&& root) = delete;
  72.     Logger& operator=(Logger&&) = delete;
  73. #endif
  74.  
  75. private:
  76.     Logger(Tag t_) : t(t_) {
  77.     }
  78.    
  79. };
  80.  
  81. int main() {
  82.     // Create a logger object with values
  83.     // and print them
  84.     //
  85.     Logger cheese;
  86.     cheese.brand = "jarlsberg";
  87.     cheese.cost = 3.33;
  88.     std::cout << "data_set = " << cheese<< '\n';
  89.     cheese.brand = "edam";
  90.     cheese.cost = 9.41;
  91.     std::cout << "data_set = " << cheese<< '\n';
  92.  
  93.     // Create a logger object and invoke each method
  94.     // 
  95. #if defined(BOX_LOG)
  96.     Logger::Instance(Tag::main).info("info No.1");
  97.     Logger::Instance(Tag::db).warning("warning No.1");
  98.  
  99.     Logger& logger = Logger::Instance(Tag::othermethod);
  100.     Logger& logger3 = Logger::Instance(Tag::othermethod2);
  101. #else
  102.     Logger::Instance().info("info No.1");
  103.     Logger::Instance().warning("warning No.1");
  104.  
  105.     Logger& logger = Logger::Instance();
  106.     Logger& logger3 = Logger::Instance();
  107. #endif
  108.     logger.info("info No.2");
  109.     logger.warning("warning No.2");
  110.  
  111.  
  112.     // start a second instance of the logger which is a copy and has same Tag
  113.     //
  114.     Logger& logger2 = logger;
  115.     logger2.info("info no.3");
  116.     logger2.warning("warning no.3");
  117.    
  118.     // write to log which is another instance and different tag with (BOX_LOG)
  119.     //
  120.     logger3.warning("warning no.3");
  121.    
  122.     // Delete the class objects and instances we used
  123.     //
  124.     (&logger)->~Logger();
  125.     (&logger2)->~Logger();
  126.     (&logger3)->~Logger();
  127.     (&cheese)->~Logger();
  128.    
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement