12 Kasım 2016 Cumartesi

thread loop_executor Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/thread/executors/loop_executor.hpp>
Constructor
Şöyle yaparız.
boost::loop_executor ex;
try_executing_one metodu
Şöyle yaparız.
while ( ex.try_executing_one() )
  std::this_thread::sleep_for(100ms);


11 Kasım 2016 Cuma

date_time gregorian Sınıfları

Giriş
Gregorian başlığı altında date, date_duration, date_period, day_iterator gibi sınıflar mevcut.

date sınıfı
date Sınıfı yazısına taşıdım.

date generator algorithms
next_weekday metodu
Verilen tarihten sonraki cumayı döndürmek istersek şöyle yaparız.
date f(date d){
  return next_weekday(d,boost::gregorian::greg_weekday(boost::date_time::Friday));
}

10 Kasım 2016 Perşembe

spirit lex tokenize metodu

Önce token'lara sayı veririz. Şöyle yaparız.
// Token ids
enum token_ids {
    ID_EOL= 100
};

// Token definition
template <typename Lexer>
struct var_replace_tokens : boost::spirit::lex::lexer<Lexer> {
  var_replace_tokens() {
    this->self.add ("\n", ID_EOL); // newline characters
  }
};
Her token'ı saymak için functor yaparız. Şöyle yaparız.
// Functor
struct replacer {
  typedef bool result_type;
  template <typename Token>
  bool operator()(Token const& t, std::size_t& lines) const  {
    switch (t.id()) {
      case ID_EOL:
        lines++;
        break;  
    }
    return true;
  }
};
lexer''ı kurarız. Şöyle yaparız.
size_t lines=0;

var_replace_tokens<
 boost::spirit::lex::lexertl::lexer<
   boost::spirit::lex::lexertl::token<
     boost::spirit::istream_iterator> > > var_replace_functor;
lexer'ı functor ile çağırırız. Şöyle yaparız.
cin.unsetf(std::ios::skipws);

boost::spirit::istream_iterator first(cin);
boost::spirit::istream_iterator last;

bool r = boost::spirit::lex::tokenize(first, last, var_replace_functor,
  boost::bind(replacer(), _1, boost::ref(lines)));

if (r) {
  cerr<<"Lines processed: "<<lines<<endl;
}  else {
  string rest(first, last);
  cerr << "Processing failed at: "<<rest<<" (line "<<lines<<")"<<endl;
}



9 Kasım 2016 Çarşamba

regex regex_search metodu

regex_search metodu - iterator + iterator + match_results + flags
Şöyle yaparız.
std::string str = "...";

boost::regex r ("...");
std::string::const_iterator start = str.begin();
std::string::const_iterator end   = str.end();
boost::match_flag_type flags = boost::match_default;

boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search (start, end, what, r, flags);

filesystem filesystem_error Sınıfı

Giriş
Bu sınıfı bir exception olduğu için catch içinde kullanırız. Şöyle yaparız.
try
{
  ...
}
catch (bfs::filesystem_error & e)
{
  ...
}
code metodu
Şöyle yaparız.
boost::system::error_code ec = e.code();

7 Kasım 2016 Pazartesi

graph copy_graph metodu

Giriş
Şu satırı dahil ederiz.
#include <boost/graph/copy.hpp>
Aynı özelliklere sahip ancak vectorS , setS gibi farklı tipler kullanan Graph nesnelerini kopyalamak için kullanılır.

Örnek 1
Elimizde iki farklı Graph tipi olsun.
typedef boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, 
  uint32_t, float> Graph1;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 
  uint32_t, float> Graph2;
Elimizde iki graph nesnesi olsun.
Graph1 g1;
...
Graph2 g2;
Şöyle yaparız.
std::map<Graph1::vertex_descriptor, int> index;
for (auto v : boost::make_iterator_range(boost::vertices(g1))) {
  index.insert(std::make_pair(v, index.size()));
}

Graph2 g2;
boost::copy_graph(g1, g2,
  boost::vertex_index_map(boost::make_assoc_property_map(index)));
Örnek 2
Elimizde iki farklı Graph tipi olsun.
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::undirectedS,
  uint32_t,float> Graph1;

typedef AdjacencyList::vertex_descriptor VertexID;

struct custom_property
{
  ...  
};

typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::undirectedS,
  custom_property,float> Graph2;
custom_property kopyalanacek şekilde şöyle olsun
struct custom_property {
  custom_property(uint32_t label = 0, float f = 0) : label(label), f(f) {}
  uint32_t label;
  float f;
};
Şöyle yaparız.
boost::copy_graph(g1,g2);
Örnek 3
Functor kullanarak şöyle yaparız.
struct vertex_copier {
  Graph1& from;
  Graph2& to;

  void operator()(Graph1::vertex_descriptor input, 
                  Graph2::vertex_descriptor output) const {
    to[output] = { from[input], 0.f };
  }
};
Şöyle yaparız.
boost::copy_graph(g1, g2, boost::vertex_copy(vertex_copier{g1, g2}));

6 Kasım 2016 Pazar

program_options variables_map Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/program_options.hpp>
Kolay kullanım için şu satırı dahil ederiz.
namespace po = boost::program_options;
Bu sınıf parse sonucunu aynı bir map şeklinde kullanabilmemizi sağlar. Sınıfın eksik bir tarafı verilen komut satırının sırasının kaybedilmesi. Eğer sıranın önemi varsa kullanılamaz.

Şöyle yaparız.
po::options_description options;
po::variables_map vm;

options.add_options()
  ("var1", po::value<int>()->required(), "var1");

po::store (po::parse_command_line(argc, argv, config_descriptor), vm);
po::notify (vm);
Sanırım bu sınıf altta boost any kütüphanesini kullanıyor

Constructor
Şöyle yaparız.
po::variables_map vm;
at metodu
Şöyle yaparız.
if (po::vm.count("bar") == 1) {
  std::cout << "bar: " << po::vm.at("bar").as<std::string>() << std::endl;
}
as metodu
as<bool>()
Şöyle yaparız
po::vm.count ("withNoDiffusionTerm") ?
 po::vm ["withNoDiffusionTerm"].as<bool>() : false);
as<int>()
Şöyle yaparız
po::vm ["domainSize"].as<int>());
Şöyle yaparız.
if (po::vm.count ("foo") == 1) {
  std::cout << "foo: " << po::vm.at ("foo").as<int>() << std::endl;
}
as<string>()
Şöyle yaparız
po::vm.count("inputImage") ? po::vm["inputImage"].as<std::string>() : "");
po::vm.count("shape") ? po::vm ["shape"].as<std::string>() : "");
Şöyle yaparız.
if (po::vm.count ("bar") == 1) {
  std::cout << "bar: " << po::vm.at("bar").as<std::string>() << std::endl;
} 
count metodu
Aranılan seçeneğin girilip girilmediğini kontrol edebilmemizi sağlar. Şöyle yaparız.
if (po::vm.count("input")) { 
   std::string my_path = po::vm["input"].as<std::string>();
   boost::filesystem::path file_path (my_path);
   std::cout << "The input path is: " << file_path << std::endl;
}
operator []
at metodu ile aynıdır.  Şöyle yaparız
po::vm.count("inputImage") ? po::vm["inputImage"].as<std::string>() : "");