aboutsummaryrefslogtreecommitdiff
path: root/src/DBusObjectPath.h
blob: c1e4af5cf002d4d19239bf67bf8825d7d5a84eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2017 Paul Nettle. 
// 
// This file is part of Gobbledegook. 
// 
// Gobbledegook is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
// 
// Gobbledegook is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
// 
// You should have received a copy of the GNU General Public License 
// along with Gobbledegook.  If not, see <http://www.gnu.org/licenses/>. 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// 
// >> 
// >>>  INSIDE THIS FILE 
// >> 
// 
// This represents a custom string type for a D-Bus object path. 
// 
// >> 
// >>>  DISCUSSION 
// >> 
// 
// A D-Bus object path is normal string in the form "/com/example/foo/bar". This class provides a set of methods for building 
// these paths safely in such a way that they are guaranteed to always provide a valid path. 
// 
// In addition to this functionality, our DBusObjectPath is its own distinct type requiring explicit conversion, providing a level 
// of protection against accidentally using an arbitrary string as an object path. 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
#pragma once
 
#include <string>
#include <ostream>
 
namespace ggk {
 
struct DBusObjectPath
{
// Default constructor (creates a root path) 
inline DBusObjectPath() { path = "/"}
 
// Copy constructor 
inline DBusObjectPath(const DBusObjectPath &path) : path(path.path) {}
 
// Constructor that accepts a C string 
// 
// Note: explicit because we don't want accidental conversion. Creating a DBusObjectPath must be intentional. 
inline explicit DBusObjectPath(const char *pPath) : path(pPath) {}
 
// Constructor that accepts a std::string 
// 
// Note: explicit because we don't want accidental conversion. Creating a DBusObjectPath must be intentional. 
inline explicit DBusObjectPath(const std::string &path) : path(path) {}
 
// Explicit conversion to std::string 
inline const std::string &toString() const { return path; }
 
// Explicit conversion to a C string 
inline const char *c_str() const { return path.c_str(); }
 
// Assignment 
inline DBusObjectPath &operator =(const DBusObjectPath &rhs)
{
if (this == &rhs) return *this;
path = rhs.path;
return *this;
}
 
// Concatenation 
inline const DBusObjectPath &append(const char *rhs)
{
if (nullptr == rhs || !*rhs) { return *this}
if (path.empty()) { path = rhs; return *this}
 
bool ls = path.back() == '/';
bool rs = *rhs == '/';
if (ls && rs) { path.erase(path.length()-1); }
if (!ls && !rs) { path += "/"}
 
path += rhs;
return *this;
}
 
// Adds a path node (in the form of an std::string) to the end of the path 
inline const DBusObjectPath &append(const std::string &rhs)
{
return append(rhs.c_str());
}
 
// Adds a path node (in the form of a DBusObjectPath) to the end of the path 
inline const DBusObjectPath &append(const DBusObjectPath &rhs)
{
return append(rhs.path.c_str());
}
 
// Adds a path node (in the form of a DBusObjectPath) to the end of the path 
inline void operator +=(const DBusObjectPath &rhs)
{
append(rhs);
}
 
// Adds a path node (in the form of a C string) to the end of the path 
inline void operator +=(const char *rhs)
{
append(rhs);
}
 
// Adds a path node (in the form of an std::string) to the end of the path 
inline void operator +=(const std::string &rhs)
{
append(rhs);
}
 
// Concats two DBusObjectPaths into one, returning the resulting path 
inline DBusObjectPath operator +(const DBusObjectPath &rhs) const
{
DBusObjectPath result(*this);
result += rhs;
return result;
}
 
// Concats a C string onto a DBusObjectPath, returning the resulting path 
inline DBusObjectPath operator +(const char *rhs) const
{
DBusObjectPath result(*this);
result += rhs;
return result;
}
 
// Concats a std::string onto a DBusObjectPath, returning the resulting path 
inline DBusObjectPath operator +(const std::string &rhs) const
{
DBusObjectPath result(*this);
result += rhs;
return result;
}
 
// Tests two DBusObjectPaths for equality, returning true of the two strings are identical 
inline bool operator ==(const DBusObjectPath &rhs) const
{
return path == rhs.path;
}
 
private:
 
std::string path;
};
 
// Mixed-mode override for adding a DBusObjectPath to a C string, returning a new DBusObjectPath result 
inline DBusObjectPath operator +(const char *lhs, const DBusObjectPath &rhs) { return DBusObjectPath(lhs) + rhs; }
 
// Mixed-mode override for adding a DBusObjectPath to a std::string, returning a new DBusObjectPath result 
inline DBusObjectPath operator +(const std::string &lhs, const DBusObjectPath &rhs) { return DBusObjectPath(lhs) + rhs; }
 
// Streaming support for our DBusObjectPath (useful for our logging mechanism) 
inline std::ostream& operator<<(std::ostream &os, const DBusObjectPath &path)
{
    os << path.toString();
    return os;
}
 
// Streaming support for our DBusObjectPath (useful for our logging mechanism) 
inline std::ostream& operator +(std::ostream &os, const DBusObjectPath &path)
{
    os << path.toString();
    return os;
}
 
}// namespace ggk