PL1

Gosh, I was reading how C# stores strings some time ago and it’s reminiscent of PL1, or at least Prime’s version PLP. A PLP string was stored with length first followed by the string itself. Strings are immutable in C# and what’s nice about that is that they’re thread safe.

I read someone’s SQLite3 blog the other day about the evils of threading. I wonder how they handle updates from the internet. This little piece of software I wrote collects information in a shop and then when the machine is connected to the internet it uploads the information that it’s been collecting while continuing to collect its information.

I’ve got another app that integrates with Google Contacts. Without threading the user can wait a very long time to refresh all the contacts. Best it happens in the background. Our user has other things to do than wait for a refresh.

I’m thinking that that author doesn’t live in the real world.

Posted in Uncategorized | Leave a comment

Dynamic Types in C# 4.0

Seems like building a bit more reflection into the language to me. I sure could have used it on OpenTrader when I was writing the code to handle scripting. Here’s an example I pinched from Microsoft


static void Main(string[] args)
{
    ExampleClass ec = new ExampleClass();
    // The following call to exampleMethod1 causes a compiler error
    // if exampleMethod1 has only one parameter. Uncomment the line
    // to see the error.
    //ec.exampleMethod1(10, 4);

    dynamic dynamic_ec = new ExampleClass();
    // The following line is not identified as an error by the
    // compiler, but it causes a run-time exception.
    dynamic_ec.exampleMethod1(10, 4);

    // The following calls also do not cause compiler errors, whether
    // appropriate methods exist or not.
    dynamic_ec.someMethod("some argument", 7, null);
    dynamic_ec.nonexistentMethod();
}

Cool, eh. Problem is I might have waited for the feature and then Open Trader wouldn’t have got written. How scripting in Open Trader works, is that the user who happens to be writing a script writes a class inheriting from a class provided by the app. I compile the user’s script by calling the built-in compiler. Then I create an instance of the class. The class has a method called Run, which is then called. So many calls. I must say the Type keyword became my friend.

The user uses the same method to create their own data file types. With their own methods for reading data. In this case the code looks through a particular assembly for all classes with a given sub class, then offers them up to the user as a choice of file type. I kinda like it.

In fact it’s such a nice technique, I use it to get properties for in the built Neural Networking and Genetic Algorithms.

That’s why I like dynamic typing. I was thinking the language needed something like that myself. Sorta like id in Objective C.

Posted in Uncategorized | Leave a comment

Frustrations

I had fun yesteray with a small application for a client, developed in Gtk#. It worked perfectly well on the Mac and on Linux, but fell over terribly on Windows. With all of the catches I had, I didn’t have one that caught this problem.

The problem was that Gtk.Pixbuf under Windows doesn’t load JPEGs, at least not the one I was loading. I converted the JPEG to a PNG file and it all worked. I’d say it was a bug in Gtk for Windows, but what do I know?

Posted in Uncategorized | Leave a comment

Next: Platform independence

The last post wasn’t really platform independent. Not at runtime anyway. I discovered that the technique is to create a class that delivers the operating system.


public class OperatingSystem
{

   [DllImport ("libc")]
   static extern int uname (IntPtr buf);

   public static string DetectOS ()
   {
      string style;
      if( !IsUnix )
      {
         style = "Windows";
	 return style;
      }

      IntPtr buf = UnixMarshal.AllocHeap(8192);
      if( uname (buf) != 0 )
      {
         style = "Unix";
         return style;
      }

      style = UnixMarshal.PtrToStringUnix (buf);
      UnixMarshal.FreeHeap(buf);
      return style;
   }

   static bool IsUnix
   {
      get {
         int p = (int) Environment.OSVersion.Platform;
         return ((p == 4) || (p == 128));
      }
   }
}

Now all you have to do is use refelction. So substituting yesterday’s first technique for MacOS in the static Main method


