Skip to content

Instantly share code, notes, and snippets.

@jmlvanre
Last active August 29, 2015 14:07
Show Gist options
  • Save jmlvanre/41f75892866e17b193ab to your computer and use it in GitHub Desktop.
Save jmlvanre/41f75892866e17b193ab to your computer and use it in GitHub Desktop.
class StreamImpl : public std::enable_shared_from_this {
  public:
  virtual ~StreamImpl() {}
  virtual Future<size_t> write(const std::string& data) = 0;
  private:
  std::shared_ptr<StreamImpl> self;
};

class SocketImpl : public StreamImpl {
  public:
  virtual ~SocketImpl() {}
  virtual Future<size_t> write(const std::string& data) override {...}
  private:
  SocketImpl(const Node& _node) : node(node) {...}
  const Node node;
  friend class Socket;
};

class Stream {
  public:
  virtual ~Stream() {}
  Future<size_t> write(const std::string& _data) {
    return data->write(data);
  }
  protected:
  Stream(std::shared_ptr<StreamImpl>&& _data) : data(_data) {}
  std::shared_ptr<StreamImpl> data;
};

class Socket : public Stream {
  public:
  virtual ~Socket() {}
  private:
  Socket(const Node &node) : Stream(make_shared<SocketImpl>(node)) {}
  friend class SocketManager;
}

class SocketManager {
  Try<Socket> connect(const Node& node) {
    return Socket(node);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment