Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/util/RefToValue.h @ 5435

Last change on this file since 5435 was 3235, checked in by rgrieder, 15 years ago

Added ScopeGuard from the Loki library. It provides a very simple mechanism to write exception-safe code. Have a look at the article:
http://www.ddj.com/cpp/184403758
The basic idea is to make a scope guard and pass it a function to be called in case you don't call the guard's dismiss() function. And you always do this in the linear code order exception if it was interrupted by an exception. This way you can make sure that resources are being freed in any case.

  • Property svn:eol-style set to native
File size: 2.0 KB
Line 
1////////////////////////////////////////////////////////////////////////////////
2// The Loki Library
3// Copyright (c) 2006 Richard Sposato
4// Copyright (c) 2006 Peter Kümmel
5// Permission to use, copy, modify, distribute and sell this software for any
6//     purpose is hereby granted without fee, provided that the above copyright
7//     notice appear in all copies and that both that copyright notice and this
8//     permission notice appear in supporting documentation.
9// The authors make no representations about the
10//     suitability of this software for any purpose. It is provided "as is"
11//     without express or implied warranty.
12////////////////////////////////////////////////////////////////////////////////
13#ifndef LOKI_REFTOVALUE_INC_
14#define LOKI_REFTOVALUE_INC_
15
16// $Id: RefToValue.h 751 2006-10-17 19:50:37Z syntheticpp $
17
18
19namespace Loki
20{
21
22    ////////////////////////////////////////////////////////////////////////////////
23    ///  \class RefToValue
24    ///
25    ///  \ingroup SmartPointerGroup
26    ///  Transports a reference as a value
27    ///  Serves to implement the Colvin/Gibbons trick for SmartPtr/ScopeGuard
28    ////////////////////////////////////////////////////////////////////////////////
29
30    template <class T>
31    class RefToValue
32    {   
33    public:
34   
35        RefToValue(T& ref) : ref_(ref) 
36        {}
37
38        RefToValue(const RefToValue& rhs) : ref_(rhs.ref_)
39        {}
40
41        operator T& () const 
42        {
43            return ref_;
44        }
45
46    private:
47        // Disable - not implemented
48        RefToValue();
49        RefToValue& operator=(const RefToValue&);
50       
51        T& ref_;
52    };
53
54
55    ////////////////////////////////////////////////////////////////////////////////
56    ///  \ingroup ExceptionGroup
57    ///  RefToValue creator.
58    ////////////////////////////////////////////////////////////////////////////////
59
60    template <class T>
61    inline RefToValue<T> ByRef(T& t)
62    {
63        return RefToValue<T>(t);
64    }   
65   
66}
67
68
69#endif // end file guardian
70
Note: See TracBrowser for help on using the repository browser.