Thursday, May 27, 2010

Visual Studio (VS) 2005 IDE for IronPython (Python) - Yang Li's blog - Site Home - MSDN Blogs

Visual Studio (VS) 2005 IDE for IronPython (Python) - Yang Li's blog - Site Home - MSDN Blogs

Provides the steps for integrating IronPython into Visual Studio 2005.

Sandcastle (software) - Wikipedia, the free encyclopedia

Sandcastle (software) - Wikipedia, the free encyclopedia

Sandcastle is a documentation generator from Microsoft that automatically produces MSDN style reference documentation out of reflection information of .NET assemblies and XML documentation comments found in the source code of these assemblies. It can also be used to produce compiled user documentation from Microsoft Assistance Markup Language with the same look and feel as reference documentation.

Thursday, May 20, 2010

Pretty printing boolean values in C++

By default boolean values (type bool, values true and false) are printed as 1 and 0 in C++ iostreams. There is a simple way to print them as their names (true/false). The following code-example demonstrates how to do that using boolalpha manipulator.

  1: #include <iostream>
  2: using namespace std;
  3: 
  4: int main()
  5: {
  6:     // By default true prints as 1 and false prints as 0
  7:     cout << true << " " <<  false << endl;
  8:     // We can enable boolalpha formatting flag 
  9:     cout << boolalpha;
 10:     // Now true and false will be inserted as their names instead
 11:     // of integral values
 12:     cout << true << " " <<  false << endl;
 13:     // We can later on unset boolalpha with noboolalpha format manipulator
 14:     cout << noboolalpha << endl;
 15:     cout << true << " " <<  false << endl;
 16:     // The boolalpha flag is not set in standard streams on initialization
 17:     return 0;
 18: }
 19: 
 20: 


The output of the program looks as follows:



  1: 1 0
  2: true false
  3: 1 0


We can notice that on the first line the output is integral. After setting boolalpha, the output becomes textual. Later on the output becomes integral again after setting noboolalpha.



Input streams



The manipulator can similarly be used on input streams for reading boolean values as true/false or 1/0.

Wednesday, May 19, 2010

Adding custom draw functionality to an MFC CButton

We will start by defining a subclass CBDButton to the MFC class CButton.


  1: class CBDButton : public CButton
  2: {
  3: public:
  4:     // overwrite void DrawItem function and draw your button
  5:     void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  6: };
  7: 


As you can notice above, we have overridden one function DrawItem in this class. Lets now look at its implementation.



 



  1: void CBDButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  2: {
  3:     // TODO: Add your code to draw the specified item
  4:     UINT uStyle = DFCS_BUTTONPUSH;
  5: 
  6:     // This code only works with buttons.
  7:     ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
  8: 
  9:     // If drawing selected, add the pushed style to DrawFrameControl.
 10:     if (lpDrawItemStruct->itemState & ODS_SELECTED)
 11:     {
 12:         uStyle |= DFCS_PUSHED;
 13:     }
 14: 
 15:     CDC dc;
 16:     dc.Attach(lpDrawItemStruct->hDC);
 17: 
 18:     // Draw the button frame.
 19:     dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);
 20: 
 21:     CRect rect = lpDrawItemStruct->rcItem;
 22:     rect.left = rect.left + 1;
 23:     rect.right = rect.right - 2;
 24:     rect.top = rect.left + 1;
 25:     rect.bottom = rect.bottom - 2;
 26:     dc.FillSolidRect(rect, m_clrBk);
 27: 
 28:     // Get the button's text.
 29:     CString strText;
 30:     GetWindowText(strText);
 31:     // Draw the button text using the text color.
 32:     COLORREF crOldColor = dc.SetTextColor(m_crText);
 33:     dc.DrawText(strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
 34:     dc.SetTextColor(crOldColor);
 35: 
 36:     dc.Detach();
 37: }

Wednesday, May 5, 2010

Testing windows live writer

Windows Live Writer is expected to help me write source code examples easily and post them to my blog seamlessly. In this post I am trying to test its functions.

Here is some C++ code:

  1: class A
  2: {
  3: public:
  4:     A();
  5:     ~A();
  6:     void f();
  7: };
  8: 
  9: // an example of implementation inheritance
 10: class B : private A
 11: {
 12: public:
 13:     B();
 14:     ~B();
 15:    void g();
 16: };
 17: 
 18: 


