1 // Copyright 2014 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROMEOS_DBUS_BINDINGS_DBUS_SIGNATURE_H_ 6 #define CHROMEOS_DBUS_BINDINGS_DBUS_SIGNATURE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include <base/macros.h> 12 #include <gtest/gtest_prod.h> // for FRIEND_TEST 13 14 namespace chromeos_dbus_bindings { 15 16 class DbusSignature { 17 public: 18 DbusSignature(); 19 virtual ~DbusSignature() = default; 20 21 // Returns a C++ typename in |output| for a D-Bus signature in |signature| 22 // and returns true on success. Returns false otherwise. 23 bool Parse(const std::string& signature, std::string* output); 24 25 void set_object_path_typename(const std::string& object_path_typename) { 26 object_path_typename_ = object_path_typename; 27 } 28 29 private: 30 friend class DbusSignatureTest; 31 FRIEND_TEST(DbusSignatureTest, DefaultObjectPathTypename); 32 FRIEND_TEST(DbusSignatureTest, ParseSuccesses); 33 34 // Typenames are C++ syntax types. 35 static const char kArrayTypename[]; 36 static const char kBooleanTypename[]; 37 static const char kByteTypename[]; 38 static const char kDefaultObjectPathTypename[]; 39 static const char kDictTypename[]; 40 static const char kDoubleTypename[]; 41 static const char kSigned16Typename[]; 42 static const char kSigned32Typename[]; 43 static const char kSigned64Typename[]; 44 static const char kStringTypename[]; 45 static const char kUnixFdTypename[]; 46 static const char kUnsigned16Typename[]; 47 static const char kUnsigned32Typename[]; 48 static const char kUnsigned64Typename[]; 49 static const char kVariantTypename[]; 50 static const char kVariantDictTypename[]; 51 static const char kPairTypename[]; 52 static const char kTupleTypename[]; 53 54 // Returns the C++ type name for the next D-Bus signature in the string at 55 // |signature| in |output|, as well as the next position within the string 56 // that parsing should continue |next|. It is not an error to pass a 57 // pointer to |signature| or nullptr as |next|. Returns true on success. 58 bool GetTypenameForSignature(std::string::const_iterator signature, 59 std::string::const_iterator end, 60 std::string::const_iterator* next, 61 std::string* output); 62 63 // Utility task for GetTypenameForSignature() which handles array objects 64 // and decodes them into a map or vector depending on the encoded sub-elements 65 // in the array. The arguments and return values are the same 66 // as GetTypenameForSignature(). 67 bool GetArrayTypenameForSignature(std::string::const_iterator signature, 68 std::string::const_iterator end, 69 std::string::const_iterator* next, 70 std::string* output); 71 72 // Utility task for GetTypenameForSignature() which handles STRUCT objects 73 // and decodes them into a pair or tuple depending on the number of structure 74 // elements. The arguments and return values are the same 75 // as GetTypenameForSignature(). 76 bool GetStructTypenameForSignature(std::string::const_iterator signature, 77 std::string::const_iterator end, 78 std::string::const_iterator* next, 79 std::string* output); 80 81 82 // The C++ typename to be used for D-Bus object pathnames. 83 std::string object_path_typename_; 84 85 DISALLOW_COPY_AND_ASSIGN(DbusSignature); 86 }; 87 88 } // namespace chromeos_dbus_bindings 89 90 #endif // CHROMEOS_DBUS_BINDINGS_DBUS_SIGNATURE_H_ 91