Skip to content

Commit

Permalink
FFI: Adds DiscreteValueRule bindings. (#107)
Browse files Browse the repository at this point in the history
Signed-off-by: Franco Cipollone <[email protected]>
  • Loading branch information
francocipollone authored Jun 5, 2024
1 parent 30f41ec commit 2854b11
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions maliput-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/api/api.h");
println!("cargo:rerun-if-changed=src/api/mod.rs");
println!("cargo:rerun-if-changed=src/api/rules/aliases.h");
println!("cargo:rerun-if-changed=src/api/rules/rules.h");
println!("cargo:rerun-if-changed=src/api/rules/mod.rs");
println!("cargo:rerun-if-changed=src/lib.rs");
Expand Down
45 changes: 45 additions & 0 deletions maliput-sys/src/api/rules/aliases.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// BSD 3-Clause License
//
// Copyright (c) 2024, Woven by Toyota.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <maliput/api/rules/discrete_value_rule.h>

#include <memory>
#include <vector>

namespace maliput {
namespace api {
namespace rules {

// Workaround for supporting nested types: https://github.com/dtolnay/cxx/issues/1198
using DiscreteValueRuleDiscreteValue = DiscreteValueRule::DiscreteValue;

} // namespace rules
} // namespace api
} // namespace maliput
34 changes: 34 additions & 0 deletions maliput-sys/src/api/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ pub mod ffi {
struct FloatWrapper {
pub value: f64,
}
/// Shared struct for pairs in a RelatedRules collection.
/// - key: Group name of the rules.
/// - value: Rule ids.
/// This is needed because maps can't be binded directly.
struct RelatedRule {
pub group_name: String,
pub rule_ids: Vec<String>,
}
/// Shared struct for pairs in a RelatedRules collection.
/// - key: Group name.
/// - value: Unique Ids.
/// This is needed because maps can't be binded directly.
struct RelatedUniqueId {
pub group_name: String,
pub unique_id: Vec<String>,
}

#[repr(i32)]
enum BulbColor {
Expand All @@ -78,6 +94,7 @@ pub mod ffi {

unsafe extern "C++" {
include!("api/rules/rules.h");
include!("api/rules/aliases.h");

// Forward declarations
#[namespace = "maliput::api"]
Expand Down Expand Up @@ -142,5 +159,22 @@ pub mod ffi {
fn string(self: &UniqueBulbGroupId) -> &CxxString;
fn UniqueBulbGroupId_traffic_light_id(id: &UniqueBulbGroupId) -> String;
fn UniqueBulbGroupId_bulb_group_id(id: &UniqueBulbGroupId) -> String;

// DiscreteValueRule::DiscreteValue bindings definitions.
type DiscreteValueRuleDiscreteValue;
fn DiscreteValueRuleDiscreteValue_value(value: &DiscreteValueRuleDiscreteValue) -> String;
fn DiscreteValueRuleDiscreteValue_severity(value: &DiscreteValueRuleDiscreteValue) -> i32;
fn DiscreteValueRuleDiscreteValue_related_rules(
value: &DiscreteValueRuleDiscreteValue,
) -> UniquePtr<CxxVector<RelatedRule>>;
fn DiscreteValueRuleDiscreteValue_related_unique_ids(
value: &DiscreteValueRuleDiscreteValue,
) -> UniquePtr<CxxVector<RelatedUniqueId>>;

// DiscreteValueRule bindings definitions.
type DiscreteValueRule;
fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
}
}
40 changes: 40 additions & 0 deletions maliput-sys/src/api/rules/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,46 @@ rust::String UniqueBulbGroupId_bulb_group_id(const UniqueBulbGroupId& id) {
return id.bulb_group_id().string();
}

rust::String DiscreteValueRuleDiscreteValue_value(const DiscreteValueRuleDiscreteValue& discrete_value) {
return rust::String(discrete_value.value);
}

rust::i32 DiscreteValueRuleDiscreteValue_severity(const DiscreteValueRuleDiscreteValue& discrete_value) {
return discrete_value.severity;
}

std::unique_ptr<std::vector<RelatedRule>> DiscreteValueRuleDiscreteValue_related_rules(const DiscreteValueRuleDiscreteValue& discrete_value) {
std::vector<RelatedRule> related_rules;
for (const auto& related_rule : discrete_value.related_rules) {
rust::Vec<rust::String> rule_ids;
for (const auto& rule_id : related_rule.second) {
rule_ids.push_back({rule_id.string()});
}
related_rules.push_back({related_rule.first, rule_ids});
}
return std::make_unique<std::vector<RelatedRule>>(std::move(related_rules));
}

std::unique_ptr<std::vector<RelatedUniqueId>> DiscreteValueRuleDiscreteValue_related_unique_ids(const DiscreteValueRuleDiscreteValue& discrete_value) {
std::vector<RelatedUniqueId> related_unique_ids;
for (const auto& related_unique_id : discrete_value.related_unique_ids) {
rust::Vec<rust::String> unique_ids;
for (const auto& rule_id : related_unique_id.second) {
unique_ids.push_back({rule_id.string()});
}
related_unique_ids.push_back({related_unique_id.first, unique_ids});
}
return std::make_unique<std::vector<RelatedUniqueId>>(std::move(related_unique_ids));
}

rust::String DiscreteValueRule_id(const DiscreteValueRule& rule) {
return rule.id().string();
}

rust::String DiscreteValueRule_type_id(const DiscreteValueRule& rule) {
return rule.type_id().string();
}

} // namespace rules
} // namespace api
} // namespace maliput

0 comments on commit 2854b11

Please sign in to comment.