Revision e1cb2c92 include/VirtualMachineAttribute.h

View differences:

include/VirtualMachineAttribute.h
17 17
#ifndef VIRTUAL_MACHINE_ATTRIBUTE_H_
18 18
#define VIRTUAL_MACHINE_ATTRIBUTE_H_
19 19

  
20
#include <vector>
21
#include <map>
22

  
23
#include "Attribute.h"
24
#include "Template.h"
20
#include "ExtendedAttribute.h"
25 21

  
26 22
/**
27 23
 *  This class represents a generic VirtualMachine attribute, it exposes the
......
31 27
 *  The attribute operates directly on the VirtualMachineTemplate attribute. IT
32 28
 *  IS NOT CLONED OR COPIED
33 29
 */
34
class VirtualMachineAttribute: public Attribute
30
class VirtualMachineAttribute: public ExtendedAttribute
35 31
{
36
public:
37
    /**
38
     *  Return the associated VectorAttribute to interface with vector attribute
39
     *  functions
40
     */
41
    VectorAttribute * vector_attribute()
42
    {
43
        return va;
44
    }
45

  
46
    /* ---------------------------------------------------------------------- */
47
    /* VectorAttribute Interface                                              */
48
    /* ---------------------------------------------------------------------- */
49
    template<typename T>
50
    int vector_value(const std::string& name, T& value) const
51
    {
52
        return va->vector_value(name, value);
53
    }
54

  
55
    string vector_value(const std::string& name) const
56
    {
57
        return va->vector_value(name);
58
    }
59

  
60
    template<typename T>
61
    void replace(const std::string& name, T value)
62
    {
63
        va->replace(name, value);
64
    }
65

  
66
    void remove(const std::string& name)
67
    {
68
        va->remove(name);
69
    }
70

  
71

  
72
    void merge(VectorAttribute* vattr, bool replace)
73
    {
74
        va->merge(vattr, replace);
75
    }
76

  
77 32
protected:
78 33
    /**
79 34
     *  Creates the attribute with a reference to a VectorAttribute. The object
......
81 36
     *    @param va pointer to the VectorAttribute.
82 37
     */
83 38
    VirtualMachineAttribute(VectorAttribute *_va):
84
        Attribute(_va->name()) ,va(_va), id(-1) {};
39
        ExtendedAttribute(_va){};
85 40

  
86 41
    VirtualMachineAttribute(VectorAttribute *_va, int _id):
87
        Attribute(_va->name()) ,va(_va), id(_id) {};
42
        ExtendedAttribute(_va, _id) {};
88 43

  
89 44
    virtual ~VirtualMachineAttribute(){};
90 45

  
91 46
    /* ---------------------------------------------------------------------- */
92
    /* Attribute Interface                                                    */
93
    /* ---------------------------------------------------------------------- */
94
    string * marshall(const char * _sep = 0) const
95
    {
96
        return va->marshall(_sep);
97
    };
98

  
99
    string * to_xml() const
100
    {
101
        return va->to_xml();
102
    };
103

  
104
    void unmarshall(const std::string& sattr, const char * _sep = 0)
105
    {
106
        va->unmarshall(sattr, _sep);
107
    }
108

  
109
    AttributeType type()
110
    {
111
        return va->type();
112
    };
113

  
114
    Attribute* clone() const
115
    {
116
        return va->clone();
117
    };
118

  
119
    /* ---------------------------------------------------------------------- */
120 47
    /* VirtualMachineAttribute Interface                                      */
121 48
    /* ---------------------------------------------------------------------- */
122 49
    /**
......
139 66
    {
140 67
        bool value;
141 68

  
142
        va->vector_value(flag, value);
69
        vector_value(flag, value);
143 70

  
144 71
        return value;
145 72
    }
146 73

  
147
    int get_id() const
148
    {
149
        return id;
150
    }
151

  
152 74
private:
153

  
154
    /* ---------------------------------------------------------------------- */
155
    /* ---------------------------------------------------------------------- */
156 75
    friend class VirtualMachineAttributeSet;
157

  
158
    /* ---------------------------------------------------------------------- */
159
    /* ---------------------------------------------------------------------- */
160
    /**
161
     *  The associated VectorAttribute
162
     */
163
    VectorAttribute * va;
164

  
165
    /**
166
     *  Set if the attribute can be addressed by an identifier, -1 otherwise
167
     */
168
    int id;
169 76
};
170 77

  
171 78
/**
172 79
 *  This class represents a set of VirtualMachineAttributes it provides fast
173 80
 *  access to individual elements (by ID) and implement collective operations
174 81
 */
