GenericType.h 2.52 KB
Newer Older
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
1
2
3
4
5
6
#ifndef GENERIC_TYPE_H
#define GENERIC_TYPE_H

/**
 * @file GenericType.h
 * @author Bryan BERTHOU (SPhN / CEA Saclay)
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
7
 * @date July 03, 2014
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
8
9
10
 * @version 1.0
 */

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
11
#include <iomanip>
Nabil Chouika's avatar
refs#16    
Nabil Chouika committed
12
#include <limits>
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
13
14
#include <sstream>
#include <string>
Nabil Chouika's avatar
refs#16    
Nabil Chouika committed
15
#include <cstddef>
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
16
17
18

namespace ElemUtils {

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
19
20
21
/**
 * @class GenericType
 *
22
23
24
25
26
27
28
 * @brief A complex object to handle different simple types in a uniform way.
 * Simple types considered: double, int, unsigned int, boolean, short, unsigned short, string, etc.
 * Various simple types are stored into a unique stream, providing a way to handle a generic type.
 * Each simple type can be retrieved from the stream using an appropriate method.
 * For example toInt() or toDouble() return respectively int or double variables.
 *
 * Warning: a generic type object can be defined from any class, but the adequate conversion function may have to be added.
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
29
 */
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
30
31
32
33
34
35
36
class GenericType {
public:
    /**
     * Construct a GenericType object from any simple type
     * @param value
     */
    template<class T>
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
37
    GenericType(const T &value) {
Nabil Chouika's avatar
refs#16    
Nabil Chouika committed
38
        m_stream << std::setprecision(std::numeric_limits<double>::digits10) << value;
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    }

    /**
     * Default destructor
     */
    virtual ~GenericType();

    /**
     * Copy constructor
     * @param other
     */
    GenericType(const GenericType &other);

    /**
     * Convert stream to double
     * @return double
     */
    double toDouble() const;

    /**
     * Concert stream to integer
     * @return int
     */
    int toInt() const;

    /**
     * Convert stream to unsigned integer
     * @return unsigned int
     */
    unsigned int toUInt() const;

    /**
     * Convert stream to boolean
     *
     * @return bool
     */
    bool toBoolean() const;

    /**
     * Convert stream to short
     */
    short toShort() const;

    /**
     * Convert stream to unsigned short
     */
    unsigned short toUShort() const;

    size_t toSize_t() const;

    /**
     * Convert stream to string
     * @return std::string
     */
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
93
94
95
    std::string getString() const;

    GenericType& operator=(const GenericType & rhs);
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
96

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
97
    void setValue(const GenericType &value);
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
98

Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
99
100
101
102
103
104
105
106
107
108
109
110
111
private:
    std::stringstream m_stream; ///< stream to store serialized simple type

    ////////////////////////////////////////////////////////////
    /// Disallow comparisons between GenericTypes
    ////////////////////////////////////////////////////////////
    bool operator ==(const GenericType& right) const;
    bool operator !=(const GenericType& right) const;
};

} // namespace NumA

#endif /* GENERIC_TYPE_H */