diff options
author | Ole Troan <ot@cisco.com> | 2016-04-09 03:16:30 +0200 |
---|---|---|
committer | Dave Wallace <dwallacelf@gmail.com> | 2016-04-20 16:50:29 +0000 |
commit | 6855f6cdfee8c479f1e0ae440ce87a91ff41a708 (patch) | |
tree | e438e21c8107a675dc3a3141c6af6ba0ab458992 /vpp-api/java/japi/vppjni_env.h | |
parent | 633951c3d8dc1640813b4778f3e35463d08f3795 (diff) |
Python-API: Inital commit of Python bindings for the VPP API.
See: https://wiki.fd.io/view/VPP/Python_API
Change-Id: If135fc32208c7031787e1935b399d930e0e1ea1f
Signed-off-by: Ole Troan <ot@cisco.com>
Diffstat (limited to 'vpp-api/java/japi/vppjni_env.h')
-rw-r--r-- | vpp-api/java/japi/vppjni_env.h | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/vpp-api/java/japi/vppjni_env.h b/vpp-api/java/japi/vppjni_env.h new file mode 100644 index 00000000000..44029c2c964 --- /dev/null +++ b/vpp-api/java/japi/vppjni_env.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Utilities for accessing Java classes/method/fields in an efficient + * manner. + */ + +/* + * A potentially-uninitialized reference to a Java class + */ +typedef struct vppjni_class { + // Fully-Qualified Class Name + const char *fqcn; + // Constructor signature + const char *init_sig; + // Global reference to class handle + jclass jclass; + // Constructor method handle + jmethodID jinit; + // Next item in linked list + struct vppjni_class *next; +} vppjni_class_t; + +typedef struct jenv_field { + // Field name + const char *name; + // Field type + const char *type; + // Defining class reference + const vppjni_class_t *clsref; + // Field handle + jfieldID jfield; + // Next item in linked list + struct jenv_field *next; +} vppjni_field_t; + +#define VPPJNI_CLASS_SYMBOL(name) vppjni_class_##name +#define VPPJNI_CLASS_INIT(name) vppjni_class_##name##_init +#define BIND_JAPI_CLASS(name, sig) \ + static vppjni_class_t VPPJNI_CLASS_SYMBOL(name); \ + static void VPPJNI_CLASS_INIT(name)(void) __attribute__((__constructor__)); \ + static void VPPJNI_CLASS_INIT(name)() \ + { \ + VPPJNI_CLASS_SYMBOL(name).fqcn = "org/openvpp/vppjapi/" #name; \ + VPPJNI_CLASS_SYMBOL(name).init_sig = sig; \ + vppjni_register_class(&VPPJNI_CLASS_SYMBOL(name)); \ + } \ + static __attribute__((unused)) jobject name##Array(JNIEnv *env, jsize length) \ + { \ + return (*env)->NewObjectArray(env, length, VPPJNI_CLASS_SYMBOL(name).jclass, NULL); \ + } \ + static jobject name##Object(JNIEnv *env, ...) \ + { \ + va_list ap; \ + va_start(ap, env); \ + jobject obj = vppjni_new_object(env, &VPPJNI_CLASS_SYMBOL(name), ap); \ + va_end(ap); \ + return obj; \ + } + +#define VPPJNI_FIELD_SYMBOL(cls, name) vppjni_field_##cls##_##name +#define VPPJNI_FIELD_INIT(cls, name) vppjni_field_##cls##_##name##_init +#define BIND_JAPI_FIELD(cls, field, sig) \ + static vppjni_field_t VPPJNI_FIELD_SYMBOL(cls, field); \ + static void VPPJNI_FIELD_INIT(cls, field)(void) __attribute__((__constructor__)); \ + static void VPPJNI_FIELD_INIT(cls, field)() \ + { \ + VPPJNI_FIELD_SYMBOL(cls, field).name = #field; \ + VPPJNI_FIELD_SYMBOL(cls, field).type = sig; \ + VPPJNI_FIELD_SYMBOL(cls, field).clsref = &VPPJNI_CLASS_SYMBOL(cls); \ + vppjni_register_field(&VPPJNI_FIELD_SYMBOL(cls, field)); \ + } +#define BIND_JAPI_BOOL_FIELD(cls, field) \ + BIND_JAPI_FIELD(cls, field, "Z"); \ + static void set_##cls##_##field(JNIEnv *env, jobject obj, jboolean value) \ + { \ + (*env)->SetBooleanField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \ + } +#define BIND_JAPI_BYTE_FIELD(cls, field) \ + BIND_JAPI_FIELD(cls, field, "B"); \ + static void set_##cls##_##field(JNIEnv *env, jobject obj, jbyte value) \ + { \ + (*env)->SetByteField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \ + } +#define BIND_JAPI_INT_FIELD(cls, field) \ + BIND_JAPI_FIELD(cls, field, "I"); \ + static void set_##cls##_##field(JNIEnv *env, jobject obj, jint value) \ + { \ + (*env)->SetIntField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \ + } +#define BIND_JAPI_OBJ_FIELD(cls, field, sig) \ + BIND_JAPI_FIELD(cls, field, sig); \ + static void set_##cls##_##field(JNIEnv *env, jobject obj, jobject value) \ + { \ + (*env)->SetObjectField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \ + } +#define BIND_JAPI_STRING_FIELD(cls, field) \ + BIND_JAPI_OBJ_FIELD(cls, field, "Ljava/lang/String;") + +jobject vppjni_new_object(JNIEnv *env, const vppjni_class_t *ptr, va_list ap) __attribute__ ((visibility ("hidden"))); +void vppjni_register_class(vppjni_class_t *ptr) __attribute__ ((visibility ("hidden"))); +void vppjni_register_field(vppjni_field_t *ptr) __attribute__ ((visibility ("hidden"))); +int vppjni_init(JNIEnv *env) __attribute__ ((visibility ("hidden"))); +void vppjni_uninit(JNIEnv *env) __attribute__ ((visibility ("hidden"))); + |