Skip to content

Commit

Permalink
Improved swig entry API
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 11, 2016
1 parent 56f397e commit d023bb9
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/frostwire/jlibtorrent/swig/entry.java
Expand Up @@ -112,6 +112,18 @@ public short getM_type_queried() {
return libtorrent_jni.entry_m_type_queried_get(swigCPtr, this);
}

public entry get(String key) {
return new entry(libtorrent_jni.entry_get(swigCPtr, this, key), false);
}

public void set(String key, String value) {
libtorrent_jni.entry_set__SWIG_0(swigCPtr, this, key, value);
}

public void set(String key, int value) {
libtorrent_jni.entry_set__SWIG_1(swigCPtr, this, key, value);
}

public byte_vector bencode() {
return new byte_vector(libtorrent_jni.entry_bencode(swigCPtr, this), true);
}
Expand Down
Expand Up @@ -603,6 +603,9 @@ public class libtorrent_jni {
public final static native String entry_to_string(long jarg1, entry jarg1_);
public final static native void entry_m_type_queried_set(long jarg1, entry jarg1_, short jarg2);
public final static native short entry_m_type_queried_get(long jarg1, entry jarg1_);
public final static native long entry_get(long jarg1, entry jarg1_, String jarg2);
public final static native void entry_set__SWIG_0(long jarg1, entry jarg1_, String jarg2, String jarg3);
public final static native void entry_set__SWIG_1(long jarg1, entry jarg1_, String jarg2, int jarg3);
public final static native long entry_bencode(long jarg1, entry jarg1_);
public final static native long entry_bdecode(long jarg1, byte_vector jarg1_);
public final static native int sha1_hash_size_get();
Expand Down
12 changes: 12 additions & 0 deletions swig/libtorrent.i
Expand Up @@ -884,6 +884,18 @@ namespace libtorrent {
};

%extend entry {
entry& get(std::string const& key) {
return $self->operator[](key);

This comment has been minimized.

Copy link
@gubatron

gubatron Feb 11, 2016

Collaborator

brainfuck

I suppose this doesn't work:
return $self[key] but this should be the equivalent.

how come it returns a entry& and not a entry*? first time I see a function returning type&

This comment has been minimized.

Copy link
@aldenml

aldenml Feb 11, 2016

Author Collaborator

$self[key] does not work because $self is a pointer, I rather use the arrow notation ($self->) than the start (*$self) in this case. Returning a reference is a legitimate technique to expose internal structures ready to be changed, but these referenced objects need to live as long as the container object is alive to guarantee memory consistency. It's very convenient in the entry type case since together with the operator= set you can have e["keyname"] = "stringvalue".

}

void set(std::string const& key, std::string const& value) {
$self->operator[](key) = value;
}

void set(std::string const& key, long const& value) {
$self->operator[](key) = value;
}

std::vector<int8_t> bencode() {
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *$self);
Expand Down
116 changes: 115 additions & 1 deletion swig/libtorrent_jni.cpp
Expand Up @@ -1226,6 +1226,15 @@ SWIGINTERN void std_vector_Sl_std_pair_Sl_std_string_Sc_dht_extension_handler_li
else
throw std::out_of_range("vector index out of range");
}
SWIGINTERN libtorrent::entry &libtorrent_entry_get(libtorrent::entry *self,std::string const &key){
return self->operator[](key);
}
SWIGINTERN void libtorrent_entry_set__SWIG_0(libtorrent::entry *self,std::string const &key,std::string const &value){
self->operator[](key) = value;
}
SWIGINTERN void libtorrent_entry_set__SWIG_1(libtorrent::entry *self,std::string const &key,long const &value){
self->operator[](key) = value;
}
SWIGINTERN std::vector< int8_t > libtorrent_entry_bencode(libtorrent::entry *self){
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *self);
Expand Down Expand Up @@ -17419,6 +17428,111 @@ SWIGEXPORT jshort JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_en
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1get(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2) {
jlong jresult = 0 ;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
libtorrent::entry *result = 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::entry **)&jarg1;
if(!jarg2) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return 0;
}
const char *arg2_pstr = (const char *)jenv->GetStringUTFChars(jarg2, 0);
if (!arg2_pstr) return 0;
std::string arg2_str(arg2_pstr);
arg2 = &arg2_str;
jenv->ReleaseStringUTFChars(jarg2, arg2_pstr);
{
try {
result = (libtorrent::entry *) &libtorrent_entry_get(arg1,(std::string const &)*arg2);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
*(libtorrent::entry **)&jresult = result;
return jresult;
}


SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1set_1_1SWIG_10(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2, jstring jarg3) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
std::string *arg3 = 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::entry **)&jarg1;
if(!jarg2) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return ;
}
const char *arg2_pstr = (const char *)jenv->GetStringUTFChars(jarg2, 0);
if (!arg2_pstr) return ;
std::string arg2_str(arg2_pstr);
arg2 = &arg2_str;
jenv->ReleaseStringUTFChars(jarg2, arg2_pstr);
if(!jarg3) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return ;
}
const char *arg3_pstr = (const char *)jenv->GetStringUTFChars(jarg3, 0);
if (!arg3_pstr) return ;
std::string arg3_str(arg3_pstr);
arg3 = &arg3_str;
jenv->ReleaseStringUTFChars(jarg3, arg3_pstr);
{
try {
libtorrent_entry_set__SWIG_0(arg1,(std::string const &)*arg2,(std::string const &)*arg3);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
}


SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1set_1_1SWIG_11(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2, jint jarg3) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
long *arg3 = 0 ;
long temp3 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::entry **)&jarg1;
if(!jarg2) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return ;
}
const char *arg2_pstr = (const char *)jenv->GetStringUTFChars(jarg2, 0);
if (!arg2_pstr) return ;
std::string arg2_str(arg2_pstr);
arg2 = &arg2_str;
jenv->ReleaseStringUTFChars(jarg2, arg2_pstr);
temp3 = (long)jarg3;
arg3 = &temp3;
{
try {
libtorrent_entry_set__SWIG_1(arg1,(std::string const &)*arg2,(long const &)*arg3);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1bencode(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
Expand Down Expand Up @@ -61009,7 +61123,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

(void)jenv;
(void)jcls;
result = (char *)("9765a1d65921c6a18b30812c3f25e79f98d90e0d");
result = (char *)("56f397e662d7a82306507668837a5fdf3af77395");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down

0 comments on commit d023bb9

Please sign in to comment.