C++ memory and design question on small project - c++

I try to create a small library to listen for multiple mice on MAC and PC. (right now MAC)
I have started something simple that does not work ATM. Since I am a noob in C++ I wanted to ask the community for help in this matter. How should I design it in code? I wanted to use smart pointers here is my code, feedl free to download it:
Github:
Open Source Project
Everything in one file:
Device Class
class Device;
class Device {
public:
Device(){
std::cout << "###### Create Device - Empty" << std::endl;
this->x_previous = 0;
this->y_previous = 0;
this->x_current = 0;
this->y_current = 0;
}
Device( size_t _deviceID, std::string _device_name){
std::cout << "###### Create Device - 0.0/0.0" << std::endl;
this->device_id = std::make_shared<size_t>(_deviceID);
this->device_name = std::make_shared<std::string>(_device_name);
this->x_previous = 0;
this->y_previous = 0;
this->x_current = 0;
this->y_current = 0;
}
Device(size_t _deviceID, std::string _device_name, float _xStart, float _yStart){
std::cout << "###### Create Device - " << _xStart << "/" << _yStart << std::endl;
this->device_id = std::make_shared<size_t>(_deviceID);
this->device_name = std::make_shared<std::string>(_device_name);
this->x_previous = _xStart;
this->y_previous = _yStart;
this->x_current = _xStart;
this->y_current = _yStart;
}
~Device(){
std::cout << "###### Destroyed Device" << std::endl;
}
const size_t getId () const{
return (size_t)this->device_id.get();
};
const std::string getName() const{
return "Not Implementet yet"; //this->device_name.get() does not work because of std::basic_string wtf?
};
const float getDeltaX() const{
return x_previous - x_current;
};
const float getDeltaY() const{
return y_previous - y_current;
};
private:
std::shared_ptr<size_t> device_id;
std::shared_ptr<std::string> device_name;
float x_previous;
float y_previous;
float x_current;
float y_current;
};
Devices Class
class Devices{
public:
Devices(){
std::cout << "###### Created Empty Devices List" << std::endl;
this->list = std::unique_ptr<std::list<Device> >();
}
explicit Devices(std::unique_ptr<std::list<Device> > _list){
std::cout << "###### Created Moved Devices List" << std::endl;
this->list = std::move(_list);
}
~Devices(){
std::cout << "###### Destroyed Devices List" << std::endl;
}
std::unique_ptr<std::list<Device> > list;
void getDevicesArray() {
CFMutableDictionaryRef usb_dictionary;
io_iterator_t io_device_iterator;
kern_return_t assembler_kernel_return_value;
io_service_t device_id;
// set up a matching dictionary for the class
usb_dictionary = IOServiceMatching(kIOUSBDeviceClassName);
if (usb_dictionary == NULL) {
std::cout << "failed to fetch USB dictionary" << std::endl;
return; // still empty
}
// Now we have a dictionary, get an iterator.
assembler_kernel_return_value = IOServiceGetMatchingServices(kIOMasterPortDefault, usb_dictionary, &io_device_iterator);
if (assembler_kernel_return_value != KERN_SUCCESS) {
std::cout << "failed to get a kern_return" << std::endl;
return; // still empty
}
io_name_t device_name = "unkown device";
device_id = IOIteratorNext(io_device_iterator); // getting first device
while (device_id) {
device_id = IOIteratorNext(io_device_iterator); //set id type: io_service_t
IORegistryEntryGetName(device_id, device_name); //set name type: io_name_t
this->list.get()->push_back(Device(device_id, device_name));
}
//Done, release the iterator
IOObjectRelease(io_device_iterator);
}
void printDeviceIDs(){
for (auto const& device : *this->list.get()) {
std::cout << "#" << device.getId() << std::endl;
std::cout << "| name: " << "\t" << device.getName() << std::endl;
std::cout << "#-----------------------------------------------#" << std::endl;
}
}
};
main
int main(int argc, const char *argv[])
{
std::shared_ptr<Devices> devices;
devices->printDeviceIDs();
devices->getDevicesArray();
devices->printDeviceIDs();
}
Someone knows of a good pattern for that?
Also maybe I use smart pointers completely wrong?
Also the iOKit library is from 1985 or something so it is not very descriptive...
Thanks in advance.

