vendredi 28 août 2015

boost:bind and io_service in two different classes

I am kind'a new to Boost.

I would like to know how to construct an io_service in one class and send it tasks from another class.

My problem is BOOST_ASIO_COMPLETION_HANDLER_CHECK complains and wont let the code compile.

He is a skeleton of my code:

type_defs.h (this has the function to be wrapped and sent to the io_service)

#include "boost/filesystem.hpp"

namespace fs = ::boost::filesystem;

typedef boost::function<void(const fs::path& path)> parse_and_aggregate_fun_t;

io_service_processors.h (this is the class that will construct the io_service and wait for job posts).

namespace asio = boost::asio;

typedef std::unique_ptr<asio::io_service::work> work_ptr;

class io_service_processors {
public:
    io_service_processors(int tc);
    virtual ~io_service_processors();

    void init();
    void post_job(parse_and_aggregate_fun_t file_job);

private:
    int thread_count;
    asio::io_service* service;
    boost::mutex mtx;
    work_ptr * work;
    boost::thread_group workers;
};

io_service_processors.cpp

namespace asio = boost::asio;   

io_service_processors::io_service_processors(int tc):thread_count(tc) {

}

io_service_processors::~io_service_processors() {
       // clean up code here removed.
}

void io_service_processors::init() {
    this->service = new asio::io_service();
    this->work = new work_ptr(new asio::io_service::work(*(this->service)));
    for(int i = 0; i < this->thread_count; ++i)
        this->workers.create_thread(boost::bind(&asio::io_service::run, this->service));
}

void io_service_processors::post_job(parse_and_aggregate_fun_t file_job) {
    this->service->post(file_job);
}

job_discoverer.cpp (this one will discover files and send their paths to to the io_service, the function parse_and_aggregate will the actual work for opening the file and processing it, process will simply post the wrapped function to the io_service, this->processor is simply a pointer to the io_service wrapper class above).

void job_discoverer::process(const fs::path path){
    std::cout << "Posting file: " << path.string() << std::endl;
    if(this->processor)
        this->processor->post_file_job(
                boost::bind(&text_file_type::parse_and_aggregate, this, path)
                );
}

void job_discoverer::parse_and_aggregate(const fs::path path) {
    std::cout << "Parsing and aggregating file: " << path.string() << std::endl;
}

Thank you!!!!!!!!!!

Ininialize a c-string in C++11?

How do I ininialize a c-string in C++11?

I tried:

char* foo = {"bar"};

but I get:

ISO C++11 does not allow conversion from string literal to 'char *

Call to derived class function in vector populated with variadic initializer list doesn’t behave polymorphically

I am using an initializer list to declare an arbitrary number of item pairs at compile time, each consisting of a key and a real type as the value. There are two derived real types in the model code: MyA and MyB, both of which derive from My. The vector gets initialized with a key and a particular derived type like, {33, MyA}. I insert this set of initializations into a vector using std::initializer_list. The MyA and MyB constructors get properly called so I know the correct objects are getting constructed.

Being able to declare/instantiate an arbitrary number of item pairs at compile time will greatly reduce the complexity of my prototype code by eliminating much redundant code and allowing me to easily add additional sets of pairs as the code matures. itemVect1 in the model code works fine.

But when I make a call on MyAFunc(), itemVect1 executes the base class’s MyFunc().

Is there a way I can use this very powerful variadic code machinery and still maintain polymorphic access to various derived types which I want to pass in in the initializer list?

I built my model code off of the code in StackOverflow “Inserting a variadic argument list into a vector?”

struct My
{
  My() {}
  void MyFunc() {}
  virtual void MyAFunc() {}
  virtual void MyBFunc() {}
};

struct MyA : My
{
  MyA() : My() {}
  void MyAFunc() {}
};

struct MyB : My
{
  MyB() : My() {}
  void MyBFunc() {}
};

template< typename KeyT, typename ValueT >
struct ItemPair
{
  ItemPair( KeyT key, ValueT val )
    : key( key ), val( val ) {}
  KeyT key;
  ValueT val;
};

typedef ItemPair< const int, My > IPIntMy;

struct ItemPairVect
{
  ItemPairVect( std::initializer_list< IPIntMy > initList )
    : ipVect( initList ) {}

  std::vector< IPIntMy > ipVect;
};

int main()
{
  ItemPairVect itemVect1 { IPIntMy { 33, MyA {} }, IPIntMy { 44, MyB {} }, IPIntMy { 55, MyA {} }, IPIntMy { 66, MyB {} } }; // works fine
  itemVect1.ipVect[ 0 ].val.MyAFunc(); // doesn't behave polymorphically, that is, it calls MyFunc() instead
}

I tried changing My to My* in the typedef which is the normal way to make an object polymorphic, but had problems getting the initializer list to parse.

Returning a constexpr std::array

When I try to compile this class, the linker tells me that FooHandler ::getFoo is is not found for architecture x86_64.

enum class Foo {Bar};


class FooHandler {

public:

    constexpr std::array<Foo, 1> static getFoo() {
        return foo;
    }

    static constexpr std::array<Foo, 1> foo = { {Foo::Bar} };
};

Is there anything I am missing here?

Value referenced by pointer changes when passing from function [duplicate]

This question already has an answer here:

I am trying to pass an array out of a function using pointers. When I dereference the pointer inside the function, it gives the correct value, but when it is dereferenced in the main function, it gives a wildly different value.

#include <iostream>

using namespace std;

int* func()  {
    int arr[4] = {0,1,2,3};
    int* ptr = arr;
    cout << ptr << endl;
    cout << *ptr << endl;
    return ptr;
}

int main()
{
    int* ptr;
    ptr = func();
    cout << ptr << endl;
    cout << *ptr << endl;

    return 0;
}

Getting std::regex_search to work [duplicate]

This question already has an answer here:

I can't seem to get std::regex_search to work. The simple code below won't return true no matter what string I input.

std::regex reg1("password");
std::string temp;
while (getline(std::cin, temp))
{
    if (std::regex_search(temp, reg1))
        std::cout<<"Match!"<<std::endl;
}

std::regex_match will return true if I input "password". I have tried using gcc 4.8.4 and clang 3.6. I'm assuming it's some silly issue, but I can't put my finger on it.

C++ cout not working

I'm using Visual studio 2010 C++11 but, no matter what I do, cout doesn't work. Every time I write it is marked as an error. I have found and tried manyways to solve it(std::, namespace std, end1) but none of them work. Are there any other ways to fix the problem or a different way to print text? Thanks in advance