技术the boost c libraries_第1页
技术the boost c libraries_第2页
技术the boost c libraries_第3页
技术the boost c libraries_第4页
技术the boost c libraries_第5页
已阅读5页,还剩153页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、The Boost C+ Libraries Front pageChapter 1: IntroductionTable of Contents1.1 C+ and Boost1.2 Development Process1.3 Installation1.4 OverviewThis book is licensed under a Creative Commons License.1.1 C+ and BoostThe Boost C+ Libraries are a collection of modern libraries based on the C+ standard. The

2、 source code is released under the Boost Software License allowing to use, modify and distribute the libraries for. The libraries are platform independent and support most popular as well as many lesser known compilers.The Boost commuis responsible for develoand publishing the Boost C+ Libraries. It

3、 consists of a relatively large group of C+developers from around the world coordinated through the web site as well as several mailing lists. The mission statement ofthe commuis to develop and collect libraries of high-quality that complement the C+ standard. Libraries that prove of value and becom

4、eimportant for the development of C+ applications stand a good chance to get included in the C+ standard someday.The Boost commuemerged around 1998 when the first revision of the C+ standard was released. It has grown continuously since thenand nowadays plays a big role in the standardization of C+.

5、 Even though there is no direct relation between the Boost commuand thestandardization committee, some of the developers are active in both groups. The next revision of the C+ standard, which is most likely approved in 2011, is going to be extended by a couple of libraries that have their roots in t

6、he Boost commu.Boost C+ Libraries are a good choice to increase the productivity of C+ projects after exhausting the C+ standard. Since C+ has evolved after the current revision of the C+ standard originated in 2003, Boost C+ Libraries offer many new features. Without waiting for the nextrevision of

7、 the C+ standard, progressin the evolution of C+ can be utilized immediately thanks to the Boost C+ Libraries.Due to the excellent reputation the Boost C+ Libraries have, being well grounded in using them certainly proves to be of valueally. It is not unusual to be asked about the Boost C+ Libraries

8、 during an interview since developers who know these libraries are usually also fond with the latest innovations of C+ and are able to write and understand modern C+ code.1.2 Development ProcessThe development of the Boost C+ Libraries is only possible because individual developers or organizations

9、support them with great engagement. Since Boost only accepts libraries that solve existing problems, exhibit a convincing design, are developed using modern C+and are documented in an understandable way, each Boost C+ Library has a lot of workit.C+ developers can engage themselves in the Boost commu

10、and propose new libraries. However, in order to convert an idea into a BoostC+ Library, a lot of time and effort needs to be invested. It is of vital importance to discuss the requirements and possible solutions with other developers and potential users in the Boost mailing lists.Besides libraries t

11、hat seem to appear from nowhere, it is also possible to nominate existing C+ libraries for the inclusion into Boost. However, since the requirements for these libraries are the same as for libraries developed for Boost in particular, there may be many changes required.Whether or not a library gets a

12、ccepted into Boost, depends on the outcome of the review process. Developers of libraries can apply for a review which usually takes places over the course of 10 days. During this time frame other developers are requested to rate the library. Based on the number of positive and negative reviews, the

13、 review manager decides whether or not the library is accepted into Boost. Since some developers are exposed to the library for the first time during the review, it is not uncommon that modifications to the library are requested during the time frame of the review.If a library is rejected due to tec

14、hnical reasons, it is still possible to revise the library and request a new review for an updated version. However, if a library is rejected because it does not solve any practical problem or because it provides an unconvincing solution to an existing problem, there is a good chance that it will be

15、 again rejected during another review.Because new libraries can be accepted at any time, a new version of the Boost C+ Libraries is released every three months. This book covers libraries found in version 1.42.0 which was released in February 2010.Please note that there exists additional libraries t

16、hat have been aly accepted but are not part of the official release of the Boost C+Libraries yet. They have to be manually installed until they get included.1.3 InstallationThe Boost C+ Libraries come as source code. While most of the libraries consist solely of header files that can be directly use

17、d, some of the libraries requires compilation. In order to make the installation process as easy as possible, an automated installation process using Boost Jam is available. Instead of validating and compiling individual libraries separately, Boost Jam installs the complete set automatically. It sup

18、ports many operating systems and compilers and knows how to compile each individual library based on appropriate configuration files.To automatically install the Boost C+ Libraries with the aid of Boost Jam, an application named bjam is utilized which is also available as source code. For some opera

19、ting systems, including Windows and Linux, precompiled binaries of bjam are available as well.In order to compile bjam itself, a simple script named build is executed which is available with the source code and is provided for different operating systems as well. For Windows, it is implemented as th

20、e batch file build.bat. For Linux, the file is called build.sh.If build is called without anyd line option, the script tries to find a suitable compiler to generate bjam. Using ad lineswitch, denoted as toolset, a particular compiler can be selected though. For Windows, build supports the toolsets v

21、c7, vc8 and vc9 to selectdifferent versions of the Microsoft C+ compiler. To compile bjam with the C+ compiler from Visual Studio 2008, thed build vc9needs to be issued. For Linux, the toolsets gcc and intel-linux are supported, amongst others, to select the C+ compiler of GCC and Intel, respectivel

22、y.The application bjam must be copied to the local Boost directory - disregarding whether it was compiled or downloaded as a precompiledbinary. bjam can then be started without anyd line option to compile and install the Boost C+ Libraries. Since the default options -used in this case - are not alwa

23、ys the best choice, the most important options are listed below:The declaration of stage or install specifies whether the Boost C+ Libraries are installed in a subdirectory named stage or system- wide. The meaning of system-wide depends on the operating system. In Windows, the target directory is C:

24、Boost; in Linux itis /usr/local. The target directory can also be explicitly specified using the -prefix option.If bjam is called without anyd line options, it will search for a suitable C+ compiler itself. A specific compiler can beselected using the -toolset option. To specify the Microsoft C+ com

25、piler from Visual Studio 2008 in Windows, bjam needs to becalled with the -toolset=msvc-9.0 option. To specify the GCC compiler in Linux, the option -toolset=gcc must be selected.Thed line option -build-type determines which builds of the libraries are created. By default, this option is set to mini

26、mal,meaning that only release builds are created. This may become an issue for developers who want to create debug builds of their projectswith Visual Studio or GCC. Since these compilers automatically try to linkthe debug builds of the Boost C+ Libraries, anerror message is displayed accordingly. I

27、n this case the option -build-type should be set to complete to generate both debug and release builds of the Boost C+ Libraries which, however, will take much longer.To create both debug and release builds of the Boost C+ Libraries using the C+ compiler from Visual Studio 2008 and install them in t

28、hedirectory D:Boost, thed executed is bjam -toolset=msvc-9.0 -build-type=complete -prefix=D:Boost install. To create them inLinux using the defauirectory, thed executed is bjam -toolset=gcc -build-type=complete install.There exist many otherd line options which can be used to specify inhow to compil

29、e the Boost C+ Libraries. I typically usethe followingd in Windows: bjam -toolset=msvc-9.0 debug release link=static runtime-link=shared install. debug and releasecause both debug and release builds to be generated. link=static only creates static libraries. runtime-link=shared specifies that the C+

30、 runtime library is dynamically linked which is the default setting for C+ projects in Visual Studio 2008.1.4 OverviewVersion 1.42.0 of the Boost C+ Libraries contains more than 90 libraries out of which this book discusses the following libraries in more:Table 1.1. Covered LibrariesShort Descriptio

31、nBoost.Any provides a data type named boost:any that allows to store arbitrary types. For example, a variable of type boost:any can first store a value of type int before replacing it with a string of type std:string.Boost.Array allows treating C+ arrays just like containers from the C+ standard. Bo

32、ost.Asio allows to develop applications that process data asynchronously such as network applications.Boost.Bimap provides a class named boost:bimap which is similar to std:map. The crucialdifference is that boost:bimap allows to search for both key and value.Boost.Bind is kind of an adapter allowin

33、g to pass functions as template parameters even if the function signature is incompatible with the template parameter.Boost.Conversion provides three casting operators to perform downcasts, cross casts and to convertbetween values of different numeric types.C+StandardBoost C+ LibraryBoost.AnyBoost.A

34、rrayBoost.AsioTR1TR2Boost.BimapBoost.BindTR1Boost.ConversionBoost.DateTimeBoost.DateTime can be used to process,and write date and time values with a flexible formatting.Boost.Exception allows adding additional data to thrown exceptions in order to provide more information in the catch handler. This

35、 helps easing the debugging process and allows betterBoost.Exceptionresponding to exceptional cases.Boost.Filesystem offers a class for processing of path information and also contains several functions to access files and directories.Boost.Format replaces the std:printf() function with a type-safe

36、and extendable boost:formatclass.Boost.Function simplifies the definition of function pointers.Boost.Interprocess allows applications to communicate via shared memory in a fast and efficient manner.Boost.Lambda allows the definition of anonymous functions. Code is declared and executed inline to avo

37、id separate function calls.Boost.Multiindex allows defining new containers that can support multiple interfaces such as the ones from std:vector and std:map.Boost.NumericConversion provides a cast operator to safely convert between values of differentBoost.FilesystemTR2Boost.FormatBoost.Function Boo

38、st.InterprocessTR1Boost.LambdaBoost.MultiindexBoost.NumericConversionnumeric types without generating an overflow or underflow condition.Boost.PointerContainer provides containers optimized for the management of dynamically allocated objects.The adapters of Boost.Ref allow passing references to unco

39、pyable objects to functions that expect copies.Boost.Regex offers functions to search text via regular expressions.Via Boost.Serialization, objects can be serialized and e.g. stored in files that can be loaded later on. Boost.Signal is a framework for event handling and is based on the so-called sig

40、nal/slot concept: Functions are associated with signals and automatically called, when the signal is triggered.Boost.SmartPoiners offers many smart pointers simplifying the management of dynamically allocated objects.Boost.Spirit allows generating parsers using a syntax similar to EBNF (Extended Bac

41、kus Naur Form). Boost.StringAlgorithms provides many stand-alone functions to facilitate string handling.Boost.System offers a framework to process system- and application-specific error codes. Boost.Thallows develomultithed applications.Boost.Tokenizer allows iterating over the individual component

42、s of a string.Boost.PointerContainerBoost.RefBoost.Regex Boost.SerializationBoost.SignalsTR1TR1Boost.SmartPoinersBoost.Spirit Boost.StringAlgorithms Boost.SystemBoost.Th Boost.Tokenizer Boost.TupleBoost.UnorderedTR1TR2 C+0xTR1TR1Boost.Tuple offers a generalized version of std:pair that allows grouof

43、 any number of data.Boost.Unordered enlarges the containers of the C+ standard by adding boost:unordered_set andboost:unordered_map.Boost.Variant permits the definition of data types that, similar to union, group multiple data types. The advantage of Boost.Variant over union is the possibility to us

44、e classes as well.Boost.VariantWhile the Technical Report 1 was published in 2003,s about the standardization in regards to C+0x and the Technical Report 2illustrate the current status quo. Since neither the next revision of the C+ standard nor the Technical Report 2 has been approved yet, they may

45、still change in the future.Copyright2008-2010 Sch lingThe Boost C+ Libraries Front pageChapter 2: Smart PointersTable of Contents2.1 General2.2 RAII2.3 Scoped Pointer2.4 Scoped Array2.5 Shared Pointer2.6 Shared Array2.7 Weak Pointer2.8 Intrusive Pointer2.9 Pointer Container2.10 ExercisesThis book is

46、 licensed under a Creative Commons License.2.1 GeneralThe first version of the C+ standard adapted in 1998 only provides one smart pointer: std:auto_ptr. In principle, it behaves like a regular pointer: It provides access to a dynamically allocated object by storing its address. However, std:auto_pt

47、r is considered intelligent and smart since it automatically releases the contained object during destruction by calling the delete operator. This certainly requires that it is provided with the address of the object - returned by the new operator - at initialization. Since the delete operator is ca

48、lled within the destructor of std:auto_ptr, the associated memory for the contained object is guaranteed to be released. This is one advantage of smart pointers.This becomes even more importn conjunction with exceptions: Without smart pointers such as std:auto_ptr, every function allocatingdynamic m

49、emory would be required to catch every possible exception in order to release the memory prior to passing on the exception to its calling function. The Boost C+ Library Smart Pointers provides many additional smart pointers that can be used in all kind of situations.2.2 RAIIThe principle of smart po

50、inters is based on a common idiom named RAII: Resource acquisition is initialization. Smart pointers are only one example for this idiom - certainly a very prominent one. Smart pointers are used to ensure that dynamically allocated memory is released properly under all circumstances,ing the develope

51、r from the burden of managing this on her own. This includes scenarios in which execution of a function is interrupted by an exception and the instruction to release the memory is skipped. This guarantee is accomplished by initializing a smart pointer with the address of a dynamically allocated obje

52、ct which in turn is used to release the memory during destruction. Since the destructor is always executed the contained memory is therefore always released.RAII is applied whenever a second instruction is mandatory in order to release a resource previously allocated by another instruction. Sinceman

53、y C+ applications require dynamically managed memory, smart pointers are an important RAII class. RAII itself can be applied in many other scenarios though.#include <windows.h>class windows_handlepublic: windows_handle(HANDLE h): handle_(h)windows_handle()Handle(handle_);HANDLE handle() constr

54、eturn handle_;private:HANDLE handle_;int main()windows_handle h(OpenProcess(PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId(); SetPriorityClass(h.handle(), HIGH_PRIORITY_CLASS);Download source codeThe above example defines a class named windows_handle that calls the functionHandle() in its destr

55、uctor. Since it is a WindowsAPI function, the program can only be executed on Windows. In Windows, many resources are required to be opened prior to their usage.This implicitly implies that resources should bed once no longer used. The class windows_handle provides a mechanism to ensure just that.An

56、 instance of type windows_handle is initialized with a handle. Windows utilizes handles in order to uniquely identify its resources. For example, the function OpenProcess() returns a handle of type HANDLE that can be used to access a currently running process. In the code example, the own process is

57、 accessed - in other words the application itself.Using the returned handle, the process priority is increased allowing the application to request more CPU time from the scheduler. This is only for illustration purposes and does not serve any real benefit though. The important point here is that the resource opened withOpenProcess() does not need to be explicitlyd usingHandle(). Certainly, the resource is likely to bed once the applicationterminates. However, in more complex applications the clas windows_handle ensures that a resource is correctlyd if no longer needed.Once a particular resou

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论