This post is an introduction to a library I have written, UnitC++.
UnitC++ is a modern, light weight, header-only c++ library for making unit testing easy. The intention of this library is to make it really easy to test c++ code in a portable way.
This post is an introduction to a library I have written, UnitC++.
UnitC++ is a modern, light weight, header-only c++ library for making unit testing easy. The intention of this library is to make it really easy to test c++ code in a portable way.
This is not as you might think, an article about implementing directed graphs in C++. The digraphs I am writing about are sequences of characters which act as a stand in for other characters. Digraphs and trigraphs exist in many languages, but I will be focusing on C++. The difference between digraphs and trigraphs is simple the number of characters, a digraph is 2 characters and a trigraph is 3 characters.
Digraphs are part of the language because in the past special characters were hard to input. This was generally because they were not on the keyboard, but in a few cases, they were not even in the code page!
So what use is this now, when modern keyboards have all the symbols we could want and we can use different encodings in source files?
Here is a screenshot of the C++ standard where it defines the allowed digraphs.
The first column are backwards compatible digraphs for C. The next two columns are rather interesting.
For example. If you write the word or in a C++ source file, the compiler will replace that with ||. This means that the following code compiles and works.
//============================================================================= int main() { bool a = false; bool b = true; std::cout << "a or b == " << (a or b) << std::endl; return 0; }
Not only will this work for built in types, this works for user defined operators too. This is because the compiler simply substitutes the symbols.
Here is an example class and calling code which uses all the C++ digraphs.
Class
This is just a stub class which defines a lot of operators which do nothing.
//============================================================================= class Example { public: //=========================================================================== // logic operators bool operator&&(const Example& t) { return true; } bool operator||(const Example& t) { return true; } bool operator!() { return true; } //=========================================================================== // bitwise operators bool operator&(const Example& t) { return true; } bool operator|(const Example& t) { return true; } bool operator^(const Example& t) { return true; } bool operator~() { return true; } //=========================================================================== // logic equals operators Example operator&=(const Example& t) { return t; } Example operator|=(const Example& t) { return t; } Example operator^=(const Example& t) { return t; } Example operator!=(const Example& t) { return t; } };
Calling Code
//============================================================================= int main() { bool a = true; bool b = true; if (a and b) { cout << "\"and\" is an operator." << endl; } if (false or b) { cout << "\"or\" is also an operator." << endl; } if (not false) { cout << "\"not\" is also an operator." << endl; } Example t_1, t_2; if (t_1 and t_2) { cout << "\"and\" even works for classes" << endl; } if (t_1 or t_2) { cout << "\"or\" even works for classes" << endl; } if (not t_1) { cout << "\"and\" even works for classes" << endl; } if (t_1 bitand t_2) { cout << "\"bitand\" also works for classes" << endl; } if (t_1 bitor t_2) { cout << "\"bitor\" even works for classes" << endl; } if (compl t_1) { cout << "\"compl\" even works for classes" << endl; } if (t_1 xor t_2) { cout << "\"xor\" even works for classes" << endl; } t_1 and_eq t_2; cout << "\"and_eq\" even works for classes" << endl; t_1 or_eq t_2; cout << "\"or_eq\" even works for classes" << endl; t_1 xor_eq t_2; cout << "\"xor_eq\" even works for classes" << endl; return 0; }
This gives the output;
"and" is an operator. "or" is also an operator. "not" is also an operator. "and" even works for classes "or" even works for classes "and" even works for classes "bitand" also works for classes "bitor" even works for classes "compl" even works for classes "xor" even works for classes "and_eq" even works for classes "or_eq" even works for classes "xor_eq" even works for classes
This file can be found here.
This is an interesting feature, but is it useful?
I think it is, I think that the code;
if (not fail and ok) { // ... }
Is more readable than;
if (!fail && ok) { // ... }
However just because these are defined in the standard does not mean they can be used. These are largely considered a legacy part of the standard and there is limited support for it.
In particular Microsoft’s C++ compiler will not compile it. Both clang and g++ will compile it however, so if you are compiling using either of these you can use this nice feature.
Thanks for reading.
If you use shell scripting a lot from the command line, this will probably improve your experience of it. This is for users of linux/mac command line or of cygwin on windows. Anywhere you can use the bash cd command.
Firstly (if you didn’t know) cd remembers the last directory you went to. To access this use;
cd -
This will change to the previous directory you were in. If you want to supercharge this you can add the following to your .bashrc/.profile/… customization file.
# petar marinov, http:/geocities.com/h2428, this is public domain cd_func () { local x2 the_new_dir adir index local -i cnt if [[ $1 == "--" ]]; then dirs -v return 0 fi the_new_dir=$1 [[ -z $1 ]] && the_new_dir=$HOME if [[ ${the_new_dir:0:1} == '-' ]]; then # # Extract dir N from dirs index=${the_new_dir:1} [[ -z $index ]] && index=1 adir=$(dirs +$index) [[ -z $adir ]] && return 1 the_new_dir=$adir fi # # '~' has to be substituted by ${HOME} [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}" # # Now change to the new dir and add to the top of the stack pushd "${the_new_dir}" > /dev/null [[ $? -ne 0 ]] && return 1 the_new_dir=$(pwd) # # Trim down everything beyond 11th entry popd -n +11 2>/dev/null 1>/dev/null # # Remove any other occurence of this dir, skipping the top of the stack for ((cnt=1; cnt <= 10; cnt++)); do x2=$(dirs +${cnt} 2>/dev/null) [[ $? -ne 0 ]] && return 0 [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}" if [[ "${x2}" == "${the_new_dir}" ]]; then popd -n +$cnt 2>/dev/null 1>/dev/null cnt=cnt-1 fi done return 0 } alias cd=cd_func
This means that you can keep a history of the past 10 directories which you have visited and change between them.
To access this list type
cd --
Which will print a list like.
0 /e/projects/dgc/or14126h_3 1 /e/projects/dgc/or14126h_3/orthotics.dev 2 /c/Users/dgc/Dropbox/Coding 3 /e/projects/dgc 4 /e/projects/dgc/or14126h_2 5 /e/devdisk/dcm/configs 6 /e/devdisk/dcm 7 /e/devdisk 8 /e
And then
cd -5
will take you to entry 5 in the list.
To be clear I did not write this, I found it in the default cygwin .bashrc. The author of this was Petar Marinov.
I have found this very helpful, particularly when using a new python module. This is the process;
This function removes that minor inconvenience and I have found it very useful.
Thanks for reading.
This post will be about the Resource Acquisition is Initialization (RAII) pattern which is a creational pattern. This is going to be the first non Gang of Four pattern I will write about.
The idea behind this pattern is to correctly dispose of all the resources that you acquire. This pattern was first written about by Bjarne Stroustrup, the creator of C++. The most common examples of this pattern are in opening and closing files and web sockets. It is also important in controlling mutexes so you can write tread safe code.
Continue reading ‘Resource Acquisition is Initialization (RAII)’
This is about the comparison operators in C++ and making them easy to implement. There are 6 comparison operators in C++; ==, !=, <, <=, >, and >=. If you want to be able to apply all of these to a class, and you have the right type of order, you only need to implement one function which will determines them all. Incidentally this is called a total order, but I won’t go into what that means here.
The tl;dr of this is look at this repository and use a class from there to easily implement custom comparison operators.
This idea is to map the order onto the real numbers (doubles). So you have a function which takes two instances of a class and returns a double. If this function returns a negative number then the first instance is less than the second, if it returns 0 they are equal and if it returns a positive number then the first instance is greater than the second.
This will be easier to see with an example, so here is a class which is just data storage for an int.
This post will be about the Iterator pattern which is a behavioural pattern.
The idea behind this pattern is to have an object which you can loop over without needing to know the internal representation of the data. While in python nothing is private so you can find out the internals of the class, the iterator pattern gives you a standard interface.
This post will be about the Builder pattern which is a creational pattern.
The idea behind the builder pattern is to abstract away the construction of an object so that many implementations can use the same builder. This separates the construction logic of the desired class from it’s representation.
The strategy pattern is a behavioural design pattern. This means that it is a common communication method between objects and not concerned with how those objects are created or structured.
The idea behind the strategy pattern is to encapsulate the implementation details of an algorithm and make them interchangeable. This gives more flexibility in how an object can be used and enables each algorithm to be tested independently of the calling object.
Continue reading ‘Strategy Pattern’
This is the first of actual posts in my series on design patterns in python.
This will be about the Facade pattern which is a structural pattern.
The facade pattern is used to make one object with a simple interface represent a complicated system. The problem often occurs in programming where you have a series of interconnected classes where the functions must be called in a certain order or have complicated interdependencies.
This pattern is to give a standard interface to such a system, so you don’t have to rely on reading how you call the system in one of the files or look at example usage.
Continue reading ‘Facade Pattern’
I don’t think this is widely known but auto was already a keyword in C++ and also C. Prior to it’s removal in C++09 it was used as a storage duration indicator. What this means is that it tells the compiler how long the storage for the variable needs to last for. The auto keyword is the default and means that the storage is available until the end of the block in which the variable is created. The fact that this is the default behaviour for C++ and it’s infrequent use lead to it being removed from the standard. As Bjarne Stroustrup writes
Several committee members trawled through millions of lines of code finding only a handful of uses — and most of those were in test suites or appeared to be bugs
So onto the new auto. Continue reading ‘Auto’