ObjectList.h
Go to the documentation of this file.
1 /****
2  * ObjectList.h
3  *
4  * Copyright 2019 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 "LinkedItemList.h"
23 #include "ObjectClass.h"
24 
25 namespace UPnP
26 {
31 template <typename ObjectType> class ObjectList : public LinkedItemList
32 {
33 public:
35  {
36  return reinterpret_cast<ObjectType*>(LinkedItemList::head());
37  }
38 
39  const ObjectType* head() const
40  {
41  return reinterpret_cast<const ObjectType*>(LinkedItemList::head());
42  }
43 
44  typename ObjectType::Iterator begin()
45  {
46  return this->head();
47  }
48 
49  typename ObjectType::Iterator end()
50  {
51  return nullptr;
52  }
53 
54  bool isEmpty() const
55  {
56  return head() == nullptr;
57  }
58 
59  size_t count() const
60  {
61  size_t n{0};
62  for(auto it = begin(); it != end(); it++) {
63  ++n;
64  }
65  return n;
66  }
67 
73  template <typename T> ObjectType* find(const T& objectType)
74  {
75  for(auto it = begin(); it != end(); it++) {
76  if(it->typeIs(objectType)) {
77  return &(*it);
78  }
79  }
80 
81  return nullptr;
82  }
83 
89  ObjectType* find(const ObjectClass& objectClass)
90  {
91  return this->find(objectClass.objectType());
92  }
93 };
94 
99 template <typename ObjectType> class OwnedObjectList : public ObjectList<ObjectType>
100 {
101 public:
102  bool remove(ObjectType* object)
103  {
104  bool res = LinkedItemList::remove(object);
105  delete object;
106  return res;
107  }
108 
109  void clear()
110  {
111  while(remove(this->head())) {
112  //
113  }
114  }
115 };
116 
117 } // namespace UPnP
Singly-linked list of items.
Definition: LinkedItemList.h:31
bool remove(LinkedItem *item)
LinkedItem * head()
Definition: LinkedItemList.h:47
Class template for singly-linked list of objects.
Definition: ObjectList.h:32
const ObjectType * head() const
Definition: ObjectList.h:39
ObjectType * find(const T &objectType)
Search list for matching entry.
Definition: ObjectList.h:73
bool isEmpty() const
Definition: ObjectList.h:54
ObjectType::Iterator begin()
Definition: ObjectList.h:44
ObjectType * find(const ObjectClass &objectClass)
Search list for matching entry given its class @objectClass Class information for object.
Definition: ObjectList.h:89
size_t count() const
Definition: ObjectList.h:59
ObjectType::Iterator end()
Definition: ObjectList.h:49
ObjectType * head()
Definition: ObjectList.h:34
Class template for singly-linked list of objects.
Definition: ObjectList.h:100
bool remove(ObjectType *object)
Definition: ObjectList.h:102
void clear()
Definition: ObjectList.h:109
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34
Definition: ActionRequest.h:25
Describes device or service class.
Definition: ObjectClass.h:36
Urn objectType() const