207 lines
6.2 KiB
C++

/**
This file is a part of rexy's matrix client
Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MATRIX_ITERABLE_HPP
#define MATRIX_ITERABLE_HPP
#include "matrix/events.hpp"
#include <rexy/string_base.hpp>
#include "raii/rjp_string.hpp"
#include "raii/rjp_iterator.hpp"
namespace matrix::sync{
namespace detail{
/*
Class for iterating over an event array returned by a sync
*/
template<class T>
class event_iterator
{
protected:
raii::rjp_array_iterator m_event;
public:
constexpr event_iterator(void):
m_event(nullptr){}
constexpr event_iterator(RJP_value* v):
m_event(v){}
constexpr event_iterator(const event_iterator& i):
m_event(i.m_event){}
constexpr bool operator==(const event_iterator& e){
return m_event == e.m_event;
}
constexpr bool operator!=(const event_iterator& e){
return m_event != e.m_event;
}
event_iterator& operator++(void){
++m_event;
return *this;
}
constexpr T operator*(void){
return T(*m_event);
}
};
/*
Class for iterating over an event array associated with a room returned by a sync
*/
template<class T>
class room_event_iterator
{
protected:
raii::rjp_array_iterator m_event;
rexy::static_string m_roomid;
public:
constexpr room_event_iterator(void):
m_event(nullptr), m_roomid(){}
constexpr room_event_iterator(RJP_value* v, const rexy::string_base& roomid):
m_event(v), m_roomid(roomid){}
constexpr room_event_iterator(const room_event_iterator& i):
m_event(i.m_event), m_roomid(i.m_roomid){}
constexpr room_event_iterator& operator=(const room_event_iterator& i){
room_event_iterator tmp(i);
return (*this = std::move(tmp));
}
constexpr room_event_iterator& operator=(room_event_iterator&& i){
m_event = i.m_event;
m_roomid = i.m_roomid;
return *this;
}
constexpr bool operator==(const room_event_iterator& e){
return m_event == e.m_event;
}
constexpr bool operator!=(const room_event_iterator& e){
return m_event != e.m_event;
}
room_event_iterator& operator++(void){
++m_event;
return *this;
}
constexpr T operator*(void){
return T(*m_event, m_roomid);
}
constexpr const rexy::static_string& roomid(void)const{
return m_roomid;
}
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/*
Base class shared by all room lists
*/
template<class T>
class iterable_event_base
{
public:
using iterator = T;
using const_iterator = const T;
protected:
iterator m_event;
public:
constexpr iterable_event_base(void) = default;
constexpr iterable_event_base(RJP_value* ev):
m_event(ev){}
constexpr iterable_event_base(const iterable_event_base& i):
m_event(i.m_event){}
constexpr const_iterator& begin(void)const{
return m_event;
}
constexpr iterator& begin(void){
return m_event;
}
constexpr const_iterator end(void)const{
return const_iterator(nullptr);
}
};
template<class T>
class iterable_room_event_base
{
public:
using iterator = T;
using const_iterator = const T;
protected:
iterator m_event;
public:
constexpr iterable_room_event_base(void) = default;
constexpr iterable_room_event_base(RJP_value* ev, const rexy::string_base& roomid):
m_event(ev, roomid){}
constexpr iterable_room_event_base(const iterable_room_event_base& i):
m_event(i.m_event){}
constexpr iterable_room_event_base& operator=(const iterable_room_event_base& i){
iterable_room_event_base tmp(i);
return (*this = std::move(tmp));
}
constexpr iterable_room_event_base& operator=(iterable_room_event_base&& i){
m_event = i.m_event;
return *this;
}
constexpr const_iterator& begin(void)const{
return m_event;
}
constexpr iterator& begin(void){
return m_event;
}
constexpr const_iterator end(void)const{
return const_iterator(nullptr, m_event.roomid());
}
};
class event_list : public iterable_event_base<detail::event_iterator<event>>
{
public:
using iterable_event_base<detail::event_iterator<event>>::iterable_event_base;
};
//Class representing a list of room state events
class room_state_event_list : public iterable_room_event_base<detail::room_event_iterator<room_state_event>>
{
public:
using iterable_room_event_base<detail::room_event_iterator<room_state_event>>::iterable_room_event_base;
};
//Class representing a list of room events
class room_event_list : public iterable_room_event_base<detail::room_event_iterator<room_event>>
{
public:
using iterable_room_event_base<detail::room_event_iterator<room_event>>::iterable_room_event_base;
};
//Class representing a list of room ephemeral events
class room_ephem_event_list : public iterable_room_event_base<detail::room_event_iterator<room_ephemeral_event>>
{
public:
using iterable_room_event_base<detail::room_event_iterator<room_ephemeral_event>>::iterable_room_event_base;
};
template<class T>
class roomcxn_event_list_base : public iterable_room_event_base<detail::room_event_iterator<T>>
{
protected:
raii::rjp_ptr m_root;
public:
constexpr roomcxn_event_list_base(void) = default;
roomcxn_event_list_base(raii::rjp_ptr& root, RJP_value* event, const rexy::string_base& roomid);
rexy::static_string start_token(void)const&;
raii::rjp_string start_token(void)&&;
rexy::static_string end_token(void)const&;
raii::rjp_string end_token(void)&&;
};
extern template class roomcxn_event_list_base<room_state_event>;
class roomcxn_message_event_list : public roomcxn_event_list_base<room_state_event>
{
public:
using roomcxn_event_list_base<room_state_event>::roomcxn_event_list_base;
};
}
#endif