Sunday, April 21, 2013

make_unique in C++14


make_unique

One of the smallest additions is actually great in its impact. It’s make_unique:
  1. auto u = make_unique<some_type>( constructor, parameters, here );
The reason make_unique has important impact is that now we can teach C++ developers to mostly never use explicit new again. In C++11 we already could teach to never use raw pointers and explicit delete again, except in rare cases that are hidden inside a class in order to do something like implement a low-level data structure. However, we could not teach to never write new because although make_shared was provided to create a shared_ptrnewwas still needed to create a unique_ptr. Now, instead of “new”, write make_unique or make_shared.
With draft C++14, we can say simply: Don’t use raw pointers, new and delete, except rarely when implementing low-level data structures. Allocate with make_unique or make_shared, use weak_ptr where appropriate to break cycles, and don’t worry about dangling pointers in C++ again.


From http://isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting

Sunday, April 14, 2013

Difference between NUL and NULL in C

From "Expert C programming - Deep C secrets" book:

The one "l" NUL ends an ASCII string,
Two two "l" NULL points to no thing.

If you check your include files, you will see definition of null as

#define NULL ((void*)0)

while NUL is first symbol in ASCII table (with value of 0).

Scrolling QScrollArea causes other widgets flickering

Today I've experienced a problem with QScrollArea which was placed besides another widget, like this:

 ________________    ______________
|                |  |              |
|                |  |              |
|                |  |              |
|                |  |              |
|   QWidget      |  |  QScrollArea |
|                |  |              |
|                |  |              |
|                |  |              |
 ----------------    --------------

Now, when my QScrollArea was scrolled into left, that caused part of QWidget that was overlapping with QScrollArea content to flicker.

To avoid this behavior, before scrolling (I am scrolling QScrollArea with buttons, but I guess connecting to the sliderPressed() and sliderRelased() of horizontalScrollBar and verticalScrollBar() should do the trick), you need to hide QScrollArea:

Monday, November 19, 2012

How to remove android NDK support from eclipse project

Today I've got stuck with pretty much weird errors in eclipse while trying to use ccache and android ndk-build. 

Apart from the fact that compiling was not successful, eclipse showed me dozens of errors which were complete nonsense. However, when I tried to compile project in cygwin console by calling ndk-build, everything well smoothly (well, not at the first time - see my next posts if you are having "Permission denied" error for accessing to some prebuilt libraries, or if you are having problems with undefined classes which are defined in std:: namespace).  

But, if I tried to run program in eclipse - project run failed because of NDK errors. So, I've found a simple solution:

a) All NDK code build using command line and ndk-build all.
b) Close eclipse, open project root, and delete .cproject file - you will remove C++ support from the project
c) Use eclipse for everything else.


Thursday, October 4, 2012

Where to keep Android application files

If you need to store configuration data, some resources, etc. in Android SDK, you should use assets folder for so. To access anything from it use AssetManager:


Activity.getAssets().open("resource.bmp");

Also, take a note that assets are read-only.

Prevent form to blink when gets activate

In my previous post, I was telling you how to run one instance of the application and how to capture command line parameters from next runs. There were one problem, though - on every run, form was getting activate message and window started to blink. To resolve this, you need to handle WM_NCACTIVATE message to ignore Activate message:


protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_NCACTIVATE)
    {
        // Use this to make it always look inactive:
        m.WParam = (IntPtr)0;

        // Alternately, use this to make it always look active:
        m.WParam = (IntPtr)1;
    }

    base.WndProc(ref m);
}

[C#] How to run single instance application and to allow other instances to notify them

Today I wrote simple timer module for 3rd party application. Problem was that 3rd party application allowed only running other executables and, optionally, passing it command line arguments. I've designed simple timer interface in C# and I got an idea that I would allow only one application instance (I only need one timer) and for controls (start/stop) I will use other instances which will notify running instance before they close itself. I've found first solution on CodeProject that worked very well, but for some reason it kept failing on Marshal.Copy call due AccessViolationException. I didn't had time to check why is that, so I've grabbed next snippet of code, and it worked! This solution is using Microsoft.VisualBasic .NET library and WindowsFormsApplicationBase class. Apparently, this approach is using Mutexes and TCP channels internally to implement this, so if you are interested, there is a link on the CodeProject article which explains this (you should read this, btw).

Ok, here is some sample code:

Running 
Your main class needs to extend WindowsFormsApplicationBase class, and you need to call Run(); method of it in Main() function:

/// 
/// We inherit from the VB.NET WindowsFormApplicationBase class, which has the 
/// single-instance functionality.
/// 
class App : WindowsFormsApplicationBase
{
 public App()
 {
  // Make this a single-instance application
  this.IsSingleInstance = true; 
  this.EnableVisualStyles = true;
  
  // There are some other things available in the VB application model, for
  // instance the shutdown style:
  this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses; 

  // Add StartupNextInstance handler
  this.StartupNextInstance += new StartupNextInstanceEventHandler(this.SIApp_StartupNextInstance);
 }
 
 static void Main(string[] args)
 {
  App myApp = new App();
  myApp.Run(args);
 }

Now, we need to override OnCreateMainForm() function to create our MainForm:

protected override void OnCreateMainForm()
{
 // Create an instance of the main form and set it in the application; 
 // but don't try to run it.
 this.MainForm = new MainForm();

 // We want to pass along the command-line arguments to this first instance

 // Allocate room in our string array
 ((MainForm)this.MainForm).Args = new string[this.CommandLineArgs.Count];

 // And copy the arguments over to our form
 this.CommandLineArgs.CopyTo(((MainForm)this.MainForm).Args, 0);
}

Last thing is setting application to pass command line arguments to our form when second instance is run:

protected void SIApp_StartupNextInstance(object sender, 
                StartupNextInstanceEventArgs eventArgs)
{
 // Copy the arguments to a string array
 string[] args = new string[eventArgs.CommandLine.Count];
 eventArgs.CommandLine.CopyTo(args, 0);

 // Create an argument array for the Invoke method
 object[] parameters = new object[2];
 parameters[0] = this.MainForm;
 parameters[1] = args;

 // Need to use invoke to b/c this is being called from another thread.
 this.MainForm.Invoke(new MainForm.ProcessParametersDelegate(
  ((MainForm)this.MainForm).ProcessParameters), 
  parameters );
}

Of course, you need to have proper delegate type in your form declared, and implemented target function for processing parameters:



public delegate void ProcessParametersDelegate(object sender, string[] args);
public void ProcessParameters(object sender, string[] args){ /* ... */ }