Skip to content

Commit 5a9253c

Browse files
committed
int events from ryml WIP: update c++ code
1 parent c4ff3f6 commit 5a9253c

9 files changed

Lines changed: 309 additions & 975 deletions

common/vars-rapidyaml.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ NATIVE_DIR := $(RAPIDYAML_DIR)/native
44
export LD_LIBRARY_PATH := $(NATIVE_DIR):$(LD_LIBRARY_PATH)
55
export $(DY)LD_LIBRARY_PATH := $(LD_LIBRARY_PATH)
66

7-
RAPIDYAML_VERSION := 0.8.0
8-
RAPIDYAML_TAG ?= d3132a25ec21c65e27ff46ab8c3d61c72a321302
7+
RAPIDYAML_VERSION := 0.9.0
8+
#RAPIDYAML_TAG ?= d3132a25ec21c65e27ff46ab8c3d61c72a321302
9+
RAPIDYAML_TAG ?= int_handler
910
RAPIDYAML_REPO := https://github.com/biojppm/rapidyaml
1011
RAPIDYAML_BUILD_TYPE ?= Release
1112
RAPIDYAML_DBG ?= 0

rapidyaml/native/CMakeLists.txt

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,71 @@ if(UNIX)
1313
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
1414
endif()
1515

16-
set(libname rapidyaml) # TODO rename to ysparse
1716