if( OperatingSystem.DetectOS() == "Darwin" )
{
   System.Reflection.Assembly assemblyMonoMac = System.Reflection.Assembly.Load("MonoMac");
   Type nsapplication = assemblyMonoMac.GetType("MonoMac.AppKit.NSApplication");
   nsapplication.GetMethod("Init").Invoke( null, new object[] {});
}

The following code fragment creates a sound buffer and then plays it. You could equally apply the same technique to yesterday’s example of turning an icon into a pixbuf.


object sndPlayer;
string OS = OperatingSystem.DetectOS();
if( OS == "Darwin" )
{
   System.Reflection.Assembly assemblyMonoMac = System.Reflection.Assembly.Load("MonoMac");
   Type nsdata = assemblyMonoMac.GetType("MonoMac.Foundation.NSData");
   object sounddata = nsdata.GetMethod("FromStream").Invoke( null, new object[] { s });
   sndPlayer = assemblyMonoMac.CreateInstance("MonoMac.AppKit.NSSound",false,System.Reflection.BindingFlags.  CreateInstance,null,new object[] { sounddata }, null, null );
   StartReminders();
}
else
{
   sndPlayer = new SoundPlayer(s);
   (sndPlayer as SoundPlayer).LoadCompleted += Handle_sndPlayer_LoadCompleted;
   (sndPlayer as SoundPlayer).LoadAsync();
}	

if( OS == "Darwin" )
{
   System.Reflection.Assembly assemblyMonoMac = System.Reflection.Assembly.Load("MonoMac");
   Type nssound = assemblyMonoMac.GetType("MonoMac.AppKit.NSSound");
   System.Reflection.MethodInfo methodInfo = nssound.GetMethod("Play");
   methodInfo.Invoke( sndPlayer, new object[] {});
}
else
   (sndPlayer as SoundPlayer).Play();

What’s left to explore is displaying unicode strings, but that’s another topic.

Posted in Uncategorized | Leave a comment

Gtk and MonoMac

I think I discovered a couple of secrets to making MonoMac work with Gtk today. They’re mindbogglingly simple.

The first looks like this:


public static void Main( string[] args )
{
   Application.Init();
#if MAC
   MonoMac.AppKit.NSApplication.Init();
#endif
   new MainWindow();
   Application.Run();
}

The second was to make NSImages turn into Pixbufs. Here’s how:


string fileType = MonoMac.AppKit.NSFileTypeForHFSTypeCode.FinderIcon;
MonoMac.AppKit.NSWorkspace workspace = MonoMac.AppKit.NSWorkspace.SharedWorkspace;
MonoMac.AppKit.NSImage folderNSImage = workspace.IconForFileType( fileType );
MonoMac.CoreGraphics.CGImage folderCGImage = folderNSImage.AsCGImage( System.Drawing.RectangleF.Empty, null, null );
MonoMac.AppKit.NSBitmapImageRep folderImageRep = new MonoMac.AppKit.NSBitmapImageRep(folderCGImage );
MonoMac.Foundation.NSData folderData = folderImageRep.TiffRepresentation;
Gdk.PixbufLoader loader = new Gdk.PixbufLoader( folderData.AsStream() );
pixbuf = loader.Pixbuf;

Easy when you know how.

Posted in Uncategorized | Leave a comment

Learning Cocoa coming from GTK# and WPF

In case you’re wondering why there’s no trading information lately, I’ve been doing work on some of my other applications. All in Gtk#.

Well, I decided to bite the bullet and learn MonoMac. The reason’s somewhat commercial and I’d like to convert some of my applications to work on the iPad and maybe even the iPhone. So I got a Mac. For the second time. And the lovely thing is that my Gtk# applications are running just fine. Mostly.

