Sunday, November 29, 2015

Aha!

 Continued from the previous Aha!.

Ameteur activists
Share with this link



Continued in the next Aha!

Saturday, November 7, 2015

Get through the certificate problems which show "This connection is untrusted"

At some places, the SSL security certificates used, don't get recognized properly and users are put through the hassle of adding an exception an innumerable number of times to connect to perfectly valid websites. This is the screen you get to see on Firefox:


Instead of always clicking the "Add Exception" button, I searched if there was something I could use to get rid of this error. Turned out that Firefox had an addon named SkipCert which automatically adds the exception and in a few seconds, you'll be taken to the website you wanted to view.


Get the addon here: https://addons.mozilla.org/en-US/firefox/addon/skip-cert-error/

As for Chrome, these appear to be the solutions: http://superuser.com/questions/104146/add-permanent-ssl-certificate-exception-in-chrome-linux


Using TBB's concurrent containers

When searching for a tutorial on TBB's concurrent containers, you might end up finding a 1 minute video on it which doesn't play because you don't have Flash installed, or might just want a tiny program to show how to start using it. This blog post shows you exactly that!

You can use a TBB concurrent vector exactly like you use an std::vector.

#include <iostream>
#include <tbb/concurrent_vector.h>

int main(int argc, char** argv)
{
    tbb::concurrent_vector vec;
    tbb::concurrent_vector::iterator vecIter;
    vec.reserve(100);
    vec.push_back(1);
   
    for(vecIter = vec.begin(); vecIter != vec.end(); vecIter++)
    {
       std::cout<< *vecIter << "\n";
    }//for
   
    vec.clear();
   
    return 0;
}//main



Just make sure you

  • Include TBB's includes folder (/usr/local/tbb44_20150728oss/include/)
  • Point the project to TBB's lib folder (/usr/local/tbb44_20150728oss/lib/ia32/gcc4.4/libtbb.so)
  • Add TBB's lib folder to LD_LIBRARY_PATH in ~/.bashrc. (export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/tbb44_20150728oss/lib/ia32/gcc4.4/")


For concurrent queue, the pop function is different.

#include <iostream>
#include <tbb/concurrent_queue.h>

int main()
{
    tbb::concurrent_queue q;
    q.push(10);
    q.try_pop();//This is thread safe. Better than using if (!q.empty()) {q.pop();}
}//main


A C++ debugging helper

When working with C++ and you're in debug mode, you might be accustomed to these:
  • Using a lot of std::cout statements
  • Seeing a lot of such output to the terminal
  • Introducing a lot of temporary code which you don't want to be part of the final build
  • Commenting out useful code which you don't want others to delete later because it's useful

and more...

In such situations, it helps to have an hpp file which you can include wherever you like, and it will provide you a lot of #define options for conditional compilation. When you are ready to release, just comment out these #define's and the changes will be applied all over your project automatically.

Checkout these...

/* Include this hpp file in any file where you need to output
 * cerr or cout. The advantage of using this file is that if you
 * just comment out the "define DEBUG" line, then all the places
 * where you used "op" to output cout's and "err" to output cerr's,
 * will become inactive. ie: They won't output anything. Keeps the 
 * code clean and efficient.
 * 
 * Usage: In your code, instead of typing std::cout<<"Hi";
 * Just type cout("Hi");
 * Same way, instead of std::cerr<<"Error"; just type cerr("Error");
 */

#ifndef DEBUGHELPER_HPP
#define DEBUGHELPER_HPP

#define DEBUG_MODE

#define USEFUL_CODE_TEMPORARILY_COMMENTED_OUT_DONT_DELETE_IT
#define CODE_FOR_TESTING_FEEL_FREE_TO_DELETE_IT

#ifdef DEBUG_MODE
    //Colour code by Vaughan Schmidt. http://www.codebuilder.me/2014/01/color-terminal-text-in-c/
    #define RESET   "\033[0m"
    #define BLACK   "\033[30m"      // Black
    #define RED     "\033[31m"      // Red
    #define GREEN   "\033[32m"      // Green
    #define YELLOW  "\033[33m"      // Yellow
    #define BLUE    "\033[34m"      // Blue
    #define MAGENTA "\033[35m"      // Magenta
    #define CYAN    "\033[36m"      // Cyan
    #define WHITE   "\033[37m"      // White
    #define BOLDBLACK   "\033[1m\033[30m"      // Bold Black
    #define BOLDRED     "\033[1m\033[31m"      // Bold Red
    #define BOLDGREEN   "\033[1m\033[32m"      // Bold Green
    #define BOLDYELLOW  "\033[1m\033[33m"      // Bold Yellow
    #define BOLDBLUE    "\033[1m\033[34m"      // Bold Blue
    #define BOLDMAGENTA "\033[1m\033[35m"      // Bold Magenta
    #define BOLDCYAN    "\033[1m\033[36m"      // Bold Cyan
    #define BOLDWHITE   "\033[1m\033[37m"      // Bold White
    #define CLEAR "\033[2J"  // clear screen escape code
    #define coutBlack(x) (std::cout << BLACK << (x) << RESET)
    #define coutBoldBlack(x) (std::cout << BOLDBLACK << (x) << RESET)
    #define coutRed(x) (std::cout << RED << (x) << RESET)
    #define coutBoldRed(x) (std::cout << BOLDRED << (x) << RESET)
    #define coutGreen(x) (std::cout << GREEN << (x) << RESET)
    #define coutBoldGreen(x) (std::cout << BOLDGREEN << (x) << RESET)
    #define coutYellow(x) (std::cout << YELLOW << (x) << RESET)
    #define coutBoldYellow(x) (std::cout << BOLDYELLOW << (x) << RESET)
    #define coutBlue(x) (std::cout << BLUE << (x) << RESET)
    #define coutBoldBlue(x) (std::cout << BOLDBLUE << (x) << RESET)
    #define coutMagenta(x) (std::cout << MAGENTA << (x) << RESET)
    #define coutBoldMagenta(x) (std::cout << BOLDMAGENTA << (x) << RESET)
    #define coutCyan(x) (std::cout << CYAN << (x) << RESET)
    #define coutBoldCyan(x) (std::cout << BOLDCYAN << (x) << RESET)
    #define coutWhite(x) (std::cout << WHITE << (x) << RESET)
    #define coutBoldWhite(x) (std::cout << BOLDWHITE << (x) << RESET)   

    #define cerr(x) (std::cerr << (x))
    #define cout(x) (std::cout << (x))
    //... etc
#else 
    #define cerr(x)
    #define cout(x)
    //... etc
#endif

#endif // DEBUGHELPER_HPP

Not only are the cout's easier to type now, they are also easy to disable.