Pregunta

I have a real problem. I try to extract the type of argument a function to build the argument by booting from another agrument. All this in order to remove the replicas of the code.

Some imformations : I use gcc version 4.8.1

So here is my minimized code (a bit long, sorry about that):

A class to serialize some type (just with int for the exemple)

class Serializer
{
public:
    explicit Serializer(unsigned int buffer_size=255) : _cursor_end(0),_cursor_begin(0), _buffer_size(buffer_size)
    {
        _buffer = new unsigned char[buffer_size];
    };
    ~Serializer(){
        delete _buffer;
    }

    Serializer& operator<<(int i){
        push(*reinterpret_cast<uint32_t*>(&i));
        return *this;

    };

    Serializer& operator>>(int& i){
        pop(*reinterpret_cast<uint32_t*>(&i));
        return *this;
    };

protected:

    unsigned char* _buffer;
    unsigned int _cursor_end;
    unsigned int _cursor_begin;
    unsigned int _buffer_size;

    inline void resize(const unsigned int buffer_cursor_end){
        unsigned char* buffer = new unsigned char[buffer_cursor_end];
        buffer = (unsigned char*)memcpy(buffer,_buffer,_buffer_size);

        delete _buffer;
        _buffer = buffer;
        _buffer_size = buffer_cursor_end;
    };

    inline void push(uint32_t& a){
        if(_buffer_size < _cursor_end + 4)
        resize(_buffer_size+128);

        uint8_t *d = (uint8_t *)&a;

        #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
        _buffer[_cursor_end++] = d[0];
        _buffer[_cursor_end++] = d[1];
        _buffer[_cursor_end++] = d[2];
        _buffer[_cursor_end++] = d[3];
        #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
        _buffer[_cursor_end++] = d[3];
        _buffer[_cursor_end++] = d[2];
        _buffer[_cursor_end++] = d[1];
        _buffer[_cursor_end++] = d[0];
        #else
        #error "byte orden not suported (PDP endian)"
        #endif

    }

    inline void pop(uint32_t& a){
        if(_cursor_begin +4 <= _cursor_end)
        {
            uint8_t *d = (uint8_t *)&a;
            #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
            d[0]= _buffer[_cursor_begin++];
            d[1]= _buffer[_cursor_begin++];
            d[2]= _buffer[_cursor_begin++];
            d[3]= _buffer[_cursor_begin++];
            #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
            d[3]= _buffer[_cursor_begin++];
            d[2]= _buffer[_cursor_begin++];
            d[1]= _buffer[_cursor_begin++];
            d[0]= _buffer[_cursor_begin++];
            #else
            #error "byte orden not suported (PDP endian)"
            #endif
        }
    };
};

and now 2 stupid function for the test

int getVersion(Serializer& sock)
{
    return 42;
}

int testParamInt(Serializer& sock,int v)
{
    std::cout<<"param:"<<v<<std::endl;
    return v+12;
}

Call them :

int main(int argc, char* argv[])
{
    {
        Serializer params;
        std::cout<<"getVersion : "<<getVersion(params)<<std::endl;;
    }

    {
        Serializer params;
        params<<42;
        int p1;
        params>>p1;
        std::cout<<"testParamInt: "<<testParamInt(params,p1)<<std::endl;
    }
   return 0;
}

output:

getVersion : 42
param:42
testParamInt: 54

Ok, no probem for the moment. Now I've try to change the call of the function, to something like that (with the same result)

int main(int argc, char* argv[])
{
    {
        Serializer params;
        std::cout<<"getVersion : "<<exec(getVersion,params)<<std::endl;;
    }

    {
        Serializer params;
        params<<42;
        std::cout<<"testParamInt: "<<exec(testParamInt,params)<<std::endl;
    }

    return 0;
};

So I've create some healper:

#include <tuple>
template<int...> struct index_tuple{};

template<int I, typename IndexTuple, typename... Types>
struct make_indexes_impl;

template<int I, int... Indexes, typename T, typename ... Types>
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...>
{
    typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type;
};

template<int I, int... Indexes>
struct make_indexes_impl<I, index_tuple<Indexes...> >
{
    typedef index_tuple<Indexes...> type;
};

template<typename ... Types>
struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...>{};

// ----------UNPACK TUPLE AND APPLY TO FUNCTION ---------

template<class Ret, class... Args, int... Indexes>
Ret apply_helper( Ret (*pf)(Serializer&,Args...),Serializer& sock, index_tuple< Indexes... >, std::tuple<Args...>&& tup)
{
    {sock>>std::get<Indexes>(tup)...};
    return pf(sock,std::forward<Args>(std::get<Indexes>(tup))...);
};

template<typename Ret,typename ... Args>
Ret apply(Ret (*pf)(Serializer&,Args...),Serializer& sock,const std::tuple<Args...>&  tup)
{
    return apply_helper(pf,sock,typename make_indexes<Args...>::type(), std::tuple<Args...>(tup));
};


template<typename Ret,typename ... Args>
Ret exec(Ret(*pf)(Serializer&,Args ...),Serializer& sock)
{
    std::tuple<Args ...> params;
    return apply(pf,sock,params);
}

But this line does not compile

{sock>>std::get<Indexes>(tup)...};

error message

test.cpp: In function ‘Ret apply_helper(Ret (*)(Serializer&, Args ...), Serializer&, index_tuple<Indexes ...>, std::tuple<_Elements ...>&&)’:
test.cpp:129:34: error: expected ‘;’ before ‘...’ token
 {sock>>std::get<Indexes>(tup)...};
                                 ^
test.cpp:129:34: error: parameter packs not expanded with ‘...’:

The aim of this line is to get the value of argumenents. So I've change it with that :

const Serializer& c{sock>>std::get<Indexes>(tup)...};

Now it's accepted, but i've a big error at run time, output:

getVersion : 42
param:42
testParamInt: 54
*** Error in `/media/HDD1/DEV/test': double free or corruption (top): 0x0000000000603010 ***
======= Backtrace: =========
...

*** Error in `./test': double free or corruption (top): 0x00000000007b8010 ***

Using gdb, give me that it crash in: Serializer::~Serializer

Now the problem is described, can you help me?


Edit: solution

#include <tuple>
#include <type_traits>
template<int...> struct index_tuple{};

template<int I, typename IndexTuple, typename... Types>
struct make_indexes_impl;

template<int I, int... Indexes, typename T, typename ... Types>
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...>
{
    typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type;
};

template<int I, int... Indexes>
struct make_indexes_impl<I, index_tuple<Indexes...> >
{
    typedef index_tuple<Indexes...> type;
};

template<typename ... Types>
struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...>
{};

// ----------UNPACK TUPLE AND APPLY TO FUNCTION ---------
//

template<class Ret, class... Args, int... Indexes>
Ret exec__( Ret (*pf)(Serializer&,Args...),Serializer& sock, index_tuple< Indexes... >, std::tuple<Args...>&& args)
{
    int ctx[] = { ((sock >> std::get<Indexes>(args)), void(), 0)... };
    (void)ctx;
    return pf(sock,std::forward<Args>(std::get<Indexes>(args))...);
};


template<typename Ret,typename ... Args>
Ret exec(Ret(*pf)(Serializer&,Args ...),Serializer& sock)
{
    return exec__(pf,sock,typename make_indexes<Args...>::type(), std::tuple<typename std::remove_reference<Args>::type...>());
}
¿Fue útil?

Solución

This should do the trick:

template<typename T, T...>
struct integer_sequence
{ };

template<std::size_t... Indices>
struct indices : integer_sequence<std::size_t, Indices...>
{ };

template<std::size_t N, std::size_t... T>
struct build_indices : build_indices<(N - 1), (N - 1), T...>
{ };

template<std::size_t... T>
struct build_indices<0, T...> : indices<T...>
{ };

template<typename Fn, typename... Args, std::size_t... Idx>
auto exec__(Fn fn, Serializer& ser, std::tuple<Args...> args, indices<Idx...>)
-> decltype(fn(ser, std::get<Idx>(args)...))
{
    int ctx[] = { ((ser >> std::get<Idx>(args)), void(), 0)... };
    (void)ctx;
    return fn(ser, std::get<Idx>(args)...);
}

template<typename R, typename... Args>
auto exec(R(*fn)(Serializer&, Args...), Serializer& ser)
-> decltype(fn(ser, Args()...))
{
    return exec__(fn, ser, std::tuple<typename std::remove_reference<Args>::type...>(), build_indices<sizeof...(Args)>());
}

Live example on ideone

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top