mercredi 1 juillet 2015

boost regex - get all matches of repeated group

I need to parse some html

<a href="...">Text 1</a><br>
<a href="...">Text 2</a><br>
...
<a href="...">Text N-1</a><br>
<a href="...">Text N</a><br>

I need all "Text ...", but I don't know what exactly is N. So I wrote this regex

(?:<a href="[^"]*">([^<]*)</a><br>){1,}

Using that regex I can get only first "Text ...", but I need text from all links. Is it possible?

set is not member of boost::PolygonSet

I am trying to overload & operator for PolygonSet but end with following error. I can understand boost::PolygonSet does not have set function but can anyone provide me input to fix this issue. I have tried view_as but did not success.

The code:

PolygonSet PolygonSet::operator&(const PolygonSet& right) const {
    using namespace boost::polygon::operators;
    PolygonSet ps (*this);
    ps &= right;
    return ps;
}

Error:

/u/logic/depot/boost_1_57_0/include/boost/polygon/detail/polygon_set_view.hpp:199:5: error: ‘set’ is not a member of ‘boost::polygon::polygon_set_mutable_traits’

Message during compilation required from here

/u/logic/depot/boost_1_57_0/include/boost/polygon/polygon_90_set_traits.hpp:57:51: error: no type named ‘coordinate_type’ in ‘boost::polygon::get_coordinate_type::traits_type {aka struct boost::polygon::polygon_set_traits}’ In file included from /u/logic/depot/boost_1_57_0/include/boost/polygon/polygon_set_data.hpp:999:0, from /u/logic/depot/boost_1_57_0/include/boost/polygon/polygon.hpp:81, from /home /cpp/src/boost_geom_api/common_headers.hpp:4,

/u/logic/depot/boost_1_57_0/include/boost/polygon/detail/polygon_set_view.hpp: In instantiation of ‘geometry_type_1& boost::polygon::self_assignment_boolean_op(geometry_type_1&, const geometry_type_2&) [with geometry_type_1 = boost_geom_api::PolygonSet; geometry_type_2 = boost_geom_api::PolygonSet; int op_type = 1]’: /u/logic/depot/boost_1_57_0/include/boost/polygon/polygon_set_concept.hpp:419:90: required from ‘typename boost::enable_if::type, typename boost::polygon::is_any_polygon_set_type::type>::type, geometry_type_1>::type& boost::polygon::operators::operator&=(geometry_type_1&, const geometry_type_2&) [with geometry_type_1 = boost_geom_api::PolygonSet; geometry_type_2 = boost_geom_api::PolygonSet; typename boost::enable_if::type, typename boost::polygon::is_any_polygon_set_type::type>::type, geometry_type_1>::type = boost_geom_api::PolygonSet]’

Alias to member function

Consider the following structs :

struct A{
    template <size_t N>
    int * get();
};

struct B{
   A a;
};

if I have an object b, I can call b.a.get<0>(), I would like to make that call more simple, something like b.get_0()

I did this :

struct B{
       A a;
       decltype(std::bind(static_cast<int*(A::*)()>(&A::get<0>), tr1::ref(a))) get_0 
        = std::bind(static_cast<int*(A::*)()>(&A::get<0>), std::ref(a));
    };

this is working, but not beautiful ...

I want to simplify this, I can add as many aliases in struct A, as few as possible in B :

struct B{
       A a;
       // Some aliases
       return_type<0> get_0 
        = call<0>;
    };

I was working with singleton pattern and shared_ptr.I was trying to make the code like this:

class A{
    private:
        static std::shared_ptr<A> instance;
        A();
    public:
        static std::shared_ptr<A> creatInstance();
};

std::shared_ptr<A> A::creatInstance(){
    if(!instance){
        instance=std::make_shared<A>();
    }
    return instance;
}

But I got a compiler error. Any thoughts? I tried to make make_shared be a friend function of class A,but it didn't work.

friend shared_ptr<A> make_shared<A>();

cannot use operator= with std::stringstream

I am trying to make a struct, in which one of the members are std::stringstream type. I am using c++11, accrdong to this I could do it: http://ift.tt/1dwDKbo Here is my code:

struct logline_t
    {
        stringstream logString; /*!< String line to be saved to a file (and printed to cout). */
        ElogLevel logLevel; /*!< The \ref ElogLevel of this line. */
        timeval currentTime; /*!< time stamp of current log line */

        logline_t& operator =(const logline_t& a)
        {
            logString = a.logString;
            logLevel = a.logLevel;
            currentTime = a.currentTime;

            return *this;
        }
    };

It doesnt compile, as I am getting this error:

error: use of deleted function ‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)’

I dont understand why doesnt it work, I have tried logString = move(a.logString); as well. same result. I would apreciate all help.

Very automatic operator generator in C++

C++ has a nice idiom that allows one to write classes that automatically fulfill certain relations between operators. For example this allows to define operator== and not bother to defined operator!= as well. This is the idea behind Boost.Operators.

This is an example:

template<class Self> // this class is defined only once
struct equally_comparable{
    friend bool operator!=(Self const& s1, Self const& s2){return !(s1==s2);}
};

This class can used repeatedly to enforce consistent logic between == and != (and avoid errors)

struct A : equally_comparable<A>{ // 
    int value;
    A(int v) : value(v){}
    friend bool operator==(A const& a1, A const& a2){return a1.value == a2.value;}
};

int main(){

    A a1{4};
    A a2{4};
    assert(a1 == a2);
    A a3{5};
    assert(a1 != a3); // uses automatically generated operator !=
}

Now, I want to go one level further and have a class similar to equally_comparable and define the function the other function. For example if operator== is defined then define operator!= (like above), but also viceversa.

The first naive attempt works

template<class Self>
struct equally_comparable{
    friend bool operator!=(Self const& s1, Self const& s2){return !(s1==s2);}
    friend bool operator==(Self const& s1, Self const& s2){return !(s1!=s2);}
};

because only one of the two functions need to be defined in struct A (either operator== or operator!=). However it is dangerous because if one forget to define either operator in A there is an infinite recursion (and a runtime segfault). It also looks fragile.

Is is possible to improve over this and detect that at least one is defined in the derived class at compile time? or more generally is there a generic way to have a class that generates the missing operators? (i.e. a step beyond Boost.Operators).

How to mock function in C when its caller function is defined in same file?

I am trying to mock a function in C, mocking works fine when the function and its caller function are defined in different files. But, when both function(function need to be mocked and its caller) are defined in same file, mocked function is not being invoked.


Case 1 :

//test.c

#include <stdio.h>

/*mocked function*/
int __wrap_func() {
   printf("Mock function !!!\n"); 
}

/*caller function*/
int myTest() {
  return func();
}

int main() {
    myTest();
    return 0;
}

//file.c
#include<stdio.h>

/*function need to be mocked*/
int func() {
  printf("Original function !!!\n");
    }


Case 2 :

//test.c
#include <stdio.h>
extern int myTest();
/*mocked function*/
int __wrap_func() {
  printf("Mock function !!!\n");
}

int main() {
    myTest();
}
//file.c
#include<stdio.h>

/*function need to be mocked*/
int func() {
  printf("Original function !!!\n");
}

/*caller function*/
int myTest() {
  return func();
}


Code compilation command : gcc -Wl,--wrap=func test.c file.c

In Case 1 . Mock function !!!
In Case 2 . Original function !!!

In case 2, mocking function is not being invoked. I am looking for a solution where I can mock function even caller and called function are in same file.