Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cppcms/form.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,63 @@ namespace cppcms {
///
virtual void clear();

///
/// Add \a subform at specific position to form. The ownership is not
/// transferred to the parent.
/// When \a index is negative it is interpreted from the back, if index
/// is greater than the number of already added elemenets, the element
/// will simply add at the end (or beginning if negative).
///
void add(int index, form &subform);

///
/// Add \a subform to form. The ownership is not transferred to
/// the parent.
///
void add(form &subform);

///
/// Add \a subform at specific position to form. The ownership is
/// transferred to the parent and the subform will be destroyed together
/// with the parent.
/// When \a index is negative it is interpreted from the back, if index
/// is greater than the number of already added elemenets, the element
/// will simply add at the end (or beginning if negative).
///
void attach(int index, form *subform);

///
/// Add \a subform to form. The ownership is transferred to
/// the parent and the subform will be destroyed together with
/// the parent.
///
void attach(form *subform);

///
/// Add \a widget at specific position to form. The ownership is not
/// transferred to to the parent.
/// When \a index is negative it is interpreted from the back, if index
/// is greater than the number of already added elemenets, the element
/// will simply add at the end (or beginning if negative).
///
void add(int index, widgets::base_widget &widget);

///
/// Add \a widget to form. The ownership is not transferred to
/// to the parent.
///
void add(widgets::base_widget &widget);

///
/// Add \a widget at specific position to form. The ownership is
/// transferred to the parent and the widget will be destroyed together
/// with the parent.
/// When \a index is negative it is interpreted from the back, if index
/// is greater than the number of already added elemenets, the element
/// will simply add at the end (or beginning if negative).
///
void attach(int index, widgets::base_widget *widget);

///
/// Add \a widget to form. The ownership is transferred to
/// the parent and the widget will be destroyed together with
Expand Down
55 changes: 45 additions & 10 deletions src/form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,62 @@ void form::clear()
}
}

namespace {

// std::advance is not applicable because UB when n > size
template<class Iter>
Iter advance(Iter it, const Iter& end, int n) {
while (n--)
if (it == end)
break;
else
++it;
return it;
}
template<class V, class T>
void add_impl(form* self, V& elements_, T* form, bool owning, int position) {
if (position < 0) {
auto it = advance(elements_.rend(), elements_.rbegin(), position * -1);
elements_.emplace(it.base() -1, form, owning);
} else {
auto it = advance(elements_.begin(), elements_.end(), position);
elements_.emplace(it, form, owning);
}
form->parent(self);
}
}
void form::add(int index, widgets::base_widget &form)
{
add_impl(this, elements_, &form, false, index);
}
void form::add(int index, form &subform)
{
add_impl(this, elements_, &subform, false, index);
}
void form::add(widgets::base_widget &form)
{
elements_.push_back(widget_type(&form,false));
form.parent(this);
add_impl(this, elements_, &form, false, elements_.size());
}
void form::add(form &subform)
{
elements_.push_back(widget_type(&subform,false));
subform.parent(this);
add_impl(this, elements_, &subform, false, elements_.size());
}

void form::attach(int index, form *subform)
{
add_impl(this, elements_, subform, true, index);
}
void form::attach(int index, widgets::base_widget *form)
{
add_impl(this, elements_, form, true, index);
}
void form::attach(form *subform)
{
elements_.push_back(widget_type(subform,true));
subform->parent(this);
add_impl(this, elements_, subform, true, elements_.size());
}

void form::attach(widgets::base_widget *subform)
void form::attach(widgets::base_widget *form)
{
elements_.push_back(widget_type(subform,true));
subform->parent(this);
add_impl(this, elements_, form, true, elements_.size());
}

struct form::iterator::_data {};
Expand Down