Ticket #141 : move asside the old util::AccessCasted for rework
..existing code still uses the old version; will switch when the new one is ready
This commit is contained in:
parent
0f37cbdf8f
commit
505903e71e
6 changed files with 186 additions and 132 deletions
176
src/lib/access-casted-o.hpp
Normal file
176
src/lib/access-casted-o.hpp
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
ACCESS-CASTED.hpp - util template to access a value using conversion or cast as appropriate
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, 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 access-casted-o.hpp
|
||||
** Helper for accessing a value, employing either conversion or downcast,
|
||||
** depending on the relation of the source type (type of the original value)
|
||||
** and the target type (type we need within the usage context).
|
||||
** When instantiating AcessCasted<TAR>, we get a template static function
|
||||
** \c AcessCasted<TAR>::access<SRC>(SRC& elm), but the actual implementation
|
||||
** is chosen using boost::type_traits. If no sensible implementation can be
|
||||
** selected, \c EmptyVal<TAR>::create() is invoked instead, which by default
|
||||
** creates a NULL value or similar by using the no-argument ctor of the
|
||||
** type TAR. Alternatively, you may define an specialisation of EmptyVal,
|
||||
** e.g. throwing an exception instead of creating a NULL value.
|
||||
**
|
||||
** @deprecated old obsolete version ///////////////////////////////////////////TICKET #738 clean-up access-casted, rewrite variant (4/2015)
|
||||
** @todo This is the obsoleted old version: It was excessively permissive, which
|
||||
** I've learned to view as a danger, since it encourages a sloppy programming style.
|
||||
** @see lumiera::WrapperPtr usage example to access a variant record
|
||||
** @see lib::InPlaceAnyHolder usage example to access a subclass in embedded storage
|
||||
**
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UTIL_ACCESS_CASTED_O_H
|
||||
#define UTIL_ACCESS_CASTED_O_H
|
||||
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/type_traits/remove_pointer.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/type_traits/is_polymorphic.hpp>
|
||||
#include <boost/type_traits/is_base_of.hpp>
|
||||
|
||||
|
||||
|
||||
namespace util {
|
||||
using boost::remove_pointer;
|
||||
using boost::remove_reference;
|
||||
using boost::is_convertible;
|
||||
using boost::is_polymorphic;
|
||||
using boost::is_base_of;
|
||||
using boost::enable_if;
|
||||
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast : boost::false_type {};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast<SRC*,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast<SRC*&,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast<SRC&,TAR&> { enum { value = is_base_of<SRC,TAR>::value };};
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct has_RTTI
|
||||
{
|
||||
typedef typename remove_pointer<
|
||||
typename remove_reference<T>::type>::type TPlain;
|
||||
|
||||
enum { value = is_polymorphic<TPlain>::value };
|
||||
};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct use_dynamic_downcast
|
||||
{
|
||||
enum { value = can_cast<SRC,TAR>::value
|
||||
&& has_RTTI<SRC>::value
|
||||
&& has_RTTI<TAR>::value
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct use_static_downcast
|
||||
{
|
||||
enum { value = can_cast<SRC,TAR>::value
|
||||
&& ( !has_RTTI<SRC>::value
|
||||
|| !has_RTTI<TAR>::value
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct use_conversion
|
||||
{
|
||||
enum { value = is_convertible<SRC,TAR>::value
|
||||
&& !( use_static_downcast<SRC,TAR>::value
|
||||
||use_dynamic_downcast<SRC,TAR>::value
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
////////////////////////////////TODO: use lib::NullValue instead
|
||||
template<typename X>
|
||||
struct EmptyVal
|
||||
{
|
||||
static X create() { return X(); }
|
||||
};
|
||||
template<typename X>
|
||||
struct EmptyVal<X*&>
|
||||
{
|
||||
static X*& create() { static X* nullP(0); return nullP; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename RET>
|
||||
struct NullAccessor
|
||||
{
|
||||
typedef RET Ret;
|
||||
|
||||
static RET access (...) { return ifEmpty(); }
|
||||
static RET ifEmpty () { return EmptyVal<RET>::create(); }
|
||||
};
|
||||
|
||||
template<typename TAR>
|
||||
struct AccessCasted : NullAccessor<TAR>
|
||||
{
|
||||
using NullAccessor<TAR>::access;
|
||||
|
||||
template<typename ELM>
|
||||
static typename enable_if< use_dynamic_downcast<ELM&,TAR>,
|
||||
TAR >::type
|
||||
access (ELM& elem)
|
||||
{
|
||||
return dynamic_cast<TAR> (elem);
|
||||
}
|
||||
|
||||
template<typename ELM>
|
||||
static typename enable_if< use_static_downcast<ELM&,TAR>,
|
||||
TAR >::type
|
||||
access (ELM& elem)
|
||||
{
|
||||
return static_cast<TAR> (elem);
|
||||
}
|
||||
|
||||
template<typename ELM>
|
||||
static typename enable_if< use_conversion<ELM&,TAR>,
|
||||
TAR >::type
|
||||
access (ELM& elem)
|
||||
{
|
||||
return elem;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace util
|
||||
#endif
|
||||
|
|
@ -33,7 +33,7 @@
|
|||
** type TAR. Alternatively, you may define an specialisation of EmptyVal,
|
||||
** e.g. throwing an exception instead of creating a NULL value.
|
||||
**
|
||||
** @todo ///////////////////////////////////////////TICKET #738 needs slight overhaul and clean-up
|
||||
** @todo WIP 4/2015 is being rewritten ///////////////////////////////////////////TICKET #738 needs slight overhaul and clean-up
|
||||
** @see lumiera::WrapperPtr usage example to access a variant record
|
||||
** @see lib::InPlaceAnyHolder usage example to access a subclass in embedded storage
|
||||
**
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@
|
|||
|
||||
#include "lib/error.hpp"
|
||||
#include "lib/bool-checkable.hpp"
|
||||
#include "lib/access-casted.hpp"
|
||||
#include "lib/access-casted-o.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
|
||||
#include "lib/variant-o.hpp"
|
||||
#include "lib/access-casted.hpp"
|
||||
#include "lib/access-casted-o.hpp"
|
||||
|
||||
#include "lib/meta/typelist.hpp"
|
||||
#include "proc/mobject/placement.hpp"
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@ TESTING "Library Test Suite: basic and low-level components" ./test-suite --grou
|
|||
|
||||
|
||||
|
||||
TEST "casting or converting access helper" AccessCasted_test <<END
|
||||
END
|
||||
|
||||
|
||||
TEST "CmdlineWrapper_test" CmdlineWrapper_test <<END
|
||||
out-lit: wrapping cmdline:...
|
||||
out-lit: -->
|
||||
|
|
|
|||
|
|
@ -23,145 +23,19 @@
|
|||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/type_traits/is_polymorphic.hpp>
|
||||
#include <boost/type_traits/is_base_of.hpp>
|
||||
#include <boost/type_traits/remove_pointer.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include "lib/access-casted.hpp"
|
||||
|
||||
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::ostream;
|
||||
|
||||
using boost::remove_pointer;
|
||||
using boost::remove_reference;
|
||||
using boost::is_convertible;
|
||||
using boost::is_polymorphic;
|
||||
using boost::is_base_of;
|
||||
using boost::enable_if;
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace meta {
|
||||
namespace util {
|
||||
namespace test {
|
||||
|
||||
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast : boost::false_type {};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast<SRC*,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast<SRC*&,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct can_cast<SRC&,TAR&> { enum { value = is_base_of<SRC,TAR>::value };};
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct has_RTTI
|
||||
{
|
||||
typedef typename remove_pointer<
|
||||
typename remove_reference<T>::type>::type TPlain;
|
||||
|
||||
enum { value = is_polymorphic<TPlain>::value };
|
||||
};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct use_dynamic_downcast
|
||||
{
|
||||
enum { value = can_cast<SRC,TAR>::value
|
||||
&& has_RTTI<SRC>::value
|
||||
&& has_RTTI<TAR>::value
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct use_static_downcast
|
||||
{
|
||||
enum { value = can_cast<SRC,TAR>::value
|
||||
&& ( !has_RTTI<SRC>::value
|
||||
|| !has_RTTI<TAR>::value
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
template <typename SRC, typename TAR>
|
||||
struct use_conversion
|
||||
{
|
||||
enum { value = is_convertible<SRC,TAR>::value
|
||||
&& !( use_static_downcast<SRC,TAR>::value
|
||||
||use_dynamic_downcast<SRC,TAR>::value
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
template<typename X>
|
||||
struct EmptyVal
|
||||
{
|
||||
static X create()
|
||||
{
|
||||
cout << " NULL() " << __PRETTY_FUNCTION__ <<"\n";
|
||||
return X();
|
||||
}
|
||||
};
|
||||
template<typename X>
|
||||
struct EmptyVal<X*&>
|
||||
{
|
||||
static X*& create()
|
||||
{
|
||||
cout << " NULL & " << __PRETTY_FUNCTION__ <<"\n";
|
||||
static X* null(0);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename RET>
|
||||
struct NullAccessor
|
||||
{
|
||||
typedef RET Ret;
|
||||
|
||||
static RET access (...) { return ifEmpty(); }
|
||||
static RET ifEmpty () { return EmptyVal<RET>::create(); }
|
||||
};
|
||||
|
||||
template<typename TAR>
|
||||
struct AccessCasted : NullAccessor<TAR>
|
||||
{
|
||||
using NullAccessor<TAR>::access;
|
||||
|
||||
template<typename ELM>
|
||||
static typename enable_if< use_dynamic_downcast<ELM&,TAR>, TAR>::type
|
||||
access (ELM& elem)
|
||||
{
|
||||
cout << " dynamic " << __PRETTY_FUNCTION__ <<"\n";
|
||||
return dynamic_cast<TAR> (elem);
|
||||
}
|
||||
|
||||
template<typename ELM>
|
||||
static typename enable_if< use_static_downcast<ELM&,TAR>, TAR>::type
|
||||
access (ELM& elem)
|
||||
{
|
||||
cout << " static " << __PRETTY_FUNCTION__ <<"\n";
|
||||
return static_cast<TAR> (elem);
|
||||
}
|
||||
|
||||
template<typename ELM>
|
||||
static typename enable_if< use_conversion<ELM&,TAR>, TAR>::type
|
||||
access (ELM& elem)
|
||||
{
|
||||
cout << " convert " << __PRETTY_FUNCTION__ <<"\n";
|
||||
return elem;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
|
@ -273,4 +147,4 @@ namespace test {
|
|||
|
||||
|
||||
|
||||
}}} // namespace lib::meta::test
|
||||
}} // namespace lib::meta::test
|
||||
|
|
|
|||
Loading…
Reference in a new issue