Implement properties in C plus plus

Here is some tricky code in C++ that I was watching recently and decided to post here for future references. Sometime the usage of operator overloading in C++ amuses me so much. Apperently there is a way to implement properties that look the same as normal properties and behave almost the same. The only problem as always is that when you get a compile time error – the messages are pretty confusing if you don’t know what’s going on. Enough said here is the code for that “beautiful” feature.

/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
#include 
 
using std::cout;
 
template
class Property
{
private:
    PropertyType data;
 
public:
    /* Use the cast type operator here */
    operator PropertyType()
    {
        return Controller::get(data);
    }
 
    PropertyType operator=(PropertyType a)
    {
        Controller::set(data, a);
        return Controller::get(data);
    }
};
 
class IntController
{
public:
    static int get(int &a)
    {
        return a;
    }
 
    static void set(int &a, int &b)
    {
        a = b;
    }
};
 
class Man
{
public:
    Property<int, IntController> HP;
};
 
void PropertyTest()
{
    Man man;
    man.HP = 7;
    man.HP = 7 + man.HP;
    cout << man.HP << "\n";
}
 
int main (int argc, char const* argv[])
{
    PropertyTest();
    return 0;
}

C++ can have nasty syntax sometimes

Hmm today on IRC in the channel ##C++ @ Freenode someone posted the following code

/*In g++ this does not work*/
template
struct hello
{
 hello(){};
 ~hello(){};
 template
 int world()
 {
  return _b+_a;
 }
};

template
void try_hello()
{
hello h;
h.world();
}

so i tried to help the person telling him that he should use a typename before h.world(); because it can ambitious for the compiler. It turns out that the right solution was

h.typename world();

someone asked why is

world()

ambitious for the compiler and seldon said that the compiler can’t know the type of world in that context. Well but world is explicitly called with


. Hmm the problem is that when the compiler see that expression it assume that

<

after world is the operator less than. Oh that my brain started to hurt badly :D . Maybe the syntax of C++ is getting really complicated with the years. I think that the new standards won’t get rid of those, because they can’t. Too much code have been written already to work that way :(

More kbgoffice fun

Yesterday i finally managed to port kbgoffice completely to Qt4, so everyone with no qt3support can compile it. I am not happy with what i got however. The code is really bad and there are no testcases so i might write those for now. With no tests the development is really hard. I might use the test framework provided with Qt which really rocks. The program is also hard coded with a class named Properties which is really broken, because it seems that when they wrote the program there was no QSettings class which is times better :) .

Eclipse for c++

Hmm lately I started using eclipse more and more. It seems they made it to not crash so much with the new release. I am pretty happy with it for C++ development it seems to own. I am not sure but i think that I am even more productive with it now than with vim. I have used the 3.2 version but it was full crap back then ;) . Besides it is written in java it is worth looking in it.

Supress some warning in vim

Maybe you know that vim parses the output from the compiler so it can jump to the right line which cause the error(:h quickfix for more info). Well it seems that the default compiler options parse even warning which are sometimes pretty annoying. There were some warning for depracated conversion from the compiler because of xmp files. Well i am sure that i don’t want to see those. So here is what i have in my ~/.vim/ftplugin/cpp.vim for some time:

let &errorformat = "%-G%f\:%l\:\ warning\:\ deprecated\ conversion\ from\ string%.%#\,".&errorformat

I know that maybe this only works for gcc, but it is a start. You can get more info about the erroformat with :h errorformat in vim. Happy vimming :)

kbgoffice fun

