fw4spl
DynamicType.hxx
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWTOOLS_DYNAMICTYPE_HXX__
8 #define __FWTOOLS_DYNAMICTYPE_HXX__
9 
10 #include <algorithm>
11 #include <limits>
12 #include <fwCore/base.hpp>
13 
14 #include "fwTools/StringKeyTypeMapping.hpp"
15 #include "fwTools/Dispatcher.hpp"
16 
17 namespace fwTools
18 {
19 
20 template<class TYPE>
22 {
23  DynamicType d;
24  d.template setType<TYPE>();
25  return d;
26 }
27 
28 struct TypeSetter
29 {
30  template<class TYPE>
31  void operator()( DynamicType & dynamicType )
32  {
33  dynamicType.setType<TYPE>();
34  }
35 };
36 
37 template<class KEYTYPE>
38 DynamicType makeDynamicType(const KEYTYPE &keyType)
39 {
40  DynamicType d;
42  assert ( d != DynamicType() ); // a type must be found !!
43  return d;
44 }
45 
46 
47 
48 template<class T>
50 {
51 public:
52 
53  template< typename PIXEL >
54  void operator()( std::pair<T,T> &minMax )
55  {
56  minMax.first = static_cast< T >( std::numeric_limits< PIXEL >::min() );
57  minMax.second = static_cast< T >( std::numeric_limits< PIXEL >::max() );
58 
59  ::fwTools::DynamicType type = ::fwTools::makeDynamicType< PIXEL >();
60  if(!std::numeric_limits< PIXEL >::is_integer)
61  {
62  // std::numeric_limits::min() returns the smallest positive value for floating types
63  minMax.first = minMax.second * -1;
64  }
65  }
66 };
67 
68 template< class TYPE>
70 {
71  std::list< std::string>::const_iterator supportedTypesIter;
72 
73  supportedTypesIter = m_managedTypes.begin();
74  while ( supportedTypesIter != m_managedTypes.end() )
75  {
76  if ( isMapping<TYPE>( *supportedTypesIter) )
77  {
78  m_value = *supportedTypesIter;
79  m_sizeof = sizeof(TYPE);
80  return;
81  }
82  ++supportedTypesIter;
83  }
84  throw std::invalid_argument("DynamicType::setType<TYPE> incorrect TYPE");
85 }
86 
87 
88 template< class TYPE>
89 bool DynamicType::isType() const
90 {
91  return isMapping<TYPE>(m_value);
92 }
93 
94 
95 template<class TYPE>
96 const std::string DynamicType::string()
97 {
98  DynamicType d;
99  d.setType<TYPE>();
100  return d.string();
101 }
102 
103 
104 template<class NEWTYPE>
105 void DynamicType::registerNewType(const std::string &newKey)
106 {
107  // ensure isMapping present and well defined
108  // if prog trap here it is because is Mapping is not well defined !!!
109  if ( isMapping<NEWTYPE>(newKey) == false )
110  {
111  throw std::invalid_argument("Dynamic::registerNewType misconception with isMapping");
112  }
113 
114  // ensure that newKey is not already used
115  if ( std::find( m_managedTypes.begin(),m_managedTypes.end(), newKey ) != m_managedTypes.end() )
116  {
117  throw std::invalid_argument("Dynamic::registerNewType newKey already used");
118  }
119 
120  // ensure that no other mapping respond to newkey
121  try
122  {
123  DynamicType dummy;
124  dummy.setType<NEWTYPE>();
125  }
126  catch ( std::exception )
127  {
128  // OK it is really a new type, insert it
129  m_managedTypes.push_back(newKey);
130  return;
131  }
132 
133  throw std::invalid_argument("Dynamic::registerNewType another isMapping is responding");
134 }
135 
136 
137 template<class T>
138 std::pair<T,T> DynamicType::minMax()
139 {
140  SLM_ASSERT("Unable to have minMax for UnSpecifiedType", this->string() != DynamicType::m_unSpecifiedType);
141  std::pair<T,T> minMax;
143  return minMax;
144 }
145 
146 } //end namespace fwTools
147 
148 #endif /*__FWTOOLS_DYNAMICTYPE_HXX__*/
static void registerNewType(const std::string &newKey)
Register a new type to be managed within DynamicType.
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
Class defining an elementary C++ type aka unsigned char, signed char, .... signed long...
Definition: DynamicType.hpp:31
void setType()
Set DynamicType value according given template.
Definition: DynamicType.hxx:69
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
static void invoke()
Instanciate and invoke all functors.
Definition: Dispatcher.hpp:99
FWTOOLS_API const std::string & string() const
Return a human readable string.
Definition: DynamicType.cpp:74
bool isType() const
Return true iff the DynamicType value represents the TYPE.
Definition: DynamicType.hxx:89
DynamicType makeDynamicType()
Helper to create object DynamicType from a given type TYPE in { (un)signed char, ... , double }.
Definition: DynamicType.hxx:21
Create an automatic template instancier exple Dispatcher< TYPESEQUENCE , FUNCTOR>::invoke("int");.
Definition: Dispatcher.hpp:85
std::pair< T, T > minMax()
return the min and max storable in the DynamicType. take care that min/max value are casted into temp...
static FWTOOLS_API const std::string m_unSpecifiedType
Value for not specified type.