Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: jni/Utils.h

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Created April 11, 2014, 1:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;")))); 123 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
124 } 124 }
125 125
126 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 126 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
127 { 127 {
128 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE; 128 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
129 } 129 }
130 130
131 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name) 131 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
132 { 132 {
133 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I")); 133 return (int32_t)env->GetIntField(jObj, env->GetFieldID(clazz, name, "I"));
134 } 134 }
135 135
136 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 136 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
137 { 137 {
138 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J")); 138 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J"));
139 } 139 }
140 140
141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) 141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
142 { 142 {
143 if (!filter.get())
144 {
145 return 0;
146 }
147
143 jclass clazz = env->FindClass(PKG("Filter")); 148 jclass clazz = env->FindClass(PKG("Filter"));
144 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 149 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
145 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter))); 150 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
146 } 151 }
147 152
148 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 153 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
149 { 154 {
155 if (!subscription.get())
156 {
157 return 0;
158 }
159
150 jclass clazz = env->FindClass(PKG("Subscription")); 160 jclass clazz = env->FindClass(PKG("Subscription"));
151 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 161 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
152 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription))); 162 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
153 } 163 }
154 164
155 #define CATCH_AND_THROW(jEnv) \ 165 #define CATCH_AND_THROW(jEnv) \
156 catch (const std::exception& except) \ 166 catch (const std::exception& except) \
157 { \ 167 { \
158 JniThrowException(jEnv, except); \ 168 JniThrowException(jEnv, except); \
159 } \ 169 } \
160 catch (...) \ 170 catch (...) \
161 { \ 171 { \
162 JniThrowException(jEnv); \ 172 JniThrowException(jEnv); \
163 } 173 }
164 174
165 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ 175 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
166 catch (const std::exception& except) \ 176 catch (const std::exception& except) \
167 { \ 177 { \
168 JniThrowException(jEnv, except); \ 178 JniThrowException(jEnv, except); \
169 return retVal; \ 179 return retVal; \
170 } \ 180 } \
171 catch (...) \ 181 catch (...) \
172 { \ 182 { \
173 JniThrowException(jEnv); \ 183 JniThrowException(jEnv); \
174 return retVal; \ 184 return retVal; \
175 } 185 }
176 186
177 const std::string GetString(JNIEnv *pEnv, jstring str);
178
179 template<class T>
180 T TrimString(T text)
181 {
182 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
183 T trimmed(text);
184 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace))));
185 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
186 return trimmed;
187 }
188
189 #endif 187 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld