Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build
docs/html

thirdparty/cpp-peglib
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@ if(XTYPES_BUILD_EXAMPLES)
compile_example(${PROJECT_NAME}_example_module SOURCE examples/module.cpp)
compile_example(${PROJECT_NAME}_example_iterators SOURCE examples/iterators.cpp)
compile_example(${PROJECT_NAME}_example_exceptions_asserts SOURCE examples/exceptions_asserts.cpp)
compile_example(${PROJECT_NAME}_example_load_idl SOURCE examples/load_idl.cpp)
# ...

# Export resources for examples
file(COPY examples/resources DESTINATION ${PROJECT_BINARY_DIR})
endif()


#####################################################################################
# Tests
#####################################################################################
Expand Down
126 changes: 126 additions & 0 deletions examples/load_idl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <xtypes/xtypes.hpp>
#include <xtypes/idl/idl.hpp>

#include <iostream>

using namespace eprosima::xtypes;

int main()
{

// Loading idl
try
{
std::cout << "LOADING" << std::endl;
std::cout << "-------" << std::endl;

idl::Context context = idl::parse_file("resources/example.idl");

if (context.success)
{
std::cout << "Context loaded correctly" << std::endl;
}
else
{
std::cout << "Error loading idl file" << std::endl;
return 1;
}

// Getting a map with all the structures
std::map<std::string, DynamicType::Ptr> types_map = context.module().get_all_types();

std::cout << std::endl << "Types loaded: " << types_map.size() << std::endl;
for (auto type_name : types_map)
{
std::cout << " " << type_name.first << std::endl;
}

std::cout << std::endl << "INTROSPECTION" << std::endl;
std::cout << "-------------" << std::endl;

// Introspect types
for (auto struct_type : types_map)
{
if (context.module().has_structure(struct_type.first))
{
const StructType& type = context.module().structure(struct_type.first);
std::cout << struct_type.first << ": " << type << std::endl;
}
else if (context.module().has_enum_32(struct_type.first))
{
const EnumerationType<uint32_t>& enum_type = context.module().enum_32(struct_type.first);
std::cout << struct_type.first << ": " << enum_type << std::endl;
}
else if (context.module().has_submodule(struct_type.first))
{
const std::shared_ptr<idl::Module> submodule_type = context.module().submodule(struct_type.first);
std::cout << struct_type.first << ": " << submodule_type << std::endl;
}
else
{
std::cout << "Unkown type: " << struct_type.first << std::endl;
}
}

std::cout << "ACCESS" << std::endl;
std::cout << "------" << std::endl;

const StructType& my_complex_struct = context.module().structure("MyComplexType");
DynamicData data(my_complex_struct);

/////
// Access primitive type

// set value
data["myPrimitiveType"]["my_float"] = 3.1415f; // Important: the 'f' to set as float and not double

// get value
std::cout << "Value inside complex struct: "
<< data["myPrimitiveType"]["my_float"].value<float>() << std::endl;

/////
// Access complex type in collection

// Types required
const StructType& my_string_struct = context.module().structure("MyStringType");
StringType string_type;

// set value
DynamicData string_key(string_type);
string_key = "key_on_map";

DynamicData string_data(my_string_struct);
string_data["my_string"] = "value_on_map";

data["myCollectionType"]["my_map"][string_key] = string_data;

// get value
DynamicData new_string_data(my_string_struct);
new_string_data = data["myCollectionType"]["my_map"][string_key];
std::cout << "Value inside collection inside complex struct: '"
<< new_string_data["my_string"].value<std::string>() << "'" << std::endl;

/////
// Access primitive type

// Types required
const EnumerationType<uint32_t>& my_enum_type = context.module().enum_32("MyEnumerationType");

// set value
DynamicData my_enum_data(my_enum_type);
my_enum_data = my_enum_type.value("BLUE");

data["mySubmoduleType"] = my_enum_data;

// get value
std::cout << "Value inside submodule struct: "
<< data["mySubmoduleType"]["my_enum"].value<uint32_t>() << std::endl;
}
catch(const std::runtime_error& e)
{
std::cout << "Exception catched: " << e.what() << std::endl;
return 1;
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/resources/example.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "sub_modules/example_aux.idl"

struct MyComplexType
{
MyAllPrimitiveType myPrimitiveType;
MyStringType myStringType;
MyInheritStringType myInheritStringType;
MyCollectionType myCollectionType;
MyEnumerationType myEnumerationType;
submodule::MySubModuleType mySubmoduleType;
};
54 changes: 54 additions & 0 deletions examples/resources/sub_modules/example_aux.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

// All Primitive types
struct MyAllPrimitiveType
{
boolean my_boolean;
char my_char;
octet my_octet;
short my_short;
unsigned short my_unsigned_short;
long my_long;
unsigned long my_unsigned_long;
long long my_long_long;
unsigned long long my_unsigned_long_long;
float my_float;
double my_double;
long double my_long_double;
};

// Struct with a string (non primitive struct)
struct MyStringType
{
string my_string;
};

// Struct wthat inherits from other struct
struct MyInheritStringType : MyStringType
{
string my_child_string;
};

// Struct of collection of primitive and non primitive types
struct MyCollectionType
{
long my_array_long[5];
sequence<string> my_sequence_long[5];
map<string, MyStringType> my_map;
};

// Enumeration
enum MyEnumerationType
{
RED,
GREEN,
BLUE
};

// Struct under a module (namespace)
module submodule
{
struct MySubModuleType
{
MyEnumerationType my_enum;
};
};
2 changes: 1 addition & 1 deletion include/xtypes/idl/Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class Module

for (const auto& pair : inner_)
{
pair.second->fill_all_types(map, add_scope);
pair.second->fill_all_types(map, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/xtypes/idl/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ class Parser
str.replace(pos, froms, escaped);
pos = str.find(from, pos + escaped_size);
}

}
}

Expand Down
8 changes: 4 additions & 4 deletions test/unitary/parser/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ TEST (IDLParser, include_from_string)
)");
std::map<std::string, DynamicType::Ptr> result = context.module().get_all_types();
EXPECT_EQ(2, result.size());
const DynamicType* my_struct = result["Test00"].get();
const DynamicType* my_struct = result["include::Test00"].get();
DynamicData data(*my_struct);
ASSERT_EQ(data["incl"]["my_string"].type().name(), "std::string");
}
Expand Down Expand Up @@ -1541,9 +1541,9 @@ TEST (IDLParser, same_struct_id_in_different_modules)
);
std::map<std::string, DynamicType::Ptr> result = context.module().get_all_types();
EXPECT_EQ(1, result.size());
const DynamicType* my_struct = result["MyStruct"].get();
const DynamicType* my_struct = result["a::b::c::MyStruct"].get();
EXPECT_EQ(my_struct->name(), "a::b::c::MyStruct");
first_struct = result["MyStruct"];
first_struct = result["a::b::c::MyStruct"];
EXPECT_EQ(first_struct.get()->name(), "a::b::c::MyStruct");
}

Expand All @@ -1565,7 +1565,7 @@ TEST (IDLParser, same_struct_id_in_different_modules)
);
std::map<std::string, DynamicType::Ptr> result = context.module().get_all_types();
EXPECT_EQ(1, result.size());
const DynamicType* my_struct = result["MyStruct"].get();
const DynamicType* my_struct = result["x::y::MyStruct"].get();
EXPECT_EQ(my_struct->name(), "x::y::MyStruct");
}

Expand Down