List of all members | Static Public Member Functions | Static Public Attributes | Static Private Member Functions
utl::ConsecutiveEnumFactory< EnumType, last, tags, NoConversionPolicy, first > Class Template Reference

Simple factory to create an enumerator for a given enumeration. More...

#include "utl/ConsecutiveEnumFactory.h"

Static Public Member Functions

static EnumType Create (const int k)
 int version of the overloaded creation method. More...
 
static EnumType Create (const std::string &tag)
 std::string version of the overloaded creation method. More...
 

Static Public Attributes

static const unsigned int kNumEnum = last - first + 1
 

Static Private Member Functions

template<typename T >
static EnumType Handle (const T &t)
 Error handling. More...
 

Detailed Description

template<typename EnumType, EnumType last, const char *const tags, class NoConversionPolicy = ThrowPolicy<EnumType, utl::IOFailureException>, EnumType first = static_cast<EnumType>(0)>
class utl::ConsecutiveEnumFactory< EnumType, last, tags, NoConversionPolicy, first >

Simple factory to create an enumerator for a given enumeration.

Provides two alternatives for creation, via an int or via an std::string, both under the form of an overloaded method called Create.

As implied by the name, it is required (but not actually checked by some language construct) that the enumeration is defined with consecutive values. Also, the fact that EnumType is actually an enum is not checked. The behavior under error (i.e. no enumerator with the given value) is defined by NoConversionPolicy. The implied condition First <= Last is not checked either. This method is mainly useful in terms of I/O of enumerators.

The Tags is expected to have a length equal to the number of enumerators (as implied by First and Last) and to be in correspondence with the order defined in the enumertion. That is Tags[i] contains the label for the enumerator with First + i integral value.

It may be convinent for clients to define a typedef as an alias for the particular needed instantiation of this class.

For a given enumerator type there should be a single instantiation of the template class.

Parameters
EnumTypeThe actual enumeration type.
LastThe greatest enumerator.
TagsLabels for each of the enumerators.
NoConversionPolicyPolicy type for no conversion. It is supposed to define a method compatible with the following signature EnumType Handle(const std::string&). This argument defaults to an exception throwing policy (utl::ThrowPolicy) , with exception type utl::IOFailureException. Via the allowed return value of Handle the user can provide an error-condition value.
FirstThe smallest enumerator. Defaults to zero (expicitly converted to the EnumType).

This class is related, is some way, to utl::ObjectFactory but this last class is much more complex (and useful) than the one here defined.

Todo:
Better diagnosis based on the actual enumeration type name.
Todo:
Maybe is useful to default the tags to null, and so handle properly the situation.
Todo:
Telescope::CachePixelCalibrations() handles a situation like this. It seems better to avoid that style.
  • It seems better to have the convertion be defined "next to" the list of enumerators,and avoid client code (Telescope in this case) to explicitly "know" the full list. Anyway in this case there is a pretty tight relation between both classes.
  • Declares a double to be filled by GetPixelData which is the usual templated proxy function for managers. I do not see why not an int?
  • Then C-style casts the double value to int. By the way, it does so in the ugly way "(int)value" instead of "int(value)". See Offline's style guide for C++ code.
  • Then does an in-place exhaustive switch-case over the enumeration.
  • Has a default to unkown that allows "trash" in the configuration data (when there is also a precise value for unknown).
Todo:
For compile-time checking consider the use of Boost library, in particular boost/type_traits/is_enum.hpp and boost/concept_check.hpp. By now it does not seem worth of it (mainly due that as of Boost v1.34.1 is_enum is broken under some compilers).
Todo:
Can the case of non-consecutive values be handled (without passing as a template argument an array with the values)?. In this case of passing the values via an another arg, it seems that it cannot be used to determine the case: one can think (at least I did) that this new argument can defaulted to null, and then if the user does not supply it an special implementation (assuming consecutive values) can be instantiated thanks to a class/struct specialization for null pointers. The problems seems to be item 5 paragraph 2 of section 14.3.2 (Template non-type arguments) of the ISO/IEC 14882 C++ Standard where it is said: "Although 0 is a valid template-argument for a non-type template-parameter of integral type, it is not a valid template-argument for a non-type template-parameter of pointer type". In any case, it seems little risky to allow a default parameter, given thet its omission would imply the asumption of consecutiveness. So it would be better to code a separate class with the following template signature: template< typename EnumType, unsigned int NumEnum, const EnumType Values[], const char* const Tags[], class NoConversionPolicy = ThrowPolicy<EnumType, utl::IOFailureException>, >
Author
Rodolfo Federico Gamarra
Date
10 Jan 2009

Definition at line 118 of file ConsecutiveEnumFactory.h.

Member Function Documentation

template<typename EnumType , EnumType last, const char *const tags, class NoConversionPolicy = ThrowPolicy<EnumType, utl::IOFailureException>, EnumType first = static_cast<EnumType>(0)>
static EnumType utl::ConsecutiveEnumFactory< EnumType, last, tags, NoConversionPolicy, first >::Create ( const int  k)
inlinestatic
template<typename EnumType , EnumType last, const char *const tags, class NoConversionPolicy = ThrowPolicy<EnumType, utl::IOFailureException>, EnumType first = static_cast<EnumType>(0)>
static EnumType utl::ConsecutiveEnumFactory< EnumType, last, tags, NoConversionPolicy, first >::Create ( const std::string &  tag)
inlinestatic

std::string version of the overloaded creation method.

Definition at line 148 of file ConsecutiveEnumFactory.h.

template<typename EnumType , EnumType last, const char *const tags, class NoConversionPolicy = ThrowPolicy<EnumType, utl::IOFailureException>, EnumType first = static_cast<EnumType>(0)>
template<typename T >
static EnumType utl::ConsecutiveEnumFactory< EnumType, last, tags, NoConversionPolicy, first >::Handle ( const T &  t)
inlinestaticprivate

Error handling.

Definition at line 123 of file ConsecutiveEnumFactory.h.

Member Data Documentation

template<typename EnumType , EnumType last, const char *const tags, class NoConversionPolicy = ThrowPolicy<EnumType, utl::IOFailureException>, EnumType first = static_cast<EnumType>(0)>
const unsigned int utl::ConsecutiveEnumFactory< EnumType, last, tags, NoConversionPolicy, first >::kNumEnum = last - first + 1
static

Number of enumerators. Calculated based on the fact that are consecutive.

Definition at line 135 of file ConsecutiveEnumFactory.h.


The documentation for this class was generated from the following file:

, generated on Tue Sep 26 2023.