Learning Boost C++ Libraries

Book description

Solve practical programming problems using powerful, portable, and expressive libraries from Boost

In Detail

Filled with dozens of working code examples that illustrate the use of over 40 popular Boost libraries, this book takes you on a tour of Boost, helping you to independently build the libraries from source and use them in your own code.

The first half of the book focuses on basic programming interfaces including generic containers and algorithms, strings, resource management, exception safety, and a miscellany of programming utilities that make everyday programming chores easy. Following a short interlude that introduces template metaprogramming and functional programming, the later chapters are devoted to systems programming interfaces, focusing on directory handling, I/O, concurrency, and network programming

What You Will Learn

  • Write efficient and maintainable code using expressive interfaces from Boost libraries
  • Leverage a variety of flexible, practical, and highly efficient containers and algorithms beyond STL
  • Solve common programming problems by applying a wide array of utility libraries
  • Design and write portable multithreaded code that is easy to read and maintain
  • Craft highly scalable and efficient TCP and UDP servers
  • Build and deploy Boost libraries across a variety of popular platforms
  • Use C++11 functionality and emulate C++11 language features in C++03 code

Table of contents

  1. Learning Boost C++ Libraries
    1. Table of Contents
    2. Learning Boost C++ Libraries
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introducing Boost
      1. How it all started
      2. What is Boost?
      3. Getting started with Boost libraries
        1. Necessary software
          1. Linux toolchain
          2. Windows toolchain
        2. Obtaining and building Boost libraries
          1. Planning your Boost sandbox
          2. Library naming conventions
          3. Library name components
          4. Library name layouts
            1. Versioned layout
            2. System layout
            3. Tagged layout
          5. Installing a Boost binary distribution
            1. Installing on Windows
            2. Installing on Linux
          6. Building and installing the Boost libraries from source
            1. Optional packages
            2. Building the Boost libraries on Linux
            3. Building the Boost libraries on Windows
        3. Using Boost libraries in your projects
          1. Linking against Boost libraries on Linux
          2. Linking against Boost libraries on Windows
        4. Building the code listings in this book
          1. CMake
          2. Code examples
      4. Self-test questions
      5. Summary
    9. 2. The First Brush with Boost's Utilities
      1. Simple data structures
        1. Boost.Optional
          1. Accessing values stored in boost::optional
          2. get_value_or
          3. Boost.Optional versus pointers
        2. Boost.Tuple
          1. Creating tuples
          2. Accessing tuple elements
          3. Comparing tuples
          4. Writing generic code using tuples
      2. Working with heterogeneous values
        1. Boost.Variant
          1. Accessing values in a variant
            1. Compile-time visitation
            2. Generic visitors
            3. Applying visitors to variants in a container
          2. Defining recursive variants
            1. The JSON format
            2. Representing JSON content with recursive variants
            3. Visiting recursive variants
        2. Boost.Any
        3. Boost.Conversion
          1. lexical_cast
      3. Handling command-line arguments
        1. Designing command-line options
          1. The diff command – a case study
        2. Using Boost.Program_Options
          1. Parsing positional parameters
          2. Multiple option values
      4. Other utilities and compile-time checks
        1. BOOST_CURRENT_FUNCTION
        2. Boost.Swap
        3. Compile-time asserts
        4. Diagnostics using preprocessor macros
      5. Self-test questions
      6. Summary
      7. References
    10. 3. Memory Management and Exception Safety
      1. Dynamic memory allocation and exception safety
        1. Exception safety and RAII
      2. Smart pointers
        1. Unique ownership semantics
          1. boost::scoped_ptr
            1. Uses of scoped_ptr
              1. Creating exception-safe scopes
              2. Transferring object ownership across functions
              3. As a class member
          2. boost::scoped_array
          3. std::unique_ptr
            1. Ownership transfer using unique_ptr
            2. Wrapping arrays in unique_ptr
            3. make_unique in C++14
        2. Shared ownership semantics
          1. boost::shared_ptr and std::shared_ptr
            1. Uses of shared_ptr
              1. As a class member
              2. Storing dynamically-allocated objects in Standard Library containers
            2. Nonowning aliases – boost::weak_ptr and std::weak_ptr
            3. A shared_ptr critique – make_shared and enable_shared_from_this
          2. Intrusive smart pointers – boost::intrusive_ptr
            1. Using intrusive_ptr
          3. shared_array
        3. Managing non-memory resources using smart pointers
      3. Self-test questions
      4. Summary
      5. References
    11. 4. Working with Strings
      1. Text processing with Boost String Algorithms library
        1. Using Boost String Algorithms
          1. Find algorithms
            1. find_first
            2. find_all
            3. find_token
            4. iter_find
            5. find
            6. find_head and find_tail
            7. Other algorithms for testing string properties
          2. Case-conversion and trimming algorithms
            1. Case-conversion algorithms
            2. Trimming algorithms
          3. The replace and erase algorithms
          4. The split and join algorithms
      2. Splitting text using the Boost Tokenizer library
        1. Tokenizing based on separators
        2. Tokenizing records with fields containing metacharacters
        3. Tokenizing records with fixed-length fields
        4. Writing your own tokenizer functions
      3. Regular expressions using Boost.Regex
        1. Regular expression syntax
          1. Atoms
          2. Quantifiers
          3. Character classes
          4. Anchors
          5. Sub-expressions
          6. Disjunctions
        2. Using Boost.Regex to parse regular expressions
          1. Matching text
          2. Searching text
          3. Tokenizing text using regex
          4. Replacing text
      4. Self-test questions
      5. Summary
    12. 5. Effective Data Structures beyond STL
      1. Boost Container library
        1. Move-awareness and in-place construction
        2. Nonstandard containers
          1. Flat associative containers
          2. slist
            1. Splicing
          3. stable_vector
          4. static_vector
      2. Fast lookups using Boost Unordered containers
      3. Containers for dynamically-allocated objects
        1. Ownership semantics of pointer containers
        2. Null pointers in pointer containers
      4. Expressive initialization and assignment using Boost.Assign
        1. Assigning lists of values to containers
        2. Initializing containers with lists of values
        3. Initializing pointer containers and assigning values
      5. Iteration patterns using Boost.Iterator
        1. Smart iteration using Boost.Iterator
          1. Filter Iterator
          2. Transform Iterator
          3. Function Output Iterator
        2. Creating conforming iterators for custom classes
      6. Self-test questions
      7. Summary
      8. References
    13. 6. Bimap and Multi-index Containers
      1. Containers for multi-criteria lookups
      2. Boost Multi-index containers
        1. Index types
        2. Range lookups using lambda
        3. Insertions and updates
      3. Boost Bimap
        1. Collection types
        2. More ways to use bimaps
          1. Tagged access
          2. Projections
      4. Self-test questions
      5. Summary
      6. References
    14. 7. Higher Order and Compile-time Programming
      1. Higher order programming with Boost
        1. Function objects
          1. Lambdas – unnamed function literals
            1. Lambda captures
          2. Delegates and closures
          3. Partial function application
      2. Compile-time programming with Boost
        1. Basic compile-time control flow using templates
          1. Branching
          2. Recursion
        2. Boost Type Traits
          1. SFINAE and enable_if / disable_if
        3. The Boost Metaprogramming Library (MPL)
          1. Metafunctions
          2. Using MPL metafunctions
      3. Domain Specific Embedded Languages
        1. Lazy evaluation
        2. Expression templates
        3. Boost Phoenix
        4. Boost Spirit Parser Framework
          1. Using Spirit Qi
            1. Predefined parsers
            2. The parsing API
            3. Parser operators and expressions
            4. Parsing directives
            5. Semantic actions
            6. Rules
            7. Parsing timestamps
      4. Self-test questions
      5. Summary
      6. References
    15. 8. Date and Time Libraries
      1. Date and time calculations with Boost Date Time
        1. Dates from the Gregorian calendar
          1. Creating date objects
          2. Handling date durations
          3. Date periods
        2. Posix time
          1. Constructing time points and durations
          2. Resolution
          3. Time periods
          4. Time iterator
      2. Using Chrono to measure time
        1. Durations
          1. Duration arithmetic
        2. Clocks and time points
      3. Measuring program performance using Boost Timer
        1. cpu_timer
        2. auto_cpu_timer
      4. Self-test questions
      5. Summary
      6. References
    16. 9. Files, Directories, and IOStreams
      1. Managing files and directories with Boost Filesystem
        1. Manipulating paths
          1. Printing paths
          2. Constructing paths
          3. Breaking paths into components
        2. Traversing directories
        3. Querying filesystem entries
        4. Performing operations on files
          1. Creating directories
          2. Creating symbolic links
          3. Copying files
          4. Moving and deleting files
          5. Path-aware fstreams
      2. Extensible I/O with Boost IOStreams
        1. Architecture of Boost IOStreams
        2. Using devices
          1. Devices for file I/O
          2. Devices for reading and writing to memory
        3. Using filters
          1. Basic filters
          2. Filters for compression and decompression
          3. Composing filters
            1. Pipelining
            2. Branching data streams with tee
      3. Self-test questions
      4. Summary
    17. 10. Concurrency with Boost
      1. Creating concurrent tasks with Boost Thread
        1. Using Boost Threads
          1. Moving threads and waiting on threads
          2. Thread IDs
          3. Cores and threads
      2. Managing shared data
        1. Creating and coordinating concurrent tasks
          1. boost::future and boost::promise
          2. Waiting for future
          3. Throwing exceptions across threads
          4. shared_future
          5. std::future and std::promise
          6. std::packaged_task and std::async
            1. std::packaged_task
            2. std::async
              1. Launch policy
        2. Lock-based thread synchronization methods
          1. Data races and atomic operations
          2. Mutual exclusion and critical sections
            1. boost::lock_guard
            2. boost::unique_lock
            3. Deadlocks
          3. Synchronizing on conditions
            1. Condition variables and producer-consumer problem
              1. Condition variable nuances
          4. The Readers-Writers problem
            1. Upgradable locks
            2. Performance of shared_mutex
          5. Standard Library primitives
      3. Boost Coroutine
        1. Asymmetric coroutines
      4. Self-test questions
      5. Summary
      6. References
    18. 11. Network Programming Using Boost Asio
      1. Task execution with Asio
        1. IO Service, queues, and handlers
          1. Handler states – run_one, poll, and poll_one
          2. post versus dispatch
        2. Concurrent execution via thread pools
          1. io_service::work
        3. Serialized and ordered execution via strands
      2. Network I/O using Asio
        1. UDP and TCP
        2. IP addresses
          1. IPv4 addresses
            1. Subnets
            2. Special addresses
            3. Handling IPv4 addresses with Boost
          2. IPv6 addresses
            1. Address classes, scopes, and subnets
            2. Special addresses
            3. Handling IPv6 addresses with Boost
        3. Endpoints, sockets, and name resolution
          1. Ports
          2. Sockets
          3. Hostnames and domain names
            1. Name resolution
        4. Buffers
          1. Buffer sequences for vectored I/O
        5. Synchronous and asynchronous communications
        6. Asio deadline timer
        7. Asynchronous logic using Asio coroutines
        8. UDP
          1. Synchronous UDP client and server
          2. Asynchronous UDP server
            1. Asynchronous UDP server using completion handler chains
            2. Asynchronous UDP server using coroutines
          3. Performance and concurrency
        9. TCP
          1. Establishing a TCP connection
            1. Client- and server-side calls
          2. Synchronous TCP client and server
            1. Concurrency and performance
          3. Asynchronous TCP server
            1. Asynchronous TCP server using coroutines
            2. Asynchronous TCP server without coroutines
            3. Lifetime of TCPAsyncConnection
            4. Performance and concurrency
            5. Inversion of control flow
      3. Self-test questions
      4. Summary
      5. References
    19. A. C++11 Language Features Emulation
      1. RAII
      2. Copy semantics
        1. The nothrow swap
      3. Move semantics and rvalue references
        1. rvalue references
          1. rvalue-reference overloads
          2. Move assignment
          3. xvalues
        2. Move emulation using Boost.Move
      4. C++11 auto and Boost.Auto
        1. Type deduction rules
        2. Common uses
        3. Boost.Auto
      5. Range-based for-loops
        1. Boost.Foreach
      6. C++11 exception-handling improvements
        1. Storing and rethrowing exceptions
        2. Storing and rethrowing exception using Boost
      7. Self-test questions
      8. References
    20. Index

Product information

  • Title: Learning Boost C++ Libraries
  • Author(s): Arindam Mukherjee
  • Release date: July 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781783551217