26 #include "cafAssert.h"
27 #include "cafFieldHandle.h"
28 #include "cafJsonDataType.h"
29 #include "cafLogger.h"
30 #include "cafMethodHandle.h"
31 #include "cafObjectCapability.h"
32 #include "cafPortableDataType.h"
33 #include "cafSignal.h"
34 #include "cafStringTools.h"
40 #include <string_view>
41 #include <type_traits>
46 class FieldCapability;
57 using InheritanceStackType = std::vector<std::string>;
58 using Ptr = std::shared_ptr<ObjectHandle>;
59 using ConstPtr = std::shared_ptr<const ObjectHandle>;
65 static std::string classKeywordStatic() {
return "ObjectHandle"; }
66 virtual std::string classKeyword()
const {
return classKeywordStatic(); }
68 virtual InheritanceStackType classInheritanceStack()
const {
return { classKeywordStatic() }; }
70 static bool matchesClassKeyword(
const std::string& classKeyword,
const InheritanceStackType& inheritanceStack )
72 return std::any_of( inheritanceStack.begin(),
73 inheritanceStack.end(),
74 [&classKeyword](
const std::string& testKeyword ) { return classKeyword == testKeyword; } );
77 static constexpr
bool isValidCharacter(
char c )
79 return caffa::StringTools::isalpha( c ) || caffa::StringTools::isdigit( c ) || c ==
'_' || c ==
':';
88 if ( type ==
"keyword" )
return false;
90 if ( caffa::StringTools::isdigit( type[0] ) )
return false;
92 auto end = std::find( type.begin(), type.end(),
'\0' );
94 auto validCount = std::count_if( type.cbegin(), end, ObjectHandle::isValidCharacter );
96 std::count_if( type.cbegin(), end, [](
auto c ) { return !ObjectHandle::isValidCharacter( c ); } );
98 return validCount > 0u && invalidCount == 0u;
101 virtual std::string classDocumentation()
const {
return ""; }
107 std::list<FieldHandle*>
fields()
const;
113 std::list<MethodHandle*>
methods()
const;
120 FieldHandle*
findField(
const std::string& keyword )
const;
127 MethodHandle*
findMethod(
const std::string& keyword )
const;
135 m_capabilities.push_back( std::move(
capability ) );
142 template <
typename CapabilityType>
145 for (
auto& cap : m_capabilities )
147 CapabilityType*
capability =
dynamic_cast<CapabilityType*
>( cap.get() );
153 virtual std::string uuid()
const {
return ""; }
154 virtual void setUuid(
const std::string& ) {}
168 void disconnectObserverFromAllSignals(
SignalObserver* observer );
198 std::map<std::string, FieldHandle*> m_fields;
201 std::map<std::string, MethodHandle*> m_methods;
204 std::vector<std::unique_ptr<ObjectCapability>> m_capabilities;
207 template <
typename T>
208 concept DerivesFromObjectHandle = std::is_base_of<ObjectHandle, T>::value;
210 template <
typename T>
214 template <
typename T>
219 template <
typename T>
225 template <DerivesFromObjectHandle DataType>
228 static std::string name() {
return std::string(
"object::" ) + DataType::classKeywordStatic(); }
234 template <DerivesFromObjectHandle DataType>
237 static std::string name() {
return std::string(
"object[]::" ) + DataType::classKeywordStatic(); }
243 template <IsSharedPtr DataType>
246 static std::string name()
248 static_assert( DerivesFromObjectHandle<typename DataType::element_type> );
249 return std::string(
"object::" ) + DataType::element_type::classKeywordStatic();
256 template <IsSharedPtr DataType>
257 struct PortableDataType<std::vector<DataType>>
259 static std::string name()
261 static_assert( DerivesFromObjectHandle<typename DataType::element_type> );
262 return std::string(
"object[]::" ) + DataType::element_type::classKeywordStatic();
269 template <DerivesFromObjectHandle DataType>
270 struct JsonDataType<DataType>
272 static nlohmann::json type()
274 auto object = nlohmann::json::object();
275 object[
"$ref"] = std::string(
"#/schemas/" ) + DataType::classKeywordStatic();
283 template <IsSharedPtr DataType>
284 struct JsonDataType<DataType>
286 static nlohmann::json type()
288 auto object = nlohmann::json::object();
289 object[
"$ref"] = std::string(
"#/schemas/" ) + DataType::element_type::classKeywordStatic();
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: cafMethodHandle.h:56
Definition: cafObjectFactory.h:39
Definition: cafObjectHandle.h:55
virtual void initAfterRead()
Definition: cafObjectHandle.h:166
void accept(Inspector *visitor) const
Definition: cafObjectHandle.cpp:138
static constexpr bool isValidKeyword(const std::string_view &type)
Definition: cafObjectHandle.h:86
CapabilityType * capability() const
Definition: cafObjectHandle.h:143
void addMethod(MethodHandle *method, const std::string &keyword, MethodHandle::Type type)
Definition: cafObjectHandle.cpp:106
std::list< FieldHandle * > fields() const
Definition: cafObjectHandle.cpp:51
std::list< MethodHandle * > methods() const
Definition: cafObjectHandle.cpp:64
void addCapability(std::unique_ptr< ObjectCapability > capability)
Definition: cafObjectHandle.h:133
FieldHandle * findField(const std::string &keyword) const
Definition: cafObjectHandle.cpp:120
void addField(FieldHandle *field, const std::string &keyword)
Definition: cafObjectHandle.cpp:91
MethodHandle * findMethod(const std::string &keyword) const
Definition: cafObjectHandle.cpp:129
virtual ObjectHandle::Ptr deepClone(caffa::ObjectFactory *optionalObjectFactory=nullptr) const =0
Deep clone the object using an optional object factory.
Definition: cafSignal.h:63
Definition: cafSignal.h:80
Main Caffa namespace.
Definition: __init__.py:1
Definition: cafPortableDataType.h:35
Definition: cafObjectHandle.h:212