Posts Tagged ‘c++’

Alchemy….

// November 20th, 2008 // Comments Off on Alchemy…. // .net, c++, RIA

I have not really been a follower of Macromedia (now part of Adobe) or its products; specially Flash, but I just found out about one research project that does capture my attention: Alchemy.

From the press release:

Alchemy is a research project that allows users to compile C and C++ code that is targeted to run on the open source ActionScript Virtual Machine (AVM2). The purpose of this preview is to assess the level of community interest in reusing existing C and C++ libraries in Web applications that run on Adobe® Flash® Player and Adobe AIR®.

This project promises that C/C++ libraries that have no OS dependencies can be run on the Adobe AIR platform. This is HUGE. Just to think that at least 90% of my libraries can be used in Web Applications is awesome.

A not so direct competitor, Microsoft’s Silverlight, does provide a way to reuse C++ libraries via C++/CLI, and it works like a charm; I happend to have a lot of C++ libraries  which have valuable code. Lately I have been reusing those libraries in Web Applications via C++/CLI and WebServices. It works, and it works fine.

I have not worked with Silverlight 2.0 yet; I have played around with it, but I have not done anything that is worth mentioning. In my tests, I see Ican also reuse those C++ libraries with C++/CLI and Webservices. The problem is that this approach will only work on Windows, since C++/CLI is not supported by Mono.

Another approach would be to make the WebServices in native C++, and call it from anywhere: Silverlight, Javascript and .Net. This would work anywhere.

This doesn’t take away the credit for Alchemy, which makes the C++ libraries natively accessible to Adobe AIR applications, in a way this is very close to what C++/CLI does.

The purpose of the Alchemy project is :

Alchemy is primarily intended to be used with C/C++ libraries that have few operating system dependencies. Ideally suited for computation-intensive use cases, such as audio/video transcoding, data manipulation, XML parsing, cryptographic functions or physics simulation, performance can be considerably faster than ActionScript 3.0 and anywhere from 2-10x slower than native C/C++ code. Alchemy is not intended for general development of SWF applications using C/C++.

Comparing it with C++/CLI, I do see an advatage from using C++/CLI to reuse C++ libraries in RIA applications over the Alchemy project: I can reuse .Net libraries with C++ native code, which gives me the best of both worlds.

I did some research on Alchemy, and it seems it doesn’t allow Flex code to be embedded with C++ native code. My research was not very comprehensive to say the least, so I could be wrong. It could also be too soon to tell since this is still in early stages of research and development.

In conclusion, this project seems very interesting, and I’m going to keep an eye out for it.

State of the Language: C++0x

// August 21st, 2008 // Comments Off on State of the Language: C++0x // c++

I just found this interview with Bjarne Stroustrup in DevX about the current state of the new C++ spec recently tagged as C++0x, and now is known as C++09.

According to the interview, these are just some of the improvements the spec will contain:

Concurrency:

  • memory model supporting modern machine architectures
  • Threading ABI
  • atomic types
  • mutexes and locks
  • thread local storage
  • asynchronous message exchange

Libraries:

  • regex: regular expressions
  • unordered_map, etc. (hash tables)
  • smart pointers
  • array: fixed-sized array
  • improvements to containers based on new C++0x features
  • tuples
  • date and time (maybe)
  • various library components to held library builders

Language:

  • rvalue references (move semantics)
  • static_assert: static assertions
  • variadic templates
  • strongly typed enumerations with scoped enumerators
  • constexpr: generalized constant expressions
  • control of alignment
  • delegating and inheriting constructors
  • auto: deducing a type from an initializer
  • decltype: a way of using the type of an expression in a declaration
  • control of defaults
  • nullptr: a name for the null pointer
  • a range-based for loop
  • lambda functions
  • raw string literals
  • UTF8 literals
  • concepts (a type system for template arguments)
  • initializer lists and uniform initializations syntax and semantics
  • in-class member initializers

Among others. Go and read the interview, it is very very interesting.

OpenGL 3.0 Spec Released Today

// August 11th, 2008 // 2 Comments » // directx, linux, OpenGL, software development

I am completely disappointed and utterly depressed. It is sad to see that a very important framework (at least for me) is going to hell. I’m sorry to say that the big problem in the world is not really proprietary software. Something I obviously learned way before, and that is why I left Linux. They all tend to disappoint. Gnome is a perfect example. It sucks, big time. And all of you open source zealots can say whatever you want, but proprietary software is not the problem.

Today, the OpenGL v3.0 spec was released, and it was a complete disappointment (worse than version 2.0). Not even one feature that was promised nearly two years ago is in the spec for version 3.0. The Khronos Group has failed to provide a worthy competitor to DirectX. And, you were right Mr.Stallman, Gates is not the problem, but it isn’t Microsoft or proprietary software. That I’m sure of. DirectX is a really great API, impressively clean and well designed, where OpenGL has been failing since version 2.0 to compete with even a shed of proof of a modern GL API. OpenGL is still poorly organized and it looks a lot more like a bunch stuff thrown under the bed.

While reading through the forums, which I have followed relentlessly, I can only find disappointment spreading through the community. The situation is so serious that people are even thinking on migrating to Vista just to get to work with the latest DirectX version.

