GenericType.h 2.16 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
11
 * @version 1.0
 */

#include <stddef.h>
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
12
#include <iomanip>
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
13
14
15
16
17
#include <sstream>
#include <string>

namespace ElemUtils {

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
18
19
20
21
22
23
24
/**
 * @class GenericType
 *
 * @brief A complex object to handle different simple type (like string, int, double, bool ...) into a unique stream.
 * It used to provide a way to handle generic type.
 * Then there is a method for each simple type that you want to get back.
 */
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
25
26
27
28
29
30
31
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
32
    GenericType(const T &value) {
Nabil Chouika's avatar
refs#16    
Nabil Chouika committed
33
        m_stream << std::setprecision(16) << value;
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
34
35
36
37
38
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
    }

    /**
     * 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
88
89
90
    std::string getString() const;

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

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

Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
94
95
96
97
98
99
100
101
102
103
104
105
106
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 */