SHARE
TWEET

Untitled

a guest Apr 21st, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. enum class msg_count_status_t
  4.    {
  5.       undefined,
  6.       defined
  7.    };
  8.  
  9. struct basic_data_t
  10.    {
  11.       int to_extract_{};
  12.       int to_handle_{};
  13.  
  14.       int common_payload_{};
  15.    };
  16.  
  17. template<typename Basic_Data>
  18. class basic_data_holder_t
  19.    {
  20.    private :
  21.       Basic_Data data_;
  22.  
  23.    protected :
  24.       void set_to_extract(int v) { data_.to_extract_ = v; }
  25.       void set_to_handle(int v) { data_.to_handle_ = v; }
  26.  
  27.       void set_common_payload(int v) { data_.common_payload_ = v; }
  28.  
  29.       const auto & data() const { return data_; }
  30.  
  31.    public :
  32.       basic_data_holder_t() = default;
  33.  
  34.       basic_data_holder_t(Basic_Data data) : data_{std::move(data)} {}
  35.  
  36.       int to_extract() const { return data_.to_extract_; }
  37.       int to_handle() const { return data_.to_handle_; }
  38.  
  39.       int common_payload() const { return data_.common_payload_; }
  40.    };
  41.  
  42. template<typename Data, typename Derived>
  43. class basic_params_t : public basic_data_holder_t<Data>
  44.    {
  45.       using base_type = basic_data_holder_t<Data>;
  46.  
  47.    public :
  48.       using actual_type = Derived;
  49.       using data_type = Data;
  50.  
  51.    protected :
  52.       actual_type & self_reference()
  53.          { return static_cast<actual_type &>(*this); }
  54.  
  55.       decltype(auto) clone_as_defined()
  56.          {
  57.             return self_reference().template clone_if_necessary<
  58.                   msg_count_status_t::defined >();
  59.          }
  60.  
  61.    public :
  62.       basic_params_t() = default;
  63.  
  64.       basic_params_t(data_type data) : base_type{std::move(data)} {}
  65.  
  66.       decltype(auto) handle_all()
  67.          {
  68.             this->set_to_handle(0);
  69.             return clone_as_defined();
  70.          }
  71.  
  72.       decltype(auto) handle_n(int v)
  73.          {
  74.             this->set_to_handle(v);
  75.             return clone_as_defined();
  76.          }
  77.  
  78.       decltype(auto) extract_n(int v)
  79.          {
  80.             this->set_to_extract(v);
  81.             return clone_as_defined();
  82.          }
  83.  
  84.       actual_type & common_payload(int v)
  85.          {
  86.             this->set_common_payload(v);
  87.             return self_reference();
  88.          }
  89.       using base_type::common_payload;
  90.    };
  91.  
  92. struct receive_specific_data_t final : public basic_data_t
  93.    {
  94.       int receive_payload_{};
  95.  
  96.       receive_specific_data_t() = default;
  97.  
  98.       receive_specific_data_t(int v) : receive_payload_{v} {}
  99.    };
  100.  
  101. template< msg_count_status_t Msg_Count_Status >
  102. class receive_specific_params_t final
  103.    : public basic_params_t<
  104.          receive_specific_data_t,
  105.          receive_specific_params_t<Msg_Count_Status> >
  106.    {
  107.       using base_type = basic_params_t<
  108.             receive_specific_data_t,
  109.             receive_specific_params_t<Msg_Count_Status> >;
  110.  
  111.    public :
  112.       template<msg_count_status_t New_Msg_Count_Status>
  113.       std::enable_if_t<
  114.             New_Msg_Count_Status != Msg_Count_Status,
  115.             receive_specific_params_t<New_Msg_Count_Status> >
  116.       clone_if_necessary() const
  117.          {
  118.             return { this->data() };
  119.          }
  120.  
  121.       template<msg_count_status_t New_Msg_Count_Status>
  122.       std::enable_if_t<
  123.             New_Msg_Count_Status == Msg_Count_Status,
  124.             receive_specific_params_t& >
  125.       clone_if_necessary()
  126.          {
  127.             return *this;
  128.          }
  129.  
  130.       receive_specific_params_t(int receive_payload)
  131.          :  base_type{ typename base_type::data_type{receive_payload} }
  132.          {}
  133.  
  134.       receive_specific_params_t(typename base_type::data_type data)
  135.          :  base_type{ std::move(data) }
  136.          {}
  137.  
  138.       int receive_payload() const { return this->data().receive_payload_; }
  139.    };
  140.  
  141. receive_specific_params_t<msg_count_status_t::undefined>
  142. make_receive_params(int receive_payload)
  143.    {
  144.       return {receive_payload};
  145.    }
  146.  
  147. template<msg_count_status_t Msg_Count_Status>
  148. void do_something(const receive_specific_params_t<Msg_Count_Status> & params)
  149.    {
  150.       static_assert(
  151.             Msg_Count_Status == msg_count_status_t::defined,
  152.             "message count to be processed/extracted should be defined "
  153.             "by using handle_all()/handle_n()/extract_n() methods" );
  154.  
  155.       std::cout << "to_handle: " << params.to_handle() << std::endl;
  156.       std::cout << "to_extract: " << params.to_extract() << std::endl;
  157.       std::cout << "common_payload: " << params.common_payload() << std::endl;
  158.       std::cout << "receive_payload: " << params.receive_payload() << std::endl;
  159.    }
  160.  
  161. int main()
  162.    {
  163.       do_something( make_receive_params(42).handle_all().common_payload(13) );
  164.    }
  165.  
  166. // vim:ts=3:sts=3:sw=3:expandtab
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