File: sample_for_tests.cc

package info (click to toggle)
chromium-browser 37.0.2062.120-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,707,260 kB
  • sloc: cpp: 8,976,677; ansic: 3,473,199; python: 586,578; asm: 449,013; xml: 184,195; java: 142,924; sh: 118,496; perl: 81,467; makefile: 27,557; yacc: 10,506; objc: 8,886; tcl: 3,186; cs: 2,252; lex: 2,213; sql: 1,198; pascal: 1,170; lisp: 790; awk: 407; ruby: 155; php: 83; sed: 52; exp: 11
file content (113 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/jni_generator/sample_for_tests.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "jni/SampleForTests_jni.h"


using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;

namespace base {
namespace android {

jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject obj) {
  return 0.0;
}

CPPClass::CPPClass() {
}

CPPClass::~CPPClass() {
}

void CPPClass::Destroy(JNIEnv* env, jobject obj) {
  delete this;
}

jint CPPClass::Method(JNIEnv* env, jobject obj) {
  return 0;
}

void CPPClass::AddStructB(JNIEnv* env, jobject obj, jobject structb) {
  long key = Java_InnerStructB_getKey(env, structb);
  std::string value = ConvertJavaStringToUTF8(
      env, Java_InnerStructB_getValue(env, structb).obj());
  map_[key] = value;
}

void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj) {
  // Iterate over the elements and do something with them.
  for (std::map<long, std::string>::const_iterator it = map_.begin();
       it != map_.end(); ++it) {
    long key = it->first;
    std::string value = it->second;
  }
  map_.clear();
}

base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString(
    JNIEnv* env, jobject obj) {
  base::android::ScopedJavaLocalRef<jstring> ret = ConvertUTF8ToJavaString(
      env, "test");
  return ret;
}

// Static free functions declared and called directly from java.
static jlong Init(JNIEnv* env, jobject obj, jstring param) {
  return 0;
}

static jdouble GetDoubleFunction(JNIEnv*, jobject) {
  return 0;
}

static jfloat GetFloatFunction(JNIEnv*, jclass) {
  return 0;
}

static void SetNonPODDatatype(JNIEnv*, jobject, jobject) {
}

static jobject GetNonPODDatatype(JNIEnv*, jobject) {
  return NULL;
}

static jint InnerFunction(JNIEnv*, jclass) {
  return 0;
}

} // namespace android
} // namespace base

int main() {
  // On a regular application, you'd call AttachCurrentThread(). This sample is
  // not yet linking with all the libraries.
  JNIEnv* env = /* AttachCurrentThread() */ NULL;

  // This is how you call a java static method from C++.
  bool foo = base::android::Java_SampleForTests_staticJavaMethod(env);

  // This is how you call a java method from C++. Note that you must have
  // obtained the jobject somehow.
  jobject my_java_object = NULL;
  int bar = base::android::Java_SampleForTests_javaMethod(
      env, my_java_object, 1, 2);

  for (int i = 0; i < 10; ++i) {
    // Creates a "struct" that will then be used by the java side.
    ScopedJavaLocalRef<jobject> struct_a =
        base::android::Java_InnerStructA_create(
            env, 0, 1,
            base::android::ConvertUTF8ToJavaString(env, "test").obj());
    base::android::Java_SampleForTests_addStructA(
        env, my_java_object, struct_a.obj());
  }
  base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object);
  return 0;
}