Today at the Bulgarian linux forum someone asked to port the kbgoffice program to KDE4. I have used the damn thing pretty much so I decided to go for it. After 2 hours of fighting with compiler errors it finally compiled but most of the code is just commented out so I got a prototype… I started to look at the classes this night and I see some bad code :( . Not sure why but many people still write ugly C++ code. The idea of the whole program is rather nice and the design is cool but there are some things that drive me nuts. For example the author reads the config file everytime he needs to save/read a property which is not efficient at all. I will try to fix some of the problems and I hope I will have the time for that. I will post a snippet which bit me today while I was working on the damn thing.

 1 #include <iostream>  2 #include <cstdlib>  3 #include <ifstream>  4 #include <string>  5 #include <map>  6  7  8 int main (int argc, char const* argv[])  9 { 10     std::map<std::string, std::string> myMap; 11     std::ifstream iofile(“example.txt”); 12     std::string key; 13     std::string value; 14     while(! iofile.eof()){ 15         getline(iofile,key); 16         int position = key.find(“=”); 17         if (position == std::string::npos)  18             break; 19         value = key; 20         value.erase(0,(position+1)); 21         key.erase(key.begin()+position, key.end()); 22         std::cout << “The key is “ << key << ” and the value is “ << value << std::endl; 23     } 24     return 0; 25 } 26 //    std::string home = getenv(“HOME”); 27 //    std::ofstream file(“example.txt”, std::ios::app | std::ios::out); 28 //    file << “WithSplash=0\n”; 29 //    file.close();

Well where is the problem with that you ask :? The answer is here so we change the code to this:

 1 #include <iostream>  2 #include <cstdlib>  3 #include <fstream>  4 #include <string>  5 #include <map>  6  7  8 int main (int argc, char const* argv[])  9 { 10     std::map<std::string, std::string> myMap; 11     std::ifstream iofile(“example.txt”); 12     std::string key; 13     std::string value; 14     while( getline(iofile,key) ){ 15         int position = key.find(“=”); 16         if (position == std::string::npos)  17             break; 18         value = key; 19         value.erase(0,(position+1)); 20         key.erase(key.begin()+position, key.end()); 21         std::cout << “The key is “ << key << ” and the value is “ << value << std::endl; 22     } 23     return 0; 24 } 25 //    std::string home = getenv(“HOME”); 26 //    std::ofstream file(“example.txt”, std::ios::app | std::ios::out); 27 //    file << “WithSplash=0\n”; 28 //    file.close();

and we are ready to continue.

A little benchmark with C++ and Java in the game.

Today i just wanted to make a benchmark and see which is faster java or C++. There are rumors over the internet that java is or will be faster than c++ some day. So today I decided to try compare the speed of those languages. We all know arrays and how bad they can be sometimes if you are not careful with them in C++, because

    • They don’t know it’s size

 

  • You cannot extend it if you reach the limit

 

 

  • The name of an array converts to a pointer to its first element at the slightest provocation(there are nice examples for this point here)

 

 

So with one word arrays are not nice. Java provides some fixes for those, because there an array know it’s size and there are no pointers :) . But what if we use a higher level abstractions from STL? Will std::vector beat the Java arrays? Here is the code:
main.cpp:


#include <vector>

#include <iostream>

int main(int argc, char* argv[]){

std::vector<int> Vec(10000);

for(int x = 0;x < Vec.size();x++){

Vec[x] =x;

std::cout << Vec[x] << std::endl;

}

}

Main.java


class Main{

public static void main(String []args){

int Vec[] = new int[10000];

for (int x = 0;x < Vec.length;x++){

Vec[x] = x;

System.out.println(x);

}

}

}

So we compile with

g++ -O2 -fomit-frame-pointer main.cpp && javac Main.java

and let’s run them through time :)

nikolavp@Nikolavp:~$ time ./a.out > /dev/null

real    0m0.010suser    0m0.004ssys     0m0.004snikolavp@Nikolavp:~$ time java Main > /dev/null

real    0m0.196suser    0m0.140ssys     0m0.052s

So C++ with vector which is supposed to be a higher level abbstraction outperformed Java’s “safe” arrays. The next time you want to write something in Java think a little bit is it really worth the machines ;) . Don’t get me wrong Java have it’s place and it made the whole world a better place, but don’t convince me, that it will be faster(or is faster now) than C++… It just never will.

#include
using namespace std;
int main(){
}


for(var i=0;i{
go("http://orkut.com")
enter("Email","myemail@gmail.com")
enter("password","secret")
sleep(600)
click("sign in")
sleep(600)
go("http://www.orkut.com/GLogin.aspx?cmd=logout")
sleep(60)
}