Ver código fonte

Addressbook code cleanup

- Remove nested objects tests playgorund
- Remove addressbook.proto from server folder(common should be used)
- Common code cleanup
Alexey Edelev 5 anos atrás
pai
commit
e6f9ccddd4

+ 3 - 3
examples/addressbook/CMakeLists.txt

@@ -82,13 +82,13 @@ if(WIN32)
     include_directories(${GTEST_INCLUDE_PATHS} "/")
 endif()
 
-file(GLOB SOURCES main.cpp addressbookengine.cpp universallistmodel.cpp universallistmodelbase.cpp testnesting.cpp nestedobject.cpp)
+file(GLOB SOURCES main.cpp addressbookengine.cpp universallistmodel.cpp universallistmodelbase.cpp)
 
 set(addressbook "addressbook_example")
 add_executable(${addressbook} ${SOURCES} ${GENERATED_SOURCES} resources.qrc)
 if(WIN32)
-    target_link_libraries(${addressbook} qtgrpc Qt5::Quick)
+    target_link_libraries(${addressbook} qtgrpc qtprotobufsupport Qt5::Quick Qt5::Qml)
 elseif(UNIX)
-    target_link_libraries(${addressbook} qtgrpc Qt5::Quick)
+    target_link_libraries(${addressbook} qtgrpc qtprotobufsupport Qt5::Quick)
 endif()
 add_dependencies(${addressbook} ${addressbookgeneration})

+ 1 - 1
examples/addressbook/addressbookengine.cpp

@@ -33,7 +33,7 @@ using namespace qtprotobuf::examples;
 
 AddressBookEngine::AddressBookEngine() : QObject()
   , m_client(new AddressBookClient)
-  , m_contacts(new ContactsListModel(this))
+  , m_contacts(new ContactsListModel({}, this))
 {
     Contacts tmp;
     std::shared_ptr<qtprotobuf::AbstractChannel> channel(new qtprotobuf::Http2Channel("localhost", 65001));

+ 1 - 0
examples/addressbook/addressbookengine.h

@@ -34,6 +34,7 @@ class AddressBookClient;
 } }
 
 using ContactsListModel = UniversalListModel<qtprotobuf::examples::Contact>;
