Sunday, October 30, 2016

Writing Linux C++ code in VS 2015

(1) Install VC++ for Linux development Extnsion
(2) sudo apt-get install openssh-server g++ gdb gdbserver on target e.g Raspery Pi.
(3) Too->Options-> search connection->conn manager to set up SSH connection to ARM Raspery Pi
(4) VS project cross platform console app, main.cpp, remote gdb
(5) Headless app will run inside Debug->Linux Console.
(6) Raspery Pi ~/project will have file to run ./console1.out
#inlcude  is all that needed for now

Using VS code C++ extension for MSVC project

(1) delete all .vscode related file in the MSVC project directories
(2) Extension search C++ to install C++ Extension from Microsoft
(3) Open Folder to point to the project direct and open .cpp file
(4) ctl-shift-build, bring up config task, select "MS build task" nothing to change in task.json
(5) hit Debug and config launch, select "C++ Launch Windows", set program "c:\test\demo\debug\demo.exe"
if missing include then set includePath in c_cpp_properties.json to point to the include path under Win32

Saturday, October 29, 2016

using iterator_trait to get iterator type to *value and iterator category template specialization to get iter type


tag dispatching--template specialization can be based on the tag

So Dispatch() can pass in typename iterator_traits<T>::iterator_category() to find matching specialization
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <list>
#include <iterator>
#include <forward_list>

using namespace std;

template<typename InputIter, typename Func>
void adjacent_pair2(InputIter first, InputIter last, Func func)
{
 if (first != last)
 {
  typename std::iterator_traits<InputIter>::value_type trailer = *first;
  //auto trailer = *first;  // C++ 11,14 cannot tell type
  ++first;
  for (; first != last; ++first)
  {
   func(trailer, *first);
   trailer = *first;
  }
 }
};

template<class C>
void print_iter_category(C c, std::random_access_iterator_tag)
{
 cout << "from random iter tag specialization" << endl;
}

template<class C>
void print_iter_category(C c, std::bidirectional_iterator_tag)
{
 cout << "from bi-direct iter tag specialization" << endl;
}

template<class C>
void print_iter_category(C c, std::input_iterator_tag)
{
 cout << "from input iter specialization" << endl;
}

template<class C>
void print_iter_category(C c, std::output_iterator_tag)
{
 cout << "from output iter specialization" << endl;
}

template<class C>
void print_iter_category(C c, std::forward_iterator_tag)
{
 cout << "from fwd iter specialization" << endl;
}

template<typename T>
void Show(T t)
{
 print_iter_category(t, typename iterator_traits<T>::iterator_category());
 print_iter_category(t, iterator_traits<T>::iterator_category());;
}


int main()
{
 vector<int> v = { 2,16,36,109,8,999,90,5 };

 func_cout a_func;
 adjacent_pair2<vector<int>::iterator, func_cout>(v.begin(), v.end(),a_func);

 list<int> l;
 Dispatch(l.end());  /bi-dir

 Dispatch(v.begin()); // vector is random iter

 istream_iterator<int> isIter;
 Dispatch(isIter);
 
 ostream_iterator<int> osIter(cout);
 Dispatch(osIter);

 forward_list<int> fl;
 Dispatch(fl.begin());   //fwd

 std::string s;
 getline(cin, s);
    return 0;
}



C++ iterator, Func as operator () or std:function and predicate as func

(1) there are various kinds of iterators: forward, random, input, output,bi-directional, iterator_traits::iterator_category
(2) for(; ...) will avoid post increment iterator, since some does not support iter++
(3) C++ func are struct with operator() overridden or std::function as a function_ptr
(4) iterator can be de-referenced or indexed in *, []

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

template<typename ForwardIter, typename Func>
void adjacent_pair(ForwardIter first, ForwardIter last, Func func)
{
 if (first != last)
 {
  ForwardIter trailer = first;
  ++first;
  for (; first != last; ++first, ++trailer)
   func(*trailer, *first); 
 }
};

