~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty

« back to all changes in this revision

Viewing changes to yo/containers/embedding.yo

  • Committer: Package Import Robot
  • Author(s): tony mancill, Frank B. Brokken, tony mancill
  • Date: 2012-02-28 00:50:21 UTC
  • mfrom: (1.1.19)
  • Revision ID: package-import@ubuntu.com-20120228005021-sz7nnodntkvgh7qf
Tags: 9.2.1-1
[ Frank B. Brokken ]
* New upstream release (using flexc++, reauthored polymorphic semantic
  values and unrestricted unions). Upstream release 9.2.0 is implied by
  this release.

[ tony mancill ]
* Set Standards-Version to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Next, we embed the unrestricted union in a surrounding aggregate: tt(class
 
2
Data). The aggregate is provided with an tt(enum Tag), declared in its public
 
3
section, so tt(Data)'s users may request tags. tt(Union) itself is for
 
4
tt(Data)'s internal use only, so tt(Union) is declared in tt(Data)'s private
 
5
section. Using a tt(struct Data) rather than tt(class Data) we start out
 
6
in a public section, saving us from having to specify the initial tt(public:)
 
7
section for tt(enum Tag):
 
8
        verb(
 
9
    struct Data
 
10
    {
 
11
        enum Tag
 
12
        {
 
13
            INT,
 
14
            STRING
 
15
        };
 
16
 
 
17
        private:
 
18
            union Union
 
19
            {
 
20
                int         u_int;
 
21
                std::string u_string;
 
22
 
 
23
                ~Union();           // no actions
 
24
                // ... to do: declarations of members
 
25
            };
 
26
 
 
27
            Tag d_tag;
 
28
            Union d_union;
 
29
    };
 
30
        )
 
31
 
 
32
tt(Data)'s constructors receive tt(int) or tt(string) values. To pass these
 
33
values on to tt(d_union), we need tt(Union) constructors for the various union
 
34
fields; matching tt(Data) constructors also initialize tt(d_tag) to proper
 
35
values:
 
36
        verb(
 
37
    Data::Union::Union(int value)
 
38
    :
 
39
        u_int(value)
 
40
    {}
 
41
    Data::Union::Union(std::string const &str)
 
42
    :
 
43
        u_string(str)
 
44
    {}
 
45
 
 
46
    Data::Data(std::string const &str)
 
47
    :
 
48
        d_tag(STRING),
 
49
        d_union(str)
 
50
    {}
 
51
    Data::Data(int value)
 
52
    :
 
53
        d_tag(INT),
 
54
        d_union(value)
 
55
    {}
 
56
        )