mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 16:35:40 +02:00
Convert jsonpull to C++ with shared_ptr and std::vector/std::string
Replace the manual malloc/realloc/free memory management in jsonpull with std::shared_ptr ownership. Each json_object now owns its children through std::vector<json_object_ptr>; raw back-pointers to parent and parser remain valid by structural invariant and are cleared on json_disconnect so detached subtrees can outlive their parser. Strings become std::string, child arrays become std::vector, and the old union becomes a struct so non-trivial members can coexist while preserving the existing o->value.xxx access paths. The old jsonpull.c is replaced by jsonpull.cpp, json_stringify now returns std::string, and all callers across tippecanoe, tile-join, tippecanoe-decode, tippecanoe-json-tool, tippecanoe-overzoom and the unit tests are updated to use json_object_ptr / json_pull_ptr. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+72
-46
@@ -1,9 +1,11 @@
|
||||
#ifndef JSONPULL_H
|
||||
#define JSONPULL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
typedef enum json_type {
|
||||
// These types can be returned by json_read()
|
||||
@@ -25,74 +27,98 @@ typedef enum json_type {
|
||||
JSON_VALUE,
|
||||
} json_type;
|
||||
|
||||
typedef struct json_object {
|
||||
struct json_object *parent;
|
||||
struct json_pull *parser;
|
||||
struct json_object;
|
||||
struct json_pull;
|
||||
|
||||
union {
|
||||
typedef std::shared_ptr<json_object> json_object_ptr;
|
||||
typedef std::shared_ptr<json_pull> json_pull_ptr;
|
||||
|
||||
// json_object owns its descendants via std::shared_ptr in std::vector<>s,
|
||||
// and keeps raw back-pointers to its parent and to the parser. The back-pointers
|
||||
// remain valid as long as the node is attached to the tree (the parent is kept
|
||||
// alive by holding a shared_ptr to this child, and the parser is kept alive by
|
||||
// the caller's json_pull_ptr). json_disconnect() splices a node out of its
|
||||
// parent and clears those back-pointers in the detached subtree, so the
|
||||
// detached subtree can outlive the original parser.
|
||||
|
||||
struct json_object : public std::enable_shared_from_this<json_object> {
|
||||
json_object *parent = nullptr;
|
||||
json_pull *parser = nullptr;
|
||||
|
||||
json_type type = JSON_NULL;
|
||||
int expect = 0;
|
||||
|
||||
// Members named to match the previous C union layout so that existing
|
||||
// access paths like `o->value.string.string` and `o->value.array.array[i]`
|
||||
// continue to work. This is no longer a union because std::string and
|
||||
// std::vector have non-trivial destructors.
|
||||
struct value_t {
|
||||
struct {
|
||||
double number;
|
||||
unsigned long long large_unsigned;
|
||||
long long large_signed;
|
||||
double number = 0;
|
||||
unsigned long long large_unsigned = 0;
|
||||
long long large_signed = 0;
|
||||
} number;
|
||||
|
||||
struct {
|
||||
char *string;
|
||||
void *refcon; // reference constant for caller's use
|
||||
std::string string;
|
||||
void *refcon = nullptr; // reference constant for caller's use
|
||||
} string;
|
||||
|
||||
struct {
|
||||
struct json_object **array;
|
||||
size_t length;
|
||||
std::vector<json_object_ptr> array;
|
||||
} array;
|
||||
|
||||
struct {
|
||||
struct json_object **keys;
|
||||
struct json_object **values;
|
||||
size_t length;
|
||||
std::vector<json_object_ptr> keys;
|
||||
std::vector<json_object_ptr> values;
|
||||
} object;
|
||||
} value;
|
||||
};
|
||||
|
||||
json_type type;
|
||||
int expect;
|
||||
} json_object;
|
||||
struct json_pull {
|
||||
const char *error = nullptr; // points at a string literal; no allocation
|
||||
int line = 1;
|
||||
|
||||
typedef struct json_pull {
|
||||
char *error;
|
||||
int line;
|
||||
ssize_t (*read)(struct json_pull *, char *buf, size_t n) = nullptr;
|
||||
void *source = nullptr;
|
||||
std::vector<char> buffer;
|
||||
ssize_t buffer_tail = 0;
|
||||
ssize_t buffer_head = 0;
|
||||
|
||||
ssize_t (*read)(struct json_pull *, char *buf, size_t n);
|
||||
void *source;
|
||||
char *buffer;
|
||||
ssize_t buffer_tail;
|
||||
ssize_t buffer_head;
|
||||
json_object_ptr container;
|
||||
json_object_ptr root;
|
||||
|
||||
json_object *container;
|
||||
json_object *root;
|
||||
std::string number_buffer;
|
||||
};
|
||||
|
||||
struct string *number_buffer;
|
||||
} json_pull;
|
||||
json_pull_ptr json_begin_file(FILE *f);
|
||||
json_pull_ptr json_begin_string(const char *s);
|
||||
|
||||
json_pull *json_begin_file(FILE *f);
|
||||
json_pull *json_begin_string(const char *s);
|
||||
json_pull_ptr json_begin(ssize_t (*read)(struct json_pull *, char *buffer, size_t n), void *source);
|
||||
|
||||
json_pull *json_begin(ssize_t (*read)(struct json_pull *, char *buffer, size_t n), void *source);
|
||||
void json_end(json_pull *p);
|
||||
// json_end is now a thin convenience that resets the caller's json_pull_ptr.
|
||||
// The parser (and any tree it still owns) is freed when the last shared_ptr
|
||||
// to it is dropped, so calling json_end is optional if the json_pull_ptr will
|
||||
// go out of scope on its own.
|
||||
void json_end(json_pull_ptr &p);
|
||||
|
||||
typedef void (*json_separator_callback)(json_type type, json_pull *j, void *state);
|
||||
|
||||
json_object *json_read_tree(json_pull *j);
|
||||
json_object *json_read(json_pull *j);
|
||||
json_object *json_read_separators(json_pull *j, json_separator_callback cb, void *state);
|
||||
void json_free(json_object *j);
|
||||
void json_disconnect(json_object *j);
|
||||
json_object_ptr json_read_tree(json_pull_ptr j);
|
||||
json_object_ptr json_read(json_pull_ptr j);
|
||||
json_object_ptr json_read_separators(json_pull_ptr j, json_separator_callback cb, void *state);
|
||||
|
||||
json_object *json_hash_get(json_object *o, const char *s);
|
||||
// json_free now just resets the caller's json_object_ptr. The subtree is
|
||||
// destroyed when the last shared_ptr to it is dropped (typically by also
|
||||
// being removed from its parent or parser).
|
||||
void json_free(json_object_ptr &j);
|
||||
|
||||
char *json_stringify(json_object *o);
|
||||
// Splice o out of its parent's array/object and clear parent/parser back-pointers
|
||||
// throughout the detached subtree so it can outlive the original parser.
|
||||
void json_disconnect(json_object_ptr j);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
json_object_ptr json_hash_get(json_object_ptr o, const char *s);
|
||||
|
||||
std::string json_stringify(json_object_ptr o);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user