Commit b563704a authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved context interface

parent 6cf55ee7
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -11,13 +11,8 @@

namespace signaltk {

Status stack_add(
    Context* ctx,
    std::unique_ptr<ElementConfig> config) {
  ctx->elements.emplace(ElementRef {
    .config = std::move(config)
  });

Status Context::element_add(ElementRef elem) {
  elements.emplace(std::move(elem));
  return Status::OK;
}

+13 −11
Original line number Diff line number Diff line
@@ -15,20 +15,22 @@

namespace signaltk {

struct Context {
  std::stack<ElementRef> elements;
  Layer frame;
};
class Context {
public:

  template <typename T>
Status stack_head_config(Context* ctx, T** head);
  Status element_get(T** elem);

  template <typename T>
Status stack_head_config(const Context& ctx, T const** head);
  Status element_get(T const** elem) const;

  Status element_add(ElementRef elem);

Status stack_add(
    Context* ctx,
    std::unique_ptr<ElementConfig> config);
  std::unique_ptr<Layer> frame;

protected:
  std::stack<ElementRef> elements;
};

} // namespace signaltk

+6 −6
Original line number Diff line number Diff line
@@ -12,22 +12,22 @@
namespace signaltk {

template <typename T>
Status stack_head_config(Context* ctx, T** head) {
  if (ctx->elements.empty()) {
Status Context::element_get(T** elem) {
  if (elements.empty()) {
    return Status::ERROR_INVALID_ELEM;
  }

  *head = static_cast<T*>(ctx->elements.top().config.get());
  *elem = static_cast<T*>(elements.top().get());
  return OK;
}

template <typename T>
Status stack_head_config(const Context& ctx, T const** head) {
  if (ctx.elements.empty()) {
Status Context::element_get(T const** elem) const {
  if (elements.empty()) {
    return Status::ERROR_INVALID_ELEM;
  }

  *head = static_cast<const T*>(ctx.elements.top().config.get());
  *elem = static_cast<const T*>(elements.top().get());
  return OK;
}

+7 −5
Original line number Diff line number Diff line
@@ -12,13 +12,15 @@

namespace signaltk {

struct ElementConfig {
  virtual ~ElementConfig() = default;
};
struct Element {

  virtual ~Element() = default;

  //virtual render(Layer* frame) = 0;

struct ElementRef {
  std::unique_ptr<ElementConfig> config;
};

using ElementRef = std::unique_ptr<Element>;

} // namespace signaltk
+14 −14
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ AxisDefinition::AxisDefinition() :

Status plot_axis_add(Context* ctx, AxisPosition pos) {
  PlotConfig* elem;
  if (auto rc = stack_head_config(ctx, &elem); rc) {
  if (auto rc = ctx->element_get(&elem); rc) {
    return rc;
  }

@@ -52,7 +52,7 @@ Status plot_axis_add(Context* ctx, AxisPosition pos) {

Status plot_axis_addtick(Context* ctx, float offset) {
  PlotConfig* elem;
  if (auto rc = stack_head_config(ctx, &elem); rc) {
  if (auto rc = ctx->element_get(&elem); rc) {
    return rc;
  }

@@ -63,7 +63,7 @@ Status plot_axis_addtick(Context* ctx, float offset) {

Status plot_axis_addlabel(Context* ctx, float offset, const char* label) {
  PlotConfig* elem;
  if (auto rc = stack_head_config(ctx, &elem); rc) {
  if (auto rc = ctx->element_get(&elem); rc) {
    return rc;
  }

@@ -184,7 +184,7 @@ Status plot_render_axis_horizontal(

Status plot_render_axis(Context* ctx, int i) {
  const PlotConfig* elem;
  if (auto rc = stack_head_config(*ctx, &elem); rc) {
  if (auto rc = ctx->element_get(&elem); rc) {
    return rc;
  }

@@ -200,32 +200,32 @@ Status plot_render_axis(Context* ctx, int i) {
          *axis,
          padding,
          padding,
          ctx->frame.height - padding,
          &ctx->frame);
          ctx->frame->height - padding,
          ctx->frame.get());
      break;
    case AxisPosition::RIGHT:
      rc = plot_render_axis_vertical(
          *axis,
          ctx->frame.width - padding,
          ctx->frame->width - padding,
          padding,
          ctx->frame.height - padding,
          &ctx->frame);
          ctx->frame->height - padding,
          ctx->frame.get());
      break;
    case AxisPosition::TOP:
      rc = plot_render_axis_horizontal(
          *axis,
          padding,
          padding,
          ctx->frame.width - padding,
          &ctx->frame);
          ctx->frame->width - padding,
          ctx->frame.get());
      break;
    case AxisPosition::BOTTOM:
      rc = plot_render_axis_horizontal(
          *axis,
          ctx->frame.height - padding,
          ctx->frame->height - padding,
          padding,
          ctx->frame.width - padding,
          &ctx->frame);
          ctx->frame->width - padding,
          ctx->frame.get());
      break;
  }

Loading