Move all C++ tests to cpp.

This commit is contained in:
2014-05-31 22:55:50 +04:00
parent 1c1c6fe543
commit 7cdff553c8
52 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
#include <iostream>
struct a
{
int m;
a()
: m(0)
{}
//private:
a( const a & _ )
: m( _.m+1)
{}
};
struct b : public a
{
int n;
b()
: n(0)
{}
};
int main()
{
b o;
b o2 = o;
std::cout << o2.m <<' '<<o2.n<< std::endl;
return 0;
}

View File

@@ -0,0 +1,36 @@
#include <iostream>
#include <exception>
#include <string>
#include <chrono>
#include <thread>
#include <boost/chrono/process_cpu_clocks.hpp>
int main ( void )
{try{
boost::chrono::process_cpu_clock clock;
boost::chrono::process_cpu_clock::time_point b = clock.now();
std::this_thread::sleep_for( std::chrono::seconds( 2 ) );
boost::chrono::process_cpu_clock::time_point e = clock.now();
boost::chrono::process_cpu_clock::duration d = e - b;
auto times = d.count();
std::cout << times.real / 1e+9 << std::endl;
std::cout << times.user / 1e+9 << std::endl;
std::cout << times.system / 1e+9 << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
}}

View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <string>
#include <list>
#include <boost/array.hpp>
#include <boost/variant.hpp>
typedef boost::variant<int,long,double, const char*, _int64, std::string*, boost::array<int,128>*> tVarinat;
int main ()
{
tVarinat v;
std::list<int> l;
printf( "size %d\n", sizeof( tVarinat ) );
printf( "size %d\n", sizeof( std::string ) );
printf( "size %d\n", sizeof( l ) );
printf( "size %d\n", sizeof( boost::array<int,128> ) );
return 0;
}

View File