As mentioned by other users this is more of a review rather than a error code question;
being that said the only thing I can find is the following:
std::unique_ptr<std::list<Device> > list;
you should change that to the following:
std::list<Device> list;
and on your main
int main(int argc, const char *argv[])
{
std::shared_ptr<Devices> devices;
devices->printDeviceIDs();
devices->getDevicesArray();
devices->printDeviceIDs();
}
should probably only suffice to use:
Devices devices; devices.printDeviceIDs();

Related

Linked list doesn't work when I tried to extract first element

I'm trying to use my own list with a class called "NameCard" which has two variables, name and phone.
However, complier crushed when I use LFirst(LData pData).
It worked with simple int type list.
any feedback would be greatly appreciated
Here is my code.
Class name: ArrayList.cpp
int ArrayList::LFirst(LData* pData)
{
if (numOfData == 0)
return 0;
curPosition = 0;
*pData = arr[0];
return 1;
}
Class name: NameCard.cpp
NameCard::NameCard()
{
}
NameCard::NameCard(const char* iName, const char* iPhone)
{
strcpy_s(name, iName);
strcpy_s(phone, iPhone);
}
void NameCard::ShowNameCardInfo()
{
std::cout << "Name: " << name << ", phone: " << phone << std::endl;
}
int NameCard::NameCompare(char* iName)
{
return strcmp(name, iName);
}
void NameCard::ChangePhoneNum(char* iPhone)
{
strcpy_s(phone, iPhone);
}
Class name: NameCardImplementation.cpp
#include <iostream>
#include "ArrayList.h"
#include "NameCard.h"
int main()
{
ArrayList list;
NameCard *pData(NULL);
NameCard nc1("Alice", "010-1111-2222");
NameCard nc2("Brandon", "010-2222-3333");
NameCard nc3("Jack", "010-3333-4444");
list.LInsert(nc1);
list.LInsert(nc2);
list.LInsert(nc3);
//nc1.ShowNameCardInfo();
//list.arr[0].ShowNameCardInfo();
//std::cout << list.LCount() << std::endl;
int a = list.LFirst(pData);
std::cout << a << std::endl;
//if (list.LFirst(pData))
//{
// pData->ShowNameCardInfo();
//}
}
Crash is due to null pointer access in ArrayList::LFirst.
From the limited code you have pasted, I see that there is no memory created for pData.
NameCard *pData(NULL);
Are you attaching the memory for pData anywhere else in your code?

Problems with repeated field in Protobuf. What is a better way to use the repeated field for serialization/deserialization?

