oo_bindgen/backend/c/cpp/conversion/
to_cpp.rs

1use crate::model::*;
2
3/// Some types have a C -> C++ conversion that is context independent
4pub(crate) trait ToCpp {
5    fn to_cpp(&self, expr: String) -> String;
6}
7
8impl ToCpp for DurationType {
9    fn to_cpp(&self, expr: String) -> String {
10        match self {
11            DurationType::Milliseconds => format!("::convert::from_milli_sec_u64({expr})"),
12            DurationType::Seconds => format!("::convert::from_sec_u64({expr})"),
13        }
14    }
15}
16
17impl<D> ToCpp for Handle<Enum<D>>
18where
19    D: DocReference,
20{
21    fn to_cpp(&self, expr: String) -> String {
22        format!("::convert::to_cpp({expr})")
23    }
24}
25
26impl ToCpp for Primitive {
27    fn to_cpp(&self, expr: String) -> String {
28        match self {
29            Self::Bool => expr,
30            Self::U8 => expr,
31            Self::S8 => expr,
32            Self::U16 => expr,
33            Self::S16 => expr,
34            Self::U32 => expr,
35            Self::S32 => expr,
36            Self::U64 => expr,
37            Self::S64 => expr,
38            Self::Float => expr,
39            Self::Double => expr,
40        }
41    }
42}
43
44impl ToCpp for BasicType {
45    fn to_cpp(&self, expr: String) -> String {
46        match self {
47            Self::Primitive(x) => x.to_cpp(expr),
48            Self::Duration(x) => x.to_cpp(expr),
49            Self::Enum(x) => x.to_cpp(expr),
50        }
51    }
52}
53
54impl ToCpp for StringType {
55    fn to_cpp(&self, expr: String) -> String {
56        format!("std::string({expr})")
57    }
58}
59
60impl ToCpp for ClassDeclarationHandle {
61    fn to_cpp(&self, expr: String) -> String {
62        format!("::convert::to_cpp({expr})")
63    }
64}