PPL Configured Java Language Interface  1.2
ppl_java_common_defs.hh
Go to the documentation of this file.
1 /* Domain-independent part of the Java interface: declarations.
2  Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3  Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_ppl_java_common_defs_hh
25 #define PPL_ppl_java_common_defs_hh 1
26 
27 #define PPL_NO_AUTOMATIC_INITIALIZATION
28 #include "ppl.hh"
29 
30 #include <jni.h>
31 #include "interfaced_boxes.hh"
32 #include "marked_pointers.hh"
33 
34 #define PPL_JAVA_UNEXPECTED \
35  throw std::runtime_error("PPL Java interface internal error")
36 
37 #define CATCH_ALL \
38  catch(const Java_ExceptionOccurred& e) { \
39  } \
40  catch(const std::overflow_error& e) { \
41  handle_exception(env, e); \
42  } \
43  catch(const std::length_error& e) { \
44  handle_exception(env, e); \
45  } \
46  catch (const std::bad_alloc& e) { \
47  handle_exception(env, e); \
48  } \
49  catch (const std::domain_error& e) { \
50  handle_exception(env, e); \
51  } \
52  catch (const std::invalid_argument& e) { \
53  handle_exception(env, e); \
54  } \
55  catch(const std::logic_error& e) { \
56  handle_exception(env, e); \
57  } \
58  catch (const std::exception& e) { \
59  handle_exception(env, e); \
60  } \
61  catch (const timeout_exception& e) { \
62  handle_exception(env, e); \
63  } \
64  catch (const deterministic_timeout_exception& e) { \
65  handle_exception(env, e); \
66  } \
67  catch (...) { \
68  handle_exception(env); \
69  }
70 
71 #define CHECK_EXCEPTION_ASSERT(env) \
72  assert(!env->ExceptionOccurred())
73 #define CHECK_EXCEPTION_THROW(env) \
74  do { \
75  if (env->ExceptionOccurred()) \
76  throw Java_ExceptionOccurred(); \
77  } while (0)
78 #define CHECK_EXCEPTION_RETURN(env, val) \
79  do { \
80  if (env->ExceptionOccurred()) \
81  return val; \
82  } while (0)
83 #define CHECK_EXCEPTION_RETURN_VOID(env) \
84  do { \
85  if (env->ExceptionOccurred()) \
86  return; \
87  } while (0)
88 #define CHECK_RESULT_ABORT(env, cond) \
89  do { \
90  if (!(cond)) \
91  abort(); \
92  } while (0)
93 #define CHECK_RESULT_ASSERT(env, cond) \
94  assert(cond)
95 #define CHECK_RESULT_THROW(env, cond) \
96  do { \
97  if (!(cond)) \
98  throw Java_ExceptionOccurred(); \
99  } while (0)
100 #define CHECK_RESULT_RETURN(env, cond, val) \
101  do { \
102  if (!(cond)) \
103  return val; \
104  } while (0)
105 #define CHECK_RESULT_RETURN_VOID(env, cond) \
106  do { \
107  if (!(cond)) \
108  return; \
109  } while (0)
110 
111 
112 namespace Parma_Polyhedra_Library {
113 
114 namespace Interfaces {
115 
116 namespace Java {
117 
118 struct Java_ExceptionOccurred : public std::exception {
119 };
120 
123 public:
124  void throw_me() const {
125  throw *this;
126  }
127  int priority() const {
128  return 0;
129  }
130 };
131 
134 public:
135  void throw_me() const {
136  throw *this;
137  }
138  int priority() const {
139  return 0;
140  }
141 };
142 
143 extern Parma_Polyhedra_Library::Watchdog* p_timeout_object;
144 
145 typedef
148 
149 extern Weightwatch* p_deterministic_timeout_object;
150 
151 void reset_timeout();
152 
153 void reset_deterministic_timeout();
154 
155 void
156 handle_exception(JNIEnv* env, const std::logic_error& e);
157 
158 void
159 handle_exception(JNIEnv* env, const std::invalid_argument& e);
160 
161 void
162 handle_exception(JNIEnv* env, const std::domain_error& e);
163 
164 void
165 handle_exception(JNIEnv* env, const std::overflow_error& e);
166 
167 void
168 handle_exception(JNIEnv* env, const std::length_error& e);
169 
170 void
171 handle_exception(JNIEnv* env, const std::bad_alloc&);
172 
173 void
174 handle_exception(JNIEnv* env, const std::exception& e);
175 
176 void
177 handle_exception(JNIEnv* env, const timeout_exception& e);
178 
179 void
180 handle_exception(JNIEnv* env, const deterministic_timeout_exception& e);
181 
182 void
183 handle_exception(JNIEnv* env);
184 
186 
191 public:
192  // The Java VM pointer (not a class, but sometimes needed).
193  JavaVM* jvm;
194  // Non-PPL types.
195  jclass Boolean;
196  jclass Integer;
197  jclass Long;
198  jclass Iterator;
199  // PPL types.
205  jclass By_Reference;
206  jclass Coefficient;
207  jclass Congruence;
208  jclass Constraint;
209  jclass Generator;
226  jclass Pair;
232  jclass PPL_Object;
234  jclass Variable;
237 
240 
242  void init_cache(JNIEnv* env);
243 
245  void clear_cache(JNIEnv* env);
246 
247 private:
249  void init_cache(JNIEnv* env, jclass& field, const char* name);
250 
252  void clear_cache(JNIEnv* env, jclass& field);
253 
254  // Private and not implemented: copy construction not allowed.
256  // Private and not implemented: copy assignment not allowed.
258 };
259 
261 
268  // Non-PPL type method IDs: stored when loading Coefficient.
273  jmethodID Long_valueOf_ID;
274  jmethodID Long_longValue_ID;
275 
276  // PPL type field and method IDs.
277  // Artificial_Parameter.
283  // Bounded_Integer_Type_Overflow.
288  // Bounded_Integer_Type_Representation.
292  // Bounded_Integer_Type_Width.
299  // By_Reference.
302  // Coefficient.
306  // Complexity_Class.
308  // Congruence.
313  // Constraint.
318  // Degenerate_Element.
320  // Generator.
321  jfieldID Generator_gt_ID;
322  jfieldID Generator_le_ID;
324  jmethodID Generator_line_ID;
325  jmethodID Generator_ray_ID;
328  // Grid_Generator.
335  // (Grid_) Generator_Type.
338  // Systems of Constraint, Congruence, Generator and Grid_Generator.
347  // System_Iterator: stored when loading Constraint_System.
351  // Linear_Expression.
354  // Classes extending Linear_Expression.
367  // MIP_Problem_Status.
372  // Optmization_Mode.
376  // PIP_Problem_Control_Parameter_Name.
380  // PIP_Problem_Control_Parameter_Value.
387  // PIP_Problem_Status.
391  // Pair.
392  jfieldID Pair_first_ID;
393  jfieldID Pair_second_ID;
394  // Poly_Con_Relation and Poly_Gen_Relation.
397  // PPL_Object.
399  // Relation_Symbol.
404  // Variable.
407  jmethodID Variable_init_ID;
408  // Variable_Stringifier.
410  // Variables_Set.
414  // Iterators on Variables_Set.
417 };
418 
420 extern Java_Class_Cache cached_classes;
421 
423 extern Java_FMID_Cache cached_FMIDs;
424 
425 
435 template <typename U, typename V>
436 U
437 jtype_to_unsigned(const V& value);
438 
440 jobject
441 bool_to_j_boolean_class(JNIEnv* env, const bool value);
442 
444 jint
445 j_integer_to_j_int(JNIEnv* env, jobject j_integer);
446 
448 jobject
449 j_int_to_j_integer(JNIEnv* env, jint value);
450 
452 jlong
453 j_long_class_to_j_long(JNIEnv* env, jobject j_long);
454 
456 jobject
457 j_long_to_j_long_class(JNIEnv* env, jlong value);
458 
463 bool
464 is_java_marked(JNIEnv* env, jobject ppl_object);
465 
466 
471 jobject
472 build_java_poly_gen_relation(JNIEnv* env, Poly_Gen_Relation& r);
473 
478 jobject
479 build_java_poly_con_relation(JNIEnv* env, Poly_Con_Relation& r);
480 
486 build_cxx_variables_set(JNIEnv* env, jobject v_set);
487 
492 jobject
493 build_java_variables_set(JNIEnv* env, const Variables_Set& v_set);
494 
500 build_cxx_relsym(JNIEnv* env, jobject j_relsym);
501 
508 build_cxx_bounded_overflow(JNIEnv* env, jobject j_bounded_overflow);
509 
516 build_cxx_bounded_width(JNIEnv* env, jobject j_bounded_width);
517 
524 build_cxx_bounded_rep(JNIEnv* env, jobject j_bounded_rep);
525 
531 build_cxx_optimization_mode(JNIEnv* env, jobject j_opt_mode);
532 
537 jobject
538 build_java_optimization_mode(JNIEnv* env, const Optimization_Mode& opt_mode);
539 
545 build_cxx_control_parameter_name(JNIEnv* env, jobject j_cp_name);
546 
551 jobject
552 build_java_control_parameter_name
553 (JNIEnv* env, const MIP_Problem::Control_Parameter_Name& cp_name);
554 
560 build_cxx_control_parameter_value(JNIEnv* env, jobject j_cp_value);
561 
566 jobject
567 build_java_control_parameter_value
568 (JNIEnv* env, const MIP_Problem::Control_Parameter_Value& cp_value);
569 
576 build_cxx_pip_problem_control_parameter_name(JNIEnv* env, jobject j_cp_name);
577 
582 jobject
583 build_java_pip_problem_control_parameter_name
584 (JNIEnv* env, const PIP_Problem::Control_Parameter_Name& cp_name);
585 
592 build_cxx_pip_problem_control_parameter_value(JNIEnv* env, jobject j_cp_value);
593 
598 jobject
599 build_java_pip_problem_control_parameter_value
600 (JNIEnv* env, const PIP_Problem::Control_Parameter_Value& cp_value);
601 
606 jobject
607 build_java_mip_status(JNIEnv* env, const MIP_Problem_Status& mip_status);
608 
613 jobject
614 build_java_pip_status(JNIEnv* env, const PIP_Problem_Status& pip_status);
615 
620 Variable
621 build_cxx_variable(JNIEnv* env, jobject j_var);
622 
627 jobject
628 build_java_variable(JNIEnv* env, const Variable var);
629 
637 void
638 Java_Variable_output_function(std::ostream& s, Variable v);
639 
645 build_cxx_coeff(JNIEnv* env, jobject j_coeff);
646 
651 jobject
652 build_java_coeff(JNIEnv* env, const Coefficient& ppl_coeff);
653 
659 build_cxx_constraint(JNIEnv* env, jobject j_constraint);
660 
666 PIP_Tree_Node::Artificial_Parameter
667 build_cxx_artificial_parameter(JNIEnv* env, jobject j_ap);
668 
674 build_cxx_linear_expression(JNIEnv* env, jobject j_le);
675 
681 build_cxx_congruence(JNIEnv* env, jobject j_cg);
682 
687 Generator
688 build_cxx_generator(JNIEnv* env, jobject j_g);
689 
695 build_cxx_grid_generator(JNIEnv* env, jobject j_g);
696 
702 build_cxx_grid_generator_system(JNIEnv* env, jobject j_gs);
703 
709 build_cxx_constraint_system(JNIEnv* env, jobject j_cs);
710 
715 PIP_Tree_Node::Artificial_Parameter
716 build_cxx_artificial_parameter(JNIEnv* env, jobject j_art_param);
717 
723 build_cxx_artificial_parameter_sequence(JNIEnv* env, jobject j_aps);
724 
730 build_cxx_generator_system(JNIEnv* env, jobject j_gs);
731 
737 build_cxx_congruence_system(JNIEnv* env, jobject j_cgs);
738 
743 jobject
744 build_java_constraint(JNIEnv* env, const Constraint& c);
745 
750 jobject
751 build_java_artificial_parameter(JNIEnv* env,
752  const PIP_Tree_Node::Artificial_Parameter& ap);
753 
758 jobject
759 build_java_congruence(JNIEnv* env, const Congruence& cg);
760 
765 jobject
766 build_java_generator(JNIEnv* env, const Generator& g);
767 
772 jobject
773 build_java_grid_generator(JNIEnv* env, const Grid_Generator& g);
774 
779 jobject
780 build_java_constraint_system(JNIEnv* env, const Constraint_System& cs);
781 
786 jobject
787 build_java_artificial_parameter_sequence
788 (JNIEnv* env, const PIP_Tree_Node::Artificial_Parameter_Sequence& aps);
789 
794 jobject
795 build_java_grid_generator_system(JNIEnv* env, const Grid_Generator_System& gs);
796 
801 jobject
802 build_java_generator_system(JNIEnv* env, const Generator_System& gs);
803 
808 jobject
809 build_java_congruence_system(JNIEnv* env, const Congruence_System& cgs);
810 
815 jobject
816 build_java_linear_expression_coefficient(JNIEnv* env, const Coefficient& coeff);
817 
822 void
823 set_generator(JNIEnv* env, jobject dst, jobject src);
824 
828 void
829 set_coefficient(JNIEnv* env, jobject dst, jobject src);
830 
835 void
836 set_by_reference(JNIEnv* env, jobject by_ref_dst, jobject src);
837 
842 jobject
843 get_by_reference(JNIEnv* env, jobject by_reference);
844 
845 
856 void
857 set_pair_element(JNIEnv* env, jobject dst_pair, int arg, jobject src);
858 
869 jobject
870 get_pair_element(JNIEnv* env, int arg, jobject pair);
871 
873 void*
874 get_ptr(JNIEnv* env, jobject ppl_object);
875 
877 template <typename T>
878 void
879 set_ptr(JNIEnv* env, jobject ppl_object,
880  const T* address, bool to_be_marked = false);
881 
886 template <typename R>
887 jobject
888 build_linear_expression(JNIEnv* env, const R& r);
889 
890 } // namespace Java
891 
892 } // namespace Interfaces
893 
894 } // namespace Parma_Polyhedra_Library
895 
896 
898 
899 #endif // !defined(PPL_ppl_prolog_common_defs_hh)
value
A cache for global references to Java classes.
std::vector< Artificial_Parameter > Artificial_Parameter_Sequence
A cache for field and method IDs of Java classes.
PPL_COEFFICIENT_TYPE Coefficient
void clear_cache(JNIEnv *env)
Resets all fields to NULL.
Java_Class_Cache & operator=(const Java_Class_Cache &)
void init_cache(JNIEnv *env)
Initializes all cache fields.