Consider the following sensor.proto file that makes use of the repeated field to initialize multiple messages.
syntax = "proto3";
package HUBSensors;
message Device {
string name = 1;
int32 id = 2;
message Sensor {
string name = 1;
double temperature = 2;
int32 humidity = 3;
enum SwitchLevel {
CLOSED = 0;
OPEN = 1;
}
SwitchLevel door = 4;
}
repeated Sensor sensors = 3;
}
Now I want to serialize some random data to a file. As an example, I will have one device with multiple sensors, hence the repeated file in the proto. I use the following code.
inline void serialize_to_file( const std::string &fileName )
{
HUBSensors::Device device;
device.set_name("HUB");
device.set_id(1234);
device.add_sensors()->set_name("Laboratory");
device.add_sensors()->set_temperature(23.3);
device.add_sensors()->set_humidity(2);
device.add_sensors()->set_door(HUBSensors::Device_Sensor_SwitchLevel_OPEN);
device.add_sensors()->set_name("Chml Laboratory");
device.add_sensors()->set_temperature(2.3);
device.add_sensors()->set_humidity(5);
device.add_sensors()->set_door(HUBSensors::Device_Sensor_SwitchLevel_CLOSED);
device.add_sensors()->set_name("GU Laboratory");
device.add_sensors()->set_temperature(8.3);
device.add_sensors()->set_humidity(2);
device.add_sensors()->set_door(HUBSensors::Device_Sensor_SwitchLevel_CLOSED);
std::ofstream ofs(fileName, std::ios_base::out | std::ios_base::binary );
device.SerializeToOstream( &ofs );
google::protobuf::ShutdownProtobufLibrary();
}
To parse the data and print out the result I use the following:
inline void parse_from_file( const std::string &fileName )
{
HUBSensors::Device device;
std::ifstream myFile;
myFile.exceptions( std::ifstream::badbit );
try {
myFile.open(fileName);
while ( myFile.good() )
{
device.ParseFromIstream( &myFile );
//std::cout << device.sensors_size() << std::endl;
std::cout << "Device Name : " << device.name() << std::endl;
std::cout << "^^^^^^" << std::endl;
for ( size_t i = 0; i < device.sensors_size(); i+=4)
{
std::cout << "Sensors Name : " << device.sensors(i).name() << std::endl;
std::cout << "Temperature : " << device.sensors(i+1).temperature() << std::endl;
std::cout << "Humidity : " << device.sensors(i+2).humidity() << std::endl;
std::cout << " Door Status : " << device.sensors(i+3).door() << std::endl;
std::cout << "^^^^^^" << std::endl;
}
}
}
catch ( const std::ifstream::failure &e )
{
std::cerr << "Error Occurred when accessing the file" << std::endl;
std::cout << e.what() << std::endl;
}
myFile.close();
google::protobuf::ShutdownProtobufLibrary();
}
A main file to reproduce the results:
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "sensor.pb.h"
int main() {
GOOGLE_PROTOBUF_VERIFY_VERSION;
const std::string fileName = "./device.data";
serialize_to_file( fileName );
parse_from_file( fileName );
return 0;
}
It doesn't seem intuitive to iterate through the total sensor size and getting the correct index to display the appropriate field.
Even by checking
std::cout << device.sensors_size() << std::endl;
will output size 12 which it does not feel correct, or
std::cout << decice.sensors(2).name() << std::endl;
will not output anything since it is in the incorrect index.
What is a better way to use the libprotobuf to define a repeated field for serializing/deserializing better.
EDIT: As the answer suggests, serializing to the file should be as
inline void serialize_to_file( const std::string &fileName )
{
HUBSensors::Device device;
device.set_name("HUB");
device.set_id(1234);
auto sensor1 = device.add_sensors();
auto sensor2 = device.add_sensors();
auto sensor3 = device.add_sensors();
sensor1->set_name("Laboratory");
sensor1->set_temperature(23.3);
sensor1->set_humidity(2);
sensor1->set_door(HUBSensors::Device_Sensor_SwitchLevel_CLOSED);
sensor2->set_name("GU Laboratory");
sensor2->set_temperature(44.3);
sensor2->set_humidity(4);
sensor2->set_door(HUBSensors::Device_Sensor_SwitchLevel_OPEN);
sensor3->set_name("Chml Laboratory");
sensor3->set_temperature(13.345);
sensor3->set_humidity(6);
sensor3->set_door(HUBSensors::Device_Sensor_SwitchLevel_CLOSED);
std::ofstream ofs(fileName, std::ios_base::out | std::ios_base::binary );
device.SerializeToOstream( &ofs );
google::protobuf::ShutdownProtobufLibrary();
}
Instead of
device.add_sensors()->set_name("Laboratory");
device.add_sensors()->set_temperature(23.3);
device.add_sensors()->set_humidity(2);
device.add_sensors()->set_door(HUBSensors::Device_Sensor_SwitchLevel_OPEN);
you should write
auto sensor = device.add_sensors();
sensor->set_name("Laboratory");
sensor->set_temperature(23.3);
sensor->set_humidity(2);
sensor->set_door(HUBSensors::Device_Sensor_SwitchLevel_OPEN);
This way you will have 3 sensors, which is your intent I suppose, and all of them will have every data member set.

ADTF recording file format

