c_cpp 反应式编程系统的原型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 反应式编程系统的原型相关的知识,希望对你有一定的参考价值。

#include <iostream>

//macro used for dynamically retrieving properties from child classes
#define CHILD_GET(var, type, prop) (reinterpret_cast<type*>(&var)->prop)

//example for reaction->event based programming

enum EventType
{
  EventType_null,
  EventType_Add,
  EventType_Number,
  EventType_Pair
};


//base event class
class Event
{
private:
  EventType _type;
public:
  Event(EventType type = EventType_null): _type(type)
  {
    
  }
  virtual ~Event()
  {
    
  }
  
  void setType(EventType type)
  {
    _type = type;
  }
  
  EventType getType() const
  {
    return _type;
  }
  
  static Event makeNull()
  {
    Event n;
    return n;
  }
};


//event number class
class EventNum : public Event
{
private:
  double _value;
public:
  EventNum(double value = 0): _value(value)
  {
    setType(EventType_Number);
  }
  ~EventNum()
  {}
  
  double& get()
  {
    return _value;
  }
};

//event add class
//doesn't hold data but functions as a convenience class
class EventAdd : public Event
{
public:
 EventAdd()
 {
   setType(EventType_Add);
 }
 ~EventAdd(){}
 
};

class EventPair: public Event
{
public:
  Event _first;
  Event _second;
public:
  EventPair(Event first, Event second):
    _first(first),
    _second(second) 
    {}
  ~EventPair(){}
  
  static EventPair make(const Event& first, const Event& second)
  {
    EventPair newpair(first, second);
    return newpair;
  }
};



Event operator% (const Event& left, const Event& right)
{
  switch(left.getType())
  {
    case EventType_Number:
      switch(right.getType())
      {
        case EventType_Number:
           return EventPair::make(left, right);
           break;
        case EventType_Add:
           break;
      }
      break;
    case EventType_Add:
      switch(right.getType())
      {
        case EventType_Number:
           break;
        case EventType_Add:
           break;
      }
      break;
    default:
     return left;
  }
}

int main() {
  EventNum foo = 1;
  EventNum doo = 3;
  Event reacted = foo % doo;
  std::cout << CHILD_GET(reacted, EventPair, _first).getType() << "\n";
}

以上是关于c_cpp 反应式编程系统的原型的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp cpp中的新事件 - 反应编程

c_cpp c中自我评估型系统的原型。

c_cpp 这是数据块系统的原型,以动态二进制格式存储数据

c_cpp 可评估语言的第一个原型

c_cpp C中不可变数据框架的原型

c_cpp Protobuf-Mutator实现的原型