GenericType.h 2.1 KB
Newer Older
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef GENERIC_TYPE_H
#define GENERIC_TYPE_H

/**
 * @file GenericType.h
 * @author Bryan BERTHOU (SPhN / CEA Saclay)
 * @date 03 July 2014
 * @version 1.0
 *
 * @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.
 */

#include <stddef.h>
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
18
#include <iomanip>
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <sstream>
#include <string>

namespace ElemUtils {

class GenericType {
public:
    /**
     * Construct a GenericType object from any simple type
     * @param value
     */
    template<class T>
    GenericType(T value) {
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
32
        m_stream << std::setprecision(17) << value;
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
33
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
88
    }

    /**
     * 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
     */
    std::string toString() const;

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

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