0

(Please Note: I am not able to embed images here. I dont have enough points for that. Could anyone please help me with that.)

I understand how to convert the struct corresponding to the following tablular format (Struct1) into msgpack format:

Struct1

For this I use the following code:

#include <sstream>
#include <iostream>
#include <msgpack.hpp>

inline std::ostream& hex_dump(std::ostream& o, std::string const& v) {
    std::ios::fmtflags f(o.flags());
    o << std::hex;
    o << "b\'";
    for (auto c : v) {
        o << "\\x" << std::setw(2) << std::setfill('0') << (static_cast<int>(c) & 0xff);
    }
    o << "\'";
    o.flags(f);
    return o;
}

struct your_type {
    int a;
    int b;
    MSGPACK_DEFINE(a, b);
};

int main() {
    // packing
    std::stringstream ss;
    std::stringstream sshex;
    std::string ssnew;
    std::vector<std::map<std::string, your_type>> v
    {
        {
            { "t",{ 1, 2} }

        },
        //{
            { "value",{6, 5 } }
        }
    };

    msgpack::pack(ss, v);


    auto oh = msgpack::unpack(ss.str().data(), ss.str().size());
    // JSON output
    std::cout << oh.get() << std::endl;

    std::cout << std::endl;
    // hex dump output
    hex_dump(sshex, ss.str()) << std::endl;

    std::cout << sshex.str();

    ssnew = sshex.str();

    return ssnew;

}

Now I would want to try the following to be converted in the same format:

I want to add each row in a loop, pass the values through the loop to the structure and then convert into msgpack format. Again, repeat the process after adding another row. I do not want to statically define the values as before. For eg:

First time - data passed for conversion (Struct2):

Struct2

Second time - data passed for conversion (Struct3): Struct3

I have tried with using int for t and int array for val. I am not able to proceed further. I have researched msgpack library but in vain. Could anyone please help me as how can I proceed with this? Even a little guidance would be a lot helpful.

  • A common implementation of tables is to represent each row with a `struct` or `class` and use `std::vector` to represent the table. Also, use `std::map` as an index table into the table (using index tables means you don't have to keep sorting the table). – Thomas Matthews Nov 29 '18 at 17:50
  • What are you trying to accomplish by having `std::vector>`? – Thomas Matthews Nov 29 '18 at 17:51
  • In my experience, you would be better off having a structure, then populating the structure from a buffer (e.g. MSGPACK). This allows you to account for Endianess, packing, padding for alignment and efficiency of processor integer types. For example, a message may have a byte field, but the processor is more efficient with an integer value. – Thomas Matthews Nov 29 '18 at 17:53
  • Thank you so much for your reply. I am trying what you suggested. I am using a struct each for each column. I will be adding rows at different stages so I chose column for each struct. Is that correct?Could you please guide me as to how can I combine different columns and their column names into a table using std::vector? – Aparna Bose Dec 03 '18 at 09:22
  • Usually, each row is modeled by a structure. You can use `std::vector` for a row. Since you didn't paste the structures as text, I can't help you further. Firewalls are blocking my access to your links. – Thomas Matthews Dec 03 '18 at 14:59
  • I have tried with the following to represent a std::map> map; I want to insert a value if I find a key in this map...I use this iterator for (auto const &ent1 : map) {auto const &outer_key = ent1.first; auto const &inner_list = ent1.second; } But I am not able to insert a value to inner_list. I have tried emplace. It doesnt give an error but doesnt insert. could you please tell me if this is correct? – Aparna Bose Dec 03 '18 at 16:16

0 Answers0