Posted on January 4, 2009 in C#, Photoshop by admin11 Comments »

Thank you for your comments. I updated the version and now it works perfectly.
1.0 - First version (buggy, was sometimes hanging system on exit)
1.1 - Bug fixed. You can now open and close Photoshop at anytime


Recently I decided to do a little drawing, and I took a look at all nice keyboard shortcuts of Photoshop that make life easier (like shown in this blog for example).

Unfortunately, one of the most important features of Photoshop has no shortcut: changing the current color with color picker ! Everytime I want to change color, I have to travel through my whole 24” screen, just to click on this stupid square !

Impossible to draw anymore, I decided to create a program to click on the square instead of me, when it detects a keyboard shortcut. Well it took me some time, but the result is quite useful. All you have to do is launch the program after Photoshop is launched, configure shortcuts you like (clicking in the yellow box and then typing combination), and keep “tools” toolbar or “color” tab visible (otherwise there will be nowhere to click !). The toolbar can be anywhere, on another screen or so.

The application can be minimized on system tray, and remembers shortcut keys between launches. It has been tested on Photoshop CS3 on Windows XP. Feel free to post comment if you have any suggestion.

♦ Download Version 1.1 ♦

Here is an example of fullscreen mode, with either tools toolbar or color tab (F6) visible, so the shortcuts work.

Posted on August 13, 2008 in C#, Programming by adminNo Comments »

In C++ projects using Visual Studio 2005, it was common to perform a different “Post-Build step” in Debug and Release (for example, copy final files to a release folder, launch an installer…).

In C# projects, Microsoft decided to remove these “per-configuration” build events. The “Event” script must be the same for every configuration. A workaround for this is tu use batch file functions top differentiate targets:

REM Here, do things for both configurations

IF NOT "$(ConfigurationName)"=="Debug" Goto :NotDebug
REM Here, do things only for debug

:NotDebug
IF NOT "$(ConfigurationName)"=="Release" Goto :NotRelease
REM Here, do things only for debug
:NotRelease

That’s as simple !

Posted on July 29, 2008 in C++, Programming by adminNo Comments »

Member function pointers

In the C# era we are living, it’s more and more important to have efficient notifications callbacks in C++. We have standard C callbacks notifications, and member function pointers.

They are usually declared as typedef void (CMyClass::*MyTypeDefName)(Parameters);and called with (pMyClass->*)m_MyCallback(params);

I used in a project it to have a simple template signal class. Today, we came across a bug : an object’s constructor was not initializing properly, filling wrong members with wrong values. It seemed he did’nt knew the right offsets for its own class ! Interesting thing, this object contained member function pointers…

Visual C++ weird implementation

The answer was “In Visual C++, member function pointers have different sizes, depending on what compiler knows about the target class”. As explained in this thread (2nd post) the member function pointers can vary from 4 bytes to 16 bytes.

16 bytes is for classes that are forward declared even if you fully declare them after ! See example below:

Case 1

class CForward {}; // full declaration only
typedef void (CForward::*FnType)(int); // sizeof(FnType) == 4

Case 2

class CForward; // forward declaration
typedef void (CForward::*FnType)(int);  // sizeof(FnType) == 16

Case 3

class CForward; // forward declaration
class CForward {}; // full declaration
typedef void (CForward::*FnType)(int);  // sizeof(FnType) == 16

So, if you forward declare a class, and include this class header, any function pointer to this class will be 16 bytes. If you only include the header, pointer will vary from 4 to 12 bytes (depending on the class).

As said in the thread, the simplest solution is to use VC++ #pragma directive, included everywhere in your code (MSDN link here)

// will force all pointers to 16 bytes
#pragma pointers_to_members(full_generality, virtual_inheritance) 

// will force all pointers to 8 bytes, use if you don't have
// virtual inheritance (general case)
#pragma pointers_to_members(full_generality, multiple_inheritance)

// will force all pointers to 4 bytes, use if you don't have
// multiple inheritance
#pragma pointers_to_members(full_generality, single_inheritance)

In strongly encourage you to use one of these methods (or to use /vmg compiler switch), to be sure all the function pointers of your code have the same size !

Don’t be afraid of using a restrictive pragma directive, VC++ throws an error message if you use too restrictive pointers: error C2287: inheritance representation: ’single_inheritance’ is less general than the required ‘multiple_inheritance’

References

Posted on June 5, 2008 in C++ by adminNo Comments »

The aim of this article is to find a way to implement elements C++ has always missed. We can rediscover it in the C#, even if many languages have this natively for years.

The typeof() function is a way to give the type of a variable at compile time. So nothing to do with RTTI, (Run time Type Information, so runtime and not compile).

foreach() is a way to iterate on the elements of a collection, knowing a minimum about the way the items are stored in the collection.

typeof() and C++

Of course, like we know, typeof() doesn’t exists in C++. We know well sizeof(), very useful when you make some template programming, but no typeof.

Hey, Boost made it ! Sure but Boost it’s thousands of template class, and a lot of programmers are afraid of it, at least afraid of using it in real professional projects. Too much files, too much includes, too much potential compile time…

We’ll try to write a simple version, without dependencies on any other file, unlike Boost’s typeof.

Boost typeof()… without Boost

template<int N> struct typeof_class;
template<class T> struct wrap_type { typedef T class_type; };
#define REGISTER_TYPEOF(N,T) template<> struct typeof_class<N> \
    { typedef wrap_type<T>::class_type type_value; }; \
    char (*typeof_fct(wrap_type<T>::class_type &))[N];
#define TYPE_OF(x) typeof_class<sizeof(*typeof_fct(x))>::type_value

How does it works ?

We know it’s impossible to resolve template class arguments implicitly. If we could, we would easily write something like

template<class T> class typeof { typedef T class_type; }
typeof<montype>::class_type maVariable = montype();

It doesn’t works. The only way of resolving automatically a parameter is to pass a parameter to a template function, for example:

template<class T> class typeoffunction(T& myTypedVar) { .... }
TheType myVar;
typeoffunction(myVar);

In this case we know the type of myTypedVar… but only within the function ! Anyway, using functions seems to be the key.

That’s why we define not only a function, but an array of function, which site is dependant on the type. So we can resolve automatically the function type passing the parameter to it, because we pass the type as parameter. If we dereference this function, we get the array, and the sizeof() this array returns a number dependant on the type.

void typetoNumber(int myParam)[123];
void typetoNumber(float myParam)[456];
int iVar;
sizeof(*typetoNumber(intVar)) // = 123
float fltVar;
sizeof(*typetoNumber(fltVar)) // = 456

At this step, we can have a number associated to a type, at compile time.

The only thing we only need to do is to implement specializations of a template class, template of an integer, and to create inside a typedef giving back the type associated to the number.

class numberToType<123> { typedef int MyType; };
class numberToType<456> { typedef float MyType; };

We can hide all these class in macros. So we can write

REGISTER_TYPEOF(1,int)
REGISTER_TYPEOF(2,float)
REGISTER_TYPEOF(3,CMyClass)
...
int x = 5;
TYPE_OF(x) y = x;

The only -important- thing is that we need to declare every type we’ll need to recognize in a typeof(). This constraint is not removable, but we’ll try to make this declaration as easy as possible.

Simplify the type declaration

We need to simplify this declaration. Using 1, 2, 3… incremental numbers seem impossible in a real world case, with multiple .h inclusions

We can use the __COUNTER__ compiler macro, that gives a different number every time it is used (fist 0, then 1…)

REGISTER_TYPEOF(__COUNTER__,int)

We can now create a type declaration macro that doesn’t takes any number. I also added all the related types registration like *, const, const*, vector, list… A single macro will also register all the related types.

REGISTER_IT_TYPEOF(bool)

Direct use : foreach()

Here we are. Our typeof() is easy to use and types are easy to declare. Let’s use it.

Classic use is a foreach() macro, that can iterate trough all the elements of an stl vector. I won’t be too long on the subject, there are many articles talking about this. The aim is to write a macro to enumerate all objects in a collection, and the code we write is independent of from collection type. (It’s the same way to iterate trough a vector, a list, ….)

#define foreach(_it_,_collection_) for(type_of(_collection_)::iterator _it_ = \
                      _collection_.begin();_it_ != _collection_.end(); ++_it_)
list<int> myList;
vector<int> myVector;
...
foreach(it, myList)
{
    cout << *it << endl;
}
foreach(it, myVector)
{
    cout << *it << endl;
}
// instead of
// for(list<int>::iterator it=myList.begin(); it!=myList.end(); ++it)
// for(vector<float>::iterator it=myVector.begin(); it!=myVector.end(); ++it) 

and there, we gain some characters to write, but also some flexibility because we write code that is resistant to a container type change.

The user (not the creator) of a collection of items who only wants to iterate trough elements is not really interested in the containter (vector, list…), this is only a concern for the creator. So the user writes his code once, and if the collection container type changes, for a reason the creator only knows, all the code reading the collection doesn’t change.

We are not using fully abstract collections, like IList<T> in C#, but we still have, for the iteration part, similar functionalities, without any performance drawback !

Conclusion

for someone writing a lot of template code, typeof() can be very useful. Even if its use need a declaration of the types, it opens a lot of possibilities.

For the foreach() even if its use must be limited to cases where we don’t delete elements within the loop, the advantage is to write always the same code, whatever the container (or the element) is. We gain efficacity writing the code, but also readability (we can be sure the user doesn’t hides uncommon instructions inside its for). Wherever you see a foreach() macro, you know it’s a simple, harmless iteration.

Source code

You can download source code of implementation of typeof() and foreach(). You should also read the article…

References