SHARE
TWEET

Untitled

a guest Mar 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // INTERFACES
  5. class Receive_handler {
  6. public:
  7.     virtual void receive(void* ptr, const std::size_t size) = 0;
  8. };
  9.  
  10. class Common_interface {
  11. public:
  12.     Common_interface(Receive_handler& receiver) : receiver_(receiver) {}
  13.     virtual void send(void* ptr, const std::size_t size) = 0;
  14.  
  15. protected:
  16.     Receive_handler& receiver_;
  17. };
  18.  
  19.  
  20. // IMPLEMENTATION
  21. class Uart: public Common_interface {
  22. public:
  23.     Uart(Receive_handler& receiver) : Common_interface(receiver) {}
  24.  
  25.     void send(void* ptr, const std::size_t size) override {
  26.         cout << "uart: " << size << endl;
  27.     }
  28.  
  29.     void set_new_data(void* ptr, const std::size_t size) {
  30.         receiver_.receive(ptr, size);
  31.     }
  32. };
  33.  
  34. class User : public Receive_handler {
  35. public:
  36.     User(Common_interface& sender) : sender_(sender) {}
  37.  
  38.     void receive(void* ptr, const std::size_t size) override {
  39.         cout << "user: " << size << endl;
  40.     }
  41.  
  42.     void do_work_with_send(void* ptr, const std::size_t size) {
  43.         sender_.send(ptr, size);
  44.     }
  45.  
  46. private:
  47.     Common_interface& sender_;
  48. };
  49.  
  50. extern Uart uart;
  51.  
  52. User user(uart);
  53. Uart uart(user);
  54.  
  55. int main() {
  56.     int buffer[2];
  57.     user.do_work_with_send(buffer, sizeof(buffer));
  58.  
  59.     int buffer2[5];
  60.     uart.set_new_data(buffer2, sizeof(buffer2));
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top