File: classstructure.yo

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-- 3,651 bytes parent folder | download
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
    Some software engineers have reservations when thinking about the
tt(Storage) class that uses pointers to store copies of value class
objects. Their argument is that value class objects can very well be stored by
value, rather than by pointer. They'd rather store value class objects by
value and polymorphic class objects by pointer.

    Such distinctions  frequently occur in template meta programming and
the following tt(struct IfElse)
        hi(template: IfElse)
        hi(template: select type given bool)
    may be used to obtain one of two types, depending on a tt(bool) selector
value.

    First define the em(generic form) of the template:
        verb(    template<bool selector, typename FirstType, typename SecondType>
    struct IfElse
    {
        typedef FirstType type;
    };)

Then define a partial specialization. The specialization represents
a specific selector value (e.g., tt(false)) and leaves the remaining types
open to further specification:
        verb(    template<typename FirstType, typename SecondType>
    struct IfElse<false, FirstType, SecondType>
    {
        typedef SecondType type;
    };)

The former (generic) definition associates tt(FirstType) with the
tt(IfElse::type) type definition, the latter definition (partially specialized
for the logical value tt(false)) associates tt(SecondType) with the
tt(IfElse::type) type definition.

The tt(IfElse) template allows us to define class templates whose data
organization is conditional to the template's parameters.
    hi(class template: conditional data organization)
 Using tt(IfElse) the tt(Storage) class may define
em(pointers) to store copies of polymorphic class type objects and em(values)
to store value class type objects:
        verb(    template <typename Type, bool isPolymorphic>
    class Storage
    {
        typedef typename IfElse<isPolymorphic, Type *, Type>::type
                DataType;

        std::vector<DataType> d_data;

        private:
            void add(Type const &obj, IntType<true>);
            void add(Type const &obj, IntType<false>);
        public:
            void add(Type const &obj);
    }

    template <typename Type, bool isPolymorphic>
    void Storage<Type, isPolymorphic>::add(Type const &obj, IntType<true>)
    {
        d_data.push_back(obj.clone());
    }

    template <typename Type, bool isPolymorphic>
    void Storage<Type, isPolymorphic>::add(Type const &obj, IntType<false>)
    {
        d_data.push_back(obj);
    }

    template <typename Type, bool isPolymorphic>
    void Storage<Type, isPolymorphic>::add(Type const &obj)
    {
        add(obj, IntType<isPolymorphic>());
    })

The above example uses tt(IfElse)'s tt(type), defined by tt(IfElse) as
either tt(FirstType) or tt(SecondType). tt(IfElse)'s tt(type) defines the
actual data type to use for tt(Storage)'s tt(vector) data type.

    The remarkable result in this example is that the em(data organization) of
the tt(Storage) class now depends on its template arguments. Since the
tt(isPolymorphic == true) situation uses different data types than the
tt(isPolymorphic == false) situation, the overloaded private tt(add) members
can utilize this difference immediately. E.g., tt(add(Type const &obj,
IntType<false>)) uses direct copy construction to store a copy of tt(obj)
in tt(d_vector).

    It is also possible to make a selection from multiple types as tt(IfElse)
structs can be nested. Realize that using tt(IfElse) never has any effect on
the size or execution time of the final executable program. The final program
simply contains the appropriate type, conditional to the type that's
eventually selected.