Newer
Older
/*
Copyright (C) 2011 Adam Wardyński, Tomasz Śniatowski, Paweł Kędzia,
Adam Radziszewski, Bartosz Broda
Part of the WCCL project
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the LICENSE and COPYING files for more details.
*/
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
91
92
93
94
95
96
97
98
#ifndef LIBWCCL_WCCLFILEOPSECTIONS_H
#define LIBWCCL_WCCLFILEOPSECTIONS_H
#include <libwccl/ops/opsequence.h>
#include <boost/unordered_map.hpp>
#include <libpwrutils/foreach.h>
namespace Wccl {
template<class T>
class WcclFileOpSections : boost::noncopyable
{
BOOST_MPL_ASSERT( (boost::is_base_of<FunctionalOpSequence, T>) );
BOOST_MPL_ASSERT_NOT( (boost::is_same<FunctionalOpSequence, T>) );
public:
typedef typename T::op_t op_t;
typedef typename boost::shared_ptr<op_t> op_ptr_t;
typedef typename boost::shared_ptr<const op_t> op_ptr_c_t;
typedef typename boost::shared_ptr<T> ptr_t;
typedef typename boost::shared_ptr<const T> ptr_c_t;
typedef typename std::vector<ptr_t> ptr_v_t;
typedef typename boost::unordered_map<std::string, ptr_t> map_t;
typedef typename T::name_op_v_t name_op_v_t;
typedef typename T::name_op_v_c_t name_op_v_c_t;
protected:
bool has_section(const std::string& name) const;
const ptr_v_t& sections();
size_t size() const;
bool empty() const;
std::vector<std::string> section_names() const;
T& get_section(const std::string& name);
const T& get_section(const std::string& name) const;
ptr_t get_section_ptr(const std::string& name);
ptr_c_t get_section_ptr(const std::string& name) const;
op_t& get_op(const std::string& name, size_t idx = 0);
const op_t& get_op(const std::string& name, size_t idx = 0) const;
op_ptr_t get_op_ptr(const std::string& name, size_t idx = 0);
op_ptr_c_t get_op_ptr(const std::string& name, size_t idx = 0) const;
name_op_v_t& add_name_op_pairs(name_op_v_t& pairs);
name_op_v_c_t& add_name_op_pairs(name_op_v_c_t& pairs) const;
name_op_v_t gen_name_op_pairs();
name_op_v_c_t gen_name_op_pairs() const;
WcclFileOpSections()
: sections_(),
name_section_map_()
{
}
void append(const ptr_t& section);
ptr_v_t sections_;
map_t name_section_map_;
};
} /* end ns Wccl */
//
// Implementation details
//
namespace Wccl {
template<class T> inline
bool WcclFileOpSections<T>::has_section(const std::string& name) const
{
return name_section_map_.find(name) != name_section_map_.end();
}
template<class T> inline
const typename WcclFileOpSections<T>::ptr_v_t& WcclFileOpSections<T>::sections()
{
return sections_;
}
template<class T> inline
bool WcclFileOpSections<T>::empty() const
{
return sections_.empty();
}
template<class T> inline
size_t WcclFileOpSections<T>::size() const
{
return sections_.size();
}
template<class T> inline
std::vector<std::string> WcclFileOpSections<T>::section_names() const
{
std::vector<std::string> v;
foreach(const ptr_t& section, sections_) {
v.push_back(section->name());
}
return v;
}
template<class T> inline
typename WcclFileOpSections<T>::ptr_t WcclFileOpSections<T>::get_section_ptr(const std::string& name)
{
typename map_t::iterator i = name_section_map_.find(name);
if (i == name_section_map_.end()) {
throw InvalidArgument("name", "Section named \"" + name + "\" does not exist.");
}
return i->second;
}
template<class T> inline
typename WcclFileOpSections<T>::ptr_c_t WcclFileOpSections<T>::get_section_ptr(const std::string& name) const
{
typename map_t::const_iterator i = name_section_map_.find(name);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
if (i == name_section_map_.end()) {
throw InvalidArgument("name", "Section named \"" + name + "\" does not exist.");
}
return i->second;
}
template<class T> inline
T& WcclFileOpSections<T>::get_section(const std::string& name)
{
return *get_section_ptr(name);
}
template<class T> inline
const T& WcclFileOpSections<T>::get_section(const std::string& name) const
{
return *get_section_ptr(name);
}
template<class T> inline
typename T::name_op_v_t& WcclFileOpSections<T>::add_name_op_pairs(name_op_v_t& pairs)
{
foreach(const ptr_t& section, sections_) {
section->add_name_op_pairs(pairs);
}
return pairs;
}
template<class T> inline
typename T::name_op_v_c_t& WcclFileOpSections<T>::add_name_op_pairs(name_op_v_c_t& pairs) const
{
foreach(const ptr_t& section, sections_) {
section->add_name_op_pairs(pairs);
}
return pairs;
}
template<class T> inline
typename T::name_op_v_t WcclFileOpSections<T>::gen_name_op_pairs()
{
name_op_v_t pairs;
foreach(const ptr_t& section, sections_) {
section->add_name_op_pairs(pairs);
}
return pairs;
}
template<class T> inline
typename T::name_op_v_c_t WcclFileOpSections<T>::gen_name_op_pairs() const
{
name_op_v_c_t pairs;
foreach(const ptr_t& section, sections_) {
section->add_name_op_pairs(pairs);
}
return pairs;
}
template<class T> inline
void WcclFileOpSections<T>::append(const ptr_t& section)
{
if (has_section(section->name())) {
throw InvalidArgument("section", "Section named \"" + section->name() + "\" already added.");
}
sections_.push_back(section);
name_section_map_[section->name()] = section;
}
template<class T> inline
typename T::op_t& WcclFileOpSections<T>::get_op(
const std::string& name,
{
return get_section(name).get(idx);
}
template<class T> inline
const typename T::op_t& WcclFileOpSections<T>::get_op(
const std::string& name,
size_t idx) const
{
return get_section(name).get(idx);
}
template<class T> inline
typename WcclFileOpSections<T>::op_ptr_t WcclFileOpSections<T>::get_op_ptr(
const std::string& name,
{
return get_section(name).get_ptr(idx);
}
template<class T> inline
typename WcclFileOpSections<T>::op_ptr_c_t WcclFileOpSections<T>::get_op_ptr(
const std::string& name,
size_t idx) const
{
return get_section(name).get_ptr(idx);
}
} /* end ns Wccl */
#endif // LIBWCCL_WCCLFILEOPSECTIONS_H