2018-03-31 17:27:13 +02:00
|
|
|
/*
|
2018-04-01 04:43:43 +02:00
|
|
|
ZOMBIE-CHECK.hpp - flatliner self-detection
|
2018-03-31 17:27:13 +02:00
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2018, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** @file dependable-base.hpp
|
2018-04-01 04:43:43 +02:00
|
|
|
** Detector to set off alarm when (re)using deceased objects.
|
2018-04-01 03:41:41 +02:00
|
|
|
** @see sync-classlock.hpp
|
|
|
|
|
** @see depend.hpp
|
2018-03-31 17:27:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2018-04-01 04:43:43 +02:00
|
|
|
#ifndef LIB_ZOMBIE_CHECK_H
|
|
|
|
|
#define LIB_ZOMBIE_CHECK_H
|
2018-03-31 17:27:13 +02:00
|
|
|
|
2018-04-01 04:43:43 +02:00
|
|
|
#include "lib/del-stash.hpp"
|
2018-04-01 03:41:41 +02:00
|
|
|
#include "lib/nocopy.hpp"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <utility>
|
2018-03-31 17:27:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
|
|
|
|
namespace nifty { // implementation details
|
|
|
|
|
|
|
|
|
|
template<class X>
|
|
|
|
|
struct Holder
|
|
|
|
|
{
|
2018-04-01 03:41:41 +02:00
|
|
|
char payload_[sizeof(X)];
|
2018-03-31 17:27:13 +02:00
|
|
|
|
2018-04-01 03:41:41 +02:00
|
|
|
//NOTE: deliberately no ctor/dtor
|
2018-03-31 17:27:13 +02:00
|
|
|
|
2018-04-01 03:41:41 +02:00
|
|
|
X&
|
|
|
|
|
access()
|
|
|
|
|
{
|
|
|
|
|
return *reinterpret_cast<X*> (&payload_);
|
|
|
|
|
}
|
2018-03-31 17:27:13 +02:00
|
|
|
};
|
|
|
|
|
|
2018-04-01 04:43:43 +02:00
|
|
|
template<class X>
|
|
|
|
|
uint Holder<X>::accessed_;
|
2018-03-31 17:27:13 +02:00
|
|
|
|
|
|
|
|
} // (End) nifty implementation details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-04-01 03:41:41 +02:00
|
|
|
* A dependable data container available with extended lifespan.
|
|
|
|
|
* Automatically plants a ref-count to ensure the data stays alive
|
|
|
|
|
* until the last static reference goes out of scope.
|
2018-03-31 17:27:13 +02:00
|
|
|
*/
|
2018-04-01 03:41:41 +02:00
|
|
|
template<class X>
|
|
|
|
|
class DependableBase
|
|
|
|
|
: util::NonCopyable
|
2018-03-31 17:27:13 +02:00
|
|
|
{
|
2018-04-01 03:41:41 +02:00
|
|
|
static nifty::Holder<X> storage_;
|
2018-03-31 17:27:13 +02:00
|
|
|
|
2018-04-01 03:41:41 +02:00
|
|
|
public:
|
|
|
|
|
template<typename...ARGS>
|
|
|
|
|
explicit
|
|
|
|
|
DependableBase (ARGS&& ...args)
|
2018-03-31 17:27:13 +02:00
|
|
|
{
|
2018-04-01 03:41:41 +02:00
|
|
|
storage_.maybeInit (std::forward<ARGS> (args)...);
|
2018-03-31 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-01 03:41:41 +02:00
|
|
|
operator X& () const
|
|
|
|
|
{
|
|
|
|
|
return storage_.access();
|
|
|
|
|
}
|
2018-03-31 17:27:13 +02:00
|
|
|
|
|
|
|
|
uint use_count() { return nifty::Holder<PerClassMonitor>::accessed_; }
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-01 03:41:41 +02:00
|
|
|
/** plant a static buffer to hold the payload X */
|
|
|
|
|
template<class X>
|
|
|
|
|
nifty::Holder<X> DependableBase<X>::storage_;
|
|
|
|
|
|
2018-03-31 17:27:13 +02:00
|
|
|
|
|
|
|
|
} // namespace lib
|
2018-04-01 04:43:43 +02:00
|
|
|
#endif /*LIB_ZOMBIE_CHECK_H*/
|