@@ -0,0 +1,68 @@
/* 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>
void prom( long long )
{
std::cout << "prom" << std::endl;
}
void fpprom( double )
{
std::cout << "fpprom" << std::endl;
}
void integral( short )
{
std::cout << "integral" << std::endl;
}
void conv_double( float )
{
std::cout << "conv_double" << std::endl;
}
void fpint( int )
{
std::cout << "fpint" << std::endl;
}
void fpint2( float )
{
std::cout << "fpint2" << std::endl;
}
int main ( void )
{try{
prom( short(10) );
fpprom( float(1.) );
integral( int(100000) );
conv_double( double(10ll) );
fpint( float(1.) );
fpint( double(2.) );
fpint2( int(2) );
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/call_constructor.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
class a
{
public:
a()
{
printf( "kuku\n" );
}
~a()
{
printf( "~a\n" );
}
int f()
{
return 0;
}
};
int main()
{
a o;
// o.a( 0 );
o.~a();
o.f();
reinterpret_cast<a*>(0)->a::a();
}

20
cpp/constStrType.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <stdio.h>
template <class T>
struct A
{
int f ( T t )
{
//T b = "valod";
printf( "%s %d\n", t, sizeof( t ) );
return sizeof( t );
}
};
int main ( void )
{
A<char*> o;
o.f( "abc" );
return 0;
}

View File

@@ -0,0 +1,71 @@
/* 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 <iomanip>
#include <exception>
#define HI_BIT_MASK( m, b ) (!((m)&unsigned(~0)<<b))
#define ENUM_SHIFT( m ) (32-(\
HI_BIT_MASK(m,0)+HI_BIT_MASK(m,1)+HI_BIT_MASK(m,2)+HI_BIT_MASK(m,3)\
+HI_BIT_MASK(m,4)+HI_BIT_MASK(m,5)+HI_BIT_MASK(m,6)+HI_BIT_MASK(m,7)\
+HI_BIT_MASK(m,8)+HI_BIT_MASK(m,9)+HI_BIT_MASK(m,10)+HI_BIT_MASK(m,11)\
+HI_BIT_MASK(m,12)+HI_BIT_MASK(m,13)+HI_BIT_MASK(m,14)+HI_BIT_MASK(m,15)\
+HI_BIT_MASK(m,16)+HI_BIT_MASK(m,17)+HI_BIT_MASK(m,18)+HI_BIT_MASK(m,19)\
+HI_BIT_MASK(m,20)+HI_BIT_MASK(m,21)+HI_BIT_MASK(m,22)+HI_BIT_MASK(m,23)\
+HI_BIT_MASK(m,24)+HI_BIT_MASK(m,25)+HI_BIT_MASK(m,26)+HI_BIT_MASK(m,27)\
+HI_BIT_MASK(m,28)+HI_BIT_MASK(m,29)+HI_BIT_MASK(m,30)+HI_BIT_MASK(m,31)))
#define ENUM_MASK(m) (~(~(unsigned long long)0<<ENUM_SHIFT(m)))
void test( int i )
{
char t[ENUM_SHIFT(2345)];
std::cout << std::setw(10) << i
<< " ==> mask:" << std::setw(10) << ENUM_MASK( i )
<< " shift: " << std::setw(10) << ENUM_SHIFT( i )
<< std::endl;
}
int main ( void )
{try{
std::cout << std::hex << std::showbase;
test( 0x00000000 );
test( 0x00000001 );
test( 0x00000010 );
test( 0x00000100 );
test( 0x00001000 );
test( 0x00010000 );
test( 0x00100000 );
test( 0x01000000 );
test( 0x10000000 );
test( 0x80000000 );
test( 0x80008000 );
test( 0x00100010 );
test( 0x000040a0 );
test( 0x000040a0 );
test( 0x00c04fa0 );
test( 0xffffffff );
test( 0xcccccccc );
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;
}}

19
cpp/dlload.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <dlfcn.h>
#include <stdio.h>
int main ()
{
// void* h1 = dlopen( "/home/vishap/p4/wb/main/stage-g/lib/amd64/libProteus.so", RTLD_LAZY|RTLD_GLOBAL);
void* h1 = dlopen( "/home/vishap/p4/wb/main/stage-g/lib/amd64/libProteus.so", RTLD_NOW|RTLD_GLOBAL);
if ( !h1 ) puts( dlerror() );
printf( "handle1 = 0x%x\n", h1 );
void* h2 = dlopen( "/home/vishap/p4/wb/main/src/icwb/test/libProteus.so", RTLD_LAZY|RTLD_GLOBAL);
if ( !h2 ) puts( dlerror() );
printf( "handle2 = 0x%x\n", h2 );
dlclose(h1);
dlclose(h2);
return 0;
}

46
cpp/dyn_cast.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include <stdio.h>
class A
{
public: virtual void* getThis()
{ return this; }
};
class B : virtual public A
{
public: virtual void* getThis()
{ return this; }
public: virtual void printB()
{ puts("B"); }
};
class C : virtual public A
{
public: virtual void* getThis()
{ return this; }
public: virtual void printC()
{ puts("C"); }
};
class D : public B,
public C
{
public: virtual void* getThis()
{ return this; }
};
int main ()
{
D o;
B* pB = reinterpret_cast<B*>( o.getThis() );
C* pC = reinterpret_cast<C*>( o.getThis() );
pB->printB();
pC->printC();
return 0;
}

View File

@@ -0,0 +1,54 @@
/* Check cf5-opt.vim defs.
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()
{
std::cout << "A::A()" << std::endl;
}
~A()
{
std::cout << "A::~A()" << std::endl;
}
};
struct B : public A
{
B()
{
std::cout << "B::B()" << std::endl;
}
~B()
{
throw std::runtime_error("test");
std::cout << "B::~B()" << std::endl;
}
};
int main ( void )
{try{
B o;
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;
}}

9
cpp/hello.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main ( void )
{
puts( "Hello GCC!!!" );
return 1;
}

10
cpp/long_long_size.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include <iostream>
int main ( void )
{
std::cout << "sizeof(long) = " << sizeof( long ) << std::endl;
std::cout << "sizeof(long long) = " << sizeof( long long ) << std::endl;
return 0;
}

332
cpp/memory_benchmark.cpp Normal file
View File

@@ -0,0 +1,332 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/Z7 /O2 /EHsc /DWIN32"
VIM: let g:cppflags=g:Iboost
VIM-: let g:wldflags=/DEBUG
VIM: let g:ldflags=g:Lboost
VIM: let g:ldlibpath=g:Bboost
VIM: let g:argv=""
VIM-: let g:cf5output=0
*/
#include <sstream>
#include <iostream>
#include <iomanip>
#include <exception>
#include <chrono>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#if 0
typedef long long duration_type;
static const size_t ce = 1024L*1024L*1024L;
duration_type scan_read( char * p, const size_t c, const size_t s )
{
auto b = std::chrono::high_resolution_clock::now();
int sum = 0;
char * qe = p+c;
for ( int n =0, ne=ce/c; n < ne; ++n )
for ( size_t i = 0; i < s; ++i )
for ( char * q = p+i; q < qe; q+=s )
sum += *q;
volatile int no_optimization = sum;
auto e = std::chrono::high_resolution_clock::now();
return std::chrono::nanoseconds(e-b).count();
}
duration_type scan_write( char * p, const size_t c, const size_t s )
{
int a = rand();
auto b = std::chrono::high_resolution_clock::now();
char * qe = p+c;
for ( int n =0, ne=ce/c; n < ne; ++n )
for ( size_t i = 0; i < s; ++i )
for ( char * q = p+i; q < qe; q+=s )
*q = a;
auto e = std::chrono::high_resolution_clock::now();
return std::chrono::nanoseconds(e-b).count();
}
template <class F>
void test_steps( F f , char * p, const size_t c, char * nm )
{
f( p, c, 1 ); // First run ignore.
std::cout << std::setw(10) << "steps";
std::cout << std::setw(16) << nm;
std::cout << std::setw(16) << nm;
std::cout << std::setw(16) << nm;
std::cout << std::setw(16) << "average";
std::cout << std::setw(16) << "deviation" << std::endl;
for ( size_t s = 1; s < c; s<<=1 )
{
std::cout << std::setw(10) << s << std::flush;
auto d1 = f( p, c, s );
std::cout << std::setw(16) << d1 << std::flush;
auto d2 = f( p, c, s );
std::cout << std::setw(16) << d2 << std::flush;
auto d3 = f( p, c, s );
std::cout << std::setw(16) << d3 << std::flush;
auto a = (d1+d2+d3)/3;
std::cout << std::setw(16) << a;
auto dev = (std::abs(d1-a)+std::abs(d2-a)+std::abs(d3-a))/3;
std::cout << std::setw(16) << dev << std::endl;
}
}
#endif
static const size_t GB = 1024L*1024L*1024L;
static const size_t MB = 1024L*1024L;
static const size_t L3CACHE = 20*MB;
class tests
{
public:
typedef long long duration_type;
struct elem_type
{
elem_type * next;
};
public:
size_t const workset;
size_t const cnt;
elem_type * const mem;
std::vector<char> cache_reset;
std::vector<duration_type> res;
public:
tests( size_t _ws = 4*GB )
: workset(_ws)
, cnt(workset/sizeof(elem_type))
, mem((elem_type*)malloc( workset ))
, cache_reset(L3CACHE)
{
}
~tests()
{
free(mem);
}
public:
void print_hdr( const char * title,
const char * param,
const char * action )
{
std::cout << std::endl << title << std::endl
<< std::setfill('-') << std::setw(90) << "" << std::endl
<< std::setfill(' ')
<< std::setw(10) << param
<< std::setw(14) << action << "#1"
<< std::setw(14) << action << "#2"
<< std::setw(14) << action << "#3"
<< std::setw(16) << "average"
<< std::setw(16) << "deviation" << std::endl;
}
void print_param( size_t param )
{
std::cout << std::setw(10) << param << std::flush;
}
void print_time( duration_type d )
{
std::cout << std::setw(16) << d << std::flush;
}
void print_avrg( duration_type avrg, duration_type dev )
{
std::cout << std::setw(16) << avrg;
std::cout << std::setw(16) << dev << std::endl;
}
void evict()
{
std::fill(cache_reset.begin(),cache_reset.end(),0);
}
//
// Make a cyclic list of length c and step s. In fact if we iterate
// through this list we read all elements of p with step s. Once we
// reach the end of the p we jump to the beginning and continue read
// the next path till the end. This continues until all elements of
// p are read. And then the whole is beginning again.
//
void make_cycle( elem_type * const p, const size_t c, const size_t s )
{
elem_type * const pe = p+c;
elem_type * h = p;
for ( size_t i = 0; i < s; ++i )
for ( elem_type * q = p+i; q < pe; q+=s )
h = h->next = q;
h->next = p;
}
//
// Print indices of list.
//
void test_print_make_cycle( elem_type * const p, const size_t c, const size_t s )
{
std::cout << "list lenght=" << c << " step=" << s << std::endl;
make_cycle( mem, c, s );
elem_type * h = p;
int l = 0;
while ( h->next != p )
{
std::cout << "[" << std::setw(2) << h->next - p << "] ";
if (!(++l%=15))
std::cout << std::endl;
h = h->next;
}
std::cout << "[" << std::setw(2) << h->next - p << "]"
<< std::endl << std::endl;
}
void test_make_cycle()
{
test_print_make_cycle( mem, 16, 1 );
test_print_make_cycle( mem, 16, 2 );
test_print_make_cycle( mem, 16, 4 );
test_print_make_cycle( mem, 32, 4 );
test_print_make_cycle( mem, 16, 8 );
test_print_make_cycle( mem, 16, 16 );
test_print_make_cycle( mem, 16, 17 );
test_print_make_cycle( mem, 1, 17 );
test_print_make_cycle( mem, 1, 1 );
}
//
// Calculate average of tests duration and the deviation.
//
std::pair<duration_type,duration_type>
average( const duration_type * test, const size_t count )
{
//
// Calc average duration.
//
duration_type avrg = 0;
for ( int i = 0; i < count; ++i )
avrg += test[i];
avrg /= count;
//
// Calc deviation from average duration.
//
duration_type dev = 0;
for ( int i = 0; i < count; ++i )
dev += std::abs( avrg - test[i] );
dev /= count;
//
return std::make_pair(avrg,dev);
}
#ifdef _MSC_VER
__declspec(noinline)
#endif
duration_type scan_read( elem_type const * const p, size_t cnt )
{
auto b = std::chrono::high_resolution_clock::now();
//
register elem_type const * h = p;
for ( size_t c = cnt; c; --c )
h = h->next;
//
// Prevents optimisation of the loop.
//
volatile static elem_type const * no_optimization = h;
//
auto e = std::chrono::high_resolution_clock::now();
return std::chrono::nanoseconds(e-b).count();
}
void benchmark_read_time(int s)
{
std::stringstream ss;
ss << "Memory continuous read. Step=" << s*sizeof(elem_type);
print_hdr( ss.str().c_str(), "wset", "read" );
const int trys = 3;
duration_type test[trys];
size_t c = 8;
for ( ; c < s; c <<=1 )
res.push_back( -1 );
for ( ; c <= cnt; c <<=1 )
{
print_param( c*sizeof(elem_type) );
make_cycle( mem, c, s );
for ( int t = 0; t < trys; ++t )
{
evict();
test[t] = scan_read( mem, cnt );
print_time( test[t] );
}
auto a = average( test, trys );
res.push_back( a.first );
print_avrg( a.first, a.second );
}
}
void benchmark_read_time()
{
size_t const se = cnt;
for ( size_t s = 1; s <= se; s <<=1 )
benchmark_read_time(s);
//
// Print header.
//
std::cout << std::endl << "Read time test." << std::endl
<< std::setfill('-') << std::setw(90) << "" << std::endl
<< std::setfill(' ')
<< std::setw(10) << "wset";
for ( size_t s = 1; s <= se; s <<=1 )
{
std::stringstream ss;
ss << "s" << s*sizeof(elem_type);
std::cout << std::setw(16) << ss.str();
}
std::cout << std::endl;
//
// Print results.
//
size_t const je = std::log(double(se))/std::log(2.)+1;
size_t const ie = res.size()/je;
for ( size_t i = 0, c = 8; i < ie; ++i, c <<=1 )
{
std::cout << std::setw(10) << c*sizeof(elem_type);
for ( size_t j = i; j < res.size(); j+=ie )
std::cout << std::setw(16) << res[j];
std::cout << std::endl;
}
}
};
int main ( void )
{try{
tests t;
//t.test_make_cycle();
t.benchmark_read_time();
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,66 @@
// toIntTest.cpp : Defines the entry point for the console application.
//
#include <omp.h>
#include <stdio.h>
#include "getCurrentTime.h"
#include "../fw/2007.12/src/base/point.h"
#if !defined(__AIX) && !defined(__sparc)
#define LOW_ENDIAN_ARCH
#endif
#ifdef _MSC_VER
#define MY_INLINE __forceinline
#else
#define MY_INLINE inline
#endif
#ifndef _MSC_VER
#define __int64 long
#endif
void test1()
{
int th_id, nthreads;
#pragma omp parallel private(th_id)
{
th_id = omp_get_thread_num();
printf("Hello World from thread %d\n", th_id);
#pragma omp barrier
if ( th_id == 0 )
{
nthreads = omp_get_num_threads();
printf("There are %d threads\n",nthreads);
}
}
}
void test2()
{
int th_id, nthreads;
#pragma omp parallel private(th_id)
{
th_id = omp_get_thread_num();
printf("Hello World from thread %d\n", th_id);
// #pragma omp barrier
if ( th_id == 0 )
{
nthreads = omp_get_num_threads();
printf("There are %d threads\n",nthreads);
}
}
}
int main(int argc, char* argv[])
{
// Init
initGetCurrentTimeLib();
// test1();
test2();
return 0;
}

View File

@@ -0,0 +1,37 @@
#include <iostream>
#include <exception>
struct A
{
int a;
int b;
A()
{
std::cout << "A::A()" << std::endl;
}
};
int main ( void )
{try{
A a[4];
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;
}}

17
cpp/shift_size.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include <iostream>
#include <iomanip>
int main ( )
{
std::cout << "shift amount: ";
int a;
std::cin >> a;
int b = 1;
int c = b << a;
std::cout << " 1 << " << a << " = " << std::hex << c << std::endl;
return 0;
}

View File

@@ -0,0 +1,35 @@
class a
{
public:
int i;
public:
virtual void f()
{
i = 0;
}
};
class b : virtual public a
{
public:
virtual void f()
{
i = 1;
}
};
int main()
{
b o;
a* pA = &o;
b* pB = static_cast<b*>(pA);
return 0;
}

725
cpp/std_vs_qt.cpp Normal file
View File

@@ -0,0 +1,725 @@
#include <stdio.h>
#include "getCurrentTime.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QString>
#include <list>
#include <memory>
#include <string>
#if defined( _MSC_VER )
#pragma intrinsic (memcpy,memcmp,memset,strcat,strcmp,strcpy,strlen)
#endif
namespace constructor
{
void * operator new( size_t count,
void * object )
{
return object;
}
}
//#define ALLOC_AT_ONCE
//#define USE_MALLOC
// TEMPLATE CLASS allocator
template<class T, int N>
class fast_allocator
#ifndef USE_MALLOC
: public std::allocator<T>
#endif
{
char memory[N];
#ifndef ALLOC_AT_ONCE
char* marker;
#endif
public:
typedef std::allocator<T> MyBase;
typedef T value_type;
typedef value_type *pointer;
typedef value_type & reference;
typedef const value_type *const_pointer;
typedef const value_type & const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template<class _Other>
struct rebind
{ // convert an allocator<_Ty> to an allocator <_Other>
typedef fast_allocator<_Other,N> other;
};
// return address of mutable _Val
pointer address(reference _Val) const
{
return (&_Val);
}
const_pointer address(const_reference _Val) const
{ // return address of nonmutable _Val
return (&_Val);
}
fast_allocator() throw()
#ifndef ALLOC_AT_ONCE
: marker( memory )
#endif
{}
// construct by copying (do nothing)
fast_allocator( const fast_allocator<T,N>& ) throw()
#ifndef ALLOC_AT_ONCE
: marker( memory )
#endif
{}
// construct from a related allocator (do nothing)
// template<class _Other>
// allocatorN(const std::allocator<_Other>&) throw()
// : marker( memory )
// {}
// deallocate object at _Ptr, ignore size
void deallocate(pointer p, size_type n )
{
#ifdef ALLOC_AT_ONCE
if ( p != static_cast<pointer>(memory) )
#ifdef USE_MALLOC
free( p );
#else
return MyBase::deallocate( p, n );
#endif
#else/*ALLOC_AT_ONCE*/
if ( p+n == marker )
marker = p;
else if ( p < static_cast<pointer>(memory)
|| static_cast<pointer>(&memory[N]) <= p )
#ifdef USE_MALLOC
free( p );
#else
return MyBase::deallocate( p, n );
#endif
#endif
}
// allocate array of n elements
pointer allocate(size_type nCount)
{
register size_type s = sizeof(T)*nCount;
#ifdef ALLOC_AT_ONCE
if ( sizeof(T)*nCount < N )
return static_cast<pointer>(memory);
#else
if ( sizeof(T)*nCount < (&memory[N] - marker) )
{
pointer p = static_cast<pointer>(marker);
marker += s;
return p;
}
#endif
else
#ifdef USE_MALLOC
return static_cast<char*>(malloc( s ));
#else
return MyBase::allocate( nCount );
#endif
}
// allocate array of n elements, ignore hint
pointer allocate(size_type nCount, const void *)
{
return allocate(nCount);
}
// construct object at _Ptr with value _Val
void construct(pointer _Ptr, const T& _Val)
{
::new (_Ptr) T(_Val);
}
// destroy object at _Ptr
void destroy( pointer p )
{
p->~T();
}
// estimate maximum array size
size_type max_size() const throw()
{
#ifdef USE_MALLOC
return (size_type)(-1) / sizeof(T);
#else
// return MyBase::max_size();
return (int)(-1) / sizeof(T);
#endif
}
// bool operator( const fast_allocator& op )
// {
// return &op == this;
// }
};
template <class T, int N>
inline
bool operator== ( const fast_allocator<T,N>& op1,
const fast_allocator<T,N>& op2 )
{
return &op1 == &op2;
}
// STRUCT char_traits<char> (FROM <string>)
template<class C>
struct fast_char_traits
{ // properties of a string or stream char element
typedef C char_type;
typedef int int_type;
typedef std::streampos pos_type;
typedef std::streamoff off_type;
// typedef _Mbstatet state_type;
// assign an element
static void assign( char_type& l, const char_type& r)
{
l = r;
}
// test for element equality
static bool eq( const char_type& l, const char_type& r)
{
return (l == r);
}
// test if l precedes r
static bool lt( const char_type& l, const char_type& r)
{
return (l < r);
}
// compare [_First1, _First1 + n) with [_First2, ...)
static int compare(const char_type *op1, const char_type *op2, size_t n)
{
return (::memcmp(op1, op2, n*sizeof(char_type)));
}
// find length of null-terminated string
static size_t length(const char_type * op)
{
return (::strlen(op));
}
// assume there is enough space in the destination buffer
static char_type * copy(char_type *dest, const char_type *src, size_t n)
{
return reinterpret_cast<char_type*>(::memcpy( dest, src, n*sizeof(char_type) ));
}
// look for _Ch in [_First, _First + n)
static const char_type * find(const char_type * src, size_t n, const char_type& ch)
{
return ((const char_type *)::memchr(src, ch, n));
}
// move [_First1, _First1 + n) to [_First2, ...)
// assume there is enough space in the destination buffer
static char_type * move(char_type *dest, const char_type *src, size_t n)
{
return reinterpret_cast<char_type*>(::memmove(dest, src, n*sizeof(char_type)));
}
// assign n * _Ch to [_First, ...)
static char_type * assign(char_type *_First, size_t n, char_type _Ch)
{
return ((char_type *)::memset(_First, _Ch, n));
}
// convert metacharacter to character
static char_type to_char_type(const int_type& chMeta)
{
return ((char_type)chMeta);
}
// convert character to metacharacter
static int_type to_int_type(const char_type& ch)
{
return ((unsigned char)ch);
}
// test for metacharacter equality
static bool eq_int_type(const int_type& l, const int_type& r)
{
return (l == r);
}
// return end-of-file metacharacter
static int_type eof()
{
return (EOF);
}
// return anything but EOF
static int_type not_eof(const int_type& chMeta)
{
return (chMeta != eof() ? chMeta : !eof());
}
};
template <
class CharType,
class Traits=fast_char_traits<CharType>,
class Allocator=fast_allocator<CharType,64>
>
class fast_basic_string : public std::basic_string< CharType, Traits, Allocator >
{
public:
typedef fast_basic_string< CharType, Traits, Allocator > MySelf;
typedef std::basic_string< CharType, Traits, Allocator > MyBase;
fast_basic_string(
const typename MyBase::value_type* _Ptr,
typename MyBase::size_type _Count,
const typename MyBase::allocator_type& _Al = Allocator ( )
)
: std::basic_string< CharType, Traits, Allocator >( _Ptr, _Count, _Al )
{}
fast_basic_string(
const typename MyBase::value_type* _Ptr,
const typename MyBase::allocator_type& _Al = Allocator ( )
)
: std::basic_string< CharType, Traits, Allocator >( _Ptr, _Al )
{}
fast_basic_string(
const MyBase& _Right,
typename MyBase::size_type _Roff = 0,
typename MyBase::size_type _Count = npos,
const typename MyBase::allocator_type& _Al = Allocator ( )
)
: std::basic_string< CharType, Traits, Allocator >( _Right, _Roff, _Count, _Al )
{}
fast_basic_string(
typename MyBase::size_type _Count,
typename MyBase::value_type _Ch,
const typename MyBase::allocator_type& _Al = Allocator ( )
)
: std::basic_string< CharType, Traits, Allocator >( _Count, _Ch, _Al )
{}
explicit fast_basic_string(
const typename MyBase::allocator_type& _Al = Allocator ( )
)
: std::basic_string< CharType, Traits, Allocator >( _Al )
{}
#if 0 //ndef _MSC_VER
bool operator == ( const MySelf& str )
{
return ( str.size() != size() ) ? false : MyBase::operator == ( str );
}
#endif
};
#if 1 //def _MSC_VER
template <class Elem, class Traits, class Alloc>
inline
bool operator==(
const fast_basic_string<Elem, Traits, Alloc>& l,
const fast_basic_string<Elem, Traits, Alloc>& r )
{ // test for string equality
return (l.size() != r.size()) ?
false : (l.compare(r) == 0);
}
#endif
typedef fast_basic_string< char > fast_string;
const char* testStr[] =
{
"1", //1
"12", //2
"123", //3
"1234", //4
"12345", //5
"123456", //6
"1234567", //7
"12345678", //8
"123456781", //9
"1234567812", //10
"12345678123", //11
"123456781234", //12
"1234567812345", //13
"12345678123456", //14
"123456781234567", //15
"1234567812345678", //16
"12345678123456781", //17
"123456781234567812", //18
"1234567812345678123", //19
"12345678123456781234567812345678", //32
"12345678123456781234567812345678123456781234567812345678123456", //62
"123456781234567812345678123456781234567812345678123456781234567", //63
"1234567812345678123456781234567812345678123456781234567812345678", //64
"12345678123456781234567812345678123456781234567812345678123456781",
"123456781234567812345678123456781234567812345678123456781234567812",
"1234567812345678123456781234567812345678123456781234567812345678123",
"12345678123456781234567812345678123456781234567812345678123456781234",
"123456781234567812345678123456781234567812345678123456781234567812345678",
"12345678123456781234567812345678123456781234567812345678123456781234567812345678",
"123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678",
"12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678",
"12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"
"12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678",
};
template <class S, class S2>
inline
int initString( const S2& str )
{
S a( str );
return 0;
}
template <class S, class S2>
inline
int assignString( const S2& str )
{
S a;
a = str;
return 0;
}
template <class S>
inline
int cmpEqString( const S& str1, const S& str2 )
{
return str1 == str2;
}
template <class S>
inline
int cmpLessString( const S& str1, const S& str2 )
{
return str1 < str2;
}
/*
std::list<QString*> listQString;
int alocQString(char const * const pStr)
{
QString* a = new QString(pStr);
listQString.push_back( a );
return 0;
}
int freeQString()
{
std::list<QString*>::iterator it = listQString.begin();
std::list<QString*>::iterator itEnd = listQString.end();
for ( ; it!=itEnd; ++it )
delete *it;
listQString.clear();
return 0;
}
std::list<std::string*> listStdString;
int alocStdString(char const * const pStr)
{
std::string* a = new std::string(pStr);
listStdString.push_back( a );
return 0;
}
int freeStdString()
{
std::list<std::string*>::iterator it = listStdString.begin();
std::list<std::string*>::iterator itEnd = listStdString.end();
for ( ; it!=itEnd; ++it )
delete *it;
listStdString.clear();
return 0;
}
*/
template< class S1,
class S2,
int (*func1)( const S1& ),
int (*func2)( const S2& ),
int nProbes >
void testStringsUnary( char const * const pStr )
{
double dwStart, dwEnd;
S1 s1_op( pStr );
S2 s2_op( pStr );
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = func1(s1_op);
dwEnd = getCurrentTime();
double dblDiff1 = dwEnd - dwStart;
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = func2(s2_op);
dwEnd = getCurrentTime();
double dblDiff2 = dwEnd - dwStart;
printf( "m1 %f m2 %f m1/m2 %f probs=%d\n", dblDiff1, dblDiff2, dblDiff1/dblDiff2, nProbes );
}
template< class S1,
class S2,
int (*func1)(const S1&, const S1& ),
int (*func2)(const S2&, const S2& ),
int nProbes >
void testStringsBinary( char const * const pStr1, char const * const pStr2 )
{
double dwStart, dwEnd;
S1 s1_op1( pStr1 );
S1 s1_op2( pStr2 );
S2 s2_op1( pStr1 );
S2 s2_op2( pStr2 );
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = func1(s1_op1,s1_op2);
dwEnd = getCurrentTime();
double dblDiff1 = dwEnd - dwStart;
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = func2(s2_op1,s2_op2);
dwEnd = getCurrentTime();
double dblDiff2 = dwEnd - dwStart;
printf( "m1 %f m2 %f m1/m2 %f probs=%d\n", dblDiff1, dblDiff2, dblDiff1/dblDiff2, nProbes );
}
/*
void testStrings( char const * const pStr )
{
double dwStart, dwEnd;
int nProbes = 5000000;
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = alocStdString(pStr);
dwEnd = getCurrentTime();
printf( "alocStdString \t%f\t\t", dwEnd - dwStart );
dwStart = getCurrentTime();
{
volatile int a = freeStdString();
}
dwEnd = getCurrentTime();
printf( "freeStdString \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = alocQString(pStr);
dwEnd = getCurrentTime();
printf( "alocQString \t%f\t\t", dwEnd - dwStart );
dwStart = getCurrentTime();
{
volatile int a = freeQString();
}
dwEnd = getCurrentTime();
printf( "freeQString \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = alocStdString(pStr);
dwEnd = getCurrentTime();
printf( "alocStdString \t%f\t\t", dwEnd - dwStart );
dwStart = getCurrentTime();
{
volatile int a = freeStdString();
}
dwEnd = getCurrentTime();
printf( "freeStdString \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = nProbes; i; --i )
volatile int a = alocQString(pStr);
dwEnd = getCurrentTime();
printf( "alocQString \t%f\t\t", dwEnd - dwStart );
dwStart = getCurrentTime();
{
volatile int a = freeQString();
}
dwEnd = getCurrentTime();
printf( "freeQString \t%f\n", dwEnd - dwStart );
}
*/
template <class S1, class S2>
void test()
{
#if 1
/*
*/
puts( "constructor(char*) \n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]); ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsUnary<char const * const,
char const * const,
initString<S1, char const * const>,
initString<S2, char const * const>,
1000000>( testStr[i] );
}
#endif
#if 1
/*
*/
puts( "constructor(native) \n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]); ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsUnary<S1,
S2,
initString<S1, S1>,
initString<S2, S2>,
1000000>( testStr[i] );
}
#endif
#if 1
/*
*/
puts( "operator = (char*) \n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]); ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsUnary<char const * const,
char const * const,
assignString<S1, char const * const>,
assignString<S2, char const * const>,
1000000>( testStr[i] );
}
#endif
#if 1
/*
*/
puts( "operator = (native) \n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]); ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsUnary<S1,
S2,
assignString<S1, S1>,
assignString<S2, S2>,
1000000>( testStr[i] );
}
#endif
#if 1
/*
*/
puts( "operator == () (equal strings)\n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]) -1; ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsBinary< S1,
S2,
cmpEqString<S1>,
cmpEqString<S2>,
1000000>( testStr[i], testStr[i] );
}
#endif
#if 1
/*
*/
puts( "operator == () (never equal strings)\n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]) -1; ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsBinary< S1,
S2,
cmpEqString<S1>,
cmpEqString<S2>,
1000000>( testStr[i], testStr[i+1] );
}
#endif
#if 1
/*
*/
puts( "operator < (less strings)\n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]) -1; ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsBinary< S1,
S2,
cmpLessString<S1>,
cmpLessString<S2>,
1000000>( testStr[i], testStr[i+1] );
}
#endif
#if 1
/*
*/
puts( "operator < () (great strings)\n"
"============================================================");
for ( int i = 0; i < sizeof(testStr)/sizeof(testStr[0]) -1; ++i )
{
printf("strlen: %3d ", strlen(testStr[i]), testStr[i] );
testStringsBinary< S1,
S2,
cmpLessString<S1>,
cmpLessString<S2>,
1000000>( testStr[i+1], testStr[i] );
}
#endif
}
int main(int argc, char *argv[])
{
// Init
initGetCurrentTimeLib();
double dwStart, dwEnd;
int nProbes;
QCoreApplication a(argc, argv);
#if 1
puts( "************************************************************\n"
"************************************************************\n"
"Testing m1=QString vs m2=std::string \n"
"************************************************************");
test<QString,std::string>();
#endif
#if 0
puts( "************************************************************\n"
"************************************************************\n"
"Testing m1=QString vs m2=fast_string \n"
"************************************************************");
test<QString,fast_string>();
#endif
// return a.exec();
return 0;
}

47
cpp/stl_vector_resize.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include <vector>
#include <iostream>
int main( void )
{
std::vector<int> v;
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.push_back( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.resize( 1 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.resize( 0 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.resize( 33 );
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
v.clear();
std::cout << "capacity " << v.capacity() << "\t\tsize " << v.size() << std::endl;
return 0;
}

10
cpp/string_size.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include <iostream>
const char str [] ="This is a [] string.";
int main ( void )
{
std::cout << sizeof(str) << std::endl;
return 1;
}

View File

@@ -0,0 +1,54 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:cppflags=g:Iboost.g:Itbb
VIM: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc
VIM: let g:ldlibpath=g:Bboost.g:Btbb
*/
#include <tbb/enumerable_thread_specific.h>
#include <tbb/task_scheduler_init.h>
#include <tbb/parallel_for_each.h>
#include <iostream>
#include <exception>
#include <vector>
/*
* 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.
*/
int main ( void )
{try{
std::vector<int> v(1024*1024);
//tbb::enumerable_thread_specific<int> count;
tbb::task_scheduler_init init3(5);
tbb::task_scheduler_init init(10);
tbb::task_scheduler_init init2(1);
tbb::enumerable_thread_specific<int> count;
tbb::parallel_for_each( v.begin(), v.end(), [&count](int& v){
++count.local();
});
int i = 0;
for ( int c : count )
{
std::cout << "Thread: " << ++i << " counted " << c << 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;
}}

View File

@@ -0,0 +1,119 @@
/* 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>
template <int a>
struct A
{
A()
{
std::cout << "A<" << a << ">" << std::endl;
}
};
template <>
struct A<5>
{
A()
{
std::cout << "special A<5>" << std::endl;
}
};
template <class b, int a>
struct B
{
B()
{
std::cout << "B<" << a << ">" << std::endl;
}
};
template <class b>
struct B<b,5>
{
B()
{
std::cout << "special B<5>" << std::endl;
}
};
template <int a>
void f()
{
std::cout << "f<" << a << ">()" << std::endl;
}
template <>
void f<5>()
{
std::cout << "special f<5>()" << std::endl;
}
template <class b, int a>
void g()
{
std::cout << "g<" << a << ">()" << std::endl;
}
/*
* no partial specialisation for functions.
template <class b>
void g<b,5>()
{
std::cout << "special g<5>()" << std::endl;
}
*/
template <>
void g<void,5>()
{
std::cout << "special g<void,5>()" << std::endl;
}
int main ( void )
{try{
A<1> a1;
A<2> a2;
A<5> a5;
A<6> a6;
B<void,1> b1;
B<void,2> b2;
B<void,5> b5;
B<void,6> b6;
f<1>();
f<2>();
f<5>();
f<6>();
g<void,1>();
g<void,2>();
g<void,5>();
g<int,5>();
g<void,6>();
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;
}}

41
cpp/temporary_objects.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include <iostream>
class a
{
public:
a()
{
std::cout << "construction" << std::endl;
}
~a()
{
std::cout << "destruction" << std::endl;
}
};
class guard
{
public:
a _a;
a* operator & ()
{
return &_a;
}
};
guard f()
{
return guard();
}
void g ( a* p )
{
std::cout << "working with a" << std::endl;
}
int main()
{
g( &f() );
return 0;
}

45
cpp/test.cpp Normal file
View File

@@ -0,0 +1,45 @@
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#if 0
template <class C>
class aaa
{
public: virtual C getC() = 0;
};
template <class C>
class ccc : public aaa<C>
{
public: virtual C getC(){ return 0; }
};
template class ccc<int>;
template class ccc<double>;
template class ccc<long long>;
class bbb : public ccc<int>,
public ccc<double>,
public ccc<long long>
{
public:
// using ccc<int>::getC;
virtual int ccc<int>::getC() { return 1; }
virtual long long ccc<long long>::getC() { return 1; }
virtual double ccc<double>::getC() { return 1; }
};
#endif
int _tmain(int argc, _TCHAR* argv[])
{
// bbb o;
// int i = static_cast<aaa<int>*>(&o)->getC();
// double d = static_cast<aaa<double>*>(&o)->getC();
// long long ll = static_cast<aaa<long long>*>(&o)->getC();
std::cout << sizeof( long long ) << std::endl;
return 0;
}

19
cpp/testLong64.cpp Normal file
View File

@@ -0,0 +1,19 @@
// testLong64.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream>
//#define long __int64
int main(int argc, char * argv[])
{
std::cout << "short " << sizeof( short ) << std::endl;
std::cout << "int " << sizeof( int ) << std::endl;
std::cout << "long " << sizeof( long ) << std::endl;
std::cout << "long long " << sizeof( long long ) << std::endl;
std::cout << "void* " << sizeof( void* ) << std::endl;
return 0;
}

132
cpp/testStaticCast.cpp Normal file
View File

@@ -0,0 +1,132 @@
#include <iostream>
template <class C> const char* tn( C ) { return "unknown"; }
template <> const char* tn( int ) { return "int"; }
template <> const char* tn( long ) { return "long"; }
template <class C>
class a
{
public:
virtual void f( C n )
{
std::cout << "a<" << tn( n ) << ">::f(" << tn( n ) << ")" << std::endl;
}
a<C>* getA( C )
{
return this;
}
};
template <class C>
class b : virtual public a<C>
{
public:
virtual void f( C n )
{
a<C>::f( n );
std::cout << "b<" << tn( n ) << ">::(" << tn( n )<< ")" << std::endl;
}
};
class c : public b<int>, public b<long>
{
public:
virtual void f( int n )
{ return f_impl( n ); }
virtual void f( long n )
{ return f_impl( n ); }
template <class D>
void f_impl( D n )
{
b<D>::f( n );
std::cout << "c::f(" << tn( n ) << ")" << std::endl;
}
using a<int>::getA;
using a<long>::getA;
/*
virtual a<int>* getA( int n )
{
return b<int>::getA( n );
}
virtual a<long>* getA( long n )
{
return b<long>::getA( n );
}
*/
};
class d : public c
{
public:
virtual void f( int n )
{ return f_impl( n ); }
virtual void f( long n )
{ return f_impl( n ); }
template <class D>
void f_impl( D n )
{
c::f( n );
std::cout << "d::f(" << tn( n )<< ")" << std::endl;
}
};
void test_cast( c* p )
{
b<int>* bi = p;
b<long>* bl = p;
a<int>* ai = p;
a<long>* al = p;
a<int>* bai = bi;
a<long>* bal = bl;
a<int>* gai = p->getA( int(0) );
a<long>* gal = p->getA( long(0) );
a<int>* dai = dynamic_cast<a<int>*>(p);
a<long>* dal = dynamic_cast<a<long>*>(p);
b<int>* dbi = dynamic_cast<b<int>*>(p);
b<long>* dbl = dynamic_cast<b<long>*>(p);
a<int>* sai = static_cast<a<int>*>(p);
a<long>* sal = static_cast<a<long>*>(p);
b<int>* sbi = static_cast<b<int>*>(p);
b<long>* sbl = static_cast<b<long>*>(p);
p->f( long(0) );
std::cout << "=================>calling p->f( long )" << std::endl;
al->f( long(0) );
std::cout << "=================>calling al->f( long )" << std::endl;
std::cout << "dai=" << dai << " dal=" << dal << std::endl;
std::cout << "sai=" << sai << " sal=" << sal << std::endl;
std::cout << "bai=" << bai << " bal=" << bal << std::endl;
std::cout << "gai=" << gai << " gal=" << gal << std::endl;
std::cout << " ai=" << ai << " al=" << al << std::endl;
std::cout << "dbi=" << dbi << " dbl=" << dbl << std::endl;
std::cout << "sbi=" << sbi << " sbl=" << sbl << std::endl;
std::cout << " bi=" << bi << " bl=" << bl << std::endl;
std::cout << " p=" << p << std::endl;
}
int main ( void )
{
d d_o;
c c_o;
test_cast( &c_o );
std::cout << "&c_o=" << &c_o << std::endl;
std::cout << std::endl
<< "------------------------------------------" << std::endl;
test_cast( &d_o );
std::cout << "&d_o=" << &d_o << std::endl;
return 0;
}

220
cpp/testX11.cpp Normal file
View File

@@ -0,0 +1,220 @@
//
// To compile use:
// g++ -L/usr/X11R6/lib64 -pthread -lX11 testX11.cpp
//
/* first include the standard headers that we're likely to need */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <pthread.h>
class threadDrawing
{
public:
Display* d;
Drawable w;
Pixmap p;
GC gc;
pthread_t t;
threadDrawing()
:
d( 0 ),
w( 0 ),
p( 0 ),
gc( 0 )
{
}
~threadDrawing()
{
deinit();
}
void init( const char * dname )
{
d = XOpenDisplay( dname );
int screen_num = XDefaultScreen(d);
unsigned long bg = XBlackPixel(d, screen_num);
unsigned long fg = XWhitePixel(d, screen_num);
w = XCreateSimpleWindow(d, XDefaultRootWindow(d),
0, 0, 1, 1,
0, fg, bg );
p = XCreatePixmap( d, w, 50, 50,
XDefaultDepth( d, XDefaultScreen( d ) ) );
gc = XCreateGC( d, w, 0, 0 );
XSetForeground( d, gc, fg );
XSetBackground( d, gc, bg );
}
void deinit()
{
if ( gc )
{
XFreeGC( d, gc );
gc = 0;
}
if ( p )
{
XFreePixmap( d, p );
p = 0;
}
if ( w )
{
XDestroyWindow( d, w );
w = 0;
}
if ( d )
{
XCloseDisplay( d );
d = 0;
}
}
static void* entry_point( void * pData)
{
threadDrawing* pThis = reinterpret_cast<threadDrawing*>(pData);
pThis->run();
return 0;
}
void start()
{
int rc = pthread_create( &t, 0, &entry_point, (void*)this );
std::cout << "Thread " << t << " created." << std::endl ;
}
void join()
{
void * s;
pthread_join( t, &s );
std::cout << "Thread " << t << " joined." << std::endl ;
}
void run()
{
// p = XCreatePixmap( d, w, 50, 50,
// XDefaultDepth( d, XDefaultScreen( d ) ) );
// GC gc = XCreateGC( d, w, 0, 0 );
XFillRectangle( d, p, gc, 0, 0, 50, 50 );
for (; true ; )
{
XDrawLine( d, p, gc, 0, 0, 50, 50 );
XDrawLine( d, p, gc, 0, 50, 50, 0 );
}
XFlush( d );
XSync( d, True );
}
};
void createThreads( Display* d, Drawable w )
{
const int tsize = 16;
typedef std::vector<threadDrawing> threads_type;
threads_type threads( tsize );
const char * dname = XDisplayString( d );
threads_type::iterator it = threads.begin();
threads_type::iterator itEnd = threads.end();
for ( ; it != itEnd; ++it )
{
it->init( dname );
it->start();
}
for ( it = threads.begin(); it != itEnd; ++it )
{
it->join();
}
}
int error_handler( Display* d, XErrorEvent* err )
{
puts( "error." );
}
int io_error_handler( Display* d )
{
puts( "io error." );
}
int main(int argc, char ** argv)
{
int screen_num, width, height;
unsigned long background, border;
Window win;
XEvent ev;
Display *dpy;
XSetErrorHandler( error_handler );
XSetIOErrorHandler( io_error_handler );
/* First connect to the display server, as specified in the DISPLAY
environment variable. */
dpy = XOpenDisplay(NULL);
if (!dpy)
{
fprintf(stderr, "unable to connect to display");
return 7;
}
/* these are macros that pull useful data out of the display object */
/* we use these bits of info enough to want them in their own variables */
screen_num = DefaultScreen(dpy);
background = BlackPixel(dpy, screen_num);
border = WhitePixel(dpy, screen_num);
width = 400; /* start with a small window */
height = 400;
win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), /* display, parent */
0,0, /* x, y: the window manager will place the window elsewhere */
width, height, /* width, height */
2, border, /* border width & colour, unless you have a window manager */
background); /* background colour */
/* tell the display server what kind of events we would like to see */
XSelectInput(dpy, win, ButtonPressMask|StructureNotifyMask );
/* okay, put the window on the screen, please */
XMapWindow(dpy, win);
//
//
//
createThreads( dpy, win );
/* as each event that we asked about occurs, we respond. In this
* case we note if the window's shape changed, and exit if a button
* is pressed inside the window */
while(1)
{
XNextEvent(dpy, &ev);
switch(ev.type)
{
case ConfigureNotify:
if (width != ev.xconfigure.width
|| height != ev.xconfigure.height)
{
width = ev.xconfigure.width;
height = ev.xconfigure.height;
printf("Size changed to: %d by %d\n", width, height);
}
break;
case ButtonPress:
XCloseDisplay(dpy);
return 0;
}
}
}

26
cpp/test_array_init.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <iostream>
class a
{
public:
void* array[16];
int a2[16];
a()
: array()
, a2()
{
}
};
int main( )
{
a o;
for ( int i = 0; i < 16; ++i )
std::cout << o.array[i] << std::endl;
for ( int i = 0; i < 16; ++i )
std::cout << o.a2[i] << std::endl;
return 1;
}

59
cpp/test_autoname_obj.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include <iostream>
#include <sstream>
#include <string>
#define FILELINE( l ) __FILE__ #l
struct fileline
{
virtual std::string msg()
{
return std::string();
}
};
template <const char * file, int line>
struct filelineT
{
virtual std::string msg()
{
std::ostringstream ss;
ss << file << ':' << line;
return ss.str();
}
};
std::string flfunc( const char * f = __FILE__, int l = __LINE__ )
{
std::ostringstream ss;
ss << f << ':' << l;
return ss.str();
}
#define FLFUNC flfunc( __FILE__, __LINE__ )
//#define filelineM {static const char * tmp = __FILE__; filelineT<tmp,__LINE__>}
//template <
struct A
{
std::string myName;
A( std::string n = FLFUNC )
: myName( n )
{}
};
int main ()
{
A a1( FLFUNC );
A a2( FLFUNC );
std::cout << a1.myName << std::endl;
std::cout << a2.myName << std::endl;
return 0;
}

52
cpp/test_const_arg.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <iostream>
void f ( int& i )
{
std::cout << "int" << std::endl;
}
void f ( const int& i )
{
std::cout << "const int" << std::endl;
}
#if 0
void f ( volatile int& i )
{
std::cout << "volatile int" << std::endl;
}
void f ( const volatile int& i )
{
std::cout << "const volatile int" << std::endl;
}
#endif
int g ( )
{
int i = 2;
return i;
}
int main( void )
{
#if 0
const volatile int cvi = 1;
f( cvi );
volatile int vi = 1;
f( vi );
#endif
const int ci = 1;
f( ci );
int i = 1;
f( i );
f( g() );
return 0;
}

View File

@@ -0,0 +1,84 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
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:boostld.g:tbbld
VIM-: let g:cf5output=0
*/
/*
* Conclusion:
* Per C++ standard since Son doesn't have explicitely defined copy
* assignement operator the an implicite one is generated which call
* Base::operatro = () as a result overriden operator in the Son is not
* called.
*/
#include <iostream>
#include <exception>
class Base
{
public:
int b;
virtual Base &operator=(const Base & o)
{ std::cout << "Base" << std::endl; b=o.b; return *this; };
};
class Derived : public Base
{
public:
int d;
virtual Base &operator=(const Base &)
{ std::cout << "Derived 1" << std::endl; return *this;};
virtual Derived &operator=(const Derived &)
{ std::cout << "Derived 2" << std::endl; return *this;};
};
class Son : public Base
{
public:
int d;
virtual Base &operator=(const Base &)
{ std::cout << "Son" << std::endl; return *this;};
};
int main ( void )
{try{
Derived d1, d2;
d1.b = 0;
d2.b = 1;
d1.d = 0;
d2.d = 1;
Base * b1 = &d1;
Base * b2 = &d2;
*b1 = *b2;
std::cout << d1.b << d1.d << std::endl;
d1 = d2;
Son s1, s2;
s1 = s2;
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;
}}

169
cpp/test_copy_elision.cpp Normal file
View File

@@ -0,0 +1,169 @@
#include <iostream>
#include <exception>
#include <string>
#include <algorithm>
struct copy_tracker
{
int v;
copy_tracker()
: v(0)
{
std::cout << "copy_tracker::copy_tracker()" << std::endl;
}
copy_tracker( const copy_tracker& c )
: v( c.v )
{
std::cout << "copy_tracker::copy_tracker( const copy_tracker& c )" << std::endl;
}
copy_tracker( copy_tracker&& c )
: v( c.v )
{
std::cout << "copy_tracker::copy_tracker( copy_tracker&& c ) MOVE " << std::endl;
}
void use_object()
{
std::cout << v << std::endl;
}
};
template <int n>
copy_tracker return_by_value()
{
return return_by_value<n-1>();
}
template <>
copy_tracker return_by_value<1>()
{
return copy_tracker();
}
template <int n>
copy_tracker return_by_value2()
{
copy_tracker o = return_by_value2<n-1>();
o.v = n;
return o;
}
template <>
copy_tracker return_by_value2<1>()
{
copy_tracker o; //return value optimization still applies.
o.v = 1;
return o;
}
template <int n>
copy_tracker static_by_value()
{
return static_by_value<n-1>();
}
template <>
copy_tracker static_by_value<1>()
{
static copy_tracker o; //return value optimization still applies.
return o;
}
template <int n>
copy_tracker move_by_value()
{
return std::move(move_by_value<n-1>());
}
template <>
copy_tracker move_by_value<1>()
{
return copy_tracker(); //return value optimization still applies.
}
template <int n>
void pass_by_value(copy_tracker o)
{
pass_by_value<n-1>(std::move(o));
// o.v = n;
// o.use_object();
}
template <>
void pass_by_value<1>(copy_tracker o)
{
o.use_object();
}
void test_copy_tracker()
{
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Return by value and assign." << std::endl;
{ copy_tracker o = return_by_value<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Return by named value and assign." << std::endl;
{ copy_tracker o = return_by_value2<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Return static and assign." << std::endl;
{ copy_tracker o = static_by_value<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Move value and assign." << std::endl;
{ copy_tracker o = move_by_value<10>(); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass lvalue." << std::endl;
{ copy_tracker o = return_by_value<10>();
pass_by_value<1>( o ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass by intermediately captured rvalue." << std::endl;
{ const copy_tracker& o = return_by_value<10>();
pass_by_value<1>( o ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass by intermediately captured rvalue." << std::endl;
{ const copy_tracker& o = return_by_value<10>();
pass_by_value<1>( static_cast<copy_tracker&&>(const_cast<copy_tracker&>(o)) ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass rvalue directly." << std::endl;
{ pass_by_value<1>( return_by_value<10>() ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
std::cout << "Pass rvalue directly." << std::endl;
{ pass_by_value<2>( return_by_value<10>() ); }
std::cout << "---------------------------------------------------------"
<< std::endl << std::endl;
}
int main ( void )
{try{
test_copy_tracker();
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
}}

View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <iomanip>
int main ( void )
{
volatile double d = 0.00000001;
volatile double a = 90;
volatile double b = a + d;
volatile double c = b - a;
std::cout << std::setprecision(15) << "a = " << a << std::endl;
std::cout << std::setprecision(15) << "b = " << b << std::endl;
std::cout << std::setprecision(15) << "c = " << c << std::endl;
std::cout << std::setprecision(15) << "d = " << d << std::endl;
std::cout << "b == d -> " << (b == d) << std::endl;
return 0;
}

10
cpp/test_empty_struct.cpp Normal file
View File

@@ -0,0 +1,10 @@
class A
{
};
int main( )
{
A* p = new A;
p->operator delete();
}

10
cpp/test_expression.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include <iostream>
int main ( void )
{
int a = 1;
std::cout << a++ << " " << a++ << std::endl;
std::cout << ++a << " " << ++a << std::endl;
return 0;
}

222
cpp/test_has_method.cpp Normal file
View File

@@ -0,0 +1,222 @@
/* 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 <type_traits>
#include <utility>
#include <exception>
#include <sstream>
struct A
{
void touch( const char * )
{
std::cout << "A::touch";
}
};
struct B
{
};
struct C : public A
{
};
struct D
{
void touch( int )
{
std::cout << "D::touch";
}
};
struct call_touch
{
template <class C>
struct has_touch
{
template <typename T, T t>
class helper{};
template <typename T>
static int test(T*, helper<void (T::*)(const char *), &T::touch>* = 0);
template <typename T>
static char test(...);
static const bool value = sizeof(test<C>(0))!=sizeof(char);
};
template <class S>
static
typename std::enable_if<has_touch<S>::value,void>::type
touch( S * s, const char * p)
{
std::cout << "call_touch: calling -> ";
return s->touch(p);
}
static void touch(...)
{
std::cout << "call_touch: no method to call";
}
};
struct call_touch2
{
template <class S>
static void touch( S * s, const char * p, decltype(&S::touch)* =0)
{
std::cout << "call_touch2: calling -> ";
return s->touch(p);
}
static void touch(...)
{
std::cout << "call_touch2: no method to call";
}
};
struct call_touch3
{
template <typename T, T t>
class helper{};
template <class S>
static void touch( S * s, const char * p, helper<void (S::*)(const char*),&S::touch>* =0)
{
std::cout << "call_touch3: calling -> ";
return s->touch(p);
}
static void touch(...)
{
std::cout << "call_touch3: no method to call";
}
};
struct call_touch4
{
template <class S>
static decltype( ((S*)0)->touch((const char *)(0)) )
touch( S * s, const char * p )
{
std::cout << "call_touch4: calling -> ";
return s->touch(p);
}
static void touch(...)
{
std::cout << "call_touch4: no method to call";
}
};
int main ( void )
{try{
A a;
B b;
C c;
D d;
std::cout << std::endl;
std::cout << "test a: ";
call_touch::touch( &a, 0 );
std::cout << std::endl;
std::cout << "test b: ";
call_touch::touch( &b, 0 );
std::cout << std::endl;
std::cout << "test c: ";
call_touch::touch( &c, 0 );
std::cout << std::endl;
std::cout << "test d: ";
call_touch::touch( &d, 0 );
std::cout << std::endl;
std::cout << std::endl;
std::cout << "test a: ";
call_touch2::touch( &a, 0 );
std::cout << std::endl;
std::cout << "test b: ";
call_touch2::touch( &b, 0 );
std::cout << std::endl;
std::cout << "test c: ";
call_touch2::touch( &c, 0 );
std::cout << std::endl;
// Compiler error is generated.
//
// std::cout << "test d: ";
// call_touch2::touch( &d, 0 );
// std::cout << std::endl;
std::cout << std::endl;
std::cout << "test a: ";
call_touch3::touch( &a, 0 );
std::cout << std::endl;
std::cout << "test b: ";
call_touch3::touch( &b, 0 );
std::cout << std::endl;
std::cout << "test c: ";
call_touch3::touch( &c, 0 );
std::cout << std::endl;
std::cout << "test d: ";
call_touch3::touch( &d, 0 );
std::cout << std::endl;
std::cout << std::endl;
std::cout << "test a: ";
call_touch4::touch( &a, 0 );
std::cout << std::endl;
std::cout << "test b: ";
call_touch4::touch( &b, 0 );
std::cout << std::endl;
std::cout << "test c: ";
call_touch4::touch( &c, 0 );
std::cout << std::endl;
std::cout << "test d: ";
call_touch4::touch( &d, 0 );
std::cout << 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;
}}

126
cpp/test_i32_overflow.cpp Normal file
View File

@@ -0,0 +1,126 @@
#include <exception>
#include <iostream>
#include <stdexcept>
inline void throw_overflow ()
{
throw std::runtime_error( "Overflow!!!" );
}
#define E void (*err)()
//template <class E>
inline int add( int op1, int op2, E )
{
asm ( "add %1, %0"
: "=r"(op1)
: "r"(op2),"0"(op1) );
asm goto( "jo %l[throw_exception]"
: : : "memory"
: throw_exception );
return op1;
throw_exception:
err();
}
//template <class E>
inline long add( long op1, long op2, E)
{
asm ( "add %1, %0"
: "=r"(op1)
: "r"(op2),"0"(op1) );
asm goto( "jo %l[throw_exception]"
: : : "memory"
: throw_exception );
return op1;
throw_exception:
err();
}
// Just test a possible implementation for 386
//template <class E>
inline long long add( long long op1, long long op2, E )
{
unsigned int op1l = (unsigned int)op1;
unsigned int op1h = (unsigned int)((unsigned long long)op1 >> 32);
unsigned int op2l = (unsigned int)op2;
unsigned int op2h = (unsigned int)((unsigned long long)op2 >> 32);
asm ( "add %2, %0\n"
"adc %3, %1"
: "=a"(op1l),"=d"(op1h)
: "g"(op2l),"g"(op2h),"0"(op1l),"1"(op1h) );
asm goto( "jo %l[throw_exception]"
: : : "memory"
: throw_exception );
return (long long)((unsigned long long)op1l | ((unsigned long long)op1h << 32));
throw_exception:
err();
}
#if 0
//
//GCC 4.5.2 crashes on this template.
//I filed a bug against GCC. #55528
//
template <class C>
inline C add2( C op1, C op2 )
{
asm ( "add %1, %0"
: "=r"(op1)
: "r"(op2),"0"(op1) );
asm goto( "jo %l[throw_exception]"
: : : "memory"
: throw_exception );
return op1;
throw_exception:
return throw_overflow();
}
#endif
template <class C>
inline void test_add( C op1, C op2 )
{try
{
std::cout << op1 << " + " << op2 << " = ";
std::cout.flush();
C r = add( op1, op2, &throw_overflow );
std::cout << r << std::endl;
}
catch ( const std::exception& e )
{
std::cout << e.what() << std::endl;
}
catch ( ... )
{
std::cout << "unknown exception" << std::endl;
}
}
int main( void )
{
test_add( 10, 20);
test_add( 100, 200);
test_add( 1000, 2000);
test_add( 1000000000, 2000000000);
test_add( 1000000000, -2000000000);
test_add( -1000000000, 2000000000);
test_add( -1000000000, -2000000000);
test_add( 1000000000L, 2000000000L);
test_add( 1000000000L,-2000000000L);
test_add( -1000000000L, 2000000000L);
test_add( -1000000000L,-2000000000L);
test_add( 7000000000000000000L, 8000000000000000000L);
test_add( 7000000000000000000L, -8000000000000000000L);
test_add( -7000000000000000000L, 8000000000000000000L);
test_add( -7000000000000000000L, -8000000000000000000L);
test_add( (long long)7000000000000000000L, (long long)8000000000000000000L);
test_add( (long long)7000000000000000000L, (long long)-8000000000000000000L);
test_add( (long long)-7000000000000000000L, (long long)8000000000000000000L);
test_add( (long long)-7000000000000000000L, (long long)-8000000000000000000L);
return 0;
}

View File

@@ -0,0 +1,39 @@
//#include <wchar_tiostream>
#include <iostream>
namespace std
{
namespace tr1
{
template <class Ch>
class A
{
public:
void f()
{
std::cout << "I am std::tr1::A<Ch>::f()" << std::endl;
}
};
template <>
void A<wchar_t>::f()
{
std::wcout << L"I am std::tr1::A<wchar_t>::f()" << std::endl;
}
}
}
namespace std
{
using std::tr1::A;
}
int main( void )
{
std::A<wchar_t> a;
a.f();
return 0;
}

151
cpp/test_new_delete.cpp Normal file
View File

@@ -0,0 +1,151 @@
#include <malloc.h>
#include <iostream>
#include <exception>
#include <string>
struct A
{
std::string s;
A()
{}
virtual ~A()
{}
void * operator new (size_t s)
{
std::cout << "A::operator new: size=" << s << std::endl;
return malloc( s );
}
void operator delete (void * p)
{
std::cout << "A::operator delete: p=" << p << std::endl;
free(p);
}
};
struct B : public A
{
std::string b;
};
struct C : public A
{
std::string c;
void * operator new (size_t s)
{
std::cout << "C::operator new: size=" << s << std::endl;
return malloc( s );
}
void operator delete (void * p)
{
std::cout << "C::operator delete: p=" << std::hex << p << std::endl;
free(p);
}
};
struct D : public B, public C
{
//
// You should define this otherwise new/delete are ambiguous.
//
void * operator new (size_t s)
{
std::cout << "D::operator new: size=" << s << std::endl;
return malloc( s );
}
void operator delete (void * p)
{
std::cout << "D::operator delete: p=" << std::hex << p << std::endl;
free(p);
}
};
struct F : public A
{
std::string str;
};
struct E : public B, public F
{
};
void * operator new ( size_t s, const std::string& str )
{
std::cout << str << std::endl;
return malloc( s );
}
int main ( void )
{try{
A * a;
//
// Operator inheritance.
//
std::cout << std::hex << std::showbase << std::endl;
std::cout << " Operator inheritance." << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "allocate and free A." << std::endl;
a = new A;
delete a;
std::cout << "allocate and free B." << std::endl;
a = new B;
delete a;
std::cout << "allocate and free C." << std::endl;
a = new C;
delete a;
std::cout << "allocate and free D." << std::endl;
D * d = new D;
std::cout << "D=" << d << " B=" << (B*)d << " C=" << (C*)d << std::endl;
a = (C*)d;
delete a;
std::cout << "allocate and free E." << std::endl;
E * e = new E;
std::cout << "E=" << e
<< " B=" << (B*)e
<< " BA=" << (A*)(B*)e
<< " F=" << (F*)e
<< " FA=" << (A*)(F*)e << std::endl;
a = (F*)e;
delete a;
//
// In place new.
//
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " In place new." << std::endl;
std::cout << "===============================================" << std::endl;
int * i = new (std::string("Hello")) int;
std::cout << std::endl;
std::cout << 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;
}}

59
cpp/test_return_code.cpp Normal file
View File

@@ -0,0 +1,59 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2"
VIM: let g:cppflags=g:boost.g:tbb.g:tbbmalloc.g:tbbmproxy
VIM: let g:ldlibpath=g:boostld.g:tbbld
VIM: let g:cf5output=0
*/
#include <iostream>
#include <exception>
#include <stdexcept>
void run( const char * cmd )
{
std::cout << cmd << " => ";
int ws = system( cmd );
if ( WIFEXITED(ws) )
std::cout << "return code " << WEXITSTATUS(ws) << std::endl;
else if ( WIFSIGNALED(ws) )
{
std::cout << "signal code " << WTERMSIG(ws);
#if 0
if ( WCOREDUMP(ws) )
std::cout << " and core dump generated." << std::endl;
else
std::cout << " and core dump is not generated." << std::endl;
#endif
std::cout << std::endl;
}
else if ( WIFSTOPPED(ws) )
{
std::cout << "stopped with signal " << WSTOPSIG(ws) << std::endl;
}
else
std::cout << "Child is not terminated normally." << std::endl;
}
int main ( int argc, char ** argv)
{
if ( argc != 1 )
{
std::string arg( argv[1] );
if ( arg == "throw" )
{
throw std::runtime_error( "runtime error" );
}
int r = atoi( arg.c_str() );
return (r) ? r : -1;
}
run("test_return_code.exe 1");
run("test_return_code.exe 2");
run("test_return_code.exe throw");
return 0;
}

68
cpp/test_rvalue_ref.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include <iostream>
#include <string>
class copy_tracker
{
int v;
public:
copy_tracker()
: v(0)
{
std::cout << "copy_tracker::copy_tracker()" << std::endl;
}
copy_tracker( const copy_tracker& c )
: v( c.v )
{
std::cout << "copy_tracker::copy_tracker( copy_tracker& c )" << std::endl;
}
void use_object() const
{
std::cout << v << std::endl;
}
};
copy_tracker create_rvalue()
{
return copy_tracker();
}
const copy_tracker create_const_rvalue()
{
return copy_tracker();
}
void f ( copy_tracker&& o )
{
o.use_object();
}
void f_const ( const copy_tracker&& o )
{
o.use_object();
}
void aaa( const std::string& s )
{
std::cout << s << std::endl;
}
int main( void )
{
f( create_rvalue() );
f_const( create_rvalue() );
f_const( create_const_rvalue() );
copy_tracker lvalue;
f( std::move(lvalue) );
f_const( std::move(lvalue) );
aaa( "aaaa");
return 0;
}

View File

@@ -0,0 +1,18 @@
#include <iostream>
#include "test_static_double.h"
int main ( void )
{
A<int> a;
A<double> b;
std::cout << a._st_double << std::endl;
std::cout << b._st_double << std::endl;
return main2();
}

View File

@@ -0,0 +1,15 @@
#include <iostream>
#include "test_static_double.h"
int main2 ( void )
{
A<int> a;
A<double> b;
std::cout << a._st_double << std::endl;
std::cout << b._st_double << std::endl;
return 0;
}

59
cpp/test_weak_memory.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include <random>
#include <atomic>
#include <thread>
#include <iostream>
#include <exception>
int main ( void )
{try{
int shared = 0;
std::atomic<int> flag;
std::random_device rd;
std::default_random_engine el(rd());
std::uniform_int_distribution<int> ud(1,10);
auto f = [&flag, &shared, &el, &ud](){
int count = 0;
while (count < 10000000)
{
volatile int busy_wait = ud(el)*100;
for ( ; busy_wait; --busy_wait );
int expected = 0;
if (flag.compare_exchange_strong(expected, 1, std::memory_order_relaxed))
{
// Lock was successful
shared++;
flag.store(0, std::memory_order_relaxed);
count++;
}
}
};
std::thread t1( f );
std::thread t2( f );
t1.join();
t2.join();
std::cout << "shared = " << shared << 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;
}}

102
cpp/thread_scheduler.cpp Normal file
View File

@@ -0,0 +1,102 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread -D_GLIBCXX_USE_SCHED_YIELD"
VIM: let g:cf5output=0
*/
/*
* Conclusion:
* Linux RH 5.7 scheduler affiliates threads to a core perhaps. As a result
* if 20 threads are working on 16 core machine then 12 of them work
* 100% of the time and the 8 are working only 50% of the time.
* 34 thread case also was tested and the result is 28 threads work 50%
* of the time and 6 threads work 33% of the time.
* This same effect is visible in Intel TBB.
* Looks RH 5.7 uses linux kernel 2.6.18 which has O(1) scheduler.
*
* Linux RH 6.0 has linux kernel 2.6.32 which has Ingo Molnár's the
* "Completely Fair Scheduler". On that system the above mentioned is not
* observed.
*/
#include <thread>
#include <vector>
#include <atomic>
#include <condition_variable>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <exception>
void busy_wait()
{
for ( volatile int j = 0; j < 10000; ++j );
// Actually this doesn't matter. Commented or uncommented the result
// is the same.
std::this_thread::yield();
}
int main ( void )
{try{
const int n = 34;
std::atomic<int> last_id(0);
typedef long long work_type;
const work_type wamount = 1024L*1024L*40;
const work_type wslot = 1;
std::atomic<work_type> last_work(0);
std::vector<std::thread> threads;
threads.reserve(n);
// std::condition_variable cv;
std::mutex m;
m.lock();
for ( int i=0; i < n; ++i )
{
threads.push_back( std::thread( [&]()
{
int id = last_id++;
{
std::unique_lock<std::mutex> ul(m);
// cv.wait(ul);
}
work_type c = 0;
while( true )
{
work_type w = atomic_fetch_add( &last_work, wslot );
if ( w >= wamount )
break;
c += wslot;
busy_wait();
}
std::stringstream ss;
ss << "Thread: " << std::setw(2) << id
<< " counted " << std::setw(15) << c
<< std::endl;
std::cout << ss.str();
}));
}
m.unlock();
// cv.notify_all();
for ( std::thread& t : threads )
t.join();
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;
}}

552
cpp/toIntTest.cpp Normal file
View File

@@ -0,0 +1,552 @@
// toIntTest.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "getCurrentTime.h"
#include "../fw/2007.12/src/base/point.h"
#if defined(__AIX) || defined(__sparc)
#define BUSERR_PROTECTION
#else
#define LOW_ENDIAN_ARCH
#endif
#if defined(__AIX) || defined(__sparc)
#define TEST_AMOUNT 100000000
#else
#define TEST_AMOUNT 1000000000
#endif
#ifdef _MSC_VER
#define MY_INLINE __forceinline
#else
#define MY_INLINE inline
#endif
#ifndef _MSC_VER
#define __int64 long
#endif
MY_INLINE bool isShortAligned( const unsigned char * p )
{
return !((int)(p) % 2);
}
MY_INLINE bool isIntAligned( const unsigned char * p )
{
return !((int)(p) % 4);
}
//
/// Convert a gdsII short to a standard short.
//
/*!
* The supplied pointer is to the buffer containing the entire gdsII short.
*/
MY_INLINE short toShort1( const unsigned char * p )
{
//
// convert the byte stream to a machine short
//
register short val = *p << 8;
val += *(p+1);
//
// check for negative
//
if ( val & 0x8000 )
{
val &= 0x7ffff;
val ^= 0x7ffff;
val = ++val * -1;
}
return val;
}
//
/// Convert a gdsII short to a standard short.
//
/*!
* The supplied pointer is to the buffer containing the entire gdsII short.
*/
/* slow on sparc */
MY_INLINE short toShort2( const unsigned char * p )
{
#ifdef BUSERR_PROTECTION
if ( isShortAligned(p) )
{
#endif
#ifdef LOW_ENDIAN_ARCH
//
// convert the byte stream to a machine short
//
register unsigned short val = *reinterpret_cast<const short*>(p);
const short mask = 0x00ff;
val = (val & mask) << 8 | (val & ~mask) >> 8;
return (unsigned short)(val);
#else
//
// convert the byte stream to a machine short
//
return *reinterpret_cast<const short*>(p);
#endif
#ifdef BUSERR_PROTECTION
}
else
return *p << 8 | *(p+1);
#endif
}
//
/// Convert a gdsII short to a standard short.
//
/*!
* The supplied pointer is to the buffer containing the entire gdsII short.
*/
/* slow on amd64 */
MY_INLINE short toShort2_( const unsigned char * p )
{
#ifdef BUSERR_PROTECTION
if ( isShortAligned(p) )
{
#endif
#ifdef LOW_ENDIAN_ARCH
//
// convert the byte stream to a machine short
//
// register short val = *p << 8;
// return val | *(p+1);
register unsigned int val = *reinterpret_cast<const short*>(p);
const int mask = 0x000000ff;
val = (val & mask) << 8 | (val & ~mask) >> 8;
return (unsigned short)(val);
#else
//
// convert the byte stream to a machine short
//
return *reinterpret_cast<const short*>(p);
#endif
#ifdef BUSERR_PROTECTION
}
else
return *p << 8 | *(p+1);
#endif
}
//
/// Convert a gdsII short to a standard short.
//
/*!
* The supplied pointer is to the buffer containing the entire gdsII short.
*/
MY_INLINE short toShort3( const unsigned char * p )
{
#ifdef BUSERR_PROTECTION
if ( isShortAligned(p) )
{
#endif
#ifdef LOW_ENDIAN_ARCH
//
// convert the byte stream to a machine short
//
register unsigned short val = *reinterpret_cast<const unsigned short*>(p);
return val >> 8 | val << 8;
#else
//
// convert the byte stream to a machine short
//
return *reinterpret_cast<const short*>(p);
#endif
#ifdef BUSERR_PROTECTION
}
else
return *p << 8 | *(p+1);
#endif
}
//
/// Convert a gdsII short to a standard short.
//
/*!
* The supplied pointer is to the buffer containing the entire gdsII short.
*/
MY_INLINE short toShort3_( const unsigned char * p )
{
#ifdef BUSERR_PROTECTION
if ( isShortAligned(p) )
{
#endif
#ifdef LOW_ENDIAN_ARCH
//
// convert the byte stream to a machine short
//
register unsigned int val = 0;
val = *reinterpret_cast<const unsigned short*>(p);
return val >> 8 | val << 8;
#else
//
// convert the byte stream to a machine short
//
return *reinterpret_cast<const short*>(p);
#endif
#ifdef BUSERR_PROTECTION
}
else
return *p << 8 | *(p+1);
#endif
}
MY_INLINE int toInt1 ( const unsigned char * p )
{
//
// convert the byte stream to a machine int
//
register int val = *p++ << 24;
val += *p++ << 16;
val += *p++ << 8;
val += *p;
//
// check for negative
//
if ( val & 0x80000000 )
{
val &= 0x7fffffff;
val ^= 0x7fffffff;
val = ++val * -1;
}
return val;
}
//
// THE BEST
//
MY_INLINE int toInt2 ( const unsigned char * p )
{
#ifdef BUSERR_PROTECTION
if ( isIntAligned(p) )
{
#endif
#ifdef LOW_ENDIAN_ARCH
//
// convert the byte stream to a machine int
//
register unsigned int val = *reinterpret_cast<const int*>(p);
const int mask = 0x00ff00ff;
val = (val & mask) << 8 | (val & ~mask) >> 8;
return val >> 16 | val << 16;
#else
//
// convert the byte stream to a machine int
//
return *reinterpret_cast<const int*>(p);
#endif
#ifdef BUSERR_PROTECTION
}
else
return *p << 24 | *(p+1) << 16 | *(p+2) << 8 | *(p+3);
#endif
}
/* This is SLOW
MY_INLINE int toInt3 ( const unsigned char * p )
{
#ifdef BUSERR_PROTECTION
if ( isIntAligned(p) )
{
#endif
#ifdef LOW_ENDIAN_ARCH
//
// convert the byte stream to a machine int
//
union
{
unsigned char val[4];
int iVal;
} o;
o.val[0] = p[3];
o.val[1] = p[2];
o.val[2] = p[1];
o.val[3] = p[0];
return o.iVal;
#else
//
// convert the byte stream to a machine int
//
return *reinterpret_cast<const int*>(p);
#endif
#ifdef BUSERR_PROTECTION
}
else
return *p << 24 | *(p+1) << 16 | *(p+2) << 8 | *(p+3);
#endif
}
*/
#ifdef __GCC__
typedef long __int64;
#endif
typedef pod::point<int> point_int;
MY_INLINE point_int toIntPoint1 ( const unsigned char * p )
{
int x = toInt2(p);
int y = toInt2(p+sizeof(int));
return point_int(x,y);
}
MY_INLINE point_int toIntPoint2 ( const unsigned char * p )
{
#if defined(LOW_ENDIAN_ARCH) && defined(_LP64)
#ifdef BUSERR_PROTECTION
if ( isIntAligned(p) )
{
#endif
//
// convert the byte stream to a machine int
//
register unsigned __int64 val
= *reinterpret_cast<const __int64*>(p);
const unsigned __int64 mask8 = 0x00ff00ff00ff00ffL;
const unsigned __int64 mask16 = 0x0000ffff0000ffffL;
val = (val & mask8) << 8 | (val & ~mask8) >> 8;
val = (val & mask16) << 16 | (val & ~mask16) >> 16;
return point_int( int((val << 32) >> 32), int(val >> 32) );
#ifdef BUSERR_PROTECTION
}
#endif
#endif
#if !defined(LOW_ENDIAN_ARCH) || !defined(_LP64) || defined( BUSERR_PROTECTION )
//
// 32b code
//
int x = toInt2(p);
int y = toInt2(p+sizeof(int));
return point_int(x,y);
#endif
}
int main(int argc, char* argv[])
{
// Init
initGetCurrentTimeLib();
unsigned char buffer1[] = {
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C };
unsigned char* buffer = buffer1;
int szNum=0x01020344;
int szNum2=0x01020304;
double dwStart, dwEnd;
//
// test toShort
//
#if 1
puts("Calculating speed for toShortX() aligned");
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort1( buffer );
dwEnd = getCurrentTime();
printf( "toShort1 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort2( buffer );
dwEnd = getCurrentTime();
printf( "toShort2 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort2_( buffer );
dwEnd = getCurrentTime();
printf( "toShort2_ \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort3( buffer );
dwEnd = getCurrentTime();
printf( "toShort3 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort3_( buffer );
dwEnd = getCurrentTime();
printf( "toShort3_ \t%f\n", dwEnd - dwStart );
#endif
#if 1
puts("Calculating speed for toShortX() not aligned");
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort1( buffer+1 );
dwEnd = getCurrentTime();
printf( "toShort1 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort2( buffer+1 );
dwEnd = getCurrentTime();
printf( "toShort2 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort2_( buffer+1 );
dwEnd = getCurrentTime();
printf( "toShort2_ \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort3( buffer+1 );
dwEnd = getCurrentTime();
printf( "toShort3 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile short a = toShort3_( buffer+1 );
dwEnd = getCurrentTime();
printf( "toShort3_ \t%f\n", dwEnd - dwStart );
#endif
//
// test toInt
//
#if 1
puts("Calculating speed for toIntX() aligned");
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile int a = toInt1( buffer );
dwEnd = getCurrentTime();
printf( "toInt1 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile int a = toInt2( buffer );
dwEnd = getCurrentTime();
printf( "toInt2 \t%f\n", dwEnd - dwStart );
// dwStart = getCurrentTime();
// for ( int i = TEST_AMOUNT; i; --i )
// volatile int a = toInt3( buffer );
// dwEnd = getCurrentTime();
// printf( "toInt3 \t%f\n", dwEnd - dwStart );
#endif
#if 1
puts("Calculating speed for toIntX() miss aligned");
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile int a = toInt1( buffer+1 );
dwEnd = getCurrentTime();
printf( "toInt1 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
volatile int a = toInt2( buffer+1 );
dwEnd = getCurrentTime();
printf( "toInt2 \t%f\n", dwEnd - dwStart );
// dwStart = getCurrentTime();
// for ( int i = TEST_AMOUNT; i; --i )
// volatile int a = toInt3( buffer+1 );
// dwEnd = getCurrentTime();
// printf( "toInt3 \t%f\n", dwEnd - dwStart );
#endif
//
// test toIntPoint
//
#if 1
puts("Calculating speed for toIntPointX()");
volatile int nTest[2];
point_int a;
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
{
nTest[0] = nTest[1] = i;
a += toIntPoint1( (const unsigned char *)&nTest );
}
dwEnd = getCurrentTime();
printf( "toIntPoint1 \t%f\n", dwEnd - dwStart );
dwStart = getCurrentTime();
for ( int i = TEST_AMOUNT; i; --i )
{
nTest[0] = nTest[1] = i;
a += toIntPoint2( (const unsigned char *)&nTest );
}
dwEnd = getCurrentTime();
printf( "toIntPoint2 \t%f\n", dwEnd - dwStart );
volatile int x = a.getX();
#endif
#if 1
puts("Checking all results for toShort1()==toShort2()");
for ( unsigned i = 0x10000; i; --i )
if ( toShort1( (const unsigned char *)&i )
!= toShort3_( (const unsigned char *)&i ) )
printf( "Error 1-3 %x o:%x m:%x\n", i,
(unsigned int)toShort1( (const unsigned char *)&i ),
(unsigned int)toShort3_( (const unsigned char *)&i ) );
puts("Checking all results for toInt1()==toInt2()");
for ( unsigned i = 0xffffffff; i; --i )
if ( toInt1( (const unsigned char *)&i )
!= toInt2( (const unsigned char *)&i ) )
printf( "Error %x o:%x m:%x\n", i,
toInt1( (const unsigned char *)&i ),
toInt2( (const unsigned char *)&i ) );
puts("Checking all results for toIntPoint1()==toIntPoint2()");
int n8Byte[2];
n8Byte[1] = 0;
for ( unsigned i = 0xffffffff; i; --i )
{
n8Byte[0] = i;
if ( toIntPoint1( (const unsigned char *)&n8Byte )
!= toIntPoint2( (const unsigned char *)&n8Byte ) )
printf( "Error %x,%x o:%x,%x m:%x,%x\n",
n8Byte[0],
n8Byte[1],
toIntPoint1( (const unsigned char *)&n8Byte ).getX(),
toIntPoint1( (const unsigned char *)&n8Byte ).getY(),
toIntPoint2( (const unsigned char *)&n8Byte ).getX(),
toIntPoint2( (const unsigned char *)&n8Byte ).getY() );
}
n8Byte[0] = 0;
for ( unsigned i = 0xffffffff; i; --i )
{
n8Byte[1] = i;
if ( toIntPoint1( (const unsigned char *)&n8Byte )
!= toIntPoint2( (const unsigned char *)&n8Byte ) )
printf( "Error %x,%x o:%x,%x m:%x,%x\n",
n8Byte[0],
n8Byte[1],
toIntPoint1( (const unsigned char *)&n8Byte ).getX(),
toIntPoint1( (const unsigned char *)&n8Byte ).getY(),
toIntPoint2( (const unsigned char *)&n8Byte ).getX(),
toIntPoint2( (const unsigned char *)&n8Byte ).getY() );
}
#endif
return 0;
}

View File

@@ -0,0 +1,29 @@
#include <iostream>
typedef long mylong;
class A
{
public:
int val;
operator mylong ()
{
return (mylong)val;
}
operator long ()
{
return (long)val;
}
};
int main ( void )
{
A o;
o.val = 30;
std::cout << (mylong)o << std::endl;
std::cout << (long)o << std::endl;
}

12
cpp/vector_bool_itor.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include <vector>
#include <algorithm>
int main ( void )
{
std::vector<bool> a;
a[0] = true;
a[1] = false;
std::sort( a.begin(), a.end() );
return 0;
}

View File

@@ -0,0 +1,48 @@
#include <stdio.h>
class A
{
public:
A()
{
puts( "called A::A()" );
}
A( int /*fake*/ )
{
puts( "called A::A( int )" );
}
};
class B : virtual public A
{
public:
B()
: A( 6 )
{
puts( "called B::B()" );
}
};
class C : public B
{
public:
C()
{
puts( "called C::C()" );
}
};
int main ()
{
puts( "constructing C object" );
C o;
puts( "" );
puts( "constructing B object" );
B ob;
return 0;
}

View File

@@ -0,0 +1,81 @@
#include <stdio.h>
class A
{
public:
typedef void (A::*virt_method_ptr)();
virt_method_ptr m_pMethod;
public:
A()
{
m_pMethod = &A::func1;
}
void call_method_through_ptr()
{
(this->*m_pMethod)();
}
virtual void func1()
{
puts( "called A::func1()" );
}
virtual void func2()
{
puts( "called A::func2()" );
}
virtual void func3()
{
puts( "called A::func3()" );
}
};
class B : public A
{
public:
int nTestPrintMe;
public:
B( int n )
{
m_pMethod = reinterpret_cast<virt_method_ptr>(&B::func4);
nTestPrintMe = n;
}
virtual void func3()
{
puts( "called B::func3()" );
}
virtual void func4()
{
printf( "called B::func4( %d ) \n", nTestPrintMe );
}
};
int main ()
{
A *o = new B( 10 );
o->call_method_through_ptr();
o->m_pMethod = &A::func1;
o->call_method_through_ptr();
o->m_pMethod = &A::func2;
o->call_method_through_ptr();
o->m_pMethod = &A::func3;
o->call_method_through_ptr();
o->m_pMethod = reinterpret_cast<A::virt_method_ptr>(&B::func4);
o->call_method_through_ptr();
return 0;
}

View File

@@ -0,0 +1,42 @@
#include <stdio.h>
class A
{
public:
virtual A* fun()
{
puts( "A* fun()" );
return new A;
}
};
class B
{
public:
virtual B* fun1()
{
puts( "B* fun()" );
return new B;
}
};
class C : public B, public A
{
public:
virtual C* fun()
{
puts( "C* fun()" );
return new C;
}
};
int main()
{
A* p = new C;
A* p_f = p->fun();
return 0;
}

69
cpp/virtual_typo.cpp Normal file
View File

@@ -0,0 +1,69 @@
#include <stdio.h>
class base
{
public:
virtual void virt_func( int ) const
{
puts( "This is base::virt_func." );
}
};
class typo_name : public base
{
public:
virtual void virt_fanc( int ) const
{
puts( "This is typo_name::virt_fanc." );
}
};
class typo_name2 : public base
{
public:
void virt_fanc( int ) const
{
puts( "This is typo_name::virt_fanc." );
}
};
class typo_arg : public base
{
public:
virtual void virt_func( long ) const
{
puts( "This is typo_arg::virt_func." );
}
};
class typo_const : public base
{
public:
virtual void virt_func( int )
{
puts( "This is typo_arg::virt_func." );
}
};
void call_virt_func( base* p )
{
p->virt_func( 0 );
}
int main ( int, char** )
{
typo_name o1;
typo_name2 o2;
typo_arg o3;
typo_const o4;
call_virt_func( &o1 );
call_virt_func( &o2 );
call_virt_func( &o3 );
call_virt_func( &o4 );
return 0;
}