File: union.h

package info (click to toggle)
c%2B%2B-annotations 11.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,244 kB
  • sloc: cpp: 21,698; makefile: 1,505; ansic: 165; sh: 121; perl: 90
file content (90 lines) | stat: -rw-r--r-- 2,311 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
89
90
#ifndef INCLUDED_UNION_
#define INCLUDED_UNION_

#include <string>

union Union
{
    enum Field                               // indicators for the used type(s)
    {
        TEXT,
        VALUE
    };

    struct Cp
    {
        Field current;
        Union const &other;
        Field next;
    };

    struct Mv
    {
        Field current;
        Union &&other;
        Field next;
    };

    private:
        std::string u_text;
        int u_value;

        static void (Union::*s_destroy[])();
        static void (Union::*s_swap[][2])(Union &other);
        static void (Union::*s_copy[])(Union const &other);

    public:
        Union(Union const &other) = delete;     // no std constructors and
        ~Union();                               // an empty destructor in
                                                // unrestrestricted unions

        Union(std::string const &text);     // init u_text      1.cc
        Union(int value);                   // init u_value     2.cc

        Union(Union const &other, Field type); // init from other, type 3.cc
        Union(Union &&tmp, Field type);        // init from tmp, type   4.cc

        Union operator=(Cp rhs);

            // acts like copy assignment. We never use sequential assignment
            // on unrestr. unions so void is returned
        void copy(Field current, Union const &other, Field next);  // 1.cc

            // like move assignment: same considerations.
        void move(Field current, Union &&tmp, Field next);         // 1.cc

        void swap(Field current, Union &other, Field next);

        void destroy(Field type);

        std::string const &text() const;
        int value() const;

    private:
        void destroyText();
        void destroyValue();                    // noOp

        void copyText(Union const &other);
        void copyValue(Union const &other);

        void swap2Text(Union &other);            // swaps equal types
        void swapTextValue(Union &other);

        void swapValueText(Union &other);        // (reverses operands)
        void swap2Value(Union &other);           // swaps equal types
};

inline void Union::destroyValue()
{}

inline std::string const &Union::text() const
{
    return u_text;
}

inline int Union::value() const
{
    return u_value;
}

#endif