1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "simulator_resource_attributes_jni.h"
22 #include "simulator_resource_model.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_error_codes.h"
26 extern SimulatorClassRefs gSimulatorClassRefs;
28 jobject JniSimulatorResourceAttribute::toJava(JNIEnv *env, jlong resource)
30 jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceAttribute,
31 gSimulatorClassRefs.classSimulatorResourceAttributeCtor, resource);
32 if (NULL == resourceObj)
40 class attribute_value_visitor : public boost::static_visitor<jobject>
43 attribute_value_visitor(JNIEnv *env) : m_Env(env) {}
45 jobject operator ()(const int &value) const
47 jobject result = m_Env->NewObject(gSimulatorClassRefs.classInteger,
48 gSimulatorClassRefs.classIntegerCtor, value);
52 jobject operator ()(const double &value) const
54 jobject result = m_Env->NewObject(gSimulatorClassRefs.classDouble,
55 gSimulatorClassRefs.classDoubleCtor, value);
59 jobject operator ()(const std::string &value) const
61 jstring str = m_Env->NewStringUTF(value.c_str());
62 return static_cast<jobject>(str);
69 JNIEXPORT void JNICALL
70 Java_org_oic_simulator_SimulatorResourceAttribute_create
71 (JNIEnv *env, jobject object, jstring attrName)
75 std::cout << "SimulatorResourceAttribute_create: AttributeName is Empty";
79 const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
83 std::string attrNameStr(attrNamePtr);
84 SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute(attrNameStr);
85 SetHandle<SimulatorResourceModel::Attribute>(env, object, attribute);
86 if (env->ExceptionCheck())
91 env->ReleaseStringUTFChars(attrName, attrNamePtr);
94 JNIEXPORT void JNICALL
95 Java_org_oic_simulator_SimulatorResourceAttribute_dispose
96 (JNIEnv *env, jobject object)
98 SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
103 JNIEXPORT int JNICALL
104 Java_org_oic_simulator_SimulatorResourceAttribute_allowedValuesSize
105 (JNIEnv *env, jobject object)
107 SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
109 if (env->ExceptionCheck() || !attribute)
111 return SIMULATOR_ERROR;
114 return attribute->getAllowedValuesSize();
117 JNIEXPORT jstring JNICALL
118 Java_org_oic_simulator_SimulatorResourceAttribute_valueToString
119 (JNIEnv *env, jobject object)
121 SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
123 if (env->ExceptionCheck() || !attribute)
128 std::string str = attribute->valueToString();
129 return env->NewStringUTF(str.c_str());
132 JNIEXPORT jstring JNICALL
133 Java_org_oic_simulator_SimulatorResourceAttribute_allowedValuesToString
134 (JNIEnv *env, jobject object)
136 SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
138 if (env->ExceptionCheck() || !attribute)
143 std::string str = attribute->allowedValuesToString();
144 return env->NewStringUTF(str.c_str());
147 JNIEXPORT jstring JNICALL
148 Java_org_oic_simulator_SimulatorResourceAttribute_getName
149 (JNIEnv *env, jobject object)
151 SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
153 if (env->ExceptionCheck() || !attribute)
158 std::string str = attribute->getName();
159 return env->NewStringUTF(str.c_str());
162 JNIEXPORT jobject JNICALL
163 Java_org_oic_simulator_SimulatorResourceAttribute_getValue
164 (JNIEnv *env, jobject object)
166 SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
168 if (env->ExceptionCheck() || !attribute)
173 return boost::apply_visitor(attribute_value_visitor(env), attribute->getValue());