I am sorry to say, that while assessing the situation, I am also going to leave OpenGL completly in favor of DirectX. I left Linux a while back because of the same feelings of disappointment, and I guess history repeats itself.

I hope, that with the recent changes in the Emacs maintenance team, the application keeps growing and moving towards the future. I would really feel helplessly depressed if Emacs turned out to be just like Gnome and OpenGL.

Today I mark the death of OpenGL for me, and any hopes of writing Games for Linux. I guess Linux was never meant for me, everything about it keeps disappointing me. (And yes I did see OpenGL as a bridge for me to work in Linux again, I wonder if there are enough bridges for me to keep Linux from drifting farther away from me).

This is indeed a sad, sad day.

RegEx Pre-Compiler

// February 28th, 2008 // Comments Off on RegEx Pre-Compiler // .net, c++, software development

I have silently added a Software tab in this site, which will contain all the applications that I want to release for free to the public. I am still deciding if I’m going to include the source code. I’ll probably will, but for now only binaries will be there.

The first application is the RegexCompiler. This is a powerful tool that makes it easy to build common regular expression into .Net assemblies. Such assemblies can be used to organize and distribute commonly used regular expressions; in some cases, it even improves performance of execution of regular expressions.

This application was built for a very regex intensive application, and I wanted to have such expressions interchangeable using DLLs for my application. In some cases, if the expression is to big, it improves the performance of the regular expression, but this is not always the case.

This is an example of the final usage of the Compiled Expressions:

SelectPattern pattern = new SelectPattern();
bool isMatch = pattern.IsMatch(""select * from table1"");

Patterns are stored in an xml file with extension *.recproj. The application provides an example which is:

< ?xml version="1.0" encoding="utf-8"?>
<recproject name="Structum.Patterns.Data">
  <assemblyfilename>Structum.Patterns.Data</assemblyfilename>
  <namespace>Structum.Patterns.Data</namespace>
  <regex name="SelectPattern">(?ism)"[ t]?.*selectb(?-ism)</regex>
  <regex name="InsertPattern">(?ism)"[ t]?.*insertb(?-ism)</regex>
  <regex name="DeletePattern">(?ism)"[ t]?.*deleteb(?-ism)</regex>
  <regex name="UpdatePattern">(?ism)"[ t]?.*updateb(?-ism)</regex>
</recproject>

The whole file represents an assembly and it is identified by the ‘assemblyfilename’.
Each ‘regex’ tag contains the name of the Class and the actual Regular Expression. In the case of the Select Pattern:

  <regex name="SelectPattern">(?ism)"[ t]?.*selectb(?-ism)</regex>

Which is looking for expressions of the form: “select * from table”. It was used to identify select statements in code.

Also, this is my first application with C++/CLI. I’m a hardcore C++ fan, and I wanted to try the CLI version. While building the application, I was trying to have a more .Net mindset than C++, so that I could really learn the language. I have to say it was a really cool experience. It has been extremely useful for me, I hope it is also helpful for everybody else.

You can download RegEx Compiler in here.

A sample project can be found here.

Elegant Code vs Skanky Code

// February 23rd, 2007 // 9 Comments » // .net, c++, software development

I have been looking for a description of elegant code, and summing up everything I have learned and seen, I compiled a list of examples of what elegant code blocks look like. Now, the actual definition of “Elegant Code” is up to the reader, since there are so many different definitions. Each developer has its own very personal definition. I have even seen comparing the feeling of elegant code to beautiful code, and both are completely subjective. My own personal definition is: “Elegant code is a block of code that does a lot with little effort.” Performance is an issue for me, but it is not required for a piece of code to be elegant.

Disclaimer: These examples are not the only ones, but I think they build up a good idea of what elegant code actually is.

* First example:

Skanky Code

if(0 == x) {
   y = 0;
} else {
   y = 1;
}

Elegant Code

y = (0 < x);

It is more elegant, because it is easier to read (it is only one line) it is faster and way cleaner.

* Let’s see another example:

Skanky Code:

x = y * 256;

Elegant Code:

x = y < < 8;

This piece of code is not about lines length, is about performance. The second one, done using bitwise operations is faster, hence more elegant.

UPDATE: It is no secret that doing bitwise operation to multiply by powers of two is faster. Nowadays, modern compilers already do this optimization before hand. So now when you write: x = y * 16 the compiler transforms that as x = y < < 4. Now, compilers have gotten so clever, that when you do y * 17 the compiler generates it as (y < < 4) + y. Even if the compilers now does this optimization, doesn't take away the merit for this code for being elegant, even if the performance with modern compilers is the same.

* Another example:

Skanky Code

if(null == x) {
  y = 0;
} else {
  y = x;
}

Elegant Code

y = (x ?? 0);

* Last example :

Skanky Code

if(&quot;sss&quot; == x) {
    y = &quot;aaa&quot;;
} else {
    y = &quot;bbb&quot;;
}

Elegant Code

y = ( &quot;sss&quot; == x ? &quot;aaa&quot; : &quot;bbb&quot;);

If you know any more examples and want to share them, please do so…