last changes from bloomberg.

This commit is contained in:
Vahagn Khachatryan
2021-03-25 08:15:36 -04:00
parent 1a17b9fe6c
commit 721e183dc9
20 changed files with 1391 additions and 0 deletions

87
cpp/boost/multi_index.cpp Normal file
View File

@@ -0,0 +1,87 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM: let g:cppflags=g:Iboost.g:Itbb
VIM: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy
VIM: let g:ldlibpath=g:Bboost.g:Btbb
VIM: let g:argv=""
VIM-: let g:cf5output=0
*/
#include <iostream>
#include <exception>
#include <boost/multi_index_container.hpp>
//#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/identity.hpp>
struct entry
{
std::string d_sessionId;
std::string d_curveId;
std::string key3;
};
typedef boost::multi_index::ordered_unique<
boost::multi_index::identity<entry>
> index_by_entry;
struct ByCurveId {};
typedef boost::multi_index::hashed_unique<
boost::multi_index::tag<ByCurveId>,
boost::multi_index::member<
entry,
std::string,
&entry::d_curveId
>
> index_ByCurveId;
struct BySessionId {};
typedef boost::multi_index::hashed_unique<
boost::multi_index::tag<BySessionId>,
boost::multi_index::member<
entry,
std::string,
&entry::d_sessionId
>
> index_BySessionId;
typedef boost::multi_index_container<
entry,
boost::multi_index::indexed_by<
//boost::multi_index::sequenced<>,
//index_by_entry,
index_BySessionId,
index_ByCurveId
>
> my_multi_index_container_type;
int main ( void )
{try{
my_multi_index_container_type c;
if ( c.get<BySessionId>().find( "tutuc" )
== c.get<BySessionId>().end() )
{
std::cout << "no tutuc\n";
}
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,102 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let b:cppflags=g:Iboost.g:Itbb
VIM-: let b:ldflags=g:Lboost.g:Ltbb
VIM-: let b:ldlibpath=g:Bboost.g:Btbb
VIM-: let b:argv=""
*/
#include <iostream>
#include <exception>
class A
{
public:
int i;
A() {
std::cout << "default ctr" << std::endl;
}
A(const A&) {
std::cout << "copy ctr" << std::endl;
}
A(A&&) {
std::cout << "move ctr" << std::endl;
}
void operator = (const A&) {
std::cout << "copy" << std::endl;
}
void operator = (A&&) {
std::cout << "move" << std::endl;
}
};
void test3( A a ){
std::cout << "by value" << std::endl;
}
void test3( A&& a ){
std::cout << "rvalue" << std::endl;
}
void test3( const A& a ){
std::cout << "const lvalue&" << std::endl;
}
void test3( A& a ){
std::cout << "lvalue &" << std::endl;
}
template <typename T>
void test2( T&& t ){
test3(std::forward<T>(t));
}
template <typename T>
void test( T t ){
//test2(std::forward<T>(t));
test3(t);
}
A rvalue()
{
return A();
}
A& lvalue()
{
static A a;
return a;
}
const A& clvalue()
{
static A a;
return a;
}
int main ( void )
{try{
test(rvalue());
test(lvalue());
test(clvalue());
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,72 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <vector>
//#include <initializer_list>
template <typename CONT>
void print( CONT&& cont )
{
std::cout << "template ";
for( auto n : cont ){
std::cout << n << ' ';
}
std::cout << std::endl;
}
#if 1
void print( const std::initializer_list<auto>& cont )
{
std::cout << "initializer_list<auto> ";
for( auto n : cont ){
std::cout << n << ' ';
}
std::cout << std::endl;
}
#if 1
void print( const std::initializer_list<int>& cont )
{
std::cout << "initializer_list<int> ";
for( auto n : cont ){
std::cout << n << ' ';
}
std::cout << std::endl;
}
#endif
#endif
int main ( void )
{try{
std::vector<int> v = {1,2,3};
print(v);
print({3,4,5});
print({3LL,4LL,5LL});
print({3LL,4,5});
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

42
cpp/cpp11/overload.cpp Normal file
View File

@@ -0,0 +1,42 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <string>
void a( std::string&& s )
{
std::cout << " by value " << std::endl;
}
void a( const std::string& s )
{
std::cout << " const ref " << std::endl;
}
int main ( void )
{try{
std::string s = "bbbbbb";
a(s);
a(std::string("aaaa"));
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

93
cpp/cpp11/test1.cpp Normal file
View File

@@ -0,0 +1,93 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <trnx_vector.h>
namespace trnx {
struct A{
int a1;
};
void test()
{
trnx::vector<A> v;
v.push_back(A{0});
v.push_back(A{1});
v.push_back(A{2});
for(const auto& x : v)
{
std::cout << x.a1 << ' ';
}
std::cout << std::endl;
}
#if 1
struct MoveOnly
{
int d_datum;
// Allow moves
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
// Prevent copies
MoveOnly(const MoveOnly&) = delete;
MoveOnly& operator=(const MoveOnly&) = delete;
};
void lab1()
{
trnx::vector<MoveOnly> v;
v.push_back(MoveOnly{0});
v.push_back(MoveOnly{1});
v.push_back(MoveOnly{2});
for(const auto& x : v)
{
std::cout << x.d_datum << ' ';
}
std::cout << std::endl;
}
#endif
void main()
{
test();
lab1();
}
} // close namespace trnx
int main ( void )
{try{
trnx::main();
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

75
cpp/cpp11/trnx_order.h Normal file
View File

@@ -0,0 +1,75 @@
#ifndef INCLUDED_TRNX_ORDER
#define INCLUDED_TRNX_ORDER
#include <bsl_iostream.h>
#include <bsl_string.h>
#include <bael_log.h>
namespace BloombergLP {
namespace trnx {
class Order
{
public:
Order()
{
BAEL_LOG_SET_CATEGORY("ORDER");
BAEL_LOG_INFO << "Order::Order()" << BAEL_LOG_END;
};
Order(const bsl::string& symbol, double price, int amount,
char side)
: d_symbol(symbol), d_price(price), d_amount(amount),
d_side(side)
{
BAEL_LOG_SET_CATEGORY("ORDER");
BAEL_LOG_INFO << "Order::Order(string, double, int, char)"
<< BAEL_LOG_END;
}
Order(const Order& other)
: d_symbol(other.d_symbol), d_price(other.d_price),
d_amount(other.d_amount), d_side(other.d_side)
{
BAEL_LOG_SET_CATEGORY("ORDER");
BAEL_LOG_INFO << "Order::Order(const Order&)" << BAEL_LOG_END;
}
~Order()
{
BAEL_LOG_SET_CATEGORY("ORDER");
BAEL_LOG_INFO << "Order::~Order()" << BAEL_LOG_END;
}
bool operator==(const Order& other) const
{
return (d_symbol == other.d_symbol &&
d_price == other.d_price &&
d_amount == other.d_amount && d_side == other.d_side);
}
const bsl::string& symbol() const { return d_symbol; }
double price() const { return d_price; }
int amount() const { return d_amount; }
char side() const { return d_side; }
private:
bsl::string d_symbol;
double d_price;
int d_amount;
char d_side;
friend bsl::ostream& operator<<(bsl::ostream& os, const Order& o);
};
inline bsl::ostream& operator<<(bsl::ostream& os, const Order& o)
{
os << "[" << o.d_symbol << ',' << o.d_price << ',' << o.d_amount
<< ',' << o.d_side << "]";
return os;
}
} // close namespace trnx
} // close namespace BloombergLP
#endif

224
cpp/cpp11/trnx_vector.h Normal file
View File

@@ -0,0 +1,224 @@
#ifndef INCLUDED_TRNX_VECTOR
#define INCLUDED_TRNX_VECTOR
#include <cstddef>
#include <exception>
#include <iterator>
#include <type_traits>
#include <memory>
#include <trnx_vector_impl.h>
namespace trnx {
template <typename T>
class vector
{
public:
typedef T value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
private:
size_type d_size;
size_type d_capacity;
typedef detail::uninitialized<T> buff_type[];
std::unique_ptr<buff_type> d_buff;
// `uninitialized<T>` represents enough uninitialized storage for an
// instance of `T`. The lifetime of the `T` instance is controlled
// manually via the `.construct` and `.destroy` member functions.
// An initialized instance can be accessed through `.get`.
public:
typedef detail::iter_impl<detail::uninitialized<T> > iterator; //CHG
typedef detail::iter_impl<const detail::uninitialized<T> > const_iterator; //CHG
// `iter_impl` is an iterator type that automatically transforms
// `uninitialized<T>*` into `T*` when dereferencing.
vector() : d_size(0), d_capacity(0), d_buff(nullptr)
{
// A default-constructed vector is empty and has no allocated buffer.
}
vector(const vector& rhs)
: d_size(rhs.d_size),
d_capacity(rhs.d_capacity),
// Allocate enough space for `rhs.d_size` items.
d_buff(std::make_unique<buff_type>(rhs.d_size))
{
// Copy-construct all of `rhs`'s elements into the current buffer.
for(size_type i = 0; i < d_size; ++i)
{
d_buff[i].construct(rhs.d_buff[i].get());
}
}
~vector()
{
// Destroy all elements in the vector. In the Standard, the order of
// destruction is unspecified.
for(size_type i = 0; i < d_size; ++i)
{
d_buff[i].destroy();
}
}
vector<T>& operator=(const vector<T>& rhs);
// The copy-constructor allocates enough memory to store `rhs`'s
// elements in a new buffer, copies over the current elements to the
// new buffer, and finally sets `d_buff` to point to the new buffer.
size_type size() const { return d_size; }
size_type capacity() const { return d_capacity; }
bool empty() const { return d_size == 0; }
iterator begin() { return iterator(d_buff.get()); }
iterator begin() const { return iterator(d_buff.get()); }
iterator end() { return iterator(d_buff.get() + d_size); }
iterator end() const { return iterator(d_buff.get() + d_size); }
const_iterator cbegin() const { return const_iterator(d_buff.get()); }
const_iterator cend() const { return const_iterator(d_buff.get() + d_size); }
reference front() { return d_buff[0].get(); }
const_reference front() const { return d_buff[0].get(); }
reference back() { return d_buff[d_size - 1].get(); }
const_reference back() const { return d_buff[d_size - 1].get(); }
reference operator[](size_type i) { return d_buff[i].get(); }
const_reference operator[](size_type i) const { return d_buff[i].get(); }
reference at(size_type i)
{
if(i >= d_size)
{
throw std::out_of_range("Index out of range");
}
else
{
return d_buff[i].get();
}
}
const_reference at(size_type i) const
{
// The following use of `const_cast` is legal and simply prevents code
// repetition with the non-`const` version of `at`.
return const_cast<vector<T>&>(*this).at(i);
}
void push_back(const T& item);
void push_back(T&& item);
void pop_back();
void clear();
void reserve(size_type new_capacity);
};
template <typename T>
void vector<T>::push_back(const T& item)
{
if(d_size >= d_capacity)
{
const size_type new_capacity = (d_capacity == 0) ? 1
: d_capacity * 2;
reserve(new_capacity);
}
d_buff[d_size++].construct(item);
}
template <typename T>
void vector<T>::push_back(T&& item)
{
if(d_size >= d_capacity)
{
const size_type new_capacity = (d_capacity == 0) ? 1
: d_capacity * 2;
reserve(new_capacity);
}
d_buff[d_size++].construct(std::move(item));
}
template <typename T>
void vector<T>::pop_back()
{
BSLS_ASSERT(!empty());
d_buff[d_size - 1].destroy();
--d_size;
}
template <typename T>
void vector<T>::clear()
{
// Destroy all existing elements
for(size_type i = 0; i < d_size; ++i)
{
d_buff[i].destroy();
}
// Set size to zero, but leave capacity unchanged
d_size = 0;
}
template <typename T>
vector<T>& vector<T>::operator=(const vector& rhs)
{
// Prevent self-assignment
if(&rhs != this)
{
// Destroy all existing elements and set size to zero
clear();
// Reserve if necessary
reserve(rhs.d_size);
// Copy elements from `rhs`
for(size_type i = 0; i < rhs.d_size; ++i)
{
d_buff[i].construct(rhs.d_buff[i].get());
}
// Update size
d_size = rhs.d_size;
}
return *this;
}
template <typename T>
void vector<T>::reserve(size_type new_capacity)
{
// Exit early if there's no need to reserve more memory
if(new_capacity <= d_capacity) { return; }
// Allocate a new buffer
auto buff = std::make_unique<buff_type>(new_capacity);
// Copy-construct existing elements into the new buffer and destroy them
// in the old one
for(size_type i = 0; i < d_size; ++i)
{
buff[i].construct(std::move(d_buff[i].get()));
d_buff[i].destroy();
}
// Deallocate old buffer, set the owned buffer to `buff`
d_buff = std::move(buff);
// Update capacity
d_capacity = new_capacity;
}
} // close namespace trnx
#endif

View File

@@ -0,0 +1,106 @@
#ifndef INCLUDED_TRNX_VECTOR_IMPL
#define INCLUDED_TRNX_VECTOR_IMPL
#include <cstddef>
#include <exception>
#include <iterator>
#include <type_traits>
namespace trnx {
namespace detail {
// This union is not meant to be shown/completely understood from the students.
//
// They should know that `uninitialized<T>` means "enough storage for a `T`
// instance, and that they can manually control the lifetime of that instance
// through the `.construct` and `.destroy` member functions.
//
// It is undefined behavior to:
// * Call `construct` on an already-initialized instance.
// * Call `destroy` on a non-initialized instance.
// * Access the reference returned by `get` on a non-initialized instance.
//
template <typename T>
union uninitialized
{
private:
T d_datum;
public:
using inner_type = T;
uninitialized() { }
~uninitialized() { }
template <typename... Ts>
void construct(Ts&&... xs)
{
new (&d_datum) T(std::forward<Ts>(xs)...);
}
void destroy()
{
d_datum.~T();
}
T& get() { return d_datum; }
const T& get() const { return d_datum; }
};
// This class is not meant to be shown/completely understood from the students.
//
// It basically unwraps `uninitialized<T>` upon dereference, behaving as any
// other normal random access iterator, transparently to the user.
//
template <typename T>
class iter_impl
{
private:
T* d_ptr;
using inner_type = typename T::inner_type;
public:
using difference_type = std::ptrdiff_t;
using value_type = inner_type;
using pointer = inner_type*;
using reference = inner_type&;
using iterator_category = std::random_access_iterator_tag;
iter_impl() : d_ptr(nullptr) { }
iter_impl(T* rhs) : d_ptr(rhs) { }
iter_impl(const iter_impl& rhs) : d_ptr(rhs.d_ptr) { }
iter_impl& operator=(const iter_impl &rhs) { d_ptr = rhs.d_ptr; return *this; }
iter_impl& operator+=(difference_type rhs) { d_ptr += rhs; return *this; }
iter_impl& operator-=(difference_type rhs) { d_ptr -= rhs; return *this; }
inner_type& operator*() const { return d_ptr->get(); }
inner_type* operator->() const { return &d_ptr->get(); }
inner_type& operator[](difference_type rhs) const { return d_ptr[rhs]; }
iter_impl& operator++() { ++d_ptr; return *this; }
iter_impl& operator--() { --d_ptr; return *this; }
iter_impl operator++(int) { iter_impl tmp(*this); ++d_ptr; return tmp; }
iter_impl operator--(int) { iter_impl tmp(*this); --d_ptr; return tmp; }
difference_type operator-(const iter_impl& rhs) const { return iter_impl(d_ptr - rhs.ptr); }
iter_impl operator+(difference_type rhs) const { return iter_impl(d_ptr + rhs); }
iter_impl operator-(difference_type rhs) const { return iter_impl(d_ptr - rhs); }
friend iter_impl operator+(difference_type lhs, const iter_impl& rhs) { return iter_impl(lhs + rhs.d_ptr); }
friend iter_impl operator-(difference_type lhs, const iter_impl& rhs) { return iter_impl(lhs - rhs.d_ptr); }
bool operator==(const iter_impl& rhs) const { return d_ptr == rhs.d_ptr; }
bool operator!=(const iter_impl& rhs) const { return d_ptr != rhs.d_ptr; }
bool operator>(const iter_impl& rhs) const { return d_ptr > rhs.d_ptr; }
bool operator<(const iter_impl& rhs) const { return d_ptr < rhs.d_ptr; }
bool operator>=(const iter_impl& rhs) const { return d_ptr >= rhs.d_ptr; }
bool operator<=(const iter_impl& rhs) const { return d_ptr <= rhs.d_ptr; }
};
} // close namespace detail
} // close namespace trnx
#endif

31
cpp/different_linkage.cpp Normal file
View File

@@ -0,0 +1,31 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
*/
#include <iostream>
#include <exception>
class Test {
public:
virtual int increment(int num) = 0;
static int increment(Test& t, int num) {return t.increment(num);}
};
class TestImpl : public Test
{
public:
virtual int increment(int num) {
std::cout << "Test " << num;
return num;
}
};
int main ( void )
{
TestImpl t;
Test::increment(t, 2);
return 0;
}

View File

@@ -0,0 +1,64 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-D__USE_XOPEN2K8 -O2 -pthread"
VIM-: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let g:cf5output=0
*/
#include <iostream>
#include <exception>
#include <stdexcept>
struct A
{
A()
{}
~A()
{}
virtual void fun()
{
std::cout << "I am a hidden function." << std::endl;
}
};
struct B : public A
{
B()
{}
~B()
{}
void fun()
{
std::cout << "I am the visible function." << std::endl;
}
};
int main ( void )
{try{
B o;
o.fun();
o.A::fun();
typedef A a_type;
o.a_type::fun();
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

55
cpp/p.cpp Normal file
View File

@@ -0,0 +1,55 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-O2 -pthread -D__USE_XOPEN2K8"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM: let g:cppflags=""
VIM: let g:ldflags=""
VIM: let g:ldlibpath=""
VIM: let g:argv=""
VIM-: let g:cf5output=0
*/
#include <iostream>
#include <exception>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
extern "C" {
#endif
int some_c_function_( int param1 );
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
}
#endif
int main ( void )
{try{
some_c_function_( 99 );
if (int i = 1)
std::cout << "lkkk" << i << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

34
cpp/shared_ptr.cpp Normal file
View File

@@ -0,0 +1,34 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <chrono>
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
//......
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

23
cpp/shared_ptr_cast.cpp Normal file
View File

@@ -0,0 +1,23 @@
/* Check cf5-opt.vim defs.
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
*/
#include <memory>
class A
{
int i;
};
class B : public A
{
int j;
};
int main()
{
std::shared_ptr<A> a = std::make_shared<B>();
std::shared_ptr<B> b = std::static_pointer_cast<B>(a);
}

View File

@@ -0,0 +1,77 @@
/* Check cf5-opt.vim defs.
-VIM-: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM: let g:argv=""
*/
#include <iostream>
#include <sstream>
#include <exception>
void test( const std::string& str_to_parse )
{
std::stringstream ss(str_to_parse);
std::string str;
int v;
ss >> str >> v;
std::cout << "str=\"" << str_to_parse << '"' <<std::endl
<< "good()=" << ss.good()
<< " fail()=" << ss.fail()
<< " bad()=" << ss.bad()
<< " eof()=" << ss.eof()
<< std::endl << std::endl;
}
void test2( const std::string& str_to_parse )
{
std::stringstream ss(str_to_parse);
std::string str;
std::string str2;
int v;
ss >> str >> v;
if ( ss.fail() && !ss.eof() )
{
ss.clear();
ss >> str2;
}
std::cout << "str=\"" << str_to_parse << '"' <<std::endl
<< "str2=\"" << str2 << '"' <<std::endl
<< "good()=" << ss.good()
<< " fail()=" << ss.fail()
<< " bad()=" << ss.bad()
<< " eof()=" << ss.eof()
<< std::endl << std::endl;
}
int main ( void )
{try{
test("string 1234");
test("string 1234 ");
test("string 1234 12");
test("string ");
test("string");
test("string aaaa");
test("string aaaa ");
test2("sss tttt");
test2("sss tttt ");
test2("sss 3456tttt");
test2("sss");
test2("sss ");
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,73 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:Ltbb
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <cstdio>
#include <tbb/flow_graph.h>
using namespace tbb::flow;
struct square {
int operator()(int v) { return v*v; }
};
struct cube {
int operator()(int v) { return v*v*v; }
};
class sum {
int &my_sum;
public:
sum( int &s ) : my_sum(s) {}
int operator()( tuple< int, int > v ) {
my_sum += get<0>(v) + get<1>(v);
return my_sum;
}
};
int main()
{try{
int result = 0;
graph g;
broadcast_node<int> input(g);
function_node<int,int> squarer( g, unlimited, square() );
function_node<int,int> cuber( g, unlimited, cube() );
join_node< tuple<int,int>, queueing > join( g );
function_node<tuple<int,int>,int>
summer( g, serial, sum(result) );
make_edge( input, squarer );
make_edge( input, cuber );
make_edge( squarer, get<0>( join.input_ports() ) );
make_edge( cuber, get<1>( join.input_ports() ) );
make_edge( join, summer );
for (int i = 1; i <= 10; ++i)
input.try_put(i);
g.wait_for_all();
printf("Final result is %d\n", result);
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,70 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:Ltbb
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <sstream>
#include <iostream>
#include <exception>
#include <memory>
#include "tbb/flow_graph.h"
using namespace tbb::flow;
typedef std::shared_ptr<std::string> msg_type;
struct body {
std::string my_name;
body( const char *name ) : my_name(name) {}
void operator()( msg_type msg ) const {
std::stringstream ss;
ss << my_name << " got " << *msg << std::endl;
std::cout << ss.str();
}
};
int main()
{try{
graph g;
broadcast_node<msg_type> start( g );
continue_node<msg_type> a( g, body("A"));
continue_node<msg_type> b( g, body("B"));
continue_node<msg_type> c( g, body("C"));
continue_node<msg_type> d( g, body("D"));
continue_node<msg_type> e( g, body("E"));
make_edge( start, a );
make_edge( start, b );
make_edge( a, c );
make_edge( b, c );
make_edge( c, d );
make_edge( a, e );
for (int i = 0; i < 3; ++i ) {
std::stringstream ss;
ss << i << std::endl;
start.try_put( std::make_shared( ss.str() ) );
g.wait_for_all();
}
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,97 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:LtbbD
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <cstdio>
#include <iostream>
#include <exception>
#include <thread>
#include <chrono>
#include <tbb/task.h>
/*
* Conclusion:
* The first task_sheduler_init is configuring number of threads in
* the thread pool. If the task scheduler initialised implicitly then
* the number of threads is the number of cores. To ensure uncomment
* the commented line below.
*/
class ChildTask : public tbb::task
{
tbb::task * execute()
{
std::printf( "ChildTask is running.\n" );
return nullptr;
}
};
class ContTask : public tbb::task
{
tbb::task * execute()
{
std::printf( "ContTask is running.\n" );
return nullptr;
}
};
tbb::task * t;
struct RootTask : public tbb::task
{
tbb::task * execute()
{
std::printf( "RootTask is running.\n" );
auto& c = *new( allocate_continuation()) ContTask();
t = &c;
tbb::task& ctask1 = *new( c.allocate_child() ) ChildTask();
tbb::task& ctask2 = *new( c.allocate_child() ) ChildTask();
c.set_ref_count( 3 );
spawn( ctask1 );
spawn( ctask2 );
std::printf( "RootTask is done.\n" );
return nullptr;
}
};
int main ( void )
{try{
auto& rtask = *new( tbb::task::allocate_root()) RootTask();
tbb::task::enqueue( rtask );
std::this_thread::sleep_for( std::chrono::seconds(1) );
std::printf( "After sleep.\n" );
tbb::task::spawn( *new (t->allocate_child()) tbb::empty_task() );
std::this_thread::sleep_for( std::chrono::seconds(1) );
std::printf( "After second sleep.\n" );
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,38 @@
/*
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let b:cppflags=g:Iboost.g:Itbb
VIM-: let b:ldflags=g:Lboost.g:Ltbb
VIM-: let b:ldlibpath=g:Bboost.g:Btbb
VIM-: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <chrono>
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
static const std::string str = "initial";
const_cast<std::string&>(str) = "test";
std::cout << str << std::endl;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}