NN – WordPress

September 30, 2008

.Net – Nullability

Filed under: .net — by NN @ 6:40 pm

.Net declares two types of types: Value types and Reference types.

What the difference ?

Reference types can be null while Value types cannot. So you have System.Nullable<T> for Value types you want to put null in.


int i = null; // Error
object o = null; // OK
System.Nullable<int> ni = null; // OK, System.Nullable is reference type.

It was a theory. Now go to practical samples:

While reference types can be null you have to check all arguments for null. Hmm..

It looks like old C++ days !

Compare:

C++

void f(A* a, B* b)
{
  if(a == NULL || b == NULL)
    return;

  // The rest code
}
 
C#
void f(A a, B b)
{
  if(a == null || b == null)
    return;

  // The rest code
}
 
So what the hack ? :)

Mainly it does not cause a problem for private methods but for all public you must check for null. Especially it comes in operator overloading and overloading Object.Equals functions.

And the solution comes true..
It simply you must not provide nullability for Reference types if you do not specify that.
Example:

class A
{
  public static A operator+(A left, A right)
  {
    // In C# you must check arguments
    // if(a == null || b == null)
    // return;
    // Because one can write:
    //
    // A a = null;
    // A b = null;
    // A c = a + b;

    // In C## language you just cannot pass null here !
    return …;
  }
}

// In C## language you specify when null is OK

class A
{
  public static A? operator-(A? left, A? right)
  {
    // Here we may check for null
    if(left == null)
      return new A(1);
    if(right == null)
      return new A(2);
    return new A(3);
  }
}

That's it.

Unfortunately it doesn't happen and you have C# , not C## ;) so you need to invent some strange things:
1. Checker class:

void F(A a)
{
  Checker.NotNull(a);
}

2. If you have metaattributes in your language (Nemerle has :) ) you can write something like this:

F([NotNull] a : A) : void
{
}

And this will produce code as mentioned in 1.

Nullability is a bigger problem then many think about it and .Net didn't solve it elegantly as it could be.
Enjoy.

September 29, 2008

Registration free COM

Filed under: c++, com — by NN @ 4:59 am

So this will be the first post, not the last, about registration free COM.
Btw co is short of COM. Why ??

The beginning is taken from KK’s blog:Registration free COM .

So what do we do:
1. Create an IDL file describing our interfaces.
2. Compile it with midl to produce for us the definitions of the interface and proxy-stub.
3. The server application uses the generated .c files and just call CoRegisterClassObject in the beginning for the factory and then registers the proxy-stub.
4. The client registers the proxy-stub and then just call naturally CoCreateInstance.
5. It works.

Some to make work.
There is a standard marshalling for standard interfaces: IPersist, IStorage, IClassFactory …
What it tells us ? That we can marshall IClassFactory for free.
So we create a global instance of our ClassFactory implementation and then register it using CoRegisterClassObject.
That’s all what is needed to make class factory in the client ! :)

The only problem that you first create a ClassFactory and then you call manually CreateInstance… Not good.

Again we have a problem.
And the brilliant solution !
If you remember [coclass] in IDL describes some class which implements the interfaces.
What if you tells that coclass implements your interface and ClassFactory too ? Hmm.

[uuid("...")]
coclass A
{
[default] interface IA;
interface IClassFactory
}

This is the trick :)
When we call CoCreateInstance in the client:
CoCreateInstance(CLSID_A, … IID_A..);
It looks for ClassFactory and finds it ! Then it calls CreateInstance of our ClassFactory implementation which creates IA implementation class.
So simple :)

Now, since the creation is the regular COM instance creation you can simply use the class in .Net like this:
A a = new AClass();

That’s all for today.

P.S.
The source code will be supplied later.

September 21, 2008

C++ & GUI

Filed under: c++ — by NN @ 7:16 pm

I have written some applications in C++ with GUI for Windows.

I have used WinAPI then MFC and the last was WTL.

But what lacks is the graphical designer. I know there is resource editor with dialogs but dialog is not a regular window and so on.

Currently I have to write some GUI application and of course it must be very small in size, so C++ seems a possible solutoin. But I want a GUI designer !

Neither MFC nor WTL provide a good GUI designer to create windows, only dialogs. :(

So what do we have today:

1. wxWidgets

wxGlade is a designer for this library


It can generate C++ code as well.

2. FLTK
fluid is the GUI designer:

3. SmartWin++
Sally is a nice IDE:

I still do not know which library is the best, since designer solve all the problems for GUI code generation :)
wxWidgets is the preferred but I may go on FLTK because I need a small executable.
Somewhen you will know.

Powered by WordPress.com