doc_view_acm.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Document/View sample for Boost.Signals2.
  2. // Expands on doc_view.cpp example by using automatic
  3. // connection management.
  4. //
  5. // Copyright Keith MacDonald 2005.
  6. // Copyright Frank Mori Hess 2009.
  7. //
  8. // Use, modification and
  9. // distribution is subject to the Boost Software License, Version
  10. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. // For more information, see http://www.boost.org
  13. #include <iostream>
  14. #include <string>
  15. #include <boost/bind.hpp>
  16. #include <boost/io/ios_state.hpp>
  17. #include <boost/signals2/signal.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. class Document
  20. {
  21. public:
  22. typedef boost::signals2::signal<void ()> signal_t;
  23. public:
  24. Document()
  25. {}
  26. /* connect a slot to the signal which will be emitted whenever
  27. text is appended to the document. */
  28. boost::signals2::connection connect(const signal_t::slot_type &subscriber)
  29. {
  30. return m_sig.connect(subscriber);
  31. }
  32. void append(const char* s)
  33. {
  34. m_text += s;
  35. m_sig();
  36. }
  37. const std::string& getText() const
  38. {
  39. return m_text;
  40. }
  41. private:
  42. signal_t m_sig;
  43. std::string m_text;
  44. };
  45. class TextView
  46. {
  47. public:
  48. // static factory function that sets up automatic connection tracking
  49. static boost::shared_ptr<TextView> create(Document& doc)
  50. {
  51. boost::shared_ptr<TextView> new_view(new TextView(doc));
  52. {
  53. typedef Document::signal_t::slot_type slot_type;
  54. slot_type myslot(&TextView::refresh, new_view.get());
  55. doc.connect(myslot.track(new_view));
  56. }
  57. return new_view;
  58. }
  59. void refresh() const
  60. {
  61. std::cout << "TextView: " << m_document.getText() << std::endl;
  62. }
  63. private:
  64. // private constructor to force use of static factory function
  65. TextView(Document &doc): m_document(doc)
  66. {}
  67. Document& m_document;
  68. };
  69. class HexView
  70. {
  71. public:
  72. // static factory function that sets up automatic connection tracking
  73. static boost::shared_ptr<HexView> create(Document& doc)
  74. {
  75. boost::shared_ptr<HexView> new_view(new HexView(doc));
  76. {
  77. typedef Document::signal_t::slot_type slot_type;
  78. slot_type myslot(&HexView::refresh, new_view.get());
  79. doc.connect(myslot.track(new_view));
  80. }
  81. return new_view;
  82. }
  83. void refresh() const
  84. {
  85. boost::io::ios_flags_saver ifs(std::cout);
  86. const std::string& s = m_document.getText();
  87. std::cout << "HexView:";
  88. for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
  89. std::cout << ' ' << std::hex << static_cast<int>(*it);
  90. std::cout << std::endl;
  91. }
  92. private:
  93. // private constructor to force use of static factory function
  94. HexView(Document& doc): m_document(doc)
  95. {}
  96. Document& m_document;
  97. };
  98. int main(int argc, char* argv[])
  99. {
  100. Document doc;
  101. boost::shared_ptr<TextView> v1 = TextView::create(doc);
  102. boost::shared_ptr<HexView> v2 = HexView::create(doc);
  103. doc.append(argc >= 2 ? argv[1] : "Hello world!");
  104. v2.reset(); // destroy the HexView, automatically disconnecting
  105. doc.append(" HexView should no longer be connected.");
  106. return 0;
  107. }