Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafObjectCollector.h
1 // ##################################################################################################
2 //
3 // Copyright (C) 2023- Kontur AS
4 //
5 // GNU Lesser General Public License Usage
6 // This library is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation; either version 2.1 of the License, or
9 // (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE.
14 //
15 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
16 // for more details.
17 //
18 // ##################################################################################################
19 #pragma once
20 
21 #include "cafChildFieldHandle.h"
22 #include "cafVisitor.h"
23 
24 #include <list>
25 
26 namespace caffa
27 {
28 class ObjectHandle;
29 
33 template <typename ObjectType = ObjectHandle>
35 {
36 public:
37  using Selector = std::function<bool( const ObjectType* )>;
38 
39  ConstObjectCollector( Selector selector = nullptr )
40  : m_selector( selector )
41  {
42  }
43 
44  void visitObject( const ObjectHandle* object ) override
45  {
46  CAFFA_ASSERT( object );
47  CAFFA_TRACE( "Visiting OBJECT " << object->uuid() );
48  auto typedObject = dynamic_cast<const ObjectType*>( object );
49  if ( typedObject && ( !m_selector || m_selector( typedObject ) ) )
50  {
51  m_objects.push_back( typedObject );
52  }
53 
54  for ( auto field : object->fields() )
55  {
56  CAFFA_TRACE( "Testing field: " << field->keyword() );
57  field->accept( this );
58  }
59  }
60 
61  void visitField( const FieldHandle* field ) override {}
62 
63  void visitChildField( const ChildFieldBaseHandle* childField ) override
64  {
65  for ( auto object : childField->childObjects() )
66  {
67  object->accept( this );
68  }
69  }
70 
71  const std::list<const ObjectType*>& objects() const { return m_objects; }
72 
73 private:
74  Selector m_selector;
75  std::list<const ObjectType*> m_objects;
76 };
77 
81 template <typename ObjectType = ObjectHandle>
82 class ObjectCollector : public Editor
83 {
84 public:
85  using Selector = std::function<bool( const ObjectType* )>;
86 
87  ObjectCollector( Selector selector = nullptr )
88  : m_selector( selector )
89  {
90  }
91 
92  void visitObject( ObjectHandle* object ) override
93  {
94  auto typedObject = dynamic_cast<ObjectType*>( object );
95  if ( typedObject && ( !m_selector || m_selector( typedObject ) ) )
96  {
97  m_objects.push_back( typedObject );
98  }
99 
100  for ( auto field : object->fields() )
101  {
102  field->accept( this );
103  }
104  }
105 
106  void visitField( FieldHandle* field ) override {}
107 
108  void visitChildField( ChildFieldBaseHandle* childField ) override
109  {
110  for ( auto object : childField->childObjects() )
111  {
112  object->accept( this );
113  }
114  }
115 
116  const std::list<ObjectType*>& objects() { return m_objects; }
117 
118 private:
119  Selector m_selector;
120  std::list<ObjectType*> m_objects;
121 };
122 } // namespace caffa
Definition: cafChildFieldHandle.h:35
Definition: cafObjectCollector.h:35
Definition: cafVisitor.h:43
Base class for all fields, making it possible to handle them generically.
Definition: cafFieldHandle.h:23
Definition: cafVisitor.h:32
Definition: cafObjectCollector.h:83
Definition: cafObjectHandle.h:55
std::list< FieldHandle * > fields() const
Definition: cafObjectHandle.cpp:51
Main Caffa namespace.
Definition: __init__.py:1