Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:43:50

0001 #ifndef GENERIC_TYPE_H
0002 #define GENERIC_TYPE_H
0003 
0004 /**
0005  * @file GenericType.h
0006  * @author Bryan BERTHOU (SPhN / CEA Saclay)
0007  * @date July 03, 2014
0008  * @version 1.0
0009  */
0010 
0011 #include <iomanip>
0012 #include <limits>
0013 #include <sstream>
0014 #include <string>
0015 #include <cstddef>
0016 #include <vector>
0017 
0018 namespace ElemUtils {
0019 
0020 /**
0021  * @class GenericType
0022  *
0023  * @brief A complex object to handle different simple types in a uniform way.
0024  * Simple types considered: double, int, unsigned int, boolean, short, unsigned short, string, etc.
0025  * Various simple types are stored into a unique stream, providing a way to handle a generic type.
0026  * Each simple type can be retrieved from the stream using an appropriate method.
0027  * For example toInt() or toDouble() return respectively int or double variables.
0028  *
0029  * Warning: a generic type object can be defined from any class, but the adequate conversion function may have to be added.
0030  */
0031 class GenericType {
0032 public:
0033     /**
0034      * Construct a GenericType object from any simple type
0035      * @param value
0036      */
0037     template<class T>
0038     GenericType(const T &value) {
0039         m_stream << std::setprecision(std::numeric_limits<double>::digits10) << value;
0040     }
0041 
0042     /**
0043      * Default destructor
0044      */
0045     virtual ~GenericType();
0046 
0047     /**
0048      * Copy constructor
0049      * @param other
0050      */
0051     GenericType(const GenericType &other);
0052 
0053     /**
0054      * Convert stream to double
0055      * @return double
0056      */
0057     double toDouble() const;
0058 
0059     /**
0060      * Concert stream to integer
0061      * @return int
0062      */
0063     int toInt() const;
0064 
0065     /**
0066      * Convert stream to unsigned integer
0067      * @return unsigned int
0068      */
0069     unsigned int toUInt() const;
0070 
0071     /**
0072      * Convert stream to boolean
0073      *
0074      * @return bool
0075      */
0076     bool toBoolean() const;
0077 
0078     /**
0079      * Convert stream to short
0080      */
0081     short toShort() const;
0082 
0083     /**
0084      * Convert stream to unsigned short
0085      */
0086     unsigned short toUShort() const;
0087 
0088     size_t toSize_t() const;
0089 
0090     std::vector<double> toVectorDouble() const;
0091 
0092     std::vector<int> toVectorInt() const;
0093 
0094     /**
0095      * Convert stream to string
0096      * @return std::string
0097      */
0098     std::string getString() const;
0099 
0100     GenericType& operator=(const GenericType & rhs);
0101 
0102     void setValue(const GenericType &value);
0103 
0104 private:
0105     std::stringstream m_stream; ///< stream to store serialized simple type
0106 
0107     ////////////////////////////////////////////////////////////
0108     /// Disallow comparisons between GenericTypes
0109     ////////////////////////////////////////////////////////////
0110     bool operator ==(const GenericType& right) const;
0111     bool operator !=(const GenericType& right) const;
0112 };
0113 
0114 } // namespace NumA
0115 
0116 #endif /* GENERIC_TYPE_H */