GenericType.h 2.63 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>
16
#include <vector>
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
17
18
19

namespace ElemUtils {

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
20
21
22
/**
 * @class GenericType
 *
23
24
25
26
27
28
29
 * @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
30
 */
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
31
32
33
34
35
36
37
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
38
    GenericType(const T &value) {
Nabil Chouika's avatar
refs#16    
Nabil Chouika committed
39
        m_stream << std::setprecision(std::numeric_limits<double>::digits10) << value;
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
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
    }

    /**
     * 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;

90
91
    std::vector<double> toVectorDouble() const;

92
93
    std::vector<int> toVectorInt() const;

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

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

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

Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
104
105
106
107
108
109
110
111
112
113
114
115
116
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 */