Advertisement
Korotkodul

logic_logic.h

Mar 16th, 2025
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #ifndef LOGIC_H
  2. #define LOGIC_H 1
  3.  
  4. #include <functional>
  5. #include <vector>
  6.  
  7.  
  8. namespace Logic {
  9.  
  10. class Element;
  11. class Source;
  12. class Operation;
  13. struct Connection;
  14.  
  15.  
  16. enum class Out_state
  17. {
  18.   direct   = 0,
  19.   inverted = 1,
  20. };
  21.  
  22.  
  23. // represents a pair of element and state of inverted property
  24. struct ElemInput
  25. {
  26.   ElemInput () = delete;
  27.   ElemInput (const ElemInput& input) = default;
  28.  
  29.   ElemInput (const Element& input_elem, bool inverted)
  30.     : elem { &input_elem }
  31.     , inv { inverted }
  32.   {}
  33.  
  34.   ElemInput& operator= (const ElemInput& input) = default;
  35.  
  36.   operator bool () const;
  37.  
  38.   const Element* elem;
  39.   bool inv;
  40. };
  41.  
  42.  
  43. using Logic_callback = std::function<void(const Element&)>;
  44.  
  45. using Input_container  = std::vector<ElemInput>;
  46. using Output_container = std::vector<Element*>;
  47.  
  48.  
  49. // abstract logic element
  50. class Element
  51. {
  52. public:
  53.   explicit Element (Out_state st = Out_state::direct, Logic_callback f = nullptr)
  54.     : inverted_out { st == Out_state::inverted }
  55.     , out { inverted_out }
  56.     , cb { f }
  57.   {}
  58.  
  59.   operator bool () const  { return out; }
  60.   bool inverted () const  { return inverted_out; }
  61.   const Output_container& get_outputs () const  { return outputs; }
  62.  
  63.   void set_callback (Logic_callback f)  { cb = f; }
  64.  
  65. protected:
  66.   void set (bool value);
  67.   // abstract method, set() should be called in calc()
  68.   virtual void calc () = 0;
  69.  
  70. private:
  71.   bool inverted_out { false };
  72.   bool out { false };
  73.   Logic_callback cb;
  74.  
  75.   // connections
  76.   Output_container outputs;
  77.  
  78.   friend Element& operator>> (Element& lhs, Operation& rhs);
  79.   friend Element& operator>> (Element& lhs, Connection rhs);
  80.   friend void check_loop (const Element& loop, const Element& elem);
  81. };
  82.  
  83.  
  84. class Source : public Element
  85. {
  86. public:
  87.   explicit Source (Out_state st = Out_state::direct, Logic_callback f = nullptr)
  88.     : Element{ st, f }
  89.   {}
  90.  
  91.   Source& operator= (bool value)  { set(value); return *this; }
  92.  
  93. protected:
  94.   virtual void calc () override;
  95. };
  96.  
  97.  
  98. // must be used only for connection operation
  99. struct Connection
  100. {
  101.   Connection () = delete;
  102.   Connection (const Connection&) = default;
  103.  
  104.   // constructor without "explicit" keyword
  105.   Connection (Operation& oper, bool inv = false)
  106.     : oper_elem { oper }
  107.     , inverted { inv }
  108.   {}
  109.  
  110.   Connection& operator= (const Connection&) = delete;
  111.  
  112.   Connection& operator~ ()
  113.   {
  114.     inverted = !inverted;
  115.     return *this;
  116.   }
  117.  
  118.   Operation& oper_elem;
  119.   bool inverted;
  120. };
  121.  
  122. class Operation : public Element
  123. {
  124. public:
  125.   explicit Operation (Out_state st = Out_state::direct, Logic_callback f = nullptr)
  126.     : Element{ st, f }
  127.   {}
  128.  
  129.   const Input_container& get_inputs () const  { return inputs; }
  130.  
  131.   Connection operator~ ()  { return Connection{ *this, true }; }
  132.  
  133. private:
  134.   Input_container inputs;
  135.  
  136.   friend Element& operator>> (Element& lhs, Operation& rhs);
  137.   friend Element& operator>> (Element& lhs, Connection rhs);
  138. };
  139.  
  140.  
  141. Element& operator>> (Element& lhs, Operation& rhs);
  142. Element& operator>> (Element& lhs, Connection rhs);
  143. void check_loop (const Element& loop, const Element& elem);
  144.  
  145.  
  146. class And : public Operation
  147. {
  148. public:
  149.   explicit And (Out_state out_inverted = Out_state::direct, Logic_callback f = nullptr)
  150.     : Operation{ out_inverted, f }
  151.   {}
  152.  
  153. protected:
  154.   virtual void calc () override;
  155. };
  156.  
  157. class Or : public Operation
  158. {
  159. public:
  160.   explicit Or (Out_state st = Out_state::direct, Logic_callback f = nullptr)
  161.     : Operation{ st, f }
  162.   {}
  163.  
  164. protected:
  165.   virtual void calc () override;
  166. };
  167.  
  168. class Xor : public Operation
  169. {
  170. public:
  171.   explicit Xor (Out_state out_inverted = Out_state::direct, Logic_callback f = nullptr)
  172.     : Operation{ out_inverted, f }
  173.   {}
  174.  
  175. protected:
  176.   virtual void calc () override;
  177. };
  178.  
  179. } // namespace Logic
  180.  
  181. #endif // LOGIC_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement