Skip to content

Crunkle/observer-ptr

 
 

Repository files navigation

observer_ptr<> for C++98 up

Language Standard Standard Standard License Build Status Build status Version download

observer-ptr is a single-file header-only library with a variant of std::observer_ptr [1] for C++98 and later.

Contents

Example usage

#include "nonstd/observer_ptr.hpp"

using namespace nonstd;

void use( observer_ptr<int> p )
{
    assert( *p == 42 );
}

int main()
{
    int a = 42;
    observer_ptr<int> p( &a );
    use( p );
}

Compile and run

prompt>g++ -std=c++03 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe

In a nutshell

observer-ptr is an implementation of the world’s dumbest smart pointer for C++98 and higher. It takes no ownership responsibility for the object it observes or watches and is intended as a near drop-in replacement for raw pointer types. As a vocabulary type it indicates intended use, easing code reading (Note 1).

Class template observer_ptr<> has been proposed for and accepted into C++17 [1][2].

The observer-ptr of this project can be used with probably any clang, g++ or MSVC compiler. It has been tested with clang 3.4, g++ 5.2 and with VC6 (VS6, no comparison of observers), VC8 (VS2005), VC10 (VS2010), VC11 (VS2012), VC12 (VS2013), VC14 (VS2015).

License

observer-ptr is distributed under the Boost Software License.

Dependencies

observer-ptr has no other dependencies than the C++ standard library.

Installation

observer-ptr is a single-file header-only library. Put observer_ptr.h in the include folder directly into the project source tree or somewhere reachable from your project.

Building the tests

To build the tests you need:

The lest test framework is included in the test folder.

The following steps assume that the observer-ptr source code has been cloned into a directory named c:\observer-ptr.

  1. Create a directory for the build outputs for a particular architecture.
    Here we use c:\observer-ptr\build-win-x86-vc10.

     cd c:\observer-ptr
     md build-win-x86-vc10
     cd build-win-x86-vc10
    
  2. Configure CMake to use the compiler of your choice (run cmake --help for a list).

     cmake -G "Visual Studio 10 2010" ..
    
  3. Build the test suite in the Debug configuration (alternatively use Release).

     cmake --build . --config Debug
    
  4. Run the test suite.

     ctest -V -C Debug
    

All tests should pass, indicating your platform is supported and you are ready to use observer-ptr.

Synopsis

Contents
Documentation of std::observer_ptr
Configuration macros

Documentation of std::observer_ptr

Depending on the compiler and C++-standard used, nonstd::observer_ptr behaves less or more like std::observer_ptr. To get an idea of the capabilities of nonstd::observer_ptr with your configuration, look at the output of the tests, issuing observer_ptr-main.t --pass @. For std::observer_ptr, see its documentation at cppreference.

Configuration macros

Standard selection macro

-Dnsop_CPLUSPLUS=199711L
Define this macro to override the auto-detection of the supported C++ standard, or if your compiler does not set the __cplusplus macro correctly.

Select std::experimental::observer_ptr or nonstd::observer_ptr

At default, observer-ptr lite uses std::experimental::observer_ptr if it is available and lets you use it via namespace nonstd. You can however override this default and explicitly request to use std::experimental::observer_ptr or observer-ptr lite's nonstd::observer_ptr as nonstd::observer_ptr via the following macros.

-Dnsop_CONFIG_SELECT_OBSERVER_PTR=nsop_OBSERVER_PTR_DEFAULT
Define this to nsop_OBSERVER_PTR_STD to select std::experimental::observer_ptr as nonstd::observer_ptr. Define this to nsop_OBSERVER_PTR_NONSTD to select nonstd::observer_ptr as nonstd::observer_ptr. Default is undefined, which has the same effect as defining to nsop_OBSERVER_PTR_DEFAULT.

Conversions

-Dnsop_CONFIG_ALLOW_IMPLICIT_CONVERSION=0
The observer_ptr from the C++17 draft provides explicit conversions to bool and to the underlying type. Explicit conversion is not available from pre-C++11 compilers. To prevent problems due to unexpected implicit conversions to bool or to the underlying type, this library does not provide these implicit conversions at default. If you still want them, define this macro to 1. Without these implicit conversions enabled, a conversion to bool via the safe bool idiom is provided. Default is 0.

Compile-time tests

-Dnsop_CONFIG_CONFIRMS_COMPILATION_ERRORS=0
Define this macro to 1 to experience the by-design compile-time errors of the observer-ptr components in the test suite. Default is 0.

Other open source implementations

None known.

Notes and references

Notes

Note 1. This conclusion may be challenged if the coding style ensures that any raw pointer is a non-owning pointer.

References

[1] Walter E. Brown. N3840: A Proposal for the World’s Dumbest Smart Pointer, v3 (PDF).1 January 2014.
[2] N4481: Tentative Working Draft, C++ Extensions for Library Fundamentals, Version 2 Section 4.2 Non-owning pointers. 12 April 2015.
[3] cppreference.com. std::experimental::observer_ptr.

Appendix

A.1 Observer Ptr test specification

Disallows to delete the observer_ptr unless implicit conversion allowed
Disallows construction from an observer_ptr of incompatible type
Disallows implicit conversion to bool unless implicit conversion allowed
Disallows implicit conversion to underlying type unless implicit conversion allowed
Disallows comparison to an observer_ptr with a different underlying type
Allows default construction
Allows construction from nullptr
Allows construction from a non-null pointer
Allows construction from an observer_ptr of compatible type
Allows to retrieve the pointer
Allows to retrieve the value pointed to
Allows to retrieve the member pointed to
Allows to test for a non-null pointer via conversion to bool
Allows to convert to the observed pointer
Allows to reset to stop observing
Allows to reset to observe another pointer
Allows to swap two observers
Specialized: Allows to swap two observers
Specialized: Allows to make an observer
Specialized: Allows to compare if an observer is equal to another observer
Specialized: Allows to compare if an observer is equal to another observer with a related watched type
Specialized: Allows to compare if an observer is not equal to another observer
Specialized: Allows to compare if an observer is not equal to another observer with a related watched type
Specialized: Allows to compare if an observer is equal to nullptr
Specialized: Allows to compare if an observer is not equal to nullptr
Specialized: Allows to compare if an observer is less than another observer
Specialized: Allows to compare if an observer is less than another observer with a related watched type
Specialized: Allows to compare if an observer is less than or equal to another observer
Specialized: Allows to compare if an observer is less than or equal to another observer with a related watched type
Specialized: Allows to compare if an observer is greater than another observer
Specialized: Allows to compare if an observer is greater than another observer with a related watched type
Specialized: Allows to compare if an observer is greater than or equal to another observer
Specialized: Allows to compare if an observer is greater than or equal to another observer with a related watched type
Specialized: Allows to compute hash

About

A single-file header-only C++98, C++03, C++11, C++14 version of ISO C++17 std::observer_ptr

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 80.9%
  • CMake 9.2%
  • Batchfile 5.8%
  • Python 4.1%