+using PhoneNumbersListModel = UniversalListModel<qtprotobuf::examples::PhoneNumber>;
 
 class AddressBookEngine : public QObject
 {

+ 4 - 12
examples/addressbook/main.cpp

@@ -34,36 +34,28 @@
 #include <contacts.h>
 #include <contact.h>
 
-#include "nestedobject.h"
-#include "testnesting.h"
 #include <QMetaProperty>
-#include<QMetaObject>
+#include <QQmlPropertyMap>
 
 using namespace qtprotobuf::examples;
 
 int main(int argc, char *argv[])
 {
+
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
     qtprotobuf::QtProtobuf::init();
     Contact::registerTypes();
     Contacts::registerTypes();
     Job::registerTypes();
     Address::registerTypes();
+    PhoneNumber::registerTypes();
 
-    qRegisterMetaType<NestedObject>("NestedObject");
-    qRegisterMetaType<TestNesting>("TestNesting");
-    qmlRegisterType<NestedObject>("qtprotobuf.examples.addressbook", 1, 0, "NestedObject");
-    qmlRegisterType<TestNesting>("qtprotobuf.examples.addressbook", 1, 0, "TestNesting");
-
-
-    TestNesting test;
-    qDebug() << "test.property" << TestNesting::staticMetaObject.property(1).userType();
-    qDebug() << "NestedObject metatypeid" << qMetaTypeId<NestedObject*>();
     qmlRegisterType<ContactsListModel>("examples.addressbook", 1, 0, "ContactsListModel");
     qmlRegisterType<qtprotobuf::examples::Job>("qtprotobuf.examples.addressbook", 1, 0, "Job");
     qmlRegisterType<qtprotobuf::examples::Address>("qtprotobuf.examples.addressbook", 1, 0, "Address");
     qmlRegisterType<qtprotobuf::examples::Contact>("qtprotobuf.examples.addressbook", 1, 0, "Contact");
     qmlRegisterType<qtprotobuf::examples::Contacts>("qtprotobuf.examples.addressbook", 1, 0, "Contacts");
+    qmlRegisterType<qtprotobuf::examples::PhoneNumber>("qtprotobuf.examples.addressbook", 1, 0, "PhoneNumber");
 
     QGuiApplication app(argc, argv);
     AddressBookEngine abEngine;

+ 2 - 2
examples/addressbook/proto/addressbook.proto

@@ -29,7 +29,7 @@ package qtprotobuf.examples;
 
 message PhoneNumber {
     uint32 countryCode = 1;
-    repeated uint64 number = 2;
+    uint64 number = 2;
 }
 
 message Address {
@@ -56,7 +56,7 @@ message Contact {
     string firstName = 1;
     string lastName = 2;
     string middleName = 3;
-    map<int32, PhoneNumber> phones = 4;
+    repeated PhoneNumber phones = 4;
     Address address = 5;
     Job job = 6;
 }

+ 6 - 5
examples/addressbook/qml/ContactList.qml

@@ -66,13 +66,14 @@ ListView {
             Row {
                 Layout.alignment: Qt.AlignVCenter
                 Text {
-                    id: job
+                    id: defaultPhoneNumberText
+                    property PhoneNumber defaultPhoneNumber: contactDelegate.contact.phonesData.length > 0 ?
+                                                                 contactDelegate.contact.phonesData[0] : null
+                    visible: defaultPhoneNumber != null
                     color: "#EEEEEE"
-                    text: contactDelegate.contact.job.title
+                    text: defaultPhoneNumber ?
+                              "+" + defaultPhoneNumber.countryCode + " " + defaultPhoneNumber.number : ""
                     font.pointSize: 12
-                    Component.onCompleted: {
-                        console.log('contactDelegate.contact.job: ' + contactDelegate.contact.job.title);
-                    }
                 }
             }
         }

+ 0 - 18
examples/addressbook/qml/main.qml

@@ -41,24 +41,6 @@ ApplicationWindow {
         color: "#81D4FA"
     }
 
-    TestNesting {
-        id: test
-        nested: NestedObject {
-            title: "qqq"
-        }
-        Component.onCompleted: {
-            console.log("test.nested " + test.nested.title)
-        }
-    }
-
-//    NestedObject {
-//        id: nested
-//        title: "qqq"
-//        Component.onCompleted: {
-//            console.log("test.nested " + nested.title)
-//        }
-//    }
-
     ContactList {
         model: abEngine.contacts
         Component.onCompleted: {

+ 3 - 1
examples/addressbook/universallistmodel.h

@@ -20,7 +20,9 @@ template <typename T>
 class UniversalListModel : public UniversalListModelBase
 {
 public:
-    UniversalListModel(QObject* parent = 0) : UniversalListModelBase(parent) {}
+    UniversalListModel(QList<QSharedPointer<T>> container = {}, QObject* parent = 0) : UniversalListModelBase(parent) {
+        reset(container);
+    }
     ~UniversalListModel() {
         clear();
     }

+ 30 - 52
examples/addressbookserver/addressbook.pb.cc

@@ -312,7 +312,7 @@ void AddDescriptorsImpl() {
   static const char descriptor[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
       "\n\021addressbook.proto\022\023qtprotobuf.examples"
       "\"2\n\013PhoneNumber\022\023\n\013countryCode\030\001 \001(\r\022\016\n\006"
-      "number\030\002 \003(\004\"j\n\007Address\022\017\n\007zipCode\030\001 \001(\004"
+      "number\030\002 \001(\004\"j\n\007Address\022\017\n\007zipCode\030\001 \001(\004"
       "\022\026\n\016streetAddress1\030\002 \001(\t\022\026\n\016streetAddres"
       "s2\030\003 \001(\t\022\r\n\005state\030\004 \001(\t\022\017\n\007country\030\005 \001(\r"
       "\"I\n\003Job\022\r\n\005title\030\001 \001(\t\0223\n\rofficeAddress\030"
@@ -404,15 +404,18 @@ PhoneNumber::PhoneNumber()
 }
 PhoneNumber::PhoneNumber(const PhoneNumber& from)
   : ::google::protobuf::Message(),
-      _internal_metadata_(NULL),
-      number_(from.number_) {
+      _internal_metadata_(NULL) {
   _internal_metadata_.MergeFrom(from._internal_metadata_);
-  countrycode_ = from.countrycode_;
+  ::memcpy(&number_, &from.number_,
+    static_cast<size_t>(reinterpret_cast<char*>(&countrycode_) -
+    reinterpret_cast<char*>(&number_)) + sizeof(countrycode_));
   // @@protoc_insertion_point(copy_constructor:qtprotobuf.examples.PhoneNumber)
 }
 
 void PhoneNumber::SharedCtor() {
-  countrycode_ = 0u;
+  ::memset(&number_, 0, static_cast<size_t>(
+      reinterpret_cast<char*>(&countrycode_) -
+      reinterpret_cast<char*>(&number_)) + sizeof(countrycode_));
 }
 
 PhoneNumber::~PhoneNumber() {
@@ -443,8 +446,9 @@ void PhoneNumber::Clear() {
   // Prevent compiler warnings about cached_has_bits being unused
   (void) cached_has_bits;
 
-  number_.Clear();
-  countrycode_ = 0u;
+  ::memset(&number_, 0, static_cast<size_t>(
+      reinterpret_cast<char*>(&countrycode_) -
+      reinterpret_cast<char*>(&number_)) + sizeof(countrycode_));
   _internal_metadata_.Clear();
 }
 
@@ -472,19 +476,14 @@ bool PhoneNumber::MergePartialFromCodedStream(
         break;
       }
 
-      // repeated uint64 number = 2;
+      // uint64 number = 2;
       case 2: {
         if (static_cast< ::google::protobuf::uint8>(tag) ==
-            static_cast< ::google::protobuf::uint8>(18u /* 18 & 0xFF */)) {
-          DO_((::google::protobuf::internal::WireFormatLite::ReadPackedPrimitive<
-                   ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>(
-                 input, this->mutable_number())));
-        } else if (
-            static_cast< ::google::protobuf::uint8>(tag) ==
             static_cast< ::google::protobuf::uint8>(16u /* 16 & 0xFF */)) {
-          DO_((::google::protobuf::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline<
+
+          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                    ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>(
-                 1, 18u, input, this->mutable_number())));
+                 input, &number_)));
         } else {
           goto handle_unusual;
         }
@@ -522,15 +521,9 @@ void PhoneNumber::SerializeWithCachedSizes(
     ::google::protobuf::internal::WireFormatLite::WriteUInt32(1, this->countrycode(), output);
   }
 
-  // repeated uint64 number = 2;
-  if (this->number_size() > 0) {
-    ::google::protobuf::internal::WireFormatLite::WriteTag(2, ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
-    output->WriteVarint32(static_cast< ::google::protobuf::uint32>(
-        _number_cached_byte_size_));
-  }
-  for (int i = 0, n = this->number_size(); i < n; i++) {
-    ::google::protobuf::internal::WireFormatLite::WriteUInt64NoTag(
-      this->number(i), output);
+  // uint64 number = 2;
+  if (this->number() != 0) {
+    ::google::protobuf::internal::WireFormatLite::WriteUInt64(2, this->number(), output);
   }
 
   if ((_internal_metadata_.have_unknown_fields() &&  ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())) {
@@ -552,17 +545,9 @@ void PhoneNumber::SerializeWithCachedSizes(
     target = ::google::protobuf::internal::WireFormatLite::WriteUInt32ToArray(1, this->countrycode(), target);
   }
 
-  // repeated uint64 number = 2;
-  if (this->number_size() > 0) {
-    target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(
-      2,
-      ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
-      target);
-    target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(
-        static_cast< ::google::protobuf::int32>(
-            _number_cached_byte_size_), target);
-    target = ::google::protobuf::internal::WireFormatLite::
-      WriteUInt64NoTagToArray(this->number_, target);
+  // uint64 number = 2;
+  if (this->number() != 0) {
+    target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(2, this->number(), target);
   }
 
   if ((_internal_metadata_.have_unknown_fields() &&  ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())) {
@@ -582,20 +567,11 @@ size_t PhoneNumber::ByteSizeLong() const {
       ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
         (::google::protobuf::internal::GetProto3PreserveUnknownsDefault()   ? _internal_metadata_.unknown_fields()   : _internal_metadata_.default_instance()));
   }
-  // repeated uint64 number = 2;
-  {
-    size_t data_size = ::google::protobuf::internal::WireFormatLite::
-      UInt64Size(this->number_);
-    if (data_size > 0) {
-      total_size += 1 +
-        ::google::protobuf::internal::WireFormatLite::Int32Size(
-            static_cast< ::google::protobuf::int32>(data_size));
-    }
-    int cached_size = ::google::protobuf::internal::ToCachedSize(data_size);
-    GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
-    _number_cached_byte_size_ = cached_size;
-    GOOGLE_SAFE_CONCURRENT_WRITES_END();
-    total_size += data_size;
+  // uint64 number = 2;
+  if (this->number() != 0) {
+    total_size += 1 +
+      ::google::protobuf::internal::WireFormatLite::UInt64Size(
+        this->number());
   }
 
   // uint32 countryCode = 1;
@@ -632,7 +608,9 @@ void PhoneNumber::MergeFrom(const PhoneNumber& from) {
   ::google::protobuf::uint32 cached_has_bits = 0;
   (void) cached_has_bits;
 
-  number_.MergeFrom(from.number_);
+  if (from.number() != 0) {
+    set_number(from.number());
+  }
   if (from.countrycode() != 0) {
     set_countrycode(from.countrycode());
   }
@@ -662,7 +640,7 @@ void PhoneNumber::Swap(PhoneNumber* other) {
 }
 void PhoneNumber::InternalSwap(PhoneNumber* other) {
   using std::swap;
-  number_.InternalSwap(&other->number_);
+  swap(number_, other->number_);
   swap(countrycode_, other->countrycode_);
   _internal_metadata_.Swap(&other->_internal_metadata_);
 }

+ 11 - 34
examples/addressbookserver/addressbook.pb.h

@@ -204,17 +204,11 @@ class PhoneNumber : public ::google::protobuf::Message /* @@protoc_insertion_poi
 
   // accessors -------------------------------------------------------
 
-  // repeated uint64 number = 2;
-  int number_size() const;
+  // uint64 number = 2;
   void clear_number();
   static const int kNumberFieldNumber = 2;
-  ::google::protobuf::uint64 number(int index) const;
-  void set_number(int index, ::google::protobuf::uint64 value);
-  void add_number(::google::protobuf::uint64 value);
-  const ::google::protobuf::RepeatedField< ::google::protobuf::uint64 >&
-      number() const;
-  ::google::protobuf::RepeatedField< ::google::protobuf::uint64 >*
-      mutable_number();
+  ::google::protobuf::uint64 number() const;
+  void set_number(::google::protobuf::uint64 value);
 
   // uint32 countryCode = 1;
   void clear_countrycode();
@@ -226,8 +220,7 @@ class PhoneNumber : public ::google::protobuf::Message /* @@protoc_insertion_poi
  private:
 
   ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
-  ::google::protobuf::RepeatedField< ::google::protobuf::uint64 > number_;
-  mutable int _number_cached_byte_size_;
+  ::google::protobuf::uint64 number_;
   ::google::protobuf::uint32 countrycode_;
   mutable ::google::protobuf::internal::CachedSize _cached_size_;
   friend struct ::protobuf_addressbook_2eproto::TableStruct;
@@ -1092,34 +1085,18 @@ inline void PhoneNumber::set_countrycode(::google::protobuf::uint32 value) {
   // @@protoc_insertion_point(field_set:qtprotobuf.examples.PhoneNumber.countryCode)
 }
 
-// repeated uint64 number = 2;
-inline int PhoneNumber::number_size() const {
-  return number_.size();
-}
+// uint64 number = 2;
 inline void PhoneNumber::clear_number() {
-  number_.Clear();
+  number_ = GOOGLE_ULONGLONG(0);
 }
-inline ::google::protobuf::uint64 PhoneNumber::number(int index) const {
+inline ::google::protobuf::uint64 PhoneNumber::number() const {
   // @@protoc_insertion_point(field_get:qtprotobuf.examples.PhoneNumber.number)
-  return number_.Get(index);
-}
-inline void PhoneNumber::set_number(int index, ::google::protobuf::uint64 value) {
-  number_.Set(index, value);
-  // @@protoc_insertion_point(field_set:qtprotobuf.examples.PhoneNumber.number)
-}
-inline void PhoneNumber::add_number(::google::protobuf::uint64 value) {
-  number_.Add(value);
-  // @@protoc_insertion_point(field_add:qtprotobuf.examples.PhoneNumber.number)
-}
-inline const ::google::protobuf::RepeatedField< ::google::protobuf::uint64 >&
-PhoneNumber::number() const {
-  // @@protoc_insertion_point(field_list:qtprotobuf.examples.PhoneNumber.number)
   return number_;
 }
-inline ::google::protobuf::RepeatedField< ::google::protobuf::uint64 >*
-PhoneNumber::mutable_number() {
-  // @@protoc_insertion_point(field_mutable_list:qtprotobuf.examples.PhoneNumber.number)
-  return &number_;
+inline void PhoneNumber::set_number(::google::protobuf::uint64 value) {
+  
+  number_ = value;
+  // @@protoc_insertion_point(field_set:qtprotobuf.examples.PhoneNumber.number)
 }
 
 // -------------------------------------------------------------------

+ 0 - 83
examples/addressbookserver/addressbook.proto

@@ -1,83 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
- *
- * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-syntax="proto3";
-
-package qtprotobuf.examples;
-
-message PhoneNumber {
-    uint32 countryCode = 1;
-    repeated uint64 number = 2;
-}
-
-message Address {
-    uint64 zipCode = 1;
-    string streetAddress1 = 2;
-    string streetAddress2 = 3;
-    string state = 4;
-    uint32 country = 5;
-}
-
-message Job {
-    string title = 1;
-    Address officeAddress = 2;
-}
-
-message Contact {
-    enum PhoneType {
-        Home = 0;
-        Work = 1;
-        Mobile = 2;
-        Other = 3;
-    };
-
-    string firstName = 1;
-    string lastName = 2;
-    string middleName = 3;
-    map<int32, PhoneNumber> phones = 4;
-    Address address = 5;
-    Job job = 6;
-}
-
-message Contacts {
-    repeated Contact list = 1;
-}
-
-message SimpleResult {
-    bool ok = 1;
-}
-
-message ListFrame {
-    sint32 start = 1;
-    sint32 end = 2;
-}
-
-service AddressBook {
-    rpc addContact(Contact) returns (Contacts) {}
-    rpc removeContact(Contact) returns (Contacts) {}
-    rpc getContacts(ListFrame) returns (Contacts) {}
-    rpc makeCall(Contact) returns (SimpleResult) {}
-    rpc navigateTo(Address) returns (SimpleResult) {}
-}

+ 4 - 0
examples/addressbookserver/main.cpp

@@ -33,6 +33,10 @@ public:
         ::qtprotobuf::examples::Job *job = new ::qtprotobuf::examples::Job;
         job->set_title("Job title");
         contact->set_allocated_job(job);
+        ::qtprotobuf::examples::PhoneNumber home;
+        home.set_countrycode(7);
+        home.set_number(1232453467);
+        (*contact->mutable_phones())[::qtprotobuf::examples::Contact::Home] = home;
         return ::grpc::Status();
     }
     ::grpc::Status makeCall(::grpc::ServerContext* context, const ::qtprotobuf::examples::Contact* request, ::qtprotobuf::examples::SimpleResult* response) override