fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <tuple>
  4. using namespace std;
  5.  
  6. /*********************************************************************
  7.  * Implementation for utility function "PrintEachInTuple" begin here
  8.  */
  9. namespace detail
  10. {
  11. template<int... Is>
  12. struct seq { };
  13.  
  14. template<int N, int... Is>
  15. struct gen_seq : gen_seq<N-1, N-1, Is...> { };
  16.  
  17. template<int... Is>
  18. struct gen_seq<0, Is...> : seq<Is...> { };
  19.  
  20. template<typename T, typename F, int... Is>
  21. void for_each(T&& t, F f, seq<Is...>)
  22. {
  23. auto l = { (f(std::get<Is>(t)), 0)... };
  24. }
  25. }
  26.  
  27. template<typename... Ts, typename F>
  28. void PrintEachInTuple(std::tuple<Ts...> const& t, F f)
  29. {
  30. detail::for_each(t, f, detail::gen_seq<sizeof...(Ts)>());
  31. }
  32.  
  33. struct print_functor
  34. {
  35. template<typename T>
  36. void operator () ( T&& t)
  37. {
  38. t.Print();
  39. }
  40. };
  41. /*
  42.  * Implementation for utility function "PrintEachInTuple" ends here
  43.  ********************************************************************/
  44.  
  45.  
  46. class DocumentStreamFactory;
  47.  
  48. /* Main class for writing stuff with. Only preamble writing implemented, yet */
  49. template<typename... Types>
  50. class DocumentStream
  51. {
  52. private:
  53. DocumentStream(Types... args)
  54. {
  55. tuple<Types...> t(args...);
  56. PrintEachInTuple(t, print_functor());
  57. }
  58.  
  59. friend class DocumentStreamFactory;
  60. };
  61.  
  62. /* Factory for DocumentStream objects */
  63. class DocumentStreamFactory
  64. {
  65. public:
  66. template<typename... Types>
  67. static DocumentStream<Types...> Create(Types... args)
  68. {
  69. return DocumentStream<Types...>(args...);
  70. }
  71.  
  72. class PreambleElement
  73. {
  74. void Print();
  75. };
  76.  
  77. class Title : public PreambleElement
  78. {
  79. public:
  80. Title() {};
  81. Title(const std::string& title) : title(title) {};
  82. void Print() const { std::cout << "\\title{\"" << title << "\"}" << endl; }
  83. private:
  84. std::string title;
  85. };
  86.  
  87. class Author : public PreambleElement
  88. {
  89. public:
  90. Author() {};
  91. Author(const std::string& author) : author(author) {};
  92. void Print() const { std::cout << "\\author{\"" << author << "\"}" << endl; }
  93. private:
  94. std::string author;
  95. };
  96.  
  97. class Date : public PreambleElement
  98. {
  99. public:
  100. Date() {};
  101. Date(int day, int month, int year) : day(day), month(month), year(year) {};
  102. static Date Today() { return Date(22, 1, 2014); } // TODO
  103.  
  104. void Print() const { std::cout << "\\date{\"" << day << ". " << month << ". " << year << "\"}" << endl; }
  105.  
  106. private:
  107. int day, month, year;
  108. };
  109. };
  110.  
  111.  
  112.  
  113. int main()
  114. {
  115. cout << "Preamble with title, author and date:" << endl;
  116. auto doc = DocumentStreamFactory::Create(DocumentStreamFactory::Title("Such title name"),
  117. DocumentStreamFactory::Author("Author name"),
  118. DocumentStreamFactory::Date::Today());
  119.  
  120. cout << endl;
  121.  
  122. cout << "Preamble with title and date:" << endl;
  123. auto doc_without_author = DocumentStreamFactory::Create(DocumentStreamFactory::Title("Such title name"),
  124. DocumentStreamFactory::Date::Today());
  125.  
  126. return 0;
  127. }
  128.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Preamble with title, author and date:
\title{"Such title name"}
\author{"Author name"}
\date{"22. 1. 2014"}

Preamble with title and date:
\title{"Such title name"}
\date{"22. 1. 2014"}