Skip to content

packages/engine/scram-node/src/expression/extern.h

The MEF facilities to call external functions in expressions.

Namespaces

Name
scram
scram::mef

Classes

Name
classscram::mef::ExternLibrary <br>The MEF construct to extend expressions with external libraries.
classscram::mef::ExternFunction< void > <br>Abstract base class for ExternFunction concrete types.
classscram::mef::ExternFunction <br>Extern function abstraction to be referenced by expressions.
classscram::mef::ExternExpression <br>Expression evaluating an extern function with expression arguments.

Source code

cpp
/*
 * Copyright (C) 2014-2018 Olzhas Rakhimov
 * Copyright (C) 2023 OpenPRA ORG Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include <boost/dll/shared_library.hpp>
#include <boost/exception/errinfo_nested_exception.hpp>
#include <boost/exception_ptr.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/system/system_error.hpp>

#include "src/element.h"
#include "src/error.h"
#include "src/expression.h"

namespace scram::mef {

class ExternLibrary : public Element, public Usage {
 public:
  static constexpr const char* kTypeString = "extern library";

  ExternLibrary(std::string name, std::string lib_path,
                const boost::filesystem::path& reference_dir, bool system,
                bool decorate);

  template <typename F>
  std::enable_if_t<std::is_function_v<F>, std::add_pointer_t<F>>
  get(const std::string& symbol) const {
    try {
      return lib_handle_.get<F>(symbol);
    } catch (const boost::system::system_error& err) {
      SCRAM_THROW(DLError(err.what()))
          << errinfo_value(symbol)
          << boost::errinfo_nested_exception(boost::current_exception());
    }
  }

 private:
  boost::dll::shared_library lib_handle_;  
};

template <typename R, typename... Args>
class ExternFunction;  // Forward declaration to specialize abstract base.

template <>
class ExternFunction<void> : public Element, public Usage {
 public:
  static constexpr const char* kTypeString = "extern function";

  using Element::Element;

  virtual ~ExternFunction() = default;

  virtual std::unique_ptr<Expression>
  apply(std::vector<Expression*> args) const = 0;
};

using ExternFunctionPtr = std::unique_ptr<ExternFunction<void>>;  
using ExternFunctionBase = ExternFunction<void>;  

template <typename R, typename... Args>
class ExternFunction : public ExternFunctionBase {
  static_assert(std::is_arithmetic_v<R>, "Numeric type functions only.");

  using Pointer = R (*)(Args...);  

 public:
  ExternFunction(std::string name, const std::string& symbol,
                 const ExternLibrary& library)
      : ExternFunctionBase(std::move(name)),
        fptr_(library.get<R(Args...)>(symbol)) {}

  R operator()(Args... args) const noexcept { return fptr_(args...); }

  std::unique_ptr<Expression>
  apply(std::vector<Expression*> args) const override;

 private:
  const Pointer fptr_;  
};

template <typename R, typename... Args>
class ExternExpression
    : public ExpressionFormula<ExternExpression<R, Args...>> {
 public:
  explicit ExternExpression(const ExternFunction<R, Args...>* extern_function,
                            std::vector<Expression*> args)
      : ExpressionFormula<ExternExpression>(std::move(args)),
        extern_function_(*extern_function) {
    if (Expression::args().size() != sizeof...(Args))
      SCRAM_THROW(
          ValidityError("The number of function arguments does not match."));
  }

  template <typename F>
  double Compute(F&& eval) noexcept {
    return Marshal(std::forward<F>(eval),
                   std::make_index_sequence<sizeof...(Args)>());
  }

 private:
  template <typename F, std::size_t... Is>
  double Marshal(F&& eval, std::index_sequence<Is...>) noexcept {
    return extern_function_(eval(Expression::args()[Is])...);
  }

  const ExternFunction<R, Args...>& extern_function_;  
};

template <typename R, typename... Args>
std::unique_ptr<Expression>
ExternFunction<R, Args...>::apply(std::vector<Expression*> args) const {
  return std::make_unique<ExternExpression<R, Args...>>(this, std::move(args));
}

}  // namespace scram::mef

Updated on 2025-11-11 at 16:51:08 +0000