C++11 added a date and time utility library called chrono, available in namespace std::chrono and header <chrono>. The problem with it is that the library is a general purpose one and therefore lacks many useful features, such as working with dates, weeks, calendars, timezones and other related features. Fortunately, a rich date and time library based on chrono has been created by Howard Hinnant and is available on github. The library is called date and is actually a collection of several small libraries:

  • date: the main library, available in header date.h, defines new date and time classes and operations with them. All the other libraries are based on this one.
  • timezones: a library for timezones, available in files tz.h/tz.cpp, based on the IANA timezone database
  • chrono_io: a library for streaming durations, available in header chrono_io.h
  • iso_week: a library that implements the ISO week calendar, available in header iso_week.h
  • julian and islamic: libraries that implement the Julian and Islamic calendars, available in headers julian.h and islamic.h

You can find all the necessary documentation on github. Here are several links:

In this article we will look at some examples for working with dates and ISO weeks. This library introduces many new types to handle various date and time representations. Among these we will look at:

  • sys_days: A count of days since std::system_clock‘s epoch. This is a time_point with a resolution of a day, and is implicitly convertible to std::system_clock::time_point, that has a much smaller resolution (millisecond or nanosecond), but not the other way around. To go the other way you must use floor().
  • year_month_day: A type that holds a day with fields for year, month (1 to 12) and day (1 to 31).
  • year_month_weekday: A type that holds a day with fields for year, month (1 to 12), a day of the week (0 to 6), and an index in the range [1, 5] that indicates the number of the week in the month.
  • year_weeknum_weekday: A type that hold a year, a weeknum (1 to 53) and a weekday (0 to 6). This can convert implicitly to and from a sys_days.

For using the library we need the following:

  • include header date.h and namespaces date and date::literals
  • for iso weeks we also need header iso_week.h and namespaces iso_week and iso_week::literals
  • NOTICE: The namespaces date::literals and iso_week::literals define types and literal operators with the same name and therefore can lead to name collisions; therefore you should only include them in the scope where you need them.

We will use the following lambda expression to print various dates to the console:

NOTICE: All the ‘today’ and related dates below are based on 2016-10-31.

Let us look at some examples:

  • create sys_days objects (including literals):

  • create year_month_day objects (including literals):

  • creating year_month_weekday literals and converting to year_month_day

  • create year_month_day values for today, yesterday and tomorrow

  • create year_month_day values for first and last day of the month

    Update: The following, as indicated by Howard Hinnant in the comments, can also be used:

  • create iso_week literals

  • get the iso week number for today

We will look at more utilities and examples in another post.

, , , , , , Hits for this post: 5454 .

Many years ago I published on my blog a helper class for working with the Windows console that was wrapping the Windows console API. Looking back at it I realized it was a pretty naive implementation. So I decided to start a new and make something more flexible and easier to use. Hopefully, I was more successful. The result is a small C++ template library called cppconlib, available on codeplex.

cppconlib is built with C++11 features and requires Visual Studio 2012 or newer. The library is available in a single header called conmanip.h and provides a set of helper classes, functions and constants for manipulating a Windows console (using the Windows console functions). The library features the following components:

  • console_context<T>: represents a context object for console operations; its main purpose is restoring console settings; typedefs for the three consoles are available (console_in_context, console_out_context and console_err_context)
  • console<T>: represents a console objects providing operations such as changing the foreground and background colors, the input mode, screen buffer size, title, and others; typedefs for the three consoles are available (console_in, console_out and console_err)
  • manipulating functions that can be used with cout/wcout and cin/wcin: settextcolor()/restoretextcolor(), setbgcolor()/restorebgcolor(), setcolors(), setmode()/clearmode(), setposx()/setposy()/setpos().

The library can be downloaded from here. Detailed documentation is available here.

cppconlib

Examples

The following example prints some text in custom colors and then reads text in a different set of colors.

cppconlib2

The following code prints a rhomb to the console:

cppconlib3

For more details and updates check the project at codeplex: https://cppconlib.codeplex.com.

UPDATE: A NuGet package for cppconlib is available.

, , , , , , , Hits for this post: 35980 .

NuGet has recently added support for native projects. This simplifies a lot deployment of native libraries. Even though cpplinq is not a big library (in fact is just a header file) I have created a NuGet package so that you are able to automatically add it to your project.

Here is what you have to do.

  1. Make sure you have NuGet 2.5 or newer, otherwise the NuGet package manager won’t show up in your VC++ projects.
  2. In the context menu for your project choose Manage NuGet Packages…
    cpplinqdemo1
  3. Search for cpplinq and install the package.
    cpplinqdemo2
  4. Include the cpplinq.hpp header and start using the library. Here is a sample to test that everything is all right.

Notice that all the settings for library (such as adding the proper entry for the include directories or defining NOMINMAX so that min and max macros will not be defined for the project) are automatically performed, so you can focus on coding.

, , , , , , , Hits for this post: 39922 .

cpplinq new operators

There are some new operators available in cpplinq: singleton, generate, pairwise and zip_with.

singleton
This operator create a range with a single element, specified as argument. Using the operator is straight forward.

generate
This operator creates a range using a given predicate.

pairwise
Generates a new range of pairs by grouping adjacent elements of an input range. On an empty range it yields an empty range.

zip_with
Generates a new range of pairs by grouping together elements from two different ranges. If the two input ranges have different sizes, the result has the size of the smallest range. If any of the input ranges is empty, the result is an empty range.

