Skip to content

Commit

Permalink
Conversion of dumpers to C++ (#278)
Browse files Browse the repository at this point in the history
ECC-1996: Dumper hierarchy: Use C++ inheritance mechanism instead of hand-crafted virtual tables
  • Loading branch information
joobog authored Dec 18, 2024
1 parent fdb3e2a commit 546c166
Show file tree
Hide file tree
Showing 174 changed files with 10,603 additions and 11,330 deletions.
36 changes: 17 additions & 19 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#

include_directories(
"${CMAKE_CURRENT_SOURCE_DIR}/dumper"
"${CMAKE_CURRENT_SOURCE_DIR}/accessor"
"${CMAKE_CURRENT_SOURCE_DIR}/geo/iterator"
"${CMAKE_CURRENT_SOURCE_DIR}/geo/nearest"
Expand Down Expand Up @@ -273,23 +274,21 @@ list( APPEND eccodes_src_files
accessor/grib_accessor_class_reference_value_error.cc
grib_memory.cc
grib_buffer.cc
grib_dumper.cc
grib_dumper_class_serialize.cc
grib_dumper_class_debug.cc
grib_dumper_class_default.cc
grib_dumper_class_bufr_encode_C.cc
grib_dumper_class_bufr_encode_filter.cc
grib_dumper_class_bufr_encode_fortran.cc
grib_dumper_class_bufr_encode_python.cc
grib_dumper_class_bufr_decode_C.cc
grib_dumper_class_bufr_decode_filter.cc
grib_dumper_class_bufr_decode_fortran.cc
grib_dumper_class_bufr_decode_python.cc
grib_dumper_class_bufr_simple.cc
grib_dumper_class_json.cc
grib_dumper_class_grib_encode_C.cc
grib_dumper_class_wmo.cc
grib_dumper_class.cc
dumper/grib_dumper_class_serialize.cc
dumper/grib_dumper_class_debug.cc
dumper/grib_dumper_class_default.cc
dumper/grib_dumper_class_bufr_encode_C.cc
dumper/grib_dumper_class_bufr_encode_filter.cc
dumper/grib_dumper_class_bufr_encode_fortran.cc
dumper/grib_dumper_class_bufr_encode_python.cc
dumper/grib_dumper_class_bufr_decode_C.cc
dumper/grib_dumper_class_bufr_decode_filter.cc
dumper/grib_dumper_class_bufr_decode_fortran.cc
dumper/grib_dumper_class_bufr_decode_python.cc
dumper/grib_dumper_class_bufr_simple.cc
dumper/grib_dumper_class_json.cc
dumper/grib_dumper_class_grib_encode_C.cc
dumper/grib_dumper_class_wmo.cc
grib_context.cc
grib_date.cc
grib_fieldset.cc
Expand Down Expand Up @@ -368,8 +367,7 @@ list( APPEND eccodes_src_files
grib_accessor_factory.h
grib_api_internal.h
eccodes_prototypes.h
grib_dumper_class.h
grib_dumper_factory.h
grib_dumper_factory.cc
grib_iterator_factory.cc
grib_nearest_factory.cc
grib_yacc.h
Expand Down
1 change: 0 additions & 1 deletion src/accessor/grib_accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
***************************************************************************/

#include "grib_accessor.h"
#include <iostream>

// Note: A fast cut-down version of strcmp which does NOT return -1
// 0 means input strings are equal and 1 means not equal
Expand Down
226 changes: 137 additions & 89 deletions src/accessor/grib_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,154 @@
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#pragma once

#include "grib_api_internal.h"
#include "grib_value.h"
#include "dumper/grib_dumper.h"

class grib_accessor
{
namespace eccodes {
class Dumper;
}

class grib_accessor {
public:
grib_accessor() :
context_(nullptr), name_(nullptr), class_name_(nullptr), name_space_(nullptr), h_(nullptr),
creator_(nullptr), length_(0), offset_(0), parent_(nullptr), next_(nullptr), previous_(nullptr),
flags_(0), sub_section_(nullptr), dirty_(0), same_(nullptr), loop_(0), vvalue_(nullptr), set_(nullptr), parent_as_attribute_(nullptr) {}
grib_accessor()
: context_(nullptr),
name_(nullptr),
class_name_(nullptr),
name_space_(nullptr),
h_(nullptr),
creator_(nullptr),
length_(0),
offset_(0),
parent_(nullptr),
next_(nullptr),
previous_(nullptr),
flags_(0),
sub_section_(nullptr),
dirty_(0),
same_(nullptr),
loop_(0),
vvalue_(nullptr),
set_(nullptr),
parent_as_attribute_(nullptr) {}

grib_accessor(const char* name) :
context_(nullptr), name_(name), class_name_(nullptr), name_space_(nullptr), h_(nullptr),
creator_(nullptr), length_(0), offset_(0), parent_(nullptr), next_(nullptr), previous_(nullptr),
flags_(0), sub_section_(nullptr), dirty_(0), same_(nullptr), loop_(0), vvalue_(nullptr), set_(nullptr), parent_as_attribute_(nullptr) {}
virtual ~grib_accessor() {}
grib_accessor(const char *name)
: context_(nullptr),
name_(name),
class_name_(nullptr),
name_space_(nullptr),
h_(nullptr),
creator_(nullptr),
length_(0),
offset_(0),
parent_(nullptr),
next_(nullptr),
previous_(nullptr),
flags_(0),
sub_section_(nullptr),
dirty_(0),
same_(nullptr),
loop_(0),
vvalue_(nullptr),
set_(nullptr),
parent_as_attribute_(nullptr) {}
virtual ~grib_accessor() {}

virtual void init_accessor(const long, grib_arguments*) = 0;
virtual void dump(grib_dumper* f) = 0;
virtual int pack_missing() = 0;
// virtual int grib_pack_zero(grib_accessor* a) = 0;
virtual int is_missing_internal() = 0;
virtual int pack_double(const double* v, size_t* len) = 0;
virtual int pack_float(const float* v, size_t* len) = 0;
virtual int pack_expression(grib_expression* e) = 0;
virtual int pack_string(const char* v, size_t* len) = 0;
virtual int pack_string_array(const char** v, size_t* len) = 0;
virtual int pack_long(const long* v, size_t* len) = 0;
virtual int pack_bytes(const unsigned char* v, size_t* len) = 0;
virtual int unpack_bytes(unsigned char* v, size_t* len) = 0;
virtual int unpack_double_subarray(double* v, size_t start, size_t len) = 0;
virtual int unpack_double(double* v, size_t* len) = 0;
virtual int unpack_float(float* v, size_t* len) = 0;
virtual int unpack_double_element(size_t i, double* v) = 0;
virtual int unpack_float_element(size_t i, float* v) = 0;
virtual int unpack_double_element_set(const size_t* index_array, size_t len, double* val_array) = 0;
virtual int unpack_float_element_set(const size_t* index_array, size_t len, float* val_array) = 0;
virtual int unpack_string(char* v, size_t* len) = 0;
virtual int unpack_string_array(char** v, size_t* len) = 0;
virtual int unpack_long(long* v, size_t* len) = 0;
virtual long get_native_type() = 0;
virtual long get_next_position_offset() = 0;
virtual size_t string_length() = 0;
virtual long byte_offset() = 0;
virtual long byte_count() = 0;
virtual int value_count(long* count) = 0;
virtual int notify_change(grib_accessor* changed) = 0;
virtual grib_accessor* clone(grib_section* s, int* err) = 0;
virtual void update_size(size_t len) = 0;
virtual int nearest_smaller_value(double val, double* nearest) = 0;
virtual size_t preferred_size(int from_handle) = 0;
virtual grib_accessor* next_accessor() = 0;
virtual void resize(size_t new_size) = 0;
virtual void destroy(grib_context* ct) = 0;
virtual int compare_accessors(grib_accessor* a2, int compare_flags);
virtual int compare(grib_accessor*) = 0;
virtual int add_attribute(grib_accessor* attr, int nest_if_clash);
virtual grib_accessor* get_attribute_index(const char* name, int* index);
virtual int has_attributes();
virtual grib_accessor* get_attribute(const char* name);
virtual void init(const long, grib_arguments*) = 0;
virtual void post_init() = 0;
virtual grib_section* sub_section() = 0;
virtual grib_accessor* create_empty_accessor() = 0;
virtual int is_missing() = 0;
virtual long next_offset() = 0;
virtual grib_accessor* next(grib_accessor*, int) = 0;
virtual int clear() = 0;
virtual grib_accessor* make_clone(grib_section*, int*) = 0;
virtual void init_accessor(const long, grib_arguments *) = 0;
virtual void dump(eccodes::Dumper *f) = 0;
virtual int pack_missing() = 0;
// virtual int grib_pack_zero(grib_accessor* a) = 0;
virtual int is_missing_internal() = 0;
virtual int pack_double(const double *v, size_t *len) = 0;
virtual int pack_float(const float *v, size_t *len) = 0;
virtual int pack_expression(grib_expression *e) = 0;
virtual int pack_string(const char *v, size_t *len) = 0;
virtual int pack_string_array(const char **v, size_t *len) = 0;
virtual int pack_long(const long *v, size_t *len) = 0;
virtual int pack_bytes(const unsigned char *v, size_t *len) = 0;
virtual int unpack_bytes(unsigned char *v, size_t *len) = 0;
virtual int unpack_double_subarray(double *v, size_t start, size_t len) = 0;
virtual int unpack_double(double *v, size_t *len) = 0;
virtual int unpack_float(float *v, size_t *len) = 0;
virtual int unpack_double_element(size_t i, double *v) = 0;
virtual int unpack_float_element(size_t i, float *v) = 0;
virtual int unpack_double_element_set(const size_t *index_array,
size_t len,
double *val_array) = 0;
virtual int unpack_float_element_set(const size_t *index_array,
size_t len,
float *val_array) = 0;
virtual int unpack_string(char *v, size_t *len) = 0;
virtual int unpack_string_array(char **v, size_t *len) = 0;
virtual int unpack_long(long *v, size_t *len) = 0;
virtual long get_native_type() = 0;
virtual long get_next_position_offset() = 0;
virtual size_t string_length() = 0;
virtual long byte_offset() = 0;
virtual long byte_count() = 0;
virtual int value_count(long *count) = 0;
virtual int notify_change(grib_accessor *changed) = 0;
virtual grib_accessor *clone(grib_section *s, int *err) = 0;
virtual void update_size(size_t len) = 0;
virtual int nearest_smaller_value(double val, double *nearest) = 0;
virtual size_t preferred_size(int from_handle) = 0;
virtual grib_accessor *next_accessor() = 0;
virtual void resize(size_t new_size) = 0;
virtual void destroy(grib_context *ct) = 0;
virtual int compare_accessors(grib_accessor *a2, int compare_flags);
virtual int compare(grib_accessor *) = 0;
virtual int add_attribute(grib_accessor *attr, int nest_if_clash);
virtual grib_accessor *get_attribute_index(const char *name, int *index);
virtual int has_attributes();
virtual grib_accessor *get_attribute(const char *name);
virtual void init(const long, grib_arguments *) = 0;
virtual void post_init() = 0;
virtual grib_section *sub_section() = 0;
virtual grib_accessor *create_empty_accessor() = 0;
virtual int is_missing() = 0;
virtual long next_offset() = 0;
virtual grib_accessor *next(grib_accessor *, int) = 0;
virtual int clear() = 0;
virtual grib_accessor *make_clone(grib_section *, int *) = 0;

public:
// TODO(maee): make private
grib_context* context_ = nullptr;
const char* name_ = nullptr; // name of the accessor
const char* class_name_ = nullptr; // name of the class (Artifact from C version of ecCodes)
const char* name_space_ = nullptr; // namespace to which the accessor belongs
grib_handle* h_ = nullptr;
grib_action* creator_ = nullptr; // action that created the accessor
long length_ = 0; // byte length of the accessor
long offset_ = 0; // offset of the data in the buffer
grib_section* parent_ = nullptr; // section to which the accessor is attached
grib_accessor* next_ = nullptr; // next accessor in list
grib_accessor* previous_ = nullptr; // next accessor in list
unsigned long flags_ = 0; // Various flags
grib_section* sub_section_ = nullptr;
// TODO(maee): make private
grib_context *context_ = nullptr;
const char *name_ = nullptr; // name of the accessor
const char *class_name_ =
nullptr; // name of the class (Artifact from C version of ecCodes)
const char *name_space_ = nullptr; // namespace to which the accessor belongs
grib_handle *h_ = nullptr;
grib_action *creator_ = nullptr; // action that created the accessor
long length_ = 0; // byte length of the accessor
long offset_ = 0; // offset of the data in the buffer
grib_section *parent_ = nullptr; // section to which the accessor is attached
grib_accessor *next_ = nullptr; // next accessor in list
grib_accessor *previous_ = nullptr; // next accessor in list
unsigned long flags_ = 0; // Various flags
grib_section *sub_section_ = nullptr;

const char* all_names_[MAX_ACCESSOR_NAMES] = {0,}; // name of the accessor
const char* all_name_spaces_[MAX_ACCESSOR_NAMES] = {0,}; // namespace to which the accessor belongs
int dirty_ = 0;
const char *all_names_[MAX_ACCESSOR_NAMES] = {
0,
}; // name of the accessor
const char *all_name_spaces_[MAX_ACCESSOR_NAMES] = {
0,
}; // namespace to which the accessor belongs
int dirty_ = 0;

grib_accessor* same_ = nullptr; // accessors with the same name
long loop_ = 0; // used in lists
grib_virtual_value* vvalue_ = nullptr; // virtual value used when transient flag on
const char* set_ = nullptr;
grib_accessor* attributes_[MAX_ACCESSOR_ATTRIBUTES] = {0,}; // attributes are accessors
grib_accessor* parent_as_attribute_ = nullptr;
grib_accessor *same_ = nullptr; // accessors with the same name
long loop_ = 0; // used in lists
grib_virtual_value *vvalue_ =
nullptr; // virtual value used when transient flag on
const char *set_ = nullptr;
grib_accessor *attributes_[MAX_ACCESSOR_ATTRIBUTES] = {
0,
}; // attributes are accessors
grib_accessor *parent_as_attribute_ = nullptr;
};
4 changes: 2 additions & 2 deletions src/accessor/grib_accessor_class_ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ size_t grib_accessor_ascii_t::string_length()
return length_;
}

void grib_accessor_ascii_t::dump(grib_dumper* dumper)
void grib_accessor_ascii_t::dump(eccodes::Dumper* dumper)
{
grib_dump_string(dumper, this, NULL);
dumper->dump_string(this, NULL);
}

long grib_accessor_ascii_t::get_native_type()
Expand Down
2 changes: 1 addition & 1 deletion src/accessor/grib_accessor_class_ascii.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class grib_accessor_ascii_t : public grib_accessor_gen_t
int unpack_string(char*, size_t* len) override;
size_t string_length() override;
int value_count(long*) override;
void dump(grib_dumper*) override;
void dump(eccodes::Dumper*) override;
void init(const long, grib_arguments*) override;
int compare(grib_accessor*) override;
};
4 changes: 2 additions & 2 deletions src/accessor/grib_accessor_class_bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ long grib_accessor_bitmap_t::next_offset()
return byte_offset() + byte_count();
}

void grib_accessor_bitmap_t::dump(grib_dumper* dumper)
void grib_accessor_bitmap_t::dump(eccodes::Dumper* dumper)
{
long len = 0;
char label[1024];

value_count(&len);
snprintf(label, sizeof(label), "Bitmap of %ld values", len);
grib_dump_bytes(dumper, this, label);
dumper->dump_bytes(this, label);
}

int grib_accessor_bitmap_t::unpack_long(long* val, size_t* len)
Expand Down
2 changes: 1 addition & 1 deletion src/accessor/grib_accessor_class_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class grib_accessor_bitmap_t : public grib_accessor_bytes_t
int unpack_string(char*, size_t* len) override;
size_t string_length() override;
long next_offset() override;
void dump(grib_dumper*) override;
void dump(eccodes::Dumper*) override;
void init(const long, grib_arguments*) override;
void update_size(size_t) override;
int unpack_double_element(size_t i, double* val) override;
Expand Down
4 changes: 2 additions & 2 deletions src/accessor/grib_accessor_class_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int grib_accessor_blob_t::unpack_bytes(unsigned char* buffer, size_t* len)
return GRIB_SUCCESS;
}

void grib_accessor_blob_t::dump(grib_dumper* dumper)
void grib_accessor_blob_t::dump(eccodes::Dumper* dumper)
{
grib_dump_bytes(dumper, this, NULL);
dumper->dump_bytes(this, NULL);
}
2 changes: 1 addition & 1 deletion src/accessor/grib_accessor_class_blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class grib_accessor_blob_t : public grib_accessor_gen_t
grib_accessor* create_empty_accessor() override { return new grib_accessor_blob_t{}; }
long get_native_type() override;
int unpack_bytes(unsigned char*, size_t* len) override;
void dump(grib_dumper*) override;
void dump(eccodes::Dumper*) override;
void init(const long, grib_arguments*) override;
};
4 changes: 2 additions & 2 deletions src/accessor/grib_accessor_class_bufr_data_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3224,11 +3224,11 @@ int grib_accessor_bufr_data_array_t::process_elements(int flag, long onlySubset,
return err;
}

void grib_accessor_bufr_data_array_t::dump(grib_dumper* dumper)
void grib_accessor_bufr_data_array_t::dump(eccodes::Dumper* dumper)
{
// grib_accessor_bufr_data_array_t *self =(grib_accessor_bufr_data_array_t*)a;
// int err=process_elements(a,PROCESS_DECODE);
// grib_dump_section(dumper,a,self->dataKeys_ ->block);
// dumper->dump_section(a,self->dataKeys_ ->block);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/accessor/grib_accessor_class_bufr_data_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class grib_accessor_bufr_data_array_t : public grib_accessor_gen_t
long next_offset() override;
int value_count(long*) override;
void destroy(grib_context*) override;
void dump(grib_dumper*) override;
void dump(eccodes::Dumper*) override;
void init(const long, grib_arguments*) override;

void accessor_bufr_data_array_set_unpackMode(int);
Expand Down
Loading

0 comments on commit 546c166

Please sign in to comment.