175
class VirtualMachineAttributeSet
82
class VirtualMachineAttributeSet : public ExtendedAttributeSet
176 83
{
177 84
protected:
178 85
    /**
179 86
     *  Creates the VirtualMachineAttribute set
180 87
     *    @param dispose elements upon set destruction
181 88
     */
182
    VirtualMachineAttributeSet(bool _dispose):dispose(_dispose){};
89
    VirtualMachineAttributeSet(bool _dispose):ExtendedAttributeSet(_dispose){};
183 90

  
184
    virtual ~VirtualMachineAttributeSet();
91
    virtual ~VirtualMachineAttributeSet(){};
185 92

  
186 93
    /* ---------------------------------------------------------------------- */
187 94
    /* Methods to access attributes                                           */
......
189 96
    /**
190 97
     *  @return attribute by id or 0 if not found
191 98
     */
192
    VirtualMachineAttribute * get_attribute(int id) const;
99
    VirtualMachineAttribute * get_attribute(int id) const
100
    {
101
        return static_cast<VirtualMachineAttribute *>(get_attribute(id));
102
    }
193 103

  
194 104
    /**
195 105
     *  @return attribute with the given flag set or 0 if not found
......
217 127
    VirtualMachineAttribute * clear_flag(const string& flag_name);
218 128

  
219 129
    /* ---------------------------------------------------------------------- */
220
    /* Iterators                                                              */
221
    /* ---------------------------------------------------------------------- */
222
    /**
223
     *  Generic iterator for the set. Wraps the STL iterator for map, can be
224
     *  used to iterate over the attributes
225
     */
226
    class AttributeIterator
227
    {
228
    public:
229
        AttributeIterator& operator=(const AttributeIterator& rhs)
230
        {
231
            map_it = rhs.map_it;
232
            return *this;
233
        }
234

  
235
        AttributeIterator& operator++()
236
        {
237
            ++map_it;
238
            return *this;
239
        }
240

  
241
        bool operator!=(const AttributeIterator& rhs)
242
        {
243
            return map_it != rhs.map_it;
244
        }
245

  
246
        AttributeIterator(){};
247
        AttributeIterator(const AttributeIterator& ait):map_it(ait.map_it){};
248
        AttributeIterator(const std::map<int,
249
                VirtualMachineAttribute *>::iterator& _map_it):map_it(_map_it){};
250

  
251
        virtual ~AttributeIterator(){};
252

  
253
    protected:
254
        std::map<int, VirtualMachineAttribute *>::iterator map_it;
255
    };
256

  
257
    AttributeIterator begin()
258
    {
259
        AttributeIterator it(a_set.begin());
260
        return it;
261
    }
262

  
263
    AttributeIterator end()
264
    {
265
        AttributeIterator it(a_set.end());
266
        return it;
267
    }
268

  
269
    typedef class AttributeIterator attribute_iterator;
270

  
271
    /* ---------------------------------------------------------------------- */
272 130
    /* Attribute map interface                                                */
273 131
    /* ---------------------------------------------------------------------- */
274 132
    /**
275
     *  Adds a new VirtualMachine attribute to the set
276
     */
277
    void add_attribute(VirtualMachineAttribute * a, int id)
278
    {
279
        a_set.insert(make_pair(id, a));
280
    }
281

  
282
    /**
283
     *  Init the attribute set from a vector
284
     *    @param id_name with the ID of the attribute
285
     *    @param auto_ids automatically generate ids for the attributes
286
     *    @param vas vector of attribute to use
287
     */
288
    void init_attribute_map(const std::string& id_name,
289
            std::vector<VectorAttribute *>& vas);
290
    /**
291 133
     *  Abstract method to create the VirtualMachineAttributes for this set
292 134
     */
293
    virtual VirtualMachineAttribute * attribute_factory(VectorAttribute * va,
135
    virtual ExtendedAttribute * attribute_factory(VectorAttribute * va,
294 136
            int id) const = 0;
295

  
296
    /* ---------------------------------------------------------------------- */
297
    /* ---------------------------------------------------------------------- */
298

  
299
    /**
300
     *  Map with the disk attributes
301
     */
302
    std::map<int, VirtualMachineAttribute *> a_set;
303

  
304
    /**
305
     *  Frees the VectorAttribute associated with each VirtualMachineAttribute
306
     *  upon object destruction
307
     */
308
    bool dispose;
309 137
};
310 138

  
311 139
#endif  /*VIRTUAL_MACHINE_ATTRIBUTE_H_*/

Also available in: Unified diff