Browse Source

Add basic addressbook example

- Fix build issue
- Add proto and project definition for address book example
Alexey Edelev 6 years ago
parent
commit
a48c0b5dc4

+ 12 - 0
CMakeLists.txt

@@ -43,6 +43,14 @@ if(DEFINED $ENV{MAKE_TESTS})
 elseif(NOT DEFINED MAKE_TESTS)
     set(MAKE_TESTS ON)
 endif()
+
+if(DEFINED $ENV{MAKE_EXAMPLES})
+    set(MAKE_EXAMPLES ${MAKE_EXAMPLES})
+elseif(NOT DEFINED MAKE_EXAMPLES)
+    set(MAKE_EXAMPLES ON)
+endif()
+
+
 if(MAKE_TESTS)
     find_package(GTest)
     if (UNIX AND NOT GTest_FOUND)
@@ -61,3 +69,7 @@ if(MAKE_TESTS)
 
     add_subdirectory("tests")
 endif()
+
+if(MAKE_EXAMPLES)
+    add_subdirectory("examples/addressbook")
+endif()

+ 84 - 0
examples/addressbook/CMakeLists.txt

@@ -0,0 +1,84 @@
+set(addressbookgeneration "addressbook_generation")
+set(ADDRESSBOOK_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
+file(MAKE_DIRECTORY ${ADDRESSBOOK_OUT_DIR})
+file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/proto/addressbook.proto)
+## test sources generation
+if(WIN32)
+    set(Protobuf_PROTOC_EXECUTABLE ${PROTOBUF_INSTALATION_PATH}/bin/protoc.exe)
+endif()
+
+add_custom_target(${addressbookgeneration})
+add_custom_command(TARGET ${addressbookgeneration}
+        COMMAND ${Protobuf_PROTOC_EXECUTABLE} --${PROJECT_NAME}_opt=out=${ADDRESSBOOK_OUT_DIR} --plugin=protoc-gen-${PROJECT_NAME}=$<TARGET_FILE:${PROJECT_NAME}> --qtprotobuf_out=${ADDRESSBOOK_OUT_DIR} -I=${CMAKE_CURRENT_SOURCE_DIR}/proto/ ${PROTO_FILES}
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/proto/
+        DEPENDS ${PROTO_FILES}
+        COMMENT "Generating test headers"
+)
+add_dependencies(${addressbookgeneration} ${PROJECT_NAME})
+
+## test sources build
+# policy enables automoc for generated files
+if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
+    cmake_policy(SET CMP0071 NEW)
+endif()
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_AUTORCC ON)
+
+if(Qt5_POSITION_INDEPENDENT_CODE)
+    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+endif()
+
+find_package(Qt5 COMPONENTS Core Quick Network REQUIRED)
+
+set(EXPECTED_GENERATED_HEADERS 
+    globalenums.h
+)
+
+foreach(EXPECTED_GENERATED_HEADER ${EXPECTED_GENERATED_HEADERS})
+    get_filename_component(GENERATED_HEADER_BASENAME ${EXPECTED_GENERATED_HEADER} NAME_WE)
+
+    list(APPEND GENERATED_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${GENERATED_HEADER_BASENAME}.cpp)
+    list(APPEND GENERATED_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/${GENERATED_HEADER_BASENAME}.h)
+    if(${CMAKE_VERSION} VERSION_LESS "3.10.0")
+       set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${GENERATED_HEADER_BASENAME}.cpp PROPERTY SKIP_AUTOMOC ON)
+    endif()
+endforeach(EXPECTED_GENERATED_HEADER)
+
+if(${CMAKE_VERSION} VERSION_LESS "3.10.0")
+    qt5_wrap_cpp(MOC_SOURCES ${GENERATED_HEADERS})
+    list(APPEND GENERATED_SOURCES ${MOC_SOURCES})
+endif()
+
+# TODO: so far the following cpp files are not generated 
+list(REMOVE_ITEM GENERATED_SOURCES
+    ${CMAKE_CURRENT_BINARY_DIR}/globalenums.cpp
+)
+set_source_files_properties(${GENERATED_SOURCES} PROPERTIES GENERATED TRUE)
+
+# TODO: but their headers should be included in sources for executable to be processed by moc
+list(APPEND GENERATED_SOURCES
+    ${CMAKE_CURRENT_BINARY_DIR}/globalenums.h
+)
+set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/globalenums.h PROPERTIES GENERATED TRUE)
+
+include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/src/protobuf ${CMAKE_SOURCE_DIR}/src/grpc)
+
+if(WIN32)
+    #Set  path to GTest build libraries
+    set(GTEST_BOTH_LIBRARIES "")
+    #Set  path to GTest include directory
+    include_directories(${GTEST_INCLUDE_PATHS} "/")
+endif()
+
+file(GLOB SOURCES main.cpp)
+
+set(addressbook "addressbook_example")
+add_executable(${addressbook} ${SOURCES} ${GENERATED_SOURCES})
+if(WIN32)
+    target_link_libraries(${addressbook} qtprotobufsupport qtgrpc Qt5::Core)
+elseif(UNIX)
+    target_link_libraries(${addressbook} qtgrpc Qt5::Quick)
+endif()
+add_dependencies(${addressbook} ${addressbookgeneration})

+ 45 - 0
examples/addressbook/main.cpp

@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+#include <QGuiApplication>
+
+#include <QGuiApplication>
+#include <QQmlApplicationEngine>
+
+#include "contacts.h"
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
+
+    QGuiApplication app(argc, argv);
+
+    QQmlApplicationEngine engine;
+    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
+    if (engine.rootObjects().isEmpty())
+        return -1;
+
+    return app.exec();
+}

+ 83 - 0
examples/addressbook/proto/addressbook.proto

@@ -0,0 +1,83 @@
+/*
+ * 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;
+
+    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 (SimpleResult) {}
+    rpc removeContact(Contact) returns (SimpleResult) {}
+    rpc getContacts(ListFrame) returns (Contacts) {}
+    rpc makeCall(Contact) returns (SimpleResult) {}
+    rpc navigateTo(Address) returns (SimpleResult) {}
+}

+ 0 - 1
tests/CMakeLists.txt

@@ -73,7 +73,6 @@ set(EXPECTED_GENERATED_HEADERS
     testserviceclient.h
     testserviceserver.h
     simpleenumlistmessage.h
-    nestedsimpleintmessage.h
 )
 
 foreach(EXPECTED_GENERATED_HEADER ${EXPECTED_GENERATED_HEADERS})