, , , , Hits for this post: 22788 .

cpplinq: set operators

In the previous posts I introduced cpplinq, a C++ template library that provides .NET-like query operators for sequences of objects in C++11. In this third installment I will discuss the set operators the library provides.

There are four set operators: distinct, union_with (called so because union is a keyword in C++), intersect_with (suffix _with is for consistency with the union operator) and except. These operators should be pretty straight forward: distinct eliminates the duplicate elements from a sequence, yielding a new sequence with only the distinct elements (in the order their original order), union_with produces the set union of two sequences, intersect_with produces the set intersection of two sequences and except produces the set difference of two sequences.

Before seeing some examples, it worth nothing that all these operators delay the traversal of the sequences until the resulting object is enumerated.

Let’s see some examples:

You can learn more about these operators (and the others that are implemented) by reading the cpplinq query operators documentation.

, , , , , Hits for this post: 29579 .

cpplinq: An introduction

cpplinq is a C++ template library that provides .NET-like query operators for sequences of objects in C++11. cpplinq is an open-source library that works both with VC++ compilers (2010 and 2012) and gcc (4.7.0 is the version used for unit tests). The library supports most of the .NET query operators and is extendable with additional operators that can suite some particular use. If you’re not familiar with .NET query operators, this article, http://msdn.microsoft.com/en-us/library/bb394939.aspx, lists and explain them.

The light-weighted library consists of a series of template classes and helper methods, provided in a single header under the namespace cpplinq.

This is a short introduction to the library. More posts will follow. Let’s see some examples.

The following code retrieves all the prime numbers from an array.

A slightly variation produces a sequence of first N prime numbers and transforms them into strings.

The next sample retrieves all the orders from a customer.

Callinq print_orders_by_cust() with argument 1 would print:

On the other hand, if you want to print the last order from a particular customer, the function above would change to something like this:

Calling print_last_order_for_customer() with argument 1 would print:

Hopefully this provides a quick view over the capabilities of the library. In following posts I will show more examples and discuss some of the query operators. To learn more about the library see the following articles:

, , , , , Hits for this post: 35599 .

Microsoft has recently announced at the AMD Fusion Developer Summit the introduction of a new technology called C++ Accelerated Massive Parallelism or shortly C++ AMP that helps C++ developers use the GPU for parallel programming. The new technology will be part of Visual C++, integrated with full support (edit, build, debug, profile) in the next version of Visual Studio. It is built in modern C++ on top of DirectX. It will provide an STL-like library, part of the Concurrency namespace, delivered in a amp.h header.

S.Somasegar (Senior Vice President of the Developer Division) said:

By building on the Windows DirectX platform, our implementation of C++ AMP allows you to target hardware from all the major hardware vendors. We expect that it will be part of the next Visual C++ compiler and fully integrated in the next release of Visual Studio experience.

You can read more about it here:

Looking forward for the first CTP and samples.

UPDATE
Herb Sutter introduces C++ AMP at the AMD Fusion Developer Summit 11

Daniel Moth digs deeper into C++ AMP with code samples and more

Herb Sutter

How long is your password? How long it will take a 100,000,000 GPU cores running at what, a million attempts per second to crack your password just by brute force? That where almost a kid can write that. An that is just a tiny example of how game changing this is.

, , , , Hits for this post: 42002 .

DotNetZip Library

.NET 3.0 provides some support for working with ZIP files. However, it has an important drawback: it only works for packages that are conformant to the Open Packaging Convention standard. Most of the ZIP files are not. Codeplex features a library called DotNetZip that provides support for packing and unpacking in C#, VB.NET or any other .NET language, but also any COM environment, including Javascript, VBSCript, VB6, VBA, PHP, Perl. In addition to the basic packing and unpacking operations, it supports password protection, UNICODE filenames, ZIP64 and AES encryption, comment, and others. Here are some simple samples.

Creating a ZIP file:

Unpacking to a target folder:

Display the content of a ZIP archive:

You can find many more examples on Codeplex: C# and VB.NET.

, , , , Hits for this post: 33590 .

MSDN Code Gallery made available an update for the Windows API Code Pack for .NET Framework 3.5 (or above), a library that provides access to some Window 7 features and some existing features in previous operating systems. It includes:

  • Windows 7 Taskbar Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars.
  • Known Folders, Windows 7 Libraries, non-file system containers, and a hierarchy of Shell Namespace entities.
  • Windows 7 Explorer Browser Control.
  • Shell property system.
  • Windows Vista and Windows 7 Common File Dialogs, including custom controls.
  • Windows Vista and Windows 7 Task Dialogs.
  • Direct3D 11.0, Direct3D 10.1/10.0, DXGI 1.0/1.1, Direct2D 1.0, DirectWrite, Windows Imaging Component (WIC) APIs. (DirectWrite and WIC have partial support)
  • Sensor Platform APIs
  • Extended Linguistic Services APIs
  • Power Management APIs
  • Application Restart and Recovery APIs
  • Network List Manager APIs
  • ommand Link control and System defined Shell icons.

The requirements for using this library are:

  • .NET Framework 3.5
  • Windows 7 RC (some features work on previous operating systems too)
  • DirectX features have dependency on Windows SDK for Windows 7 RC and March 2009 release of DirectX SDK

You can download the Code Pack library from here.

, , , Hits for this post: 35531 .