Saturday, January 31, 2009

Prefix and Postfix operators in C++?

Thanks to stackoverflow "JProgrammer"

"Taking a leaf from Scott Meyers, More Effective c++ Item 6: Distinguish between prefix and postfix forms of increment and decrement operations.

The prefix version is always preferred over the postfix in regards to objects, especially in regards to iterators.

The reason for this if you look at the call pattern of the operators.

// Prefix
Integer& Integer::operator++()
{
*this += 1;
return *this;
}

// Postfix
const Integer Integer::operator++(int)
{
Integer oldValue = *this;
++(*this);
return oldValue;
}

Looking at this example it is easy to see how the prefix operator will always be more efficient than the postfix. Because of the need for a temporary object in the use of the postfix.

This is why when you see examples using iterators they always use the prefix version.

But as you point out for int's there is effectively no difference because of compiler optimisation that can take place.
"

Wednesday, January 21, 2009

XML Streaming

// Creates a new reader (potentially recycled).
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(inputStream);

while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
switch (reader.next()) {
case XMLStreamConstants.START_ELEMENT:
if (reader.getLocalName().equals("Time")) {
// Reads primitive types (int) attributes directly (no memory allocation).
time.hour = reader.getAttributeValue("hour").toInt();
time.minute = reader.getAttributeValue("minute").toInt();
time.second = reader.getAttributeValue("second").toInt();
}
...
break;
}
}

XML Streaming

// Creates a new reader (potentially recycled).
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(inputStream);

while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
switch (reader.next()) {
case XMLStreamConstants.START_ELEMENT:
if (reader.getLocalName().equals("Time")) {
// Reads primitive types (int) attributes directly (no memory allocation).
time.hour = reader.getAttributeValue("hour").toInt();
time.minute = reader.getAttributeValue("minute").toInt();
time.second = reader.getAttributeValue("second").toInt();
}
...
break;
}
}

Wednesday, January 14, 2009

Setting up a multiple display with MAC and linux

This is my personal preferences can change:

1. Run Synergy server on linux
http://synergy2.sourceforge.net/

2. Run synergyc client on mac
http://sourceforge.net/projects/synergykm

3. Edit .synergy.conf on your home directory

section: screens
linux desktop:
mac client:
end
section: links
mac client:
right = linux desktop
linux desktop:
left = mac client
end

4. On you mac terminal i do this
alias command="killall -c ssh; ssh -nf -L 24800:localhost:24800 -L 5917:localhost:5901 -L 5918:localhost:5902 linuxbox -- 'sleep 999999999'"

There could be a better method here

5. For connecting the macbook to another monitor use this
mini dvi to vga adapter
http://store.apple.com/us/product/M9320G/A

You might have to restart your mac book here.

Vola you are done.