background preloader

Boost

Facebook Twitter

Install boost 1.53 on ubuntu by package manager. Smart Pointers to boost your code. Download source files - 45.3 Kb Contents Smart Pointers can greatly simplify C++ development.

Smart Pointers to boost your code

Chiefly, they provide automatic memory management close to more restrictive languages (like C# or VB), but there is much more they can do. I already know smart pointers, but why should I use boost? What are Smart Pointers? The name should already give it away: A Smart Pointer is a C++ object that acts like a pointer, but additionally deletes the object when it is no longer needed. "No longer needed" is hard to define, since resource management in C++ is very complex. Many libraries provide smart pointer implementations with different advantages and drawbacks.

Boost provides the following smart pointer implementations: Let's start with the simplest one: The first: boost::scoped_ptr<T> scoped_ptr is the simplest smart pointer provided by boost. A note on the samples:The samples use a helper class, CSample, that prints diagnostic messages when it it constructed, assigned, or destroyed. Integrating with Boost.Asio. The urdl::read_stream class allows applications to use Urdl's functionality in conjunction with Boost.Asio.

Integrating with Boost.Asio

To synchronously open a URL, we may use: #include <urdl/read_stream.hpp> ... boost::asio::io_service io_service; ... urdl::read_stream stream(io_service); stream.open(" ... boost::system::error_code ec;stream.open(" ec); To asynchronously open a URL, we can write: and the callback function open_handler will be invoked once the asynchronous operation completes. Tutorial - 1.53. Boost.Function has two syntactical forms: the preferred form and the portable form.

Tutorial - 1.53

The preferred form fits more closely with the C++ language and reduces the number of separate template parameters that need to be considered, often improving readability; however, the preferred form is not supported on all platforms due to compiler bugs. The compatible form will work on all compilers supported by Boost.Function. Consult the table below to determine which syntactic form to use for your compiler.

If your compiler does not appear in this list, please try the preferred syntax and report your results to the Boost list so that we can keep this table up-to-date. A function wrapper is defined simply by instantiating the function class template with the desired return type and argument types, formulated as a C++ function type. By default, function object wrappers are empty, so we can create a function object to assign to f: f = int_div(); std::cout << f(5, 3) << std::endl; Buffer. The boost::asio::buffer function is used to create a buffer object to represent raw memory, an array of POD elements, a vector of POD elements, or a std::string.

buffer

A buffer object represents a contiguous region of memory as a 2-tuple consisting of a pointer and size in bytes. A tuple of the form {void*, size_t} specifies a mutable (modifiable) region of memory. Similarly, a tuple of the form {const void*, size_t} specifies a const (non-modifiable) region of memory. These two forms correspond to the classes mutable_buffer and const_buffer, respectively. To mirror C++'s conversion rules, a mutable_buffer is implicitly convertible to a const_buffer, and the opposite conversion is not permitted. The simplest use case involves reading or writing a single buffer of a specified size: The Boost C++ Libraries. A guide to getting started with boost. Bind.hpp documentation - 1.53. Contents Purpose Using bind with functions and function pointers Using bind with function objects Using bind with pointers to members Using nested binds for function composition Overloaded operators Examples Using bind with standard algorithms.

bind.hpp documentation - 1.53

Daytime.1 - A synchronous TCP daytime client - 1.53. This tutorial program shows how to use asio to implement a client application with TCP.

Daytime.1 - A synchronous TCP daytime client - 1.53

Tutorial - 1.40. Basic Skills The tutorial programs in this first section introduce the fundamental concepts required to use the asio toolkit.

Tutorial - 1.40

Before plunging into the complex world of network programming, these tutorial programs illustrate the basic skills using simple asynchronous timers. Introduction to Sockets The tutorial programs in this section show how to use asio to develop simple client and server programs. These tutorial programs are based around the daytime protocol, which supports both TCP and UDP. The first three tutorial programs implement the daytime protocol using TCP.

The next three tutorial programs implement the daytime protocol using UDP. The last tutorial program in this section demonstrates how asio allows the TCP and UDP servers to be easily combined into a single program. Minimal ASIO TCP client. Without a client, we can't even check if the ASIO TCP server we have written in the previous post actually works.

Minimal ASIO TCP client

Here are the include files we'll need for our client: It should be clear why we need Boost ASIO and the standard iostream, boost array is not strictly a necessity, but helps us to keep the code simpler. We'll show how and why at the due time. We need to provide the client the host name and the port for establish a connection. Host name is going to be passed from the command line by program arguments, the port number is, for this simple test app, just a fixed constant defined in this way: Note that, differently from the server, here we need it defined as a string. C++ - Using boost thread and a non-static class function. C++ - EOF with boost.

Basic_stream_socket - 1.37. Examples - 1.53. Allocation This example shows how to customise the allocation of memory associated with asynchronous operations.

Examples - 1.53

Buffers This example demonstrates how to create reference counted buffers that can be used with socket read and write operations. Chat This example implements a chat server and client. The following POSIX-specific chat client demonstrates how to use the posix::stream_descriptor class to perform console input and output. Echo A collection of simple clients and servers, showing the use of both synchronous and asynchronous operations. Daytime.2 - A synchronous TCP daytime server - 1.40. This tutorial program shows how to use asio to implement a server application with TCP.

Daytime.2 - A synchronous TCP daytime server - 1.40

#include <ctime>#include <iostream>#include <string>#include <boost/asio.hpp> using boost::asio::ip::tcp; We define the function make_daytime_string() to create the string to be sent back to the client. This function will be reused in all of our daytime server applications. std::string make_daytime_string(){ using namespace std; time_t now = time(0); return ctime(&now);} int main(){ try { boost::asio::io_service io_service;

Daytime.1 - A synchronous TCP daytime client - 1.53. A guide to getting started with boost. Bind.hpp documentation - 1.53. The Boost C++ Libraries. Bind.hpp documentation - 1.53. Contents Purpose Using bind with functions and function pointers. Integrating with Boost.Asio. Package management - How can I find Boost version. Filesystem Tutorial. Introduction Preliminaries Reporting the size of a file - (tut1.cpp) Using status queries to determine file existence and type - (tut2.cpp) Directory iteration plus catching exceptions - (tut3.cpp) Using path decomposition, plus sorting results - (tut4.cpp) Class path: Constructors, including Unicode - (tut5.cpp) Class path: Generic format vs. Native format Class path: Iterators, observers, composition, decomposition, and query - (path_info.cpp) Error reporting Introduction This tutorial develops a little command line program to list information about files and directories - essentially a much simplified version of the POSIX ls or Windows dir commands.

We'll start with the simplest possible version and progress to more complex functionality. Along the way we'll digress to cover topics you'll need to know about to understand Boost.Filesystem. Source code for each of the tutorial programs is available, and you are encouraged to compile, test, and experiment with it. Preliminaries Try this: Filesystem Library. Introduction The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs. The intent is not to compete with Python, Perl, or shell languages, but rather to provide portable filesystem operations when C++ is already the language of choice. The design encourages, but does not require, safe and portable filesystem usage. The Filesystem Library supplies several headers, all in directory boost/filesystem: