Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafChildArrayField.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 2011-2013 Ceetron AS
5 // Copyright (C) 2013- Ceetron Solutions AS
6 // Copyright (C) 2022- Kontur AS
7 //
8 // GNU Lesser General Public License Usage
9 // This library is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation; either version 2.1 of the License, or
12 // (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
15 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 // FITNESS FOR A PARTICULAR PURPOSE.
17 //
18 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
19 // for more details.
20 //
21 // ##################################################################################################
22 #pragma once
23 
24 #include "cafAssert.h"
25 #include "cafChildArrayFieldAccessor.h"
26 #include "cafChildArrayFieldHandle.h"
27 #include "cafObjectHandle.h"
28 #include "cafObjectHandlePortableDataType.h"
29 
30 #include <memory>
31 
32 namespace caffa
33 {
34 
39 template <typename DataTypePtr>
40  requires is_pointer<DataTypePtr>
42 {
43 public:
44  using DataType = typename std::remove_pointer<DataTypePtr>::type;
45 
46  using Ptr = std::shared_ptr<DataType>;
47  using ConstPtr = std::shared_ptr<const DataType>;
48  using FieldDataType = DataTypePtr;
49 
52 
54  : m_fieldDataAccessor( std::make_unique<DirectStorageAccessor>( this ) )
55  {
56  static_assert( std::is_base_of<ObjectHandle, DataType>::value &&
57  "Child Array fields can only contain ObjectHandle-derived objects" );
58  }
59  ~ChildArrayField() override;
60 
61  // Access operators
62  operator std::vector<std::shared_ptr<DataType>>() { return this->objects(); }
63  operator std::vector<std::shared_ptr<const DataType>>() const { return this->objects(); }
64 
65  size_t size() const override { return m_fieldDataAccessor->size(); }
66  void clear() override;
67  std::shared_ptr<ObjectHandle> at( size_t index ) override;
68  std::vector<std::shared_ptr<DataType>> objects();
69  std::vector<std::shared_ptr<const DataType>> objects() const;
70  void setObjects( std::vector<std::shared_ptr<DataType>>& objects );
71 
72  // std::vector-like access
73 
74  std::shared_ptr<DataType> operator[]( size_t index ) const;
75 
76  void push_back( std::shared_ptr<DataType> pointer );
77  void push_back_obj( std::shared_ptr<ObjectHandle> obj ) override;
78  void insert( size_t index, std::shared_ptr<DataType> pointer );
79  void insertAt( size_t index, std::shared_ptr<ObjectHandle> obj ) override;
80  void erase( size_t index ) override;
81 
82  // Child objects
83  std::vector<std::shared_ptr<ObjectHandle>> childObjects() override;
84  std::vector<std::shared_ptr<const ObjectHandle>> childObjects() const override;
85  void removeChildObject( std::shared_ptr<const ObjectHandle> object );
86 
87  std::string dataType() const override { return PortableDataType<std::vector<DataType>>::name(); }
88 
89  bool isReadable() const override { return m_fieldDataAccessor != nullptr && m_fieldDataAccessor->hasGetter(); }
90  bool isWritable() const override { return m_fieldDataAccessor != nullptr && m_fieldDataAccessor->hasSetter(); }
91 
92  void setAccessor( std::unique_ptr<ChildArrayFieldAccessor> accessor ) override
93  {
94  m_fieldDataAccessor = std::move( accessor );
95  }
96 
97  virtual std::string childClassKeyword() const override { return std::string( DataType::classKeywordStatic() ); }
98 
99 private: // To be disabled
100  ChildArrayField( const ChildArrayField& ) = delete;
101  ChildArrayField& operator=( const ChildArrayField& ) = delete;
102 
103 private:
104  std::unique_ptr<DataAccessor> m_fieldDataAccessor;
105 };
106 
107 } // End of namespace caffa
108 
109 #include "cafChildArrayField.inl"
void push_back(std::shared_ptr< DataType > pointer)
Assign a shared pointer.
Definition: cafChildArrayField.inl:64
A non-templated base interface for ChildArrayField<DataType*> Used so we can have pointers to any Chi...
Definition: cafChildArrayFieldHandle.h:38
void insertAt(size_t index, std::shared_ptr< ObjectHandle > obj) override
Insert an object at a particular index. Ownership will be taken.
Definition: cafChildArrayField.inl:120
virtual std::string childClassKeyword() const override
Get the class keyword of the contained child(ren)
Definition: cafChildArrayField.h:97
void push_back_obj(std::shared_ptr< ObjectHandle > obj) override
Assign a shared pointer.
Definition: cafChildArrayField.inl:83
void clear() override
Clears the container.
Definition: cafChildArrayField.inl:135
bool isReadable() const override
Definition: cafChildArrayField.h:89
void erase(size_t index) override
Removes the pointer at index from the container and deletes the object pointed to.
Definition: cafChildArrayField.inl:152
~ChildArrayField() override
Implementation of ChildArrayField<>
Definition: cafChildArrayField.inl:35
size_t size() const override
Get the number of child objects.
Definition: cafChildArrayField.h:65
std::shared_ptr< ObjectHandle > at(size_t index) override
Get a raw pointer to the object at a particular index.
Definition: cafChildArrayField.inl:283
void insert(size_t index, std::shared_ptr< DataType > pointer)
Definition: cafChildArrayField.inl:100
Field class to handle a collection of Object derived pointers The ChildArrayField will take over owne...
Definition: cafChildArrayField.h:41
Definition: cafPortableDataType.h:35
Definition: cafChildArrayFieldAccessor.h:64
void setAccessor(std::unique_ptr< ChildArrayFieldAccessor > accessor) override
Set a new accessor.
Definition: cafChildArrayField.h:92
Main Caffa namespace.
Definition: cafApplication.h:30
Definition: cafChildArrayFieldAccessor.h:29
bool isWritable() const override
Definition: cafChildArrayField.h:90
void removeChildObject(std::shared_ptr< const ObjectHandle > object)
Removes all instances of object pointer from the container without deleting the object.
Definition: cafChildArrayField.inl:187
void setObjects(std::vector< std::shared_ptr< DataType >> &objects)
Assign objects to the field, replacing the current child objects.
Definition: cafChildArrayField.inl:171