I am coding an ADTF recording file reader in C++. I have already read the header using the structure specified here
https://support.digitalwerk.net/adtf_libraries/adtf-streaming-library/v2/DATFileFormatSpecification.pdf
typedef struct tagFileHeader {
int ui32FileId;
int ui32VersionId;
int ui32Flags;
int ui32ExtensionCount;
long long ui64ExtensionOffset;
long long ui64DataOffset;
long long ui64DataSize;
long long ui64ChunkCount;
long long ui64MaxChunkSize;
long long ui64Duration;
long long ui64FileTime;
char ui8HeaderByteOrder;
long long ui64TimeOffset;
char ui8PatchNumber;
char _reserved[54];
char strDescription[1912];
} tFileHeader; // size is 2048 Bytes
I read the heder
ifstream file("myfile.dat", std::ifstream::binary);
char buffer[2048];
file.read(buffer, 2048);
const tagFileHeader* header = reinterpret_cast<const tagFileHeader*>(buffer);
And now I need to read the chunks. This is the chunks header, extracted from the same document
typedef struct tagChunkHeader {
long long ui64TimeStamp;
int ui32RefMasterTableIndex;
int ui32OffsetToLast;
int ui32Size;
short ui16StreamId;
short ui16Flags;
long long ui64StreamIndex;
} tChunkHeader; // size is 32 Bytes
Reading the chunks
for (int c = 0; c < header->ui64ChunkCount; ++c)
{
char chunkHeaderBuffer[32];
file.read(chunkHeaderBuffer, 32);
const tChunkHeader* chunk = reinterpret_cast<const tChunkHeader*>(chunkHeaderBuffer);
//Skeep chunk data
file.seekg(chunk->ui32Size, ios_base::cur);
}
I don't know how to interpret the chunk data. Is this specified in another document that I am missing?
Thanks
For the sake of completeness:
The chunk data layout depends on the original sample data and the used serialization. So there is not one single data layout. You have to deserialize the chunk data with the correct deserialization implementation and can then interpret the deserialized data with the correct struct definition. The information about the used serialization is stored within the index extension of a stream.
As C-3PFLO has already stated, the adtf_file library does all this for you, but you need all required deserializer plugins.
Here is a example (based on upcoming ADTF File Library 0.5.0) how to access dat files and extend the reader with additional adtffileplugins. Use this read dat files which contains e.g. flexray data recorded with ADTF 2.x:
/**
* #file
* ADTF File Access example
*
* #copyright
* #verbatim
Copyright # 2017 Audi Electronics Venture GmbH. All rights reserved.
This Source Code Form is subject to the terms of the Mozilla
Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular file, then
You may include the notice in a location (such as a LICENSE file in a
relevant directory) where a recipient would be likely to look for such a notice.
You may add additional accurate notices of copyright ownership.
#endverbatim
*/
#include <adtf_file/standard_adtf_file_reader.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <map>
// initalize ADTF File and Plugin Mechanism
static adtf_file::Objects oObjects;
static adtf_file::PluginInitializer oInitializer([]
{
adtf_file::add_standard_objects();
});
void query_file_info(adtf_file::Reader& reader)
{
using namespace adtf_file;
//setup file version
uint32_t ifhd_version = reader.getFileVersion();
std::string adtf_version("ADTF 3 and higher");
if (ifhd_version < ifhd::v400::version_id)
{
adtf_version = "below ADTF 3";
}
//begin print
std::cout << std::endl << "File Header" << std::endl;
std::cout << "------------------------------------------------------------------------------" << std::endl;
std::cout << "File version : " << reader.getFileVersion() << " - " << adtf_version << std::endl;
std::cout << "Date : " << reader.getDateTime().format("%d.%m.%y - %H:%M:%S") << std::endl;
std::cout << "Duration : " << reader.getDuration().count() << std::endl;
std::cout << "Short description : " << getShortDescription(reader.getDescription()) << std::endl;
std::cout << "Long description : " << getLongDescription(reader.getDescription()) << std::endl;
std::cout << "Chunk count : " << reader.getItemCount() << std::endl;
std::cout << "Extension count : " << reader.getExtensions().size() << std::endl;
std::cout << "Stream count : " << reader.getStreams().size() << std::endl;
std::cout << std::endl << "Streams" << std::endl;
std::cout << "------------------------------------------------------------------------------" << std::endl;
auto streams = reader.getStreams();
for (const auto& current_stream : streams)
{
auto property_stream_type = std::dynamic_pointer_cast<const PropertyStreamType>(current_stream.initial_type);
if (property_stream_type)
{
std::string stream_meta_type = property_stream_type->getMetaType();
std::cout << "Stream #" << current_stream.stream_id << " : " << current_stream.name << std::endl;
std::cout << " MetaType : " << stream_meta_type << std::endl;
property_stream_type->iterateProperties(
[&](const char* name,
const char* type,
const char* value) -> void
{
std::cout << " " << name << " - " << value << std::endl;
});
}
}
}
class StreamsInfo
{
typedef std::map<uint16_t, std::chrono::microseconds> LastTimesMap;
typedef std::map<uint16_t, std::string> StreamNameMap;
public:
StreamsInfo(adtf_file::Reader& reader)
{
auto streams = reader.getStreams();
for (auto current_stream : streams)
{
_map_stream_name[current_stream.stream_id] = current_stream.name;
UpdateType(current_stream.stream_id, current_stream.initial_type);
}
}
~StreamsInfo() = default;
std::string GetDiffToLastChunkTime(const uint16_t& stream_id, const std::chrono::microseconds& current_time)
{
return GetLastTimeStamp(_map_last_chunk_time, stream_id, current_time);
}
std::string GetDiffToLastSampleStreamTime(const uint16_t& stream_id, const std::chrono::microseconds& current_time)
{
return GetLastTimeStamp(_map_last_stream_time, stream_id, current_time);
}
std::string GetStreamName(const uint16_t& stream_id)
{
return _map_stream_name[stream_id];
}
void UpdateType(const uint16_t& stream_id, const std::shared_ptr<const adtf_file::StreamType>& type)
{
auto property_stream_type = std::dynamic_pointer_cast<const adtf_file::PropertyStreamType>(type);
if (property_stream_type)
{
_map_stream_meta_type[stream_id] = property_stream_type->getMetaType();
}
}
std::string GetLastStreamMetaType(const uint16_t& stream_id)
{
return _map_stream_meta_type[stream_id];
}
private:
std::string GetLastTimeStamp(LastTimesMap& map_last_times,
const uint16_t& stream_id,
const std::chrono::microseconds& current_time)
{
std::chrono::microseconds result(-1);
LastTimesMap::iterator it = map_last_times.find(stream_id);
if (it != map_last_times.end())
{
result = current_time - it->second;
it->second = current_time;
}
else
{
if (current_time.count() != -1)
{
map_last_times[stream_id] = current_time;
}
}
if (result.count() >= 0)
{
return a_util::strings::format("%lld", result.count());
}
else
{
return "";
}
}
LastTimesMap _map_last_chunk_time;
LastTimesMap _map_last_stream_time;
StreamNameMap _map_stream_name;
StreamNameMap _map_stream_meta_type;
};
void access_file_data(adtf_file::Reader& reader, const std::string& csv_file_path)
{
using namespace adtf_file;
//load stream information
StreamsInfo stream_info(reader);
std::cout << std::endl << "File data" << std::endl;
std::cout << "------------------------------------------------------------------------------" << std::endl;
utils5ext::File csv_file;
csv_file.open(csv_file_path, utils5ext::File::om_append | utils5ext::File::om_write);
//set the labels
csv_file.writeLine("stream;stream_name;chunk_type;stream_type;chunk_time;samplestream_time;chunk_time_delta_to_lastofstream;samplestream_time_delta_to_lastofstream");
size_t item_count = 0;
for (;; ++item_count)
{
try
{
auto item = reader.getNextItem();
std::chrono::microseconds chunk_time = item.time_stamp;
std::string chunk_type;
auto type = std::dynamic_pointer_cast<const StreamType>(item.stream_item);
auto data = std::dynamic_pointer_cast<const Sample>(item.stream_item);
auto trigger = std::dynamic_pointer_cast<const Trigger>(item.stream_item);
std::chrono::microseconds sample_time(-1);
std::string sample_time_string("");
if (type)
{
//the type change is part of the
chunk_type = "stream_type";
stream_info.UpdateType(item.stream_id,
type);
}
else if (data)
{
chunk_type = "sample";
auto sample_data = std::dynamic_pointer_cast<const DefaultSample>(data);
if (sample_data)
{
sample_time = sample_data->getTimeStamp();
sample_time_string = a_util::strings::format("%lld", sample_time.count());
}
}
else if (trigger)
{
chunk_type = "trigger";
}
csv_file.writeLine(a_util::strings::format("%d;%s;%s;%s;%lld;%s;%s;%s",
static_cast<int>(item.stream_id),
stream_info.GetStreamName(item.stream_id).c_str(),
chunk_type.c_str(),
stream_info.GetLastStreamMetaType(item.stream_id).c_str(),
chunk_time.count(),
sample_time_string.c_str(),
stream_info.GetDiffToLastChunkTime(item.stream_id, chunk_time).c_str(),
stream_info.GetDiffToLastSampleStreamTime(item.stream_id, sample_time).c_str()
));
}
catch (const exceptions::EndOfFile&)
{
break;
}
}
csv_file.close();
}
adtf_file::Reader create_reader(const a_util::filesystem::Path& adtfdat_file_path)
{
//open file -> create reader from former added settings
adtf_file::Reader reader(adtfdat_file_path,
adtf_file::getFactories<adtf_file::StreamTypeDeserializers,
adtf_file::StreamTypeDeserializer>(),
adtf_file::getFactories<adtf_file::SampleDeserializerFactories,
adtf_file::SampleDeserializerFactory>(),
std::make_shared<adtf_file::sample_factory<adtf_file::DefaultSample>>(),
std::make_shared<adtf_file::stream_type_factory<adtf_file::DefaultStreamType>>());
return reader;
}
int main(int argc, char* argv[])
{
if (argc < 3 || argv[1] == NULL || argv[2] == NULL)
{
std::cerr << "usage: " << argv[0] << " <adtfdat> <csv> [<adtffileplugin> ...]" << std::endl;
return -1;
}
//set path for adtfdat|dat and csv file
a_util::filesystem::Path adtfdat_file = argv[1];
a_util::filesystem::Path csv_file = argv[2];
try
{
//verify adtf|dat file
if (("adtfdat" != adtfdat_file.getExtension())
&& ("dat" != adtfdat_file.getExtension()))
{
throw std::runtime_error(adtfdat_file + " is not valid, please use .adtfdat (ADTF 3.x) or .dat (ADTF 2.x).");
}
//verify csv file
if ("csv" != csv_file.getExtension())
{
throw std::runtime_error(csv_file + " is not valid, please use .csv for sample data export.");
}
//check for additional adtffileplugins
for (int i = 3; i < argc; i++)
{
a_util::filesystem::Path adtffileplugin = argv[i];
if ("adtffileplugin" == adtffileplugin.getExtension())
{
adtf_file::loadPlugin(adtffileplugin);
}
}
//setup reader
auto reader = create_reader(adtfdat_file);
//print information about adtfdat|dat file
query_file_info(reader);
//export sample data
access_file_data(reader, csv_file);
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return -2;
}
return 0;
}
Is there any reason why you try to reimplement an ADTF DAT File Reader ? It will be provided by the ADTF Streaming Library and should provide to access any data stored in a dat file. See the File Access Example (https://support.digitalwerk.net/adtf_libraries/adtf-streaming-library/v2/api/page_fileaccess.html) how to use the reader as well as the API itself and all other examples.
Hint: You can also use the successor - ADTF File Library with the same possibilities but with two more benefits: Complete Open Source to see how the (adtf)dat file handling works and also support for files created with ADTF 3.x. See https://support.digitalwerk.net/adtf_libraries/adtf-file-library/v0/html/index.html
For those interested in downloading the streaming Library, here follows the link
https://support.digitalwerk.net/projects/download-center/repository/show/adtf-libraries/adtf-streaming-library/release-2.9.0

Pass variable to PUGIXML xml_tree_walker

I am using pugixml to parse a file. I have been using an xml_tree_walker and I have a variable I want to modify as the walker walks through the xml. I am currently using a global variable but don't want to. Is there a way to pass a variable to the walker by reference? If this is the case, would that mean I would need to modify the for_each function prototype in the pugixml source?
Below is the way I am currently using the walker.
struct simple_walker: pugi::xml_tree_walker
{
virtual bool for_each(pugi::xml_node& node)
{
for (int i = 0; i < depth(); ++i) std::cout << " "; // indentation
std::cout << node_types[node.type()] << ": name='" << node.name() << "', value='" << node.value() << "'\n";
some_global = node.name(); // I don't want this to be a global
return true; // continue traversal
}
};
I am calling the walker like so:
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(XML_FILE);
simple_walker mySimpleWalker;
doc.traverse(mySimpleWalker);
One thing you can do is keep a member reference in your walker to the object you are capturing information in. Something like:
struct captured_info // where my captured info goes
{
std::string name;
int value;
std::time_t date;
// ...
};
struct simple_walker: pugi::xml_tree_walker
{
// must supply a captured_info object to satisfy
// this constructor
simple_walker(captured_info& info): info(info) {}
virtual bool for_each(pugi::xml_node& node)
{
for (int i = 0; i < depth(); ++i) std::cout << " "; // indentation
std::cout << node_types[node.type()] << ": name='" << node.name() << "', value='" << node.value() << "'\n";
info.name = node.name();
info.value = std::stoi(node.value());
// ... etc
return true; // continue traversal
}
captured_info& info;
};
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(XML_FILE);
captured_info info; // the output!!!
simple_walker mySimpleWalker(info); // configure for output
doc.traverse(mySimpleWalker);
std::cout << info.name << '\n'; // etc...

LLVM's bitcode wrong detections of function's parameters

I'm using LLVM api in order to parse bitcode files. I have the following snippet and I'm using this command to generate the bitcode $CC -emit-llvm -c -g source.c where CC is set to the clang path.
#include <stdio.h>
struct Point {
int a;
int b;
};
int func_0(struct Point p, int x) {
return 0;
}
The TypeID is supposed to have a numeric value, based on the type of the parameter. However, both for the integer x and the struct Point I obtain the value of 10 which is referred as a TokenTyID. So, I decided to use the functions isIntegerTy() and isStructTy(), respectively, to see if at least in this case, I obtain the right result. This solution works for the integer parameter x, but not for the struct. How can I correctly identify structs and read their fields?
Just to completeness, to parse the bitcode I use this code:
using namespace llvm;
int main(int argc, char** argv) {
LLVMContext context;
OwningPtr<MemoryBuffer> mb;
MemoryBuffer::getFile(FileName, mb);
Module *m = ParseBitcodeFile(mb.get(), context);
for (Module::const_iterator i = m->getFunctionList().begin(), e = m->getFunctionList().end(); i != e; ++i) {
if (i->isDeclaration() || i->getName().str() == "main")
continue;
std::cout << i->getName().str() << std::endl;
Type* ret_type = i->getReturnType();
std::cout << "\t(ret) " << ret_type->getTypeID() << std::endl;
Function::const_arg_iterator ai;
Function::const_arg_iterator ae;
for (ai = i->arg_begin(), ae = i->arg_end(); ai != ae; ++ai) {
Type* t = ai->getType();
std::cout << "\t" << ai->getName().str() << " " << t->getTypeID()
<< "(" << t->getFunctionNumParams() << ")"
<< " is struct? " << (t->isStructTy() ? "Y" : "N")
<< " is int? " << (t->isIntegerTy() ? "Y" : "N")
<< "\n";
}
}
return 0;
}
I read this post Why does Clang coerce struct parameters to ints about the translation performed by clang with the structs and I'm pretty sure that is my same problem.
Since clang changes the function signature in the IR, you will have to get that information using debug info. Here is some rough code:
DITypeIdentifierMap TypeIdentifierMap;
DIType* getLowestDINode(DIType* Ty) {
if (Ty->getTag() == dwarf::DW_TAG_pointer_type ||
Ty->getTag() == dwarf::DW_TAG_member) {
DIType *baseTy =
dyn_cast<DIDerivedType>(Ty)->getBaseType().resolve(TypeIdentifierMap);
if (!baseTy) {
errs() << "Type : NULL - Nothing more to do\n";
return NULL;
}
//Skip all the DINodes with DW_TAG_typedef tag
while ((baseTy->getTag() == dwarf::DW_TAG_typedef || baseTy->getTag() == dwarf::DW_TAG_const_type
|| baseTy->getTag() == dwarf::DW_TAG_pointer_type)) {
if (DITypeRef temp = dyn_cast<DIDerivedType>(baseTy)->getBaseType())
baseTy = temp.resolve(TypeIdentifierMap);
else
break;
}
return baseTy;
}
return Ty;
}
int main(int argc, char** argv) {
LLVMContext context;
OwningPtr<MemoryBuffer> mb;
MemoryBuffer::getFile(FileName, mb);
Module *m = ParseBitcodeFile(mb.get(), context);
if (NamedMDNode *CU_Nodes = m.getNamedMetadata("llvm.dbg.cu")) {
TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
}
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
F.getAllMetadata(MDs);
for (auto &MD : MDs) {
if (MDNode *N = MD.second) {
if (auto *subRoutine = dyn_cast<DISubprogram>(N)->getType()) {
if (!subRoutine->getTypeArray()[0]) {
errs() << "return type \"void\" for Function : " << F.getName().str()
<< "\n";
}
const auto &TypeRef = subRoutine->getTypeArray();
for (int i=0; i<TypeRef.size(); i++) {
// Resolve the type
DIType *Ty = ArgTypeRef.resolve(TypeIdentifierMap);
DIType* baseTy = getLowestDINode(Ty);
if (!baseTy)
return;
// If that pointer is a struct
if (baseTy->getTag() == dwarf::DW_TAG_structure_type) {
std::cout << "structure type name: " << baseTy->getName().str() << std::endl();
}
}
}
}
}
}
I know it looks ugly but using debug info is not easy.