Guest User

Untitled

a guest
Mar 18th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <vector>
  2. #include <memory>
  3. #include <iostream>
  4. #include <ostream>
  5.  
  6. class IConfig
  7. {
  8. public:
  9.     virtual ~IConfig() { }
  10.  
  11.     virtual std::ostream& output(std::ostream& o) const { return o; }
  12.  
  13.     friend std::ostream& operator<<(std::ostream& o, const IConfig &c)
  14.     {
  15.         return o << c.output(o);
  16.     }
  17. };
  18.  
  19. class MultiConfig : public IConfig
  20. {
  21. public:
  22.     virtual ~MultiConfig() { }
  23.  
  24.     virtual std::ostream& output(std::ostream& o) const
  25.     {
  26.         for (std::vector<std::string>::const_iterator cit = m_vals.begin();
  27.              cit != m_vals.end(); cit ++)
  28.              o << (*cit) << std::endl;
  29.         return o;
  30.     }
  31.  
  32. protected:
  33.     std::vector<std::string> m_vals;
  34. };
  35.  
  36. class SingleConfig : public IConfig
  37. {
  38. public:
  39.     virtual ~SingleConfig() { }
  40.  
  41.     virtual std::ostream& output(std::ostream& o) const
  42.     {
  43.         return o << m_val;
  44.     }
  45. protected:
  46.     std::string m_val;
  47. };
  48.  
  49. class IndexConfig : public IConfig
  50. {
  51. public:
  52.     virtual ~IndexConfig() { }
  53.  
  54.     virtual std::ostream& output(std::ostream& o) const
  55.     {
  56.         return o << m_val;
  57.     }
  58.  
  59. protected:
  60.     std::string m_val;
  61.     int m_index;
  62. };
  63.  
  64. class IpConfig : public SingleConfig
  65. {
  66. public:
  67.     IpConfig() { m_val = "8.8.8.8"; }
  68.     virtual ~IpConfig() { }
  69. };
  70.  
  71. using ConfigPtr = std::shared_ptr<IConfig>;
  72. using Configs = std::vector<ConfigPtr>;
  73.  
  74. int main(int argc, char* argv[])
  75. {
  76.     ConfigPtr c(new IpConfig());
  77.     Configs s;
  78.     s.push_back(c);
  79.     for (Configs::const_iterator cit = s.begin(); cit != s.end(); cit++)
  80.     std::cout << (**cit) << std::endl;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment