dbus-cxx
property.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later OR BSD-3-Clause
2/***************************************************************************
3 * Copyright (C) 2020 by Robert Middleton *
4 * robert.middleton@rm5248.com *
5 * *
6 * This file is part of the dbus-cxx library. *
7 ***************************************************************************/
8#include <dbus-cxx/enums.h>
9#include <dbus-cxx/dbus-cxx-config.h>
10#include <dbus-cxx/variant.h>
11#include <dbus-cxx/signature.h>
12#include <dbus-cxx/utility.h>
13#include <sigc++/sigc++.h>
14#include <memory>
15#include <sstream>
16
17#ifndef DBUSCXX_PROPERTY_H
18#define DBUSCXX_PROPERTY_H
19
20namespace DBus {
21
22class Interface;
23
28protected:
29 PropertyBase( std::string name, PropertyAccess access, PropertyUpdateType update );
30
32
33public:
34
39 std::string name() const;
40
46 Variant variant_value() const;
47
49
51
66 void set_value( Variant value );
67
68 virtual std::string introspect( int ){ return std::string(); }
69
70private:
71 void setInterface( Interface* );
72
73private:
74 class priv_data;
75
76 DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
77
78 friend class Interface;
79};
80
87template <typename T_type>
88class Property : public PropertyBase {
89private:
90 Property( std::string name, PropertyAccess access, PropertyUpdateType update ) :
91 PropertyBase( name, access, update ) {}
92
93public:
94 static std::shared_ptr<Property<T_type>> create( std::string name, PropertyAccess access, PropertyUpdateType update ) {
95 return std::shared_ptr<Property<T_type>>( new Property<T_type>( name, access, update ) );
96 }
97
98 void set_value( T_type t ) {
100 }
101
102 T_type value() const {
103 T_type t = variant_value();
104 return t;
105 }
106
107 virtual std::string introspect( int space_depth ) {
108 std::ostringstream sout;
109 std::string spaces;
111
112 for( int i = 0; i < space_depth; i++ ) { spaces += " "; }
113
114 sout << spaces << "<property"
115 << " name=\"" << name() << "\""
116 << " type=\"" << sig.dbus_sig() << "\""
117 << " access=\"";
118 switch( access_type() ){
120 sout << "read";
121 break;
123 sout << "write";
124 break;
126 sout << "readwrite";
127 break;
128 }
129 sout << "\" ";
130
131 switch( update_type() ){
133 /* By default, the signal is emitted with the value included */
135 break;
138 break;
140 sout << DBUS_CXX_PROPERTY_EMITS_CHANGE_SIGNAL_ANNOTATION << "=\"invalidates\"";
141 break;
144 break;
145 }
146 sout << "/>\n";
147
148 return sout.str();
149 }
150};
151
152} /* namespace DBus */
153
154#endif /* DBUSCXX_PROPERTY_H */
An Interface represents a local copy of a DBus interface.
Definition: interface.h:41
Base type of Property to allow for storage in e.g.
Definition: property.h:27
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
void setInterface(Interface *)
Definition: property.cpp:68
~PropertyBase()
Definition: property.cpp:38
PropertyBase(std::string name, PropertyAccess access, PropertyUpdateType update)
Definition: property.cpp:33
PropertyUpdateType update_type() const
Definition: property.cpp:50
std::string name() const
Get the name of this propery.
Definition: property.cpp:42
Variant variant_value() const
Get the value of this property as a Variant.
Definition: property.cpp:46
PropertyAccess access_type() const
Definition: property.cpp:64
virtual std::string introspect(int)
Definition: property.h:68
void set_value(Variant value)
Set the value of this property.
Definition: property.cpp:54
Represents a local DBus property.
Definition: property.h:88
static std::shared_ptr< Property< T_type > > create(std::string name, PropertyAccess access, PropertyUpdateType update)
Definition: property.h:94
virtual std::string introspect(int space_depth)
Definition: property.h:107
Property(std::string name, PropertyAccess access, PropertyUpdateType update)
Definition: property.h:90
T_type value() const
Definition: property.h:102
void set_value(T_type t)
Definition: property.h:98
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Definition: signature.h:210
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
PropertyAccess
Definition: enums.h:46
PropertyUpdateType
Definition: enums.h:23
@ DoesNotUpdate
The property does not emit the PropertyChanged signal whenever it changes.
@ Const
This property does not change during the lifetime.
@ Updates
When this property changes, the PropertyChanged signal will be emitted with the new value.
@ Invalidates
When this property cahnges, the PropertyChanged signal will be emitted but the new value will not be ...
#define DBUS_CXX_PROPERTY_EMITS_CHANGE_SIGNAL_ANNOTATION
Definition: utility.h:33
int value
Definition: uv-caller.cpp:11