Following is some Python code:



  1: import os
  2: import sys
  3: 
  4: def main():
  5:     filepath = sys.argv[1]
  6:     contents = open(filepath).readlines()
  7:     print 'Number of lines: ' , len(contents)
  8: 
  9: if __name__ == '__main__':
 10:     main()


A Haskell code example:



  1: double:: Integer -> Integer
  2: double x = x + x
  3: 
  4: square :: Integer -> Integer
  5: square x = x * x
  6: 
  7: main = do
  8:          print $ square 10
  9:          print $ double 20
 10: 
 11: 


Some JavaScript:



  1: fucntion f()
  2: {
  3:      alert('Hello World');
  4: }
  5: 


 



Some HTML:



  1: <html>
  2:   <head>
  3:     <title>Hello World </title>
  4:   </head>
  5:   <body>
  6:     <p> Don't Panic </p>
  7:   </body>
  8: </html>
  9: 


Some CSS:



  1: .header
  2: {
  3:   forground-color: black;
  4:   background-color: red;
  5:   text-size: 20;
  6: }
  7: 

I guess this is more than enough for me. I am happy it works nicely.

Tuesday, May 4, 2010

Side effects and functional programming

What are side effects in programming?

When we call a function in a typical programming language (C, C++, Java, Python, etc.), the function gets some input arguments and returns an output value.

  • The function has some local variables which are lost after the function execution completes (no side effect)
  • The function may compute a value which it returns (no side effect)
  • If the arguments are references/pointers to some objects, the function may modify them (a side effect to the input arguments)
  • The function may modify some global variable (a side effect)
  • The function may output something on output or to a file or a socket (a side effect to the outside world with which the program is associated)
  • The function may read something from input or from a file or a socket (a side effect to the outside world with which the program is associated)
  • A member function may alter the state of the object with which it is associated (a side effect)

Essentially if the function is touching anything apart from computing a simple result and returning it, then the function is causing some form of side effect.

What is functional programming?

Functional programming is a programming paradigm where functions are free from side effects. i.e. they take some input values as arguments and generate an output value as result and do nothing else. They don't touch anything in the outside world.

This approach is useful since this allows for easy composition of functions to create more powerful functionality without worrying about any side effects. Since the functions don't have any side effects, hence their behavior are well understood and combining them becomes straight-forward. If you think about it, side-effect free functions are very close to mathematical functions (a mapping from a domain to a range thats all).

Can we live without side effects?

No. All kinds of IO is side effects. Most useful software requires some form of IO (files, networks, databases, GUIs, you name it). Hence a programming paradigm which doesn't support side-effects, is pretty much useless.

How functional programming works then?

So here is the trick. A well designed functional programming language has two parts. One part focuses on side effects (its a small part), while the rest of the language provides extensive support for pure functional programming (side-effect free). The program design focuses on clear separation between side-effect free computations and side-effect full user/system interfaces. The full power of techniques like currying, higher order functions, lazy evaluation, etc. is utilized in the functional part.

An example?

Here is a simple example which shows how to compute the square of a given number (in Haskell).

square x = x * x

main = print $ square 4

square is a function which computes the square of a given number. This is a side-effect free function. main uses this function to compute the square of four and print on standard output. main is side-effect full.

The language requires that effect free functions cannot call effect full functions while effect full functions can call other effect full functions as well as effect free functions. Thus the effect free part of the program is completely segregated.




Are C++ Raw Pointers First Class Objects?

I had this doubt nagging me for a long time whether C++ raw pointers are first class objects or not. Wikipedia had a certain definition about it and C++ raw pointers seemed to satisfy all the requirements. PPD (our Head of Engineering) had taken a lecture on RAII (Resource Acquisition Is Initialization) principle some time back and in that context he had suggested that C++ raw pointers are not first class objects. I wasn't fully sure whether I understood this well or not. So I raised this question on Stack Overflow (http://stackoverflow.com/questions/2695169/are-raw-c-pointers-first-class-objects).

It turned out that different people had different opinions about it. Some even trashed the Wikipedia definition. Finally I got PPD also on this discussion to provide a clear perspective on this. His answer can be seen from the above post in detail.

In short both "Pointers are first class objects" and "Pointers are not first class objects" statements are true. It depends purely on the context in which we are discussing about them.
If we are using the pointers merely to point to some data (some object) then they are first class object. If we are using the pointers to hold the data (hence have an ownership semantics attached to them) then they are not first class objects.

For more in depth explanation, read on the stack overflow link above.