Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
SharedFunctionPtr.hpp
Go to the documentation of this file.
1 /*
2  * $Id: SharedFunctionPtr.hpp 135 2013-06-29 15:09:42Z jdl3 $
3  * Copyright (C) 2010, 2011, 2013 John D Lamb
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifndef IPO_DETAIL_SHAREDFUNCTIONPTR_HPP
21 #define IPO_DETAIL_SHAREDFUNCTIONPTR_HPP
22 
23 #include<new>
24 #include"../../ipo_function/Function.hpp"
25 
26 namespace ipo {
27  namespace detail {
32  public:
37  pointer = 0;
38  count = 0; // initially nullptr will do
39  owner = false;
40  }
41  // Refines assignable
48  pointer = function;
49  // just plausibly we could allocate Function but not count
50  try { count = new size_t; } catch( std::bad_alloc& e ){
51  // try to tidy up before rethrowing
52  delete pointer;
53  throw e;
54  }
55  *count = 1; // initially there is just one reference to pointer
56  owner = true;
57  }
64  pointer = &function;
65  // just plausibly we could fail to allocate count: no further action needed.
66  count = new size_t;
67  *count = 1; // initially there is just one reference to pointer
68  owner = false;
69  }
70  // copy constructor
76  count = v.count; if( count != 0 ) ++*count; owner = v.owner; }
77  // assignment operator
83  // first, possibly delete anything pointed to by this
84  if( count == 0 or --*count == 0 ){
85  if( pointer != 0 and true == owner ) delete pointer;
86  delete count;
87  } // Then copy
88  pointer = v.pointer; count = v.count; if( count != 0 ) ++*count; owner = v.owner;
89  return *this;
90  }
91  // destructor
96  if( count == 0 or --*count == 0 ){
97  // could have allocated null pointer
98  if( pointer != 0 and true == owner ) delete pointer;
99  delete count;
100  }
101  }
102  // Refines equality comparable
108  std::swap( count, v.count );
109  v.pointer = nullptr;
110  v.count = 0;
111  v.owner = false;
112  }
119  SharedFunctionPtr( std::move( v ) ).swap( *this );
120  return *this;
121  }
122  // == operator
128  bool operator==( SharedFunctionPtr const& v ) const { return pointer == v.pointer; }
129  // != operator
136  bool operator!=( SharedFunctionPtr const& v ) const { return not operator==( v ); }
137  // Refines forward container
144  std::swap( pointer, v.pointer );
145  std::swap( count, v.count );
146  }
157  private:
165  size_t* count;
169  bool owner;
170  public:
171  // shared reference functions
176  ipo_function::Function* get() const { return pointer; }
182  bool unique() const { return count != 0 and *count == 1; }
187  size_t use_count() const { return count == 0 ? 0 : *count; }
193  explicit operator bool() const { return pointer != 0; }
194  };
195  }
196 }
197 
198 #endif
bool operator==(SharedFunctionPtr const &v) const
Two SharedFunctionPtr are identically equal if they point to the same object.
Shared pointer to ipo::Function object.
SharedFunctionPtr(SharedFunctionPtr &&v)
Move constructor.
bool operator!=(SharedFunctionPtr const &v) const
Two SharedFunctionPtr are different equal if they point to different objectd.
SharedFunctionPtr()
The default constructor is only really useful for assigning to.
size_t use_count() const
Find how many SharedFunctionPtr objects share this pointer.
SharedFunctionPtr(ipo_function::Function &function)
Construct from a Function object.
SharedFunctionPtr & operator=(SharedFunctionPtr const &v)
The assignment operator.
SharedFunctionPtr(SharedFunctionPtr const &v)
The copy constructor.
void swap(SharedFunctionPtr &v)
Swap two SharedFunctionPtr objects.
void swap(Array &first, Array &second)
Swap contents of container with those of another.
Definition: Array.hpp:736
SharedFunctionPtr(ipo_function::Function *function)
Construct from a pointer to a function.
This class computes a function at a vector.
Definition: Function.hpp:38
ipo_function::Function * operator->() const
Get a pointer to the function.
~SharedFunctionPtr()
The destructor only deletes the pointers if count reaches zero.
SharedFunctionPtr & operator=(SharedFunctionPtr &&v)
Move operator.
ipo_function::Function & operator*() const
Get a reference to the function.
size_t * count
The shared reference count.
ipo_function::Function * pointer
The shared pointer.
bool unique() const
Find if this is the only object sharing the gsl_SharedFunctionPtr.
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28