template<typename SourceIter, typename TargetIter, typename Predicate>
std::pair<SourceIter, TargetIter> copy_while(SourceIter srcFirst, SourceIter srcLast, TargetIter tgt, Predicate p)
{
 for (; srcFirst != srcLast && p(*srcFirst); ++srcFirst,++tgt )
  *tgt = *srcFirst;
 return std::make_pair(srcFirst,tgt);
};

struct func_cout
{
 inline void operator() (int a, int b) const { cout << a << " " << b << endl; }
};

struct func_predicate_even
{
 inline bool operator() (int a) const { return a % 2==0; }
} 

inline void print(int a,int b) { cout << a << "|" << b << endl;}

int main()
{
 vector<int> v = { 2,16,36,109,8,999,90,5 };

 func_cout a_func;
 adjacent_pair<vector<int>::iterator, func_cout>(v.begin(), v.end(),a_func);
 std::function<void(int,int)> f_ptr = print;
 adjacent_pair<vector<int>::iterator, std::function<void(int, int)>>(v.begin(), v.end(), f_ptr);

 vector<int> w(5);
 func_predicate_even pred;
 auto ret_iter = copy_while(v.begin(),v.end(), w.begin(),pred);
 cout << *ret_iter.first <<"**"<< *ret_iter.second <<"copied "<<w[1]<< endl;

 std::string s;
 getline(cin, s);
    return 0;
}


Thursday, October 27, 2016

Transform a type into awaitable by Adapter or pass trhough


#include "stdafx.h"

#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <future>
#include <thread>

using namespace std;
using namespace std::experimental;

template<typename T>
struct await_adapter
{
 T const& t_;
 bool await_readY() const;
 void await_suspend(std::experimental::coroutine_handle<> h) const;
 auto await_resume() const;
};

template<typename T>
class IAsyncOperation 
{

};

template<typename T>
await_adapter<IAsyncOperation<T>> operator co_await(IAsyncOperation<T> const& async)
{
 return async;
};

struct pass_through
{
 int result;
 bool await_ready() { return true; }
 void await_suspend(std::experimental::coroutine_handle<> h) {};
 auto await_resume() { return result; }
};

struct value
{
 int result;
};

pass_through operator co_await(value v)  // overload operator to make awaitable
{
 return{ v.result };
};

void test()
{
 auto i = co_await pass_through{ 123 };
 auto j = co_await value{ 456 };
}

int main()
{
 cout << "main thread id:" << std::this_thread::get_id() <<endl;
 test();
 //cout << r.get() << endl; //get causes return, no SM setup
 
 std::string s;
 getline(cin, s);
    return 0;
}


Tuesday, October 25, 2016

co_await vs. future.get()

co_wait will setup a state machine to return, future has to call get() to return

#include "stdafx.h"
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <future>
#include <thread>

using namespace std;
using namespace std::experimental;

std::future<int> produceAsync()
{
 return std::async(std::launch::async, [] {
  cout << " future thread id:"<< std::this_thread::get_id()<<endl;
  std::this_thread::sleep_for(5s);
  return 42;
 });
}

std::future<void> ConsumeAsync()
{
 auto r = co_await produceAsync(); // set up resume state machine
 cout << r << endl;
}

int main()
{
 cout << "main thread id:" << std::this_thread::get_id() <<endl;
 ConsumeAsync();


 auto r = produceAsync();
 //cout << r.get() << endl; //get causes return, no SM setup

 std::string s;
 getline(cin, s);
    return 0;
}



Monday, October 24, 2016

generator pipeline and accumulate


#include "stdafx.h"
#include <experimental\generator>
#include <experimental\coroutine>
#include <iostream>
#include <string>
#include <numeric>

using namespace std;
using namespace std::experimental;

std::experimental::generator<int> gen()
{
 for (int i = 0;; ++i)
  co_yield i;
};

std::experimental::generator<int> take_until(std::experimental::generator<int> g, int sentinel)
{
 for (auto v : g)
 {
  if (v == sentinel)
   break;
  co_yield v;
 }
}


int main()
{
 auto g1 = take_until(gen(), 10);
 auto a = std::accumulate(g1.begin(), g1.end(), 0);
  cout << a << endl;

 std::string s;
 getline(cin, s);
    return 0;
}