But when it came to Cocoa, things were a little different. The trouble was that my brand new MiniMac comes with Lion and installs XCode 4. Ouch. That means I can’t easily build nib files, if you know what they are. They connect the visual interface to the classes. And unlike Gtk# or even WPF for that matter you can’t, for all intensive purposes, do the job programmatically. When I asked Kangaroo why? He told me that well, Cocoa on the Mac is 15 years old. Aaah.

So, I spoke to more of the lovely people at Xamarin. They really are lovely. And their advice was to compile the latest master of MonoDevelop. So while I gather my forces to download the latest Master and compile it on my machine I’m going to dig into Objective C a little. The general advice is that you have to at least be able to read Objective C, even if you’re programming in C#.

Then when it’s all going. I’m off to rewrite the UI on one or two of my smaller apps. Maybe I’ll even do OpenTrader for Cocoa, but that ain’t small.

Posted in Uncategorized | Leave a comment

WPF vs Gtk#

A few years ago, I had a large project which I developed using WPF (Windows Presentation Foundation). It’s a great development environment. It’s tidy, development is fast and its flexible. And the ability to design visually is extremely good. Why people develop only for Windows stick to Windows Forms, I don’t know.

OpenTrader, however, I’m developing for Linux and am using Gtk#. It’s got a lovely architecture, but development time is slower, at least for the presentation elements because you’re working at a lower level. And the visual design studio is crap which means that you have to develop the design in code. The advantage is that Gtk# is for Mono which is a cross-platform architecture and thus I can run software in Linux, which I use as a preference over Windows.

I can see the difficulty in developing WPF for Mono and can’t be bothered learning Moonlight – Silverlight’s equivalent in Mono. And quite frankly I can develop a visual interface reasonably rapidly in Gtk#. So, I probably won’t go back.

Another thing I like about WPF is that it does enforce you to separate data and visual design. I particularly liked its use of Observable Collections, to the point where I now separate out the database handling completely from the internal data and from the presentation elements. Should I want to change a database handler I can easily.

C# manages this all very, very well. I like C#, I think Microsoft did a great job. Sun’s bitchiness with Microsoft over Java meant a much better language in C# and IMHO Python is just a mess. The Mono boys have done a great job translating .Net. WPF? Well I learned from it and moved on. Though I won’t be leaving C# any time soon.

Posted in Uncategorized | 1 Comment

A bit of perspective

I spent a few minutes looking at the Dow Jones index yesterday morning. On the 3rd of September, 1929 it had reached a high of 381.17. By the 13th of November it had reached a low of 198.69, but that was just the beginning. The next few years tell a story of things getting much worse. By the 8th of July the index had reached a low of 41.81. It wasn’t until 24th of November 1954 that the indexed reached the high of 1929. That’s 25 years.

Our recent high was on the 9th of October 2007 at 14,164. Only 3 1/2 years later we’re back at 12,000 which was a mid 2006 figure.

Posted in Uncategorized | Leave a comment

Index data

I spent last night, while my wife was out, and this morning I’ve been grabbing the Dow Jones index and oil prices. I managed to get Dow Jones back to 1901, but oil only back to 2006. I’d like to find earlier data for both. Dow constructed the index back in 1884, but where is the data? It’s interesting looking at history through a financial lens.

Luckily I’ve got loads of memory as it takes a bit of a load with the way I’m storing data. I might have to change storage of dataseries to an array rather than as a generic list.

Posted in Uncategorized | Leave a comment

Troubles with oversight

In a fit of “I think it would be great to have the new version of MonoDevelop” last night, all because I thought it would be nice to see CIL on the fly I downloaded the new MonoDevelop, overlooking the warning that you shouldn’t backport Mono. Well it required all and sundy to be installed, which I duly did. What a drama. It’s taken some effort to get Mono and MonoDevelop going again and it’s not perfect, but it’s good enough.

Here were I think the keys, if you run into the same issues.

export MONO_GAC_PREFIX=/usr/local:/usr
sudo dpkg-reconfigure mono-gac

Posted in Uncategorized | Leave a comment