Function Hiding


If there is a functions in base class and one in subclass that has the same name,
the one in subclass always hides the one in base class, regardless of the parameter list and virtual function or not.
Access base class function through sub class object with a different parameter is a compiling error.
Consider this code snippet:

struct CBase
{
	void funcNonVirtual(int i)
	{
		printf("CBase::funcNonVirtual\n");
	}
};
struct CSub: CBase
{
	void funcNonVirtual(char * string)
	{
		printf("CSub::funcNonVirtual\n");
	}
};
void TestHide()
{
	CSub Sub;
	Sub.funcNonVirtual(2); //comiling error
}
Advertisements

Variadic templates


A function can take any number any type of arguments in in cpp11

template <class T>
void VariadicPrint(T tail)
{
	cout << tail << endl;
}

template <class T, class ...Args>
void VariadicPrint(T head, Args... rest)
{
	cout << head << " ";
	VariadicPrint(rest...);
}

void TestVariadic()
{
	VariadicPrint(1, "a"); //output: 1 a
}

VariadicPrint(1, “a”) outputs “1 a”

decltype in lambda


decltype is to calculate type from object expression
Demonstrated by the lambda used to initialize std::set
decltype is required since that’s the only way to access the type of the lambda when the lambda is embedded.

lambda is no more than syntactic shortcut to functor.

void TestLambdaInSet()
{
	bool bGreat = true;
	auto cmp = [=, &bGreat](int a, int b)
	{
		if (bGreat)
			return a > b;
		else
			return a < b;
	};
	std::set myset(cmp);
	myset.insert(1); myset.insert(2);
	printf("first item should be greatest since bGreat = true. got %d", *myset.begin());
	//output is 2 as expected
	bGreat = false;
	myset.clear();
	myset.insert(1); myset.insert(2);
	printf("first item should be smallest since bGreat = false. got %d", *myset.begin());
	//output is 1 as expected
}

void main()
{
	TestLambdaInSet();
}

Reduce web server log size


How to exclude log messages based on IP and request type, so that access.log doesn’t take too much space too soon.
As a public facing streaming server, nginx server’s access.log grows fast for three reasons:
1. due to HLS streaming, most requests are about .ts segment files, which we don’t want to log.
2. some requests are from web crawlers such as Googlebot
3. some IPs request files that we have no idea what they are about, which need to be ignored.

Excluding certain messages from logs/access.log is configured in nginx.conf.
This statement below in nginx.conf says: log only if user defined variable $loggable is true.
access_log logs/access.log combined if=$loggable;
This statement can be in http{} or server{} or location{}.

To set $loggable, we have statements below in. e.g. the server{} section:
set $loggable 1;
if ( $ext_in_log = 0 ) {
set $loggable 0;
}
if ( $bot_in_log = 0 ) {
set $loggable 0;
}
if ( $ip_in_log = 0 ) {
set $loggable 0;
}
which says: if these three user defined variables, ext_in_log, bot_in_log, ip_in_log, are true, set $loggable to 0.

To set these three variables:
check files extension, .ts, to decide if the request is about a video segment.
map $request $ext_in_log {
~*(.*?)\.ts.* 0;
default 1;
}
check if the request is from web crawlers.
map $http_user_agent $bot_in_log {
~*Googlebot 0;
~*bingbot 0;
default 1;
}
check if the request is from certain IPs
geo $remote_addr $ip_in_log {
208.71.208.0/22 0;
172.105.77.67 0;
default 1;
}
Here is a sample nginx.conf file: http://riowing.net/p/nginx.conf