18-
add_library(${libname}
17+
add_library(ysparse
1918
#
2019
# JNI bridge
2120
org_rapidyaml_Rapidyaml.h
2221
org_rapidyaml_Rapidyaml.cpp
2322
#
2423
# ysparse files
2524
ysparse_common.hpp
26-
ysparse_evt_handler.hpp
27-
ysparse_evt_handler.cpp
2825
ysparse_evt.hpp
2926
ysparse_evt.cpp
3027
#
3128
# files required from rapidyaml
29+
rapidyaml/src/c4/yml/common.hpp
3230
rapidyaml/src/c4/yml/common.cpp
31+
rapidyaml/src/c4/yml/node_type.hpp
3332
rapidyaml/src/c4/yml/node_type.cpp
33+
rapidyaml/src/c4/yml/parse.hpp
3434
rapidyaml/src/c4/yml/parse.cpp
35-
rapidyaml/src/c4/yml/tree.cpp
3635
rapidyaml/src/c4/yml/tag.cpp
3736
rapidyaml/src/c4/yml/reference_resolver.cpp
37+
rapidyaml/src_extra/c4/yml/extra/event_handler_ints.hpp
38+
rapidyaml/src_extra/c4/yml/extra/event_handler_ints.cpp
3839
#
3940
# files required from rapidyaml/ext/c4core
41+
rapidyaml/ext/c4core/src/c4/substr.hpp
42+
rapidyaml/ext/c4core/src/c4/base64.hpp
4043
rapidyaml/ext/c4core/src/c4/base64.cpp
44+
rapidyaml/ext/c4core/src/c4/error.hpp
4145
rapidyaml/ext/c4core/src/c4/error.cpp
46+
rapidyaml/ext/c4core/src/c4/language.hpp
4247
rapidyaml/ext/c4core/src/c4/language.cpp
48+
rapidyaml/ext/c4core/src/c4/utf.hpp
4349
rapidyaml/ext/c4core/src/c4/utf.cpp
4450
)
45-
target_include_directories(${libname} PUBLIC
51+
target_include_directories(ysparse PUBLIC
4652
${CMAKE_CURRENT_LIST_DIR}
4753
${CMAKE_CURRENT_LIST_DIR}/rapidyaml/src
54+
${CMAKE_CURRENT_LIST_DIR}/rapidyaml/src_extra
4855
${CMAKE_CURRENT_LIST_DIR}/rapidyaml/ext/c4core/src
4956
)
50-
target_compile_definitions(${libname} PUBLIC
57+
target_compile_definitions(ysparse PUBLIC
5158
RYML_WITH_TAB_TOKENS
5259
RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS
5360
$<$<BOOL:${YSPARSE_TIMED}>:YSPARSE_TIMED>
5461
$<$<BOOL:${YSPARSE_DBG}>:RYML_DBG>
5562
)
56-
set_target_properties(${libname} PROPERTIES CXX_STANDARD 17)
63+
set_target_properties(ysparse PROPERTIES CXX_STANDARD 17)
5764

58-
target_include_directories(${libname} PUBLIC ${JNI_INCLUDE_DIRS})
65+
target_include_directories(ysparse PUBLIC ${JNI_INCLUDE_DIRS})
5966

60-
add_executable(${libname}-test ysparse_test.cpp)
61-
target_link_libraries(${libname}-test ${libname})
62-
add_custom_target(${libname}-test-run
63-
DEPENDS ${libname}-test
64-
COMMAND $<TARGET_FILE:${libname}-test>
67+
add_executable(ysparse-test
68+
ysparse_test.cpp
69+
rapidyaml/src_extra/c4/yml/extra/event_handler_ints_utils.hpp
70+
rapidyaml/src_extra/c4/yml/extra/event_handler_ints_utils.cpp
71+
rapidyaml/src_extra/c4/yml/extra/scalar.cpp
72+
)
73+
target_link_libraries(ysparse-test ysparse)
74+
add_custom_target(ysparse-test-run
75+
DEPENDS ysparse-test
76+
COMMAND $<TARGET_FILE:ysparse-test>
6577
COMMENT "running C++ tests"
6678
)
67-
add_custom_target(${libname}-test-run-timing
68-
DEPENDS ${libname}-test
69-
COMMAND $<TARGET_FILE:${libname}-test> --timing
79+
add_custom_target(ysparse-test-run-timing
80+
DEPENDS ysparse-test
81+
COMMAND $<TARGET_FILE:ysparse-test> --timing
7082
COMMENT "running C++ tests, with timing"
7183
)

rapidyaml/native/org_rapidyaml_Rapidyaml.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ JNIEXPORT jint JNICALL
3535
Java_org_rapidyaml_Rapidyaml_ysparse_1parse(JNIEnv *env, jobject,
3636
jlong obj, jstring jfilename,
3737
jbyteArray src, jint src_len,
38+
jbyteArray arena, jint arena_len,
3839
jintArray dst, jint dst_len)
3940
{
4041
TIMED_SECTION("jni:ysparse", (size_type)src_len);
42+
jbyte* arena_ = nullptr;
4143
jbyte* src_ = nullptr;
4244
int* dst_ = nullptr;
4345
const char *filename = nullptr;
4446
jboolean dst_is_copy = false;
4547
jboolean src_is_copy = false;
48+
jboolean arena_is_copy = false;
4649
{
4750
TIMED_SECTION("jni:ysparse/get_jni", (size_type)src_len);
4851
// this is __S__L__O__W__
@@ -52,6 +55,10 @@ Java_org_rapidyaml_Rapidyaml_ysparse_1parse(JNIEnv *env, jobject,
5255
TIMED_SECTION("jni:ysparse/GetByteArray(src)");
5356
src_ = env->GetByteArrayElements(src, &src_is_copy);
5457
}
58+
{
59+
TIMED_SECTION("jni:ysparse/GetByteArray(arena)");
60+
arena_ = env->GetByteArrayElements(arena, &arena_is_copy);
61+
}
5562
{
5663
TIMED_SECTION("jni:ysparse/GetIntArray(dst)");
5764
dst_ = env->GetIntArrayElements(dst, &dst_is_copy);
@@ -68,6 +75,7 @@ Java_org_rapidyaml_Rapidyaml_ysparse_1parse(JNIEnv *env, jobject,
6875
{
6976
rc = ysparse_parse((ysparse*)obj, filename,
7077
(char*)src_, src_len,
78+
(char*)arena_, arena_len,
7179
dst_, dst_len);
7280
}
7381
catch (YsParseError const& exc)
@@ -86,6 +94,10 @@ Java_org_rapidyaml_Rapidyaml_ysparse_1parse(JNIEnv *env, jobject,
8694
TIMED_SECTION("jni:ysparse/ReleaseByteArray(src)");
8795
env->ReleaseByteArrayElements(src, src_, 0);
8896
}
97+
{
98+
TIMED_SECTION("jni:ysparse/ReleaseByteArray(arena)");
99+
env->ReleaseByteArrayElements(arena, arena_, 0);
100+
}
89101
{
90102
TIMED_SECTION("jni:ysparse/ReleaseIntArray(dst)");
91103
env->ReleaseIntArrayElements(dst, dst_, 0);
@@ -103,27 +115,35 @@ JNIEXPORT jint JNICALL
103115
Java_org_rapidyaml_Rapidyaml_ysparse_1parse_1buf(JNIEnv *env, jobject,
104116
jlong obj, jstring jfilename,
105117
jobject src, jint src_len,
118+
jobject arena, jint arena_len,
106119
jobject dst, jint dst_len)
107120
{
108121
TIMED_SECTION("jni:ysparse_buf", (size_type)src_len);
122+
char* arena_ = nullptr;
109123
char* src_ = nullptr;
110124
int* dst_ = nullptr;
111125
const char *filename = nullptr;
112126
{
113127
TIMED_SECTION("jni:ysparse_buf/get_jni", (size_type)src_len);
114128
src_ = (char*)env->GetDirectBufferAddress(src);
129+
arena_ = (char*)env->GetDirectBufferAddress(arena);
115130
dst_ = (int*)env->GetDirectBufferAddress(dst);
116131
filename = env->GetStringUTFChars(jfilename, 0);
117132
if(!src_)
118133
throw_runtime_exception(env, "null pointer: src");
134+
if(!arena_)
135+
throw_runtime_exception(env, "null pointer: arena");
119136
if(!dst_)
120137
throw_runtime_exception(env, "null pointer: dst");
121138
}
122139
{
123140
TIMED_SECTION("jni:ysparse_buf/parse", (size_type)src_len);
124141
try
125142
{
126-
return ysparse_parse((ysparse*)obj, filename, src_, src_len, dst_, dst_len);
143+
return ysparse_parse((ysparse*)obj, filename,
144+
src_, src_len,
145+
arena_, arena_len,
146+
dst_, dst_len);
127147
}
128148
catch (YsParseError const& exc)
129149
{

rapidyaml/native/ysparse_common.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ RYML_EXPORT void ysparse_timing_set(bool yes);
3434

3535
#ifndef YSPARSE_TIMED
3636
#define TIMED_SECTION(...)
37-
#error
3837
#else
3938
#include <stdio.h>
4039
#include <chrono>

rapidyaml/native/ysparse_evt.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,35 @@ RYML_EXPORT void ysparse_destroy(ysparse *obj)
3838
_RYML_CB_FREE(get_callbacks(), obj, ysparse, 1);
3939
}
4040

41-
RYML_EXPORT size_type ysparse_parse(ysparse *obj,
42-
const char *filename,
43-
char *ys, size_type ys_size,
44-
evt::DataType *events, size_type evt_size)
41+
RYML_EXPORT bool ysparse_parse(ysparse *obj,
42+
const char *filename,
43+
char *ys, size_type ys_size,
44+
char *arena, size_type arena_size,
45+
int *events, size_type evt_size)
4546
{
4647
TIMED_SECTION("cpp:ysparse", ys_size);
4748
csubstr filename_ = filename ? to_csubstr(filename) : csubstr{};
4849
substr ys_(ys, (size_t)ys_size);
50+
substr arena_(arena, (size_t)arena_size);
4951
{
5052
TIMED_SECTION("cpp:ysparse/reset");
51-
obj->reset(ys_, events, evt_size);
52-
obj->m_handler.reserve(256u);
53+
obj->reset(ys_, arena_, events, evt_size);
5354
}
5455
{
5556
TIMED_SECTION("cpp:ysparse/parse", ys_size);
5657
obj->m_parser.parse_in_place_ev(filename_, ys_);
5758
}
58-
return (size_type)obj->m_handler.m_evt_curr;
59+
return obj->m_handler.fits_buffers();
60+
}
61+
62+
RYML_EXPORT int ysparse_parse_reqsize_evt(ysparse *obj)
63+
{
64+
return obj->m_handler.required_size_events();
65+
}
66+
67+
RYML_EXPORT int ysparse_parse_reqsize_arena(ysparse *obj)
68+
{
69+
return (int)obj->m_handler.required_size_arena();
5970
}
6071

6172
#if defined(__cplusplus)

rapidyaml/native/ysparse_evt.hpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#ifndef YSPARSE_EVT_HPP_
33
#define YSPARSE_EVT_HPP_
44

5-
#include "ysparse_evt_handler.hpp"
5+
#include "c4/yml/parse_engine.hpp"
6+
#include "c4/yml/parse_engine.def.hpp"
7+
#include "c4/yml/extra/event_handler_ints.hpp"
68
#include "ysparse_common.hpp"
79

810
#if defined(__cplusplus)
@@ -11,17 +13,17 @@ extern "C" {
1113

1214
struct RYML_EXPORT ysparse
1315
{
14-
ys::EventHandlerEvt m_handler;
15-
c4::yml::ParseEngine<ys::EventHandlerEvt> m_parser;
16+
c4::yml::extra::EventHandlerInts m_handler;
17+
c4::yml::ParseEngine<c4::yml::extra::EventHandlerInts> m_parser;
1618
ysparse()
1719
: m_handler()
1820
, m_parser(&m_handler)
1921
{
2022
RYML_CHECK(m_parser.options().scalar_filtering());
2123
}
22-
void reset(c4::csubstr src, evt::DataType *evt, int32_t evt_size)
24+
void reset(c4::substr src, c4::substr arena, int32_t *evt, int32_t evt_size)
2325
{
24-
m_handler.reset(src, evt, evt_size);
26+
m_handler.reset(src, arena, evt, evt_size);
2527
}
2628
};
2729

@@ -43,18 +45,20 @@ RYML_EXPORT void ysparse_destroy(ysparse *ryml2evt);
4345
* length of the string in the `ys` string. The `ys` string is mutated
4446
* during parsing.
4547
*
46-
* @return the size needed for `evt`. The caller must check if the
47-
* returned size is larger than `evt_size`. If so, this means that
48-
* `evt` could not accomodate all events produced from `ys`, and is
49-
* incomplete. The caller must then (1) resize `evt` to at least the
50-
* return value, (2) re-copy the original YS into `ys` and (3) call
51-
* again this function, passing in the resized `evt` and the fresh
52-
* copy in `ys`.
48+
* @return true if the `evt` and `arena` buffers were large enough to
49+
* accomodate the result The caller must check this value. When false,
50+
* it means that at least one of the buffers could not accomodate the
51+
* result. The caller must then (1) resize `evt` to at least
52+
* the return value, (2) re-copy the original YS into `ys` and (3)
53+
* call again this function, passing in the resized `evt` and the
54+
* fresh copy in `ys`.
5355
*
54-
* @note nothing is written beyond `evt_size`. This means that when
55-
* `evt_size` is 0, then `evt` can be null. This function can be
56-
* safely called for any valid pair of `evt` and `evt_size`, and will
57-
* always return the same required size.
56+
* @note nothing is written beyond `evt_size` or `arena_size`. This
57+
* means that when `evt_size`/`arena_size` is 0, then `evt`/`arena`
58+
* can be null. This function can be safely called for any valid pair
59+
* of `evt`+`evt_size` and `arena`/`arena_size`, and the same required
60+
* size will always be reported. The same applies for
61+
* `arena`+`arena_size`.
5862
*
5963
* For example, the YAML `say: 2 + 2` produces the following sequence of
6064
* 12 integers:
@@ -82,10 +86,17 @@ RYML_EXPORT void ysparse_destroy(ysparse *ryml2evt);
8286
* in-place in the input string, and the extra integers will pertain
8387
* to the resulting filtered string.
8488
*/
85-
RYML_EXPORT size_type ysparse_parse(ysparse *ryml2evt,
86-
const char *filename,
87-
char *ys, size_type ys_size,
88-
evt::DataType *evt, size_type evt_size);
89+
RYML_EXPORT bool ysparse_parse(ysparse *ryml2evt,
90+
const char *filename,
91+
char *ys, size_type ys_size,
92+
char *arena, size_type arena_size,
93+
int *evt, size_type evt_size);
94+
95+
/** Get the required size for the event buffer, from the last parse call */
96+
RYML_EXPORT int ysparse_parse_reqsize_evt(ysparse *ryml2evt);
97+
98+
/** Get the required size for the arena buffer, from the last parse call */
99+
RYML_EXPORT int ysparse_parse_reqsize_arena(ysparse *ryml2evt);
89100

90101
#if defined(__cplusplus)
91102
}

rapidyaml/native/ysparse_evt_handler.cpp

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)