mirror of https://github.com/ecmwf/eccodes.git
Added 4 more level-2 compiling accessors
This commit is contained in:
parent
6c506b3111
commit
d07769044f
|
@ -225,6 +225,9 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
|||
# We're indexing into a container...
|
||||
cpparray_access = array_access.ArrayAccess(literal.Literal(cpparg.name), cppright)
|
||||
return cpparray_access
|
||||
# .data() version causes issues
|
||||
#cppleft = literal.Literal(f"{cpparg.name}.data()")
|
||||
#return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright)
|
||||
|
||||
elif cppbinary_op.is_arithmetic():
|
||||
cpparg = arg_utils.to_cpparg(cppleft, self._conversion_data)
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
import utils.debug as debug
|
||||
import grib_accessor.grib_accessor_conversion_pack.conversion_pack_updates.base_conversion_pack_updates as base_conversion_pack_updates
|
||||
from code_object.arg import Arg
|
||||
from code_object_converter.conversion_pack.funcsig_mapping import FuncSigMapping
|
||||
from code_object_converter.conversion_pack.arg_indexes import ArgIndexes
|
||||
from code_object.funcsig import FuncSig
|
||||
from code_object.code_interface import NONE_VALUE
|
||||
|
||||
class StepHumanReadableConversionPackUpdates(base_conversion_pack_updates.BaseConversionPackUpdates):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
self._funcsig_mappings.extend([
|
||||
# template<typename T> static int unpack_helper(grib_accessor* a, T* val, size_t* len)
|
||||
FuncSigMapping(FuncSig("int", "get_step_human_readable", [Arg("grib_handle*", "h"), Arg("char*", "result"), Arg("size_t*", "length")]),
|
||||
FuncSig("GribStatus", "getStepHumanReadable", [NONE_VALUE, Arg("std::string&", "result"), NONE_VALUE]),
|
||||
ArgIndexes(cbuffer=1, clength=2, cpp_container=1)),
|
||||
])
|
|
@ -18,6 +18,7 @@ import code_object.cast_expression as cast_expression
|
|||
import code_object.macro_instantation as macro_instantation
|
||||
import code_object.struct_member_access as struct_member_access
|
||||
import code_object.array_access as array_access
|
||||
from code_object.arg import Arg
|
||||
from utils.string_funcs import strip_semicolon
|
||||
|
||||
from grib_accessor.grib_accessor_conversion_pack.grib_accessor_special_function_call_conversion import special_function_name_mapping
|
||||
|
@ -142,6 +143,11 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
|||
updated_cppfunction_call = self.validate_function_call_args(updated_cppfunction_call, mapping.cppfuncsig)
|
||||
debug.line("apply_special_function_call_conversions", f"Updated C++ function call=[{debug.as_debug_string(cppfunction_call)}] to [{debug.as_debug_string(updated_cppfunction_call)}]")
|
||||
return updated_cppfunction_call
|
||||
|
||||
if cppfunction_call.name == "eccGribGetStringLength":
|
||||
updated_args = [literal.Literal("AccessorName(name_)")]
|
||||
updated_args.append(cppfunction_call.args[1].operand) # strip leading & - so this arg is a unary_operation
|
||||
return function_call.FunctionCall(cppfunction_call.name, updated_args)
|
||||
|
||||
for cfuncname, cppfuncname in special_function_name_mapping.items():
|
||||
if cfunction_call.name == cfuncname:
|
||||
|
@ -150,6 +156,20 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
|||
else:
|
||||
return as_commented_out_code(cfunction_call, f"Removed call to {cfuncname}")
|
||||
|
||||
if cppfunction_call.name == "memcpy":
|
||||
updated_args = [arg for arg in cppfunction_call.args]
|
||||
if isinstance(updated_args[1], array_access.ArrayAccess):
|
||||
argname = arg_utils.extract_name(updated_args[1])
|
||||
assert argname
|
||||
argvalue = strip_semicolon(updated_args[1].index.as_string())
|
||||
updated_args[1] = binary_operation.BinaryOperation(literal.Literal(f"{argname}.data()"),
|
||||
"+",
|
||||
literal.Literal(argvalue))
|
||||
updated_cppfunction_call = function_call.FunctionCall("copyBuffer", updated_args)
|
||||
debug.line("apply_special_function_call_conversions", f"Updated memcpy: [{debug.as_debug_string(cppfunction_call)}]->[{debug.as_debug_string(updated_cppfunction_call)}]")
|
||||
return updated_cppfunction_call
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def validate_variable_declaration(self, cvariable_declaration, cppvariable_declaration):
|
||||
|
|
|
@ -7,7 +7,6 @@ special_function_name_mapping = {
|
|||
"fgets" : "fgetsFromString",
|
||||
"grib_context_log" : "gribLog",
|
||||
"codes_fopen" : "fopenFromString",
|
||||
"memcpy" : "copyBuffer",
|
||||
"strncpy" : "copyString",
|
||||
# Deleted functions
|
||||
"grib_context_free" : "",
|
||||
|
|
|
@ -81,4 +81,10 @@ grib_value_funcsig_mappings = [
|
|||
|
||||
FuncSigMapping( FuncSig("int", "grib_set_long_array_internal", [Arg("const grib_handle*", "h"), Arg("const char*", "name"), Arg("long*", "val"), Arg("size_t*", "length")]),
|
||||
FuncSig("GribStatus", "gribSetLong", [NONE_VALUE, Arg("AccessorName const&", "name"), Arg("std::vector<long> const&", "value")])),
|
||||
|
||||
FuncSigMapping( FuncSig("int", "grib_get_string_length", [Arg("const grib_handle*", "h"), Arg("const char*", "name"), Arg("size_t*", "size")]),
|
||||
FuncSig("GribStatus", "gribGetStringLength", [NONE_VALUE, Arg("AccessorName const&", "name"), Arg("size_t&", "size")])),
|
||||
|
||||
FuncSigMapping( FuncSig("int", "ecc__grib_get_string_length", [Arg("grib_accessor*", "a"), Arg("size_t*", "size")]),
|
||||
FuncSig("GribStatus", "eccGribGetStringLength", [Arg("AccessorName const&", "name"), Arg("size_t&", "size")])),
|
||||
]
|
||||
|
|
|
@ -46,15 +46,29 @@ int scanString(std::string buffer, size_t offset, std::string format, Args&&...
|
|||
|
||||
// Helper to support memcpy from a C buffer into a std::vector
|
||||
template<typename OUT_TYPE, typename IN_TYPE>
|
||||
void copyBuffer(std::vector<OUT_TYPE>& out, IN_TYPE* in, size_t numBytes)
|
||||
//void copyBuffer(std::vector<OUT_TYPE>& out, IN_TYPE const* in, size_t numBytes)
|
||||
void copyBuffer(OUT_TYPE& out, IN_TYPE const* in, size_t numBytes)
|
||||
{
|
||||
assert(sizeof(OUT_TYPE)==sizeof(IN_TYPE));
|
||||
|
||||
size_t numEntries = numBytes/sizeof(IN_TYPE);
|
||||
|
||||
std::copy(out.begin(), out.begin() + numEntries, in);
|
||||
|
||||
std::vector<IN_TYPE> inVec(in, in + numEntries);
|
||||
|
||||
// TODO: Check if back_inserter required
|
||||
std::copy(inVec.begin(), inVec.end(), out.begin());
|
||||
}
|
||||
|
||||
// Helper to support memcpy from a C buffer into a std::string
|
||||
/*
|
||||
void copyBuffer(std::string& out, char const* in, size_t numBytes)
|
||||
{
|
||||
size_t numEntries = numBytes/sizeof(char);
|
||||
|
||||
std::string inStr(in, in + numEntries);
|
||||
|
||||
// TODO: Check if back_inserter required
|
||||
std::copy(inStr.begin(), inStr.end(), out.begin());
|
||||
}*/
|
||||
|
||||
// Helper to replace strncpy
|
||||
void copyString(std::string& dest, std::string src, size_t count);
|
||||
|
||||
|
|
|
@ -248,4 +248,16 @@ GribStatus gribSetString(AccessorName const& name, std::string value)
|
|||
return GribStatus{ret};
|
||||
}
|
||||
|
||||
GribStatus gribGetStringLength(AccessorName const& name, size_t& size)
|
||||
{
|
||||
Assert(false);
|
||||
return GribStatus::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
GribStatus eccGribGetStringLength(AccessorName const& name, size_t& size)
|
||||
{
|
||||
Assert(false);
|
||||
return GribStatus::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -56,4 +56,8 @@ GribStatus gribSetArray(AccessorName const& name, std::vector<T> const& value)
|
|||
}
|
||||
}
|
||||
|
||||
// Stub functions (TO DO)
|
||||
GribStatus gribGetStringLength(AccessorName const& name, size_t& size);
|
||||
GribStatus eccGribGetStringLength(AccessorName const& name, size_t& size);
|
||||
|
||||
}
|
Loading…
Reference in New Issue