From fd36ee37d9a2810ec25eac3ced1e5382ec00825a Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Thu, 7 Sep 2017 21:40:51 +0200 Subject: [PATCH] Some convenience methods for RBA/RDB methods. --- src/rdb/rdb/gsiDeclRdb.cc | 77 ++++++++++++++++++++++++++++++++++++++- testdata/ruby/rdbTest.rb | 33 ++++++++++++++++- 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/rdb/rdb/gsiDeclRdb.cc b/src/rdb/rdb/gsiDeclRdb.cc index a8e118a9f..66e786091 100644 --- a/src/rdb/rdb/gsiDeclRdb.cc +++ b/src/rdb/rdb/gsiDeclRdb.cc @@ -718,12 +718,18 @@ static void add_value (rdb::Item *item, const rdb::ValueWrapper &value) item->values ().add (value); } +template +static void add_value_t (rdb::Item *item, const T &value) +{ + add_value (item, rdb::ValueWrapper (new rdb::Value (value))); +} + static void clear_values (rdb::Item *item) { item->set_values (rdb::Values ()); } -Class decl_RdbItem ("RdbItem", +Class decl_RdbItem ("RdbItem", gsi::method ("database", (const rdb::Database *(rdb::Item::*)() const) &rdb::Item::database, "@brief Gets the database object that item is associated with\n" "\n" @@ -795,6 +801,42 @@ Class decl_RdbItem ("RdbItem", "@args value\n" "@param value The value to add.\n" ) + + gsi::method_ext ("add_value", &add_value_t, + "@brief Adds a polygon object to the values of this item\n" + "@args value\n" + "@param value The polygon to add.\n" + "This method has been introduced in version 0.25 as a convenience method." + ) + + gsi::method_ext ("add_value", &add_value_t, + "@brief Adds a box object to the values of this item\n" + "@args value\n" + "@param value The box to add.\n" + "This method has been introduced in version 0.25 as a convenience method." + ) + + gsi::method_ext ("add_value", &add_value_t, + "@brief Adds an edge object to the values of this item\n" + "@args value\n" + "@param value The edge to add.\n" + "This method has been introduced in version 0.25 as a convenience method." + ) + + gsi::method_ext ("add_value", &add_value_t, + "@brief Adds an edge pair object to the values of this item\n" + "@args value\n" + "@param value The edge pair to add.\n" + "This method has been introduced in version 0.25 as a convenience method." + ) + + gsi::method_ext ("add_value", &add_value_t, + "@brief Adds a string object to the values of this item\n" + "@args value\n" + "@param value The string to add.\n" + "This method has been introduced in version 0.25 as a convenience method." + ) + + gsi::method_ext ("add_value", &add_value_t, + "@brief Adds a numeric value to the values of this item\n" + "@args value\n" + "@param value The value to add.\n" + "This method has been introduced in version 0.25 as a convenience method." + ) + gsi::method_ext ("clear_values", &clear_values, "@brief Removes all values from this item\n" ) + @@ -963,6 +1005,26 @@ void create_items_from_edge_pair_array (rdb::Database *db, rdb::id_type cell_id, } } +static rdb::Item *create_item (rdb::Database *db, rdb::id_type cell_id, rdb::id_type cat_id) +{ + if (! db->cell_by_id (cell_id)) { + throw tl::Exception (tl::to_string (QObject::tr ("Not a valid cell ID: %1").arg (cell_id))); + } + if (! db->category_by_id (cat_id)) { + throw tl::Exception (tl::to_string (QObject::tr ("Not a valid category ID: %1").arg (cat_id))); + } + return db->create_item (cell_id, cat_id); +} + +static rdb::Item *create_item_from_objects (rdb::Database *db, rdb::Cell *cell, rdb::Category *cat) +{ + if (cell && cat) { + return db->create_item (cell->id (), cat->id ()); + } else { + return 0; + } +} + Class decl_ReportDatabase ("ReportDatabase", gsi::constructor ("new", &create_rdb, "@brief Creates a report database\n" @@ -1147,11 +1209,22 @@ Class decl_ReportDatabase ("ReportDatabase", "@param category_id The ID of the category for which to retrieve the number\n" "@return The total number of items visited for the given cell and the given category\n" ) + - gsi::method ("create_item", &rdb::Database::create_item, + gsi::method_ext ("create_item", &create_item, "@brief Creates a new item for the given cell/category combination\n" "@args cell_id, category_id\n" "@param cell_id The ID of the cell to which the item is associated\n" "@param category_id The ID of the category to which the item is associated\n" + "\n" + "A more convenient method that takes cell and category objects instead of ID's is the " + "other version of \\create_item.\n" + ) + + gsi::method_ext ("create_item", &create_item_from_objects, + "@brief Creates a new item for the given cell/category combination\n" + "@args cell, category\n" + "@param cell The cell to which the item is associated\n" + "@param category The category to which the item is associated\n" + "\n" + "This convenience method has been added in version 0.25.\n" ) + gsi::method_ext ("create_items", &create_items_from_region, "@brief Creates new polygon items for the given cell/category combination\n" diff --git a/testdata/ruby/rdbTest.rb b/testdata/ruby/rdbTest.rb index 86f34dd09..5a040b66e 100644 --- a/testdata/ruby/rdbTest.rb +++ b/testdata/ruby/rdbTest.rb @@ -138,7 +138,21 @@ class RDB_TestClass < TestBase assert_equal(cat.num_items_visited, 0) assert_equal(cats.num_items_visited, 0) - item2 = db.create_item(cell2.rdb_id, cats.rdb_id) + begin + item = db.create_item(1000, cat.rdb_id) + assert_equal(false, true) + rescue => ex + assert_equal(ex.to_s, "Not a valid cell ID: 1000 in ReportDatabase::create_item") + end + + begin + item = db.create_item(cell.rdb_id, 1001) + assert_equal(false, true) + rescue => ex + assert_equal(ex.to_s, "Not a valid category ID: 1001 in ReportDatabase::create_item") + end + + item2 = db.create_item(cell2, cats) assert_equal(db.num_items, 2) assert_equal(db.num_items_visited, 0) assert_equal(db.num_items(cell2.rdb_id, cats.rdb_id), 1) @@ -429,6 +443,23 @@ class RDB_TestClass < TestBase item.each_value { |v| vv.push(v) } assert_equal(vv.size, 0) + item.clear_values + item.add_value(1.0) + item.add_value("hello") + item.add_value(RBA::DPolygon::new(RBA::DBox::new(1, 2, 3, 4))) + item.add_value(RBA::DBox::new(11, 12, 13, 14)) + item.add_value(RBA::DEdge::new(21, 22, 23, 24)) + item.add_value(RBA::DEdgePair::new(RBA::DEdge::new(31, 32, 33, 34), RBA::DEdge::new(41, 42, 43, 44))) + vv=[] + item.each_value { |v| vv.push(v) } + assert_equal(vv.size, 6) + assert_equal(vv[0].to_s, "float: 1") + assert_equal(vv[1].to_s, "text: hello") + assert_equal(vv[2].to_s, "polygon: (1,2;1,4;3,4;3,2)") + assert_equal(vv[3].to_s, "box: (11,12;13,14)") + assert_equal(vv[4].to_s, "edge: (21,22;23,24)") + assert_equal(vv[5].to_s, "edge-pair: (31,32;33,34)/(41,42;43,44)") + end # Multiple items