LRE
Sign In
Sign Up
Sign In
Sign Up
Manage this list
×
Keyboard Shortcuts
Thread View
j
: Next unread message
k
: Previous unread message
j a
: Jump to all threads
j l
: Jump to MailingList overview
2024
December
November
October
September
August
July
June
May
April
March
February
January
2023
December
November
October
September
August
July
June
May
April
March
February
January
2022
December
November
October
September
August
July
June
May
April
March
February
January
2021
December
November
October
September
August
July
June
May
April
March
February
January
2020
December
November
October
September
August
July
June
May
April
March
February
January
2019
December
November
October
September
August
July
June
May
April
March
February
January
2018
December
November
October
September
August
July
June
May
April
March
February
January
2017
December
November
October
September
August
July
June
May
April
March
February
January
2016
December
November
October
September
August
July
June
May
April
March
February
January
2015
December
November
October
September
August
July
June
May
April
March
February
January
2014
December
November
October
September
August
July
June
May
April
March
February
January
2013
December
November
October
September
August
July
June
May
April
March
February
January
2012
December
November
October
September
August
July
June
May
April
March
February
January
2011
December
November
October
September
August
July
June
May
April
March
February
January
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008
December
November
October
September
August
July
June
May
April
March
February
January
2007
December
November
October
September
August
July
June
May
April
March
February
January
2006
December
November
October
September
August
July
June
May
April
March
February
January
2005
December
November
October
September
August
July
June
May
April
March
February
January
2004
December
November
October
September
August
July
June
May
April
March
List overview
Download
Olena-patches
February 2005
----- 2024 -----
December 2024
November 2024
October 2024
September 2024
August 2024
July 2024
June 2024
May 2024
April 2024
March 2024
February 2024
January 2024
----- 2023 -----
December 2023
November 2023
October 2023
September 2023
August 2023
July 2023
June 2023
May 2023
April 2023
March 2023
February 2023
January 2023
----- 2022 -----
December 2022
November 2022
October 2022
September 2022
August 2022
July 2022
June 2022
May 2022
April 2022
March 2022
February 2022
January 2022
----- 2021 -----
December 2021
November 2021
October 2021
September 2021
August 2021
July 2021
June 2021
May 2021
April 2021
March 2021
February 2021
January 2021
----- 2020 -----
December 2020
November 2020
October 2020
September 2020
August 2020
July 2020
June 2020
May 2020
April 2020
March 2020
February 2020
January 2020
----- 2019 -----
December 2019
November 2019
October 2019
September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
----- 2018 -----
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
----- 2017 -----
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
----- 2016 -----
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
----- 2015 -----
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
----- 2014 -----
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
----- 2013 -----
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
----- 2012 -----
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
----- 2011 -----
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
----- 2010 -----
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
----- 2009 -----
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
----- 2008 -----
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
----- 2007 -----
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
----- 2006 -----
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
----- 2005 -----
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
----- 2004 -----
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
olena-patches@lrde.epita.fr
4 participants
14 discussions
Start a n
N
ew thread
proto-1.0 34: Add preconditions.
by Thierry GERAUD
https://svn/svn/oln/prototypes/proto-1.0/metalic
Index: ChangeLog from Thierry GERAUD <theo(a)tegucigalpa.lrde.epita.fr> * mlc/tracked_ptr.hh: Add preconditions. 2005-02-08 Thierry GERAUD <theo(a)tegucigalpa.lrde.epita.fr> Index: mlc/tracked_ptr.hh --- mlc/tracked_ptr.hh (revision 31) +++ mlc/tracked_ptr.hh (working copy) @@ -241,6 +241,7 @@ const T*const operator->() const { invariant(proxy_ != 0); + precondition(proxy_->ptr_ != 0); return proxy_->ptr_; } @@ -251,6 +252,7 @@ T*const operator->() { invariant(proxy_ != 0); + precondition(proxy_->ptr_ != 0); return proxy_->ptr_; }
19 years, 10 months
1
0
0
0
proto-1.0 33: Preconditions-oriented patch.
by Thierry GERAUD
19 years, 10 months
1
0
0
0
proto-1.0 32: Ensure g++-2.95 compliance.
by Thierry GERAUD
2005-02-08 Thierry GERAUD <theo(a)tegucigalpa.lrde.epita.fr> * oln/core/value_box.hh: Ensure g++-2.95 compliance. * oln/core/abstract/piter.hh: Likewise. * oln/core/abstract/point.hh: Likewise. * oln/core/abstract/size.hh: Likewise. * oln/core/2d/dpoint2d.hh: Likewise. * oln/core/2d/array2d.hh: Likewise. * oln/core/2d/fwd_piter2d.hh: Likewise. * oln/core/2d/point2d.hh: Likewise. * oln/core/2d/size2d.hh: Likewise. Index: oln/core/value_box.hh =================================================================== --- oln/core/value_box.hh (revision 31) +++ oln/core/value_box.hh (working copy) @@ -28,7 +28,7 @@ #ifndef OLENA_CORE_VALUE_BOX_HH # define OLENA_CORE_VALUE_BOX_HH -# include <ostream> +# include <iostream> # include <oln/core/macros.hh> Index: oln/core/abstract/piter.hh =================================================================== --- oln/core/abstract/piter.hh (revision 31) +++ oln/core/abstract/piter.hh (working copy) @@ -97,7 +97,7 @@ void invalidate() { this->exact().impl_invalidate(); - postcondition(not this->is_valid()); + postcondition(! this->is_valid()); } protected: Index: oln/core/abstract/point.hh =================================================================== --- oln/core/abstract/point.hh (revision 31) +++ oln/core/abstract/point.hh (working copy) @@ -92,7 +92,7 @@ */ bool operator!=(const point& rhs) const { - return not this->operator==(rhs); + return ! this->operator==(rhs); } typedef oln_dpoint_type(E) dpoint_type; Index: oln/core/abstract/size.hh =================================================================== --- oln/core/abstract/size.hh (revision 31) +++ oln/core/abstract/size.hh (working copy) @@ -52,7 +52,7 @@ template <typename Ep> bool operator!=(const size<Ep>& rhs) const { - return not this->operator==(rhs); + return ! this->operator==(rhs); } protected: Index: oln/core/2d/dpoint2d.hh =================================================================== --- oln/core/2d/dpoint2d.hh (revision 31) +++ oln/core/2d/dpoint2d.hh (working copy) @@ -28,7 +28,7 @@ #ifndef OLENA_CORE_2D_DPOINT2D_HH # define OLENA_CORE_2D_DPOINT2D_HH -# include <ostream> +# include <iostream> # include <oln/core/coord.hh> # include <oln/core/2d/point2d.hh> @@ -69,12 +69,12 @@ bool operator==(const dpoint2d& rhs) const { - return this->row_ == rhs.row_ and this->col_ == rhs.col_; + return this->row_ == rhs.row_ && this->col_ == rhs.col_; } bool operator!=(const dpoint2d& rhs) const { - return not this->operator==(rhs); + return ! this->operator==(rhs); } const dpoint2d operator+(const dpoint2d& rhs) const Index: oln/core/2d/array2d.hh =================================================================== --- oln/core/2d/array2d.hh (revision 31) +++ oln/core/2d/array2d.hh (working copy) @@ -115,8 +115,8 @@ void impl_resize(const size2d& s) { - precondition(s.nrows() > 0 and - s.ncols() > 0 and + precondition(s.nrows() > 0 && + s.ncols() > 0 && s.border() >= 0); invariant_(); this->clear_data(); @@ -147,18 +147,18 @@ bool impl_hold(const point2d& p) const { return - p.row() >= 0 and - p.row() < size_.nrows() and - p.col() >= 0 and + p.row() >= 0 && + p.row() < size_.nrows() && + p.col() >= 0 && p.col() < size_.ncols(); } bool impl_hold_large(const point2d& p) const { return - p.row() >= - size_.border() and - p.row() < size_.nrows() + size_.border() and - p.col() >= - size_.border() and + p.row() >= - size_.border() && + p.row() < size_.nrows() + size_.border() && + p.col() >= - size_.border() && p.col() < size_.ncols() + size_.border(); } @@ -193,16 +193,16 @@ void invariant_() const { - invariant((buffer_ != 0 and - array_ != 0 and - size_.nrows() > 0 and - size_.ncols() > 0 and + invariant((buffer_ != 0 && + array_ != 0 && + size_.nrows() > 0 && + size_.ncols() > 0 && size_.border() >= 0) - or - (buffer_ == 0 and - array_ == 0 and - size_.nrows().is_undefined() and - size_.ncols().is_undefined() and + || + (buffer_ == 0 && + array_ == 0 && + size_.nrows().is_undefined() && + size_.ncols().is_undefined() && size_.border().is_undefined())); } Index: oln/core/2d/fwd_piter2d.hh =================================================================== --- oln/core/2d/fwd_piter2d.hh (revision 31) +++ oln/core/2d/fwd_piter2d.hh (working copy) @@ -70,35 +70,35 @@ { this->p_.row() = 0; this->p_.col() = 0; - postcondition(this->p_.row().is_defined() and this->p_.col().is_defined()); + postcondition(this->p_.row().is_defined() && this->p_.col().is_defined()); } bool impl_is_valid() const { - precondition(this->p_.row().is_defined() and this->p_.col().is_defined()); + precondition(this->p_.row().is_defined() && this->p_.col().is_defined()); return this->p_.row() < this->s_.nrows(); } void impl_next() { - precondition(this->p_.row().is_defined() and this->p_.col().is_defined()); - precondition(this->p_.row() >= 0 and this->p_.row() <= this->s_.nrows() - and - this->p_.col() >= 0 and this->p_.col() <= this->s_.ncols()); + precondition(this->p_.row().is_defined() && this->p_.col().is_defined()); + precondition(this->p_.row() >= 0 && this->p_.row() <= this->s_.nrows() + && + this->p_.col() >= 0 && this->p_.col() <= this->s_.ncols()); ++this->p_.col(); if (this->p_.col() != this->s_.ncols()) return; this->p_.col() = 0; precondition(this->p_.row() != this->s_.nrows()); ++this->p_.row(); - postcondition(this->p_.row().is_defined() and this->p_.col().is_defined()); + postcondition(this->p_.row().is_defined() && this->p_.col().is_defined()); } void impl_invalidate() { this->p_.row() = this->s_.nrows(); this->p_.col() = this->s_.ncols(); - postcondition(this->p_.row().is_defined() and this->p_.col().is_defined()); + postcondition(this->p_.row().is_defined() && this->p_.col().is_defined()); } }; Index: oln/core/2d/point2d.hh =================================================================== --- oln/core/2d/point2d.hh (revision 31) +++ oln/core/2d/point2d.hh (working copy) @@ -28,7 +28,7 @@ #ifndef OLENA_CORE_2D_POINT2D_HH # define OLENA_CORE_2D_POINT2D_HH -# include <ostream> +# include <iostream> # include <oln/core/abstract/point.hh> # include <oln/core/coord.hh> @@ -95,7 +95,7 @@ bool impl_eq(const point2d& rhs) const { - return this->row_ == rhs.row_ and this->col_ == rhs.col_; + return this->row_ == rhs.row_ && this->col_ == rhs.col_; } protected: Index: oln/core/2d/size2d.hh =================================================================== --- oln/core/2d/size2d.hh (revision 31) +++ oln/core/2d/size2d.hh (working copy) @@ -28,7 +28,7 @@ #ifndef OLENA_CORE_2D_SIZE2D_HH # define OLENA_CORE_2D_SIZE2D_HH -# include <ostream> +# include <iostream> # include <oln/core/abstract/size.hh> # include <oln/core/coord.hh> @@ -73,7 +73,7 @@ bool impl_eq(const size2d& rhs) const { - return this->nrows_ == rhs.nrows_ and + return this->nrows_ == rhs.nrows_ && this->ncols_ == rhs.ncols_; }
19 years, 10 months
1
0
0
0
proto-1.0 30: Bug fixes.
by Thierry GERAUD
2005-02-08 Thierry GERAUD <theo(a)tegucigalpa.lrde.epita.fr> * oln/core/value_box.hh: Unbox image attribute. * oln/core/abstract/image_with_data.hh: Clean up in ctors. * oln/core/abstract/data_storage.hh: Fix bug in size(). * oln/core/2d/array2d.hh: Fix name of size impl. Index: olena/oln/core/value_box.hh =================================================================== --- olena/oln/core/value_box.hh (revision 29) +++ olena/oln/core/value_box.hh (working copy) @@ -30,10 +30,6 @@ # include <ostream> -// for mlc::box<T>: - -# include <mlc/box.hh> - # include <oln/core/macros.hh> // the proper macro: @@ -91,7 +87,7 @@ template <typename V> value_box& operator=(const V& value) { - ima_->set(p_, value); + ima_.set(p_, value); return *this; } @@ -107,7 +103,7 @@ template <typename II> value_box& operator=(const value_box<II>& rhs) { - ima_->set(p_, rhs); // automatic conversion from rhs to oln_value_type(I) + ima_.set(p_, rhs); // automatic conversion from rhs to oln_value_type(I) return *this; } @@ -122,7 +118,7 @@ template <typename A, typename V> value_box& set(void (I::*method)(A), const V& value) { - ima_->set(p_, method, value); + ima_.set(p_, method, value); return *this; } @@ -137,13 +133,13 @@ template <typename V> operator const V() const { - const V value = ima_->get(p_); + const V value = ima_.get(p_); return value; } operator const oln_value_type(I)() const { - return ima_->get(p_); + return ima_.get(p_); } @@ -160,7 +156,7 @@ const oln_value_type(I) value() const { - return ima_->get(p_); + return ima_.get(p_); } @@ -174,8 +170,8 @@ /// Ctor (restricted access). - value_box(I& ima, const oln_point_type(I)& p) : - ima_(ima), + value_box(abstract::image<I>& ima, const oln_point_type(I)& p) : + ima_(ima.exact()), p_(p) { } @@ -192,7 +188,7 @@ ! attributes ! *------------*/ - mlc::box<I> ima_; + I& ima_; oln_point_type(I) p_; }; @@ -216,10 +212,9 @@ public: + /*! \brief Assignment (op=) is declared but undefined. + */ - // FIXME: no operator= was provided here so 'self = self' was possible... - // FIXME: below, a single decl has been added; to be tested... - template <typename V> void operator=(const V&) const; @@ -232,13 +227,13 @@ template <typename V> operator const V() const { - const V value = ima_->get(p_); + const V value = ima_.get(p_); return value; } operator const oln_value_type(I)() const { - return ima_->get(p_); + return ima_.get(p_); } @@ -255,7 +250,7 @@ const oln_value_type(I) value() const { - return ima_->get(p_); + return ima_.get(p_); } // IDEA: provide op-> @@ -270,8 +265,8 @@ /// Ctor (restricted access). - value_box(const I& ima, const oln_point_type(I)& p) : - ima_(ima), + value_box(const abstract::image<I>& ima, const oln_point_type(I)& p) : + ima_(ima.exact()), p_(p) { } @@ -288,7 +283,7 @@ ! attributes ! *------------*/ - mlc::box<const I> ima_; + const I& ima_; oln_point_type(I) p_; }; Index: olena/oln/core/abstract/image_with_data.hh =================================================================== --- olena/oln/core/abstract/image_with_data.hh (revision 29) +++ olena/oln/core/abstract/image_with_data.hh (working copy) @@ -137,18 +137,18 @@ /*! \brief Constructor (protected) with no memory allocation for ** data. */ - image_with_data() + image_with_data() : + data_(0) { - data_ = 0; } /*! \brief Constructor (protected) with memory allocation for ** data. */ - image_with_data(const oln_size_type(E)& size) + image_with_data(const oln_size_type(E)& size) : + data_(new oln_value_container_type(E)(size)) { - data_ = new oln_value_container_type(E)(size); } Index: olena/oln/core/abstract/data_storage.hh =================================================================== --- olena/oln/core/abstract/data_storage.hh (revision 29) +++ olena/oln/core/abstract/data_storage.hh (working copy) @@ -54,7 +54,7 @@ const oln_size_type(E)& size() const { - return this->exact().impl_size(this->s); + return this->exact().impl_size(); } void resize(const oln_size_type(E)& s) Index: olena/oln/core/2d/array2d.hh =================================================================== --- olena/oln/core/2d/array2d.hh (revision 29) +++ olena/oln/core/2d/array2d.hh (working copy) @@ -108,7 +108,7 @@ invariant_(); } - const size2d& size() const + const size2d& impl_size() const { return size_; } Index: metalic/mlc/tracked_ptr.hh =================================================================== --- metalic/mlc/tracked_ptr.hh (revision 29) +++ metalic/mlc/tracked_ptr.hh (working copy) @@ -28,10 +28,11 @@ #ifndef METALIC_TRACKED_PTR_HH # define METALIC_TRACKED_PTR_HH -# include <ostream> +# include <iostream> # include <set> # include <map> +# include <mlc/contract.hh> namespace mlc { @@ -42,93 +43,148 @@ namespace internal { + /*! \class tracked_ptr_proxy<T> + ** + ** Class that effectively holds a pointer. For use in class + ** tracked_ptr. + ** + ** Parameter T is the type of pointed data. + */ + template <class T> - class tracked_ptr_proxy + struct /* HERE: class */ tracked_ptr_proxy { friend class tracked_ptr<T>; - // attributes + /// Attributes. + T* ptr_; std::set<const tracked_ptr<T>*> holders_; - // w/o impl + /// Cpy ctor is not impled. tracked_ptr_proxy(const tracked_ptr_proxy&); + + /// Assignmt op is not impled. void operator=(const tracked_ptr_proxy&); - // methods - tracked_ptr_proxy() : - ptr_(0) - { - } - + /*! \brief Ctor. + ** + ** We have a new proxy so we have to make sure that the pointer, + ** if not null, was not already proxified, meaning that this new + ** proxy handles 'new' data. + */ tracked_ptr_proxy(T* ptr) : - ptr_(0) + ptr_(0), + holders_() { - set_ptr(ptr); + if (ptr != 0) + { + invariant(proxy_of()[ptr] == 0); + this->ptr_ = ptr; + proxy_of()[this->ptr_] = this; + } + else + this->ptr_ = 0; } - void set_ptr(T* new_ptr) + + /*! \brief Change data pointer. + ** + ** If the change is effective, former data are deleted. + */ + void set_ptr(T* ptr) { - if (new_ptr == this->ptr_) - return; - T* old_ptr = this->ptr_; - if (old_ptr != 0) + if (ptr == this->ptr_) { - assert(proxy_of()[old_ptr] == this); - proxy_of().erase(old_ptr); + // nothing to be done + return; + } + + if (this->ptr_ != 0) + { + invariant(proxy_of()[this->ptr_] == this); + proxy_of().erase(this->ptr_); + delete this->ptr_; + this->ptr_ = 0; // safety }; - this->ptr_ = new_ptr; - if (new_ptr != 0) + + // same code as ctor: + if (ptr != 0) { - assert(proxy_of()[new_ptr] == 0); - proxy_of()[new_ptr] = this; - }; + invariant(proxy_of()[ptr] == 0); + this->ptr_ = ptr; + proxy_of()[this->ptr_] = this; + } + else + this->ptr_ = 0; } + + /*! \brief Register a new holder. + ** + ** \precondition Test that it is effectively a new holder. + */ void register_holder(const tracked_ptr<T>* holder) { - assert(holder != 0); - assert(holders_.find(holder) == holders_.end()); + precondition(holder != 0); + precondition(holders_.find(holder) == holders_.end()); holders_.insert(holder); } + + /*! \brief Unregister a holder. + ** + ** If it is the last holder and if data is not null, data are + ** deleted. In that case, the return value (true) allows the + ** tracked_pointer to know that the proxy has to be deleted. + ** + ** \precondition Test that it is effectively a holder. + ** \return True if data is deleted, false otherwise + */ + bool unregister_holder(const tracked_ptr<T>* holder) { - assert(holder != 0); - assert(holders_.size() > 0); + // "simple" tests: + precondition(holder != 0); + precondition(holders_.size() > 0); - typename std::set<const tracked_ptr<T>*>::iterator e = holders_.find(holder); - assert(e != holders_.end()); - holders_.erase(e); + // "main" test: + // note that the couple of "simple" tests above are subtests of the following one + typename std::set<const tracked_ptr<T>*>::iterator h = holders_.find(holder); + precondition(h != holders_.end()); - if (holders_.size() == 0) + holders_.erase(h); + + if (holders_.size() == 0 && this->ptr_ != 0) { - proxy_of()[ptr_] = 0; - delete ptr_; - ptr_ = 0; + proxy_of()[this->ptr_] = 0; + delete this->ptr_; + this->ptr_ = 0; // safety return true; } + return false; } + // procedures typedef std::map<const T*, const tracked_ptr_proxy*> map_type; static map_type& proxy_of() { - static map_type proxy_of_; - return proxy_of_; + static map_type* proxy_of_ = new map_type; + return *proxy_of_; } friend std::ostream& operator<<(std::ostream& ostr, const tracked_ptr_proxy& proxy) { - ostr << "[ptr=" << proxy.ptr_ << " holders=("; + ostr << "proxy " << &proxy << " [ptr=" << proxy.ptr_ << " holders=("; typename std::set<const tracked_ptr<T>*>::const_iterator i = proxy.holders_.begin(); bool ok = i != proxy.holders_.end(); while (ok) @@ -143,7 +199,7 @@ }; - } // end of namspace internal + } // end of namespace internal @@ -154,155 +210,185 @@ { public: + /*! \brief Mimics the behavior of op* for a pointer in the const case. + ** + ** \invariant Pointer proxy exists. + ** \precondition Data exists (is not null). + */ const T& operator*() const { - assert(proxy_ != 0 and proxy_->ptr_ != 0); + invariant(proxy_ != 0); + precondition(proxy_->ptr_ != 0); return *(proxy_->ptr_); } + /*! \brief Mimics the behavior of op* for a pointer in the mutable case. + ** + ** \invariant Pointer proxy exists. + ** \precondition Data exists (is not null). + */ T& operator*() { - assert(proxy_ != 0 and proxy_->ptr_ != 0); + invariant(proxy_ != 0); + precondition(proxy_->ptr_ != 0); return *(proxy_->ptr_); } + /*! \brief Mimics the behavior of op-> for a pointer in the const case. + ** + ** \invariant Pointer proxy exists. + */ const T*const operator->() const { - assert(proxy_ != 0); + invariant(proxy_ != 0); return proxy_->ptr_; } + /*! \brief Mimics the behavior of op-> for a pointer in the mutable case. + ** + ** \invariant Pointer proxy exists. + */ T*const operator->() { - assert(proxy_ != 0); + invariant(proxy_ != 0); return proxy_->ptr_; } + /// Coercion towards Boolean (for arithmetical tests). operator bool() const { - return proxy_ != 0 and proxy_->ptr_ != 0; + invariant(proxy_ != 0); + return proxy_->ptr_ != 0; } - bool operator not() const + /// Negation (for arithmetical tests). + bool operator !() const { - return not bool(*this); + invariant(proxy_ != 0); + return ! bool(*this); } + /// Comparison 'equal to' (for arithmetical tests). bool operator==(int b) const { + invariant(proxy_ != 0); return bool(*this) == bool(b); } + /// Comparison 'not equal to' (for arithmetical tests). bool operator!=(int b) const { + invariant(proxy_ != 0); return bool(*this) != bool(b); } - // hook: - const internal::tracked_ptr_proxy<T>* proxy() const { return proxy_; } + /// Hook that gives access to the data proxy. + const internal::tracked_ptr_proxy<T>* proxy() const { return this->proxy_; } + internal::tracked_ptr_proxy<T>* proxy() { return this->proxy_; } - // ctors - tracked_ptr() : - proxy_(0) - { - invariant_(); - } - - tracked_ptr(T* ptr) : + /*! \brief Ctor. + ** + ** Creates a proxy that knows the target data pointer. This + ** pointer can be null (0). + */ + tracked_ptr(T* ptr = 0) : proxy_(new internal::tracked_ptr_proxy<T>(ptr)) { - proxy_->register_holder(this); + this->proxy_->register_holder(this); invariant_(); } - // shallow copy + /*! \brief Cpy ctor performs a shallow copy. + ** + ** The data proxy of \a rhs is shared with the constructed + ** tracked_ptr. + */ tracked_ptr(const tracked_ptr& rhs) : proxy_(rhs.proxy_) { - if (proxy_ != 0) - proxy_->register_holder(this); + precondition(rhs.proxy_ != 0); + this->proxy_->register_holder(this); invariant_(); } - // assignment + /*! \brief Assignment operator with rhs being a tracked pointer. + ** + ** The lhs proxy unregisters its data and registers the rhs data. + ** In the case of both pointers have the same value, no action is + ** performed and a warning is produced. The unregistration + ** process may desallocate data if the target pointer was the only + ** access to those data. + */ tracked_ptr& operator=(const tracked_ptr& rhs) { - invariant_(); - if (rhs == 0) - return *this; + invariant(this->proxy_ != 0); + precondition(rhs.proxy_ != 0); + + invariant_(); // safety + if (rhs.proxy_ == this->proxy_) - return *this; - if (this->release_proxy_()) - delete this->proxy_; + { + std::cerr << "warning: nothing done cause both objects share the same data!" << std::endl; + // FIXME: say more + // e.g.: ima1 = ima2; ima2 = ima1; + return *this; + } + + if (this->proxy_->unregister_holder(this)) + { + // the current 'tracked pointer' was the only access to + // some data (this->proxy_->ptr_); data has been + // desallocated, so it goes the same for the proxy: + delete this->proxy_; + } + this->proxy_ = rhs.proxy_; this->proxy_->register_holder(this); + invariant_(); return *this; } + + /*! \brief Assignment operator with rhs being a data pointer. + ** + ** This assignment just changes the pointed data. + */ tracked_ptr& operator=(T* ptr) { - if (proxy_ != 0) - { - assert(proxy_->holders_.size() <= 1); - this->release_proxy_(); - } - else - proxy_ = new internal::tracked_ptr_proxy<T>(ptr); - this->proxy_->register_holder(this); + invariant(this->proxy_ != 0); + this->proxy_->set_ptr(ptr); return *this; } - // dtor + // Dtor. + ~tracked_ptr() { - invariant_(); - if (release_proxy_()) - delete proxy_; - proxy_ = 0; - } - - // dangerous! (?) - - void reset_() - { - invariant_(); - if (proxy_ == 0 or proxy_->ptr_ == 0) - return; - T* ptr = proxy_->ptr_; - std::set<const tracked_ptr<T>*>& holders = proxy_->holders_; - typename std::set<const tracked_ptr<T>*>::iterator i; - for (i = holders.begin(); i != holders.end(); ++i) + invariant(this->proxy_ != 0); + if (this->proxy_->unregister_holder(this)) { - if (*i == this) - continue; - tracked_ptr<T>* holder = const_cast<tracked_ptr<T>*>(*i); - holder->proxy_ = 0; + // the current 'tracked pointer' was the only access to + // some data (this->proxy_->ptr_); data has been + // desallocated, so it goes the same for the proxy: + delete this->proxy_; } - delete this->proxy_->ptr_; - delete this->proxy_; - this->proxy_ = 0; + this->proxy_ = 0; // safety } private: + /// Only attribute. + internal::tracked_ptr_proxy<T>* proxy_; - bool release_proxy_() - { - invariant_(); - if (proxy_ == 0) - return false; - return proxy_->unregister_holder(this); - } - void invariant_() const { if (proxy_ == 0) @@ -313,9 +399,15 @@ assert((*i)->proxy_ == this->proxy_); } + friend + std::ostream& operator<<(std::ostream& ostr, const tracked_ptr& ptr) + { + invariant(ptr.proxy_ != 0); + return ostr << "tracked_ptr " << &ptr << " --> " << *(ptr.proxy_); + } }; + - } // end of namespace mlc Index: metalic/mlc/any.hh =================================================================== --- metalic/mlc/any.hh (revision 29) +++ metalic/mlc/any.hh (working copy) @@ -32,7 +32,7 @@ -// abrev +// abbrev #define any_mem(E) any <E, dispatch_policy::best_memory> @@ -46,14 +46,13 @@ { struct best_speed; struct best_memory; - struct compromise; + struct simple; }; // any - template <typename E, - typename Dispatch_Policy = dispatch_policy::best_memory> + template <typename E, typename Dispatch_Policy = dispatch_policy::simple> struct any; @@ -71,12 +70,34 @@ precondition(exact_ptr != 0); return *exact_ptr; } + + /// Assignment (op=). + + any& operator=(const any& rhs) + { + // do nothing + // so that "this->exact_ptr" is unchanged + return *this; + } + + /// Copy Ctor. + + any(const any& rhs) : + exact_ptr(0) // safety + { + // this->exact_ptr is *not* copied + // its value has to be set in subclasses copy ctors. + } + protected: any(E* exact_ptr) : exact_ptr(exact_ptr) {} virtual ~any() {} + + /// Only attribute. E* exact_ptr; }; + template <typename E> struct any__best_speed : public any<E, dispatch_policy::best_speed> { @@ -104,8 +125,8 @@ protected: any() {} - any(E* exact_ptr); // safety: w/o impl - virtual ~any() {} + any(E* exact_ptr_does_not_exist_in_this_version); // safety + virtual ~any() {} // FIXME: virtual for a "best memory" version!? }; template <typename E> @@ -123,11 +144,22 @@ - (const char*)(void*)(&any_mem(E)::exact_obj); - // "compromise" version of 'any' + // "simple" version of 'any' template <typename E> struct any <E, - dispatch_policy::compromise>; // FIXME: not yet impled + dispatch_policy::simple> + { + E& exact() { + return *(E*)(void*)this; + } + const E& exact() const { + return *(const E*)(const void*)this; + } + protected: + any() {} + any(E* exact_ptr_does_not_exist_in_this_version); // safety + }; } // end of namespace mlc
19 years, 10 months
1
0
0
0
← Newer
1
2
Older →
Jump to page:
1
2
Results per page:
10
25
50
100
200