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>
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 
20 namespace DBus {
21 
22 class Interface;
23 
27 class PropertyBase {
28 protected:
29  PropertyBase( std::string name, PropertyAccess access, PropertyUpdateType update );
30 
31  ~PropertyBase();
32 
33 public:
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 spaces ){ return std::string(); }
69 
70 private:
71  void setInterface( Interface* );
72 
73 private:
74  class priv_data;
75 
76  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
77 
78  friend class Interface;
79 };
80 
87 template <typename T_type>
88 class Property : public PropertyBase {
89 private:
90  Property( std::string name, PropertyAccess access, PropertyUpdateType update ) :
91  PropertyBase( name, access, update ) {}
92 
93 public:
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  sout << DBUS_CXX_PROPERTY_EMITS_CHANGE_SIGNAL_ANNOTATION << "=\"const\"";
134  break;
136  sout << DBUS_CXX_PROPERTY_EMITS_CHANGE_SIGNAL_ANNOTATION << "=\"invalidates\"";
137  break;
139  sout << DBUS_CXX_PROPERTY_EMITS_CHANGE_SIGNAL_ANNOTATION << "=\"false\"";
140  break;
141  }
142  sout << "/>\n";
143 
144  return sout.str();
145  }
146 };
147 
148 } /* namespace DBus */
149 
150 #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
void set_value(Variant value)
Set the value of this property.
Definition: property.cpp:54
virtual std::string introspect(int spaces)
Definition: property.h:68
Represents a local DBus property.
Definition: property.h:88
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
static std::shared_ptr< Property< T_type > > create(std::string name, PropertyAccess access, PropertyUpdateType update)
Definition: property.h:94
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Definition: signature.h:200
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.
@ 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:32