|
| 1 | +// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @file IdlParser_main.cpp |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +#define FASTDDS_ENFORCE_LOG_INFO |
| 21 | + |
| 22 | +#include <DynamicTypeBuilderFactoryImpl.hpp> |
| 23 | +#include <fastrtps/log/Log.h> |
| 24 | + |
| 25 | +#include <iostream> |
| 26 | + |
| 27 | +using namespace eprosima::fastdds::dds; |
| 28 | +using eprosima::fastdds::dds::Log; |
| 29 | + |
| 30 | +int main( |
| 31 | + int argc, |
| 32 | + char** argv) |
| 33 | +{ |
| 34 | + Log::SetVerbosity(Log::Kind::Info); |
| 35 | + Log::SetCategoryFilter(std::regex("IDLPARSER")); |
| 36 | + |
| 37 | + std::cout << "Processing forward declarations:" << std::endl; |
| 38 | + std::string test00 = |
| 39 | + R"( |
| 40 | + struct StructDcl; |
| 41 | + union UnionDcl; |
| 42 | + )"; |
| 43 | + DynamicType::_ref_type fwd_dcl_type = DynamicTypeBuilderFactory::get_instance()->create_type_w_idl(test00)->build(); |
| 44 | + if (nullptr != fwd_dcl_type) |
| 45 | + { |
| 46 | + EPROSIMA_LOG_INFO(IDLPARSER, "Succeeded to create DynamicType from IDL: " << test00); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + EPROSIMA_LOG_ERROR(IDLPARSER, "Failed to create DynamicType from IDL: " << test00); |
| 51 | + } |
| 52 | + |
| 53 | + // std::cout << "Processing literals, expressions, and consts:" << std::endl; |
| 54 | + // std::string test01 = |
| 55 | + // R"( |
| 56 | + // // boolean literals in IDL are case sensitive and should be exactly TRUE or FALSE |
| 57 | + // const boolean C_BOOL = TRUE; |
| 58 | + // const string<100> C_STR = "hello"; |
| 59 | + // const int32 C_LITERALS1 = (0x5 | (4 + 3)) + (4 << 1) * 2; // 23 |
| 60 | + // const int32 C_LITERALS2 = (02 + 0x1) * 2; // 6 |
| 61 | + // const int32 C_LITERALS3 = 4 / ( 07 & 0x2 ); // 2 |
| 62 | + // const int32 C_LITERALS4 = -(5 * ( 03 + 0xF )); // -90 |
| 63 | + // const int32 C_LITERALS5 = 0xf0 & ~(4 * ( 0x7 - 02 )); // 224 |
| 64 | + // const int32 C_LITERALS6 = (0x7 | 0x9) & ~(6 * ( 024 - 5 )); // 5 |
| 65 | + // const float C_LITERALS7 = (2 + 4) / 3.0d; /* 2.0 */ |
| 66 | + // const float C_LITERALS8 = (2e0 + 4) / 3.0d; /* 2.0 */ |
| 67 | + // )"; |
| 68 | + // idl::Context context01 = idl::parse(test01); |
| 69 | + |
| 70 | + // std::cout << "Processing IDL string:" << std::endl; |
| 71 | + // std::string test02 = |
| 72 | + // R"( |
| 73 | + // struct InnerType |
| 74 | + // { |
| 75 | + // uint32 im1; |
| 76 | + // float im2; |
| 77 | + // }; |
| 78 | + // )"; |
| 79 | + // idl::Context context02 = idl::parse(test02); |
| 80 | + |
| 81 | + // std::cout << "Processing IDL file:" << std::endl; |
| 82 | + // idl::Context context03 = idl::parse_file("idl/test02.idl"); |
| 83 | + |
| 84 | + // std::string test04 = |
| 85 | + // R"( |
| 86 | + // enum e_test { a, b, c }; |
| 87 | + // )"; |
| 88 | + // idl::Context context04 = idl::parse(test04); |
| 89 | + |
| 90 | + // std::string test05 = |
| 91 | + // R"( |
| 92 | + // struct Test05 |
| 93 | + // { |
| 94 | + // long my_long; |
| 95 | + // short my_short; |
| 96 | + // long long my_llong; |
| 97 | + // int32 my_int; |
| 98 | + // unsigned short my_ushort; |
| 99 | + // unsigned long my_ulong; |
| 100 | + // long double my_ldouble; |
| 101 | + // float my_float; |
| 102 | + // double my_double; |
| 103 | + // }; |
| 104 | + // )"; |
| 105 | + // idl::Context context05 = idl::parse(test05); |
| 106 | + |
| 107 | + // std::string test06 = |
| 108 | + // R"( |
| 109 | + // struct Point { |
| 110 | + // float x; |
| 111 | + // float y; |
| 112 | + // float z; |
| 113 | + // }; |
| 114 | + // typedef Point PointAlias; |
| 115 | + // typedef Point PointArrayOf10[ 10 /**/]; |
| 116 | + // typedef PointAlias TwoDimPointArray[5][3]; |
| 117 | + // )"; |
| 118 | + // idl::Context context06 = idl::parse(test06); |
| 119 | + |
| 120 | + // std::string test07 = |
| 121 | + // R"( |
| 122 | + // union MyUnion switch (int32) { |
| 123 | + // case 0: |
| 124 | + // case 1: |
| 125 | + // long myLong; |
| 126 | + // case 2: |
| 127 | + // double myDouble; |
| 128 | + // default: |
| 129 | + // char myChar; |
| 130 | + // }; |
| 131 | + // )"; |
| 132 | + // idl::Context context07 = idl::parse(test07); |
| 133 | + |
| 134 | + std::string idl_spec = |
| 135 | + R"( |
| 136 | + struct InnerType |
| 137 | + { |
| 138 | + uint32 im1; |
| 139 | + float im2; |
| 140 | + }; |
| 141 | + )"; |
| 142 | + |
| 143 | + DynamicType::_ref_type inner_type = DynamicTypeBuilderFactory::get_instance()->create_type_w_idl(idl_spec)->build(); |
| 144 | + if (nullptr != inner_type) |
| 145 | + { |
| 146 | + EPROSIMA_LOG_INFO(IDLPARSER, "Succeeded to create DynamicType from IDL: " << idl_spec); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + EPROSIMA_LOG_ERROR(IDLPARSER, "Failed to create DynamicType from IDL: " << idl_spec); |
| 151 | + } |
| 152 | + |
| 153 | + // idl::Context context = idl::parse(idl_spec); |
| 154 | + // v1_3::DynamicType inner = context.module().structure("InnerType"); |
| 155 | + |
| 156 | + // v1_3::DynamicTypeBuilderFactory& factory = v1_3::DynamicTypeBuilderFactory::get_instance(); |
| 157 | + // v1_3::DynamicTypeBuilder_ptr builder = factory.create_struct_type(); |
| 158 | + // builder->set_name("OuterType"); |
| 159 | + // builder->add_member(0, "om1", factory.get_string_type()); |
| 160 | + // auto outer = builder->build(); |
| 161 | + |
| 162 | + // idl::Module root; |
| 163 | + // idl::Module& submod_a = root.create_submodule("a"); |
| 164 | + // idl::Module& submod_b = root.create_submodule("b"); |
| 165 | + // idl::Module& submod_aa = submod_a.create_submodule("a"); |
| 166 | + // submod_aa.structure(std::move(const_cast<v1_3::DynamicType&>(*outer))); |
| 167 | + // submod_b.structure(inner); |
| 168 | + // /* |
| 169 | + // root |
| 170 | + // \_a |
| 171 | + // | \_ a _ OuterType |
| 172 | + // | |
| 173 | + // \_ b _ InnerType |
| 174 | + // */ |
| 175 | + // std::cout << std::boolalpha; |
| 176 | + // std::cout << "Does a::a::OuterType exists?: " << root.has_structure("a::a::OuterType") << std::endl; |
| 177 | + // std::cout << "Does ::InnerType exists?: " << root.has_structure("::InnerType") << std::endl; |
| 178 | + // std::cout << "Does InnerType exists?: " << root.has_structure("InnerType") << std::endl; |
| 179 | + // std::cout << "Does OuterType exists?: " << root.has_structure("OuterType") << std::endl; |
| 180 | + // std::cout << "Does ::b::InnerType exists?: " << root.has_structure("::b::InnerType") << std::endl; |
| 181 | + // std::cout << "Does b::InnerType exists?: " << root.has_structure("b::InnerType") << std::endl; |
| 182 | + |
| 183 | + // v1_3::DynamicData* outer_data = v1_3::DynamicDataFactory::get_instance()->create_data( |
| 184 | + // std::make_shared<v1_3::DynamicType>(root["a"]["a"].structure("OuterType")) |
| 185 | + // ); // ::a::a::OuterType |
| 186 | + // v1_3::MemberId member_id = outer_data->get_member_id_by_name("om1"); |
| 187 | + // outer_data->set_string_value("This is a string.", member_id); |
| 188 | + |
| 189 | + // std::string scope_inner_type = inner.get_name(); // b::InnerType |
| 190 | + // v1_3::DynamicData* inner_data = v1_3::DynamicDataFactory::get_instance()->create_data( |
| 191 | + // std::make_shared<v1_3::DynamicType>(root.structure(scope_inner_type)) |
| 192 | + // ); |
| 193 | + // inner_data->set_uint32_value(32u, inner_data->get_member_id_by_name("im1")); |
| 194 | + // inner_data->set_float32_value(3.14159265f, inner_data->get_member_id_by_name("im2")); |
| 195 | + |
| 196 | + Log::Flush(); |
| 197 | + Log::Reset(); |
| 198 | + |
| 199 | + return 0; |
| 200 | +} |
0 commit comments