BaseObject.h
Go to the documentation of this file.
1 /****
2  * BaseObject.h
3  *
4  * Copyright 2020 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming UPnP Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "LinkedItem.h"
23 #include "ObjectClass.h"
24 #include <WString.h>
25 #include <Delegate.h>
31 #include <Network/SSDP/Urn.h>
32 
33 namespace UPnP
34 {
35 using namespace SSDP;
36 
37 class BaseObject;
38 
39 struct SearchFilter {
40  using Callback = Delegate<void(BaseObject* object, SearchMatch match)>;
41 
42  SearchFilter(const MessageSpec& ms, uint32_t delayMs) : ms(ms), delayMs(delayMs)
43  {
44  }
45 
46  const MessageSpec& ms;
47  uint32_t delayMs;
50 };
51 
55 class BaseObject : public LinkedItem
56 {
57 public:
66  virtual bool formatMessage(Message& msg, MessageSpec& ms) = 0;
67 
73  virtual void sendMessage(Message& msg, MessageSpec& ms);
74 };
75 
79 template <typename ObjectType, typename BaseObjectType> class ObjectTemplate : public BaseObjectType
80 {
81 public:
82  class Iterator
83  {
84  public:
85  using iterator_category = std::random_access_iterator_tag;
87  using difference_type = std::ptrdiff_t;
88  using pointer = ObjectType*;
90 
91  Iterator(ObjectType* x) : o(x)
92  {
93  }
94 
95  Iterator(ObjectType& x) : o(&x)
96  {
97  }
98 
99  Iterator(const Iterator& other) : o(other.o)
100  {
101  }
102 
104  {
105  o = o->getNext();
106  return *this;
107  }
108 
110  {
111  Iterator tmp(*this);
112  operator++();
113  return tmp;
114  }
115 
116  bool operator==(const Iterator& rhs) const
117  {
118  return o == rhs.o;
119  }
120 
121  bool operator!=(const Iterator& rhs) const
122  {
123  return o != rhs.o;
124  }
125 
127  {
128  return *o;
129  }
130 
132  {
133  return o;
134  }
135 
136  operator ObjectType*()
137  {
138  return o;
139  }
140 
141  private:
142  ObjectType* o;
143  };
144 
146  {
147  return reinterpret_cast<ObjectType*>(this->next());
148  }
149 
150  Iterator begin() const
151  {
152  return Iterator(this);
153  }
154 
155  Iterator end() const
156  {
157  return Iterator(nullptr);
158  }
159 };
160 
161 } // namespace UPnP
Defines the information used to create an outgoing message.
Definition: MessageSpec.h:75
Message using regular HTTP header management class.
Definition: SSDP/src/include/Network/SSDP/Message.h:72
The String class.
Definition: WString.h:137
Objects which hook into the SSDP message stack.
Definition: BaseObject.h:56
virtual void sendMessage(Message &msg, MessageSpec &ms)
Called by framework to construct then send a message.
virtual bool formatMessage(Message &msg, MessageSpec &ms)=0
Standard fields have been completed.
Base class template for items in a list.
Definition: LinkedItem.h:32
Definition: BaseObject.h:83
Iterator & operator++()
Definition: BaseObject.h:103
ObjectType & operator*()
Definition: BaseObject.h:126
bool operator!=(const Iterator &rhs) const
Definition: BaseObject.h:121
std::random_access_iterator_tag iterator_category
Definition: BaseObject.h:85
std::ptrdiff_t difference_type
Definition: BaseObject.h:87
Iterator operator++(int)
Definition: BaseObject.h:109
Iterator(ObjectType &x)
Definition: BaseObject.h:95
ObjectType & reference
Definition: BaseObject.h:89
ObjectType value_type
Definition: BaseObject.h:86
ObjectType * pointer
Definition: BaseObject.h:88
ObjectType * operator->()
Definition: BaseObject.h:131
Iterator(ObjectType *x)
Definition: BaseObject.h:91
Iterator(const Iterator &other)
Definition: BaseObject.h:99
bool operator==(const Iterator &rhs) const
Definition: BaseObject.h:116
Base class template for linked items with type casting.
Definition: BaseObject.h:80
Iterator begin() const
Definition: BaseObject.h:150
ObjectType * getNext() const
Definition: BaseObject.h:145
Iterator end() const
Definition: BaseObject.h:155
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34
SerializationFormat operator++(SerializationFormat &fmt)
Definition: ArduinoJson.h:120
Definition: SSDP/src/include/Network/SSDP/Message.h:32
SearchMatch
Determines the kind of match obtained when scanning incoming packets.
Definition: MessageSpec.h:61
Definition: ActionRequest.h:25
Definition: BaseObject.h:39
String targetString
Full search target value.
Definition: BaseObject.h:48
uint32_t delayMs
Message delay.
Definition: BaseObject.h:47
const MessageSpec & ms
Specification for message to be sent.
Definition: BaseObject.h:46
SearchFilter(const MessageSpec &ms, uint32_t delayMs)
Definition: BaseObject.h:42
Callback callback
Called on a match.
Definition: BaseObject.h:49