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

configure_measures -> expr_to_measures

parent a67ad449
Loading
Loading
Loading
Loading
+0 −19
Original line number Diff line number Diff line
@@ -222,25 +222,6 @@ ReturnCode configure_strings(

  return configure_vec<std::string>(bind(&configure_string, _1, _2), data)(prop);
}

ReturnCode configure_measures(
    const Expr* prop,
    std::vector<Measure>* measures) {
  if (plist::is_enum(prop, "csv")) {
    SeriesRef data;
    if (auto rc = parse_data_series_csv(prop, &data); !rc) {
      return rc;
    }

    for (auto v : series_to_float(*data)) {
      measures->emplace_back(from_user(v));
    }

    return OK;
  }

  return configure_vec<Measure>(bind(&configure_measure, _1, _2), measures)(prop);
}
*/
} // namespace fviz
+0 −32
Original line number Diff line number Diff line
@@ -29,38 +29,6 @@ ReturnCode parseEnum(
  *value = def->second;
  return ReturnCode::success();
}

template <typename T>
ParserFn configure_vec(ParseToFn<T> parser, std::vector<T>* values) {
  return [parser, values] (const plist::Property& prop) -> ReturnCode {
    switch (plist::is_list(prop)) {

      case true: {
        for (const auto& cld : *prop.next) {
          T val;
          if (auto rc = parser(cld, &val); !rc) {
            return rc;
          }

          values->emplace_back(val);
        }

        return OK;
      }

      case false: {
        T val;
        if (auto rc = parser(prop, &val); !rc) {
          return rc;
        }

        values->emplace_back(val);
        return OK;
      }

    }
  };
}
*/

} // namespace fviz
+7 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "config_helpers.h"
#include "sexpr_conv.h"
#include "utils/fileutil.h"
#include "utils/csv.h"
#include "utils/algo.h"
@@ -57,6 +57,12 @@ ReturnCode expr_to_measure_opt(
  return OK;
}

ReturnCode expr_to_measures(
    const Expr* expr,
    std::vector<Measure>* measures) {
  return expr_tov<Measure>(expr, bind(&expr_to_measure, _1, _2), measures);
}

ReturnCode expr_to_color(
    const Expr* expr,
    Color* value) {
+19 −0
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ namespace fviz {

using ExprConv = std::function<ReturnCode (const Expr*)>;

template <typename T>
using ExprConvTo = std::function<ReturnCode (const Expr*, T* to)>;

ReturnCode expr_to_string(
    const Expr* prop,
    std::string* value);
@@ -32,6 +35,10 @@ ReturnCode expr_to_measure_opt(
    const Expr* expr,
    std::optional<Measure>* value);

ReturnCode expr_to_measures(
    const Expr* expr,
    std::vector<Measure>* measures);

ReturnCode expr_to_color(
    const Expr* prop,
    Color* value);
@@ -40,5 +47,17 @@ ReturnCode expr_to_color_opt(
    const Expr* expr,
    std::optional<Color>* var);

template <typename T>
ReturnCode expr_tov(
    const Expr* expr,
    ExprConvTo<T> conv,
    std::vector<T>* values);

template <typename T>
ExprConv expr_tov_fn(
    ExprConvTo<T> conv,
    std::vector<T>* values);

} // namespace fviz

#include "sexpr_conv_impl.h"

core/sexpr_conv_impl.h

0 → 100644
+54 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

namespace fviz {

template <typename T>
ReturnCode expr_tov(
    const Expr* expr,
    ExprConvTo<T> conv,
    std::vector<T>* values) {
  if (!expr || !expr_is_list(expr)) {
    return ReturnCode::errorf(
        "EARG",
        "argument error; expected a list, got: $0",
        "..."); // FIXME
  }

  for (expr = expr_get_list(expr); expr; expr = expr_next(expr)) {
    T v;
    if (auto rc = conv(expr, &v); !rc) {
      return rc;
    }

    values->emplace_back(std::move(v));
  }

  return OK;
}

template <typename T>
ExprConv expr_tov_fn(
    ExprConvTo<T> conv,
    std::vector<T>* values) {
  return bind(
      &expr_tov<T>,
      std::placeholders::_1,
      conv,
      values);
}

} // namespace fviz