celix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [celix] pnoltes commented on a change in pull request #203: Feature/osgi promise
Date Wed, 22 Apr 2020 06:48:50 GMT

pnoltes commented on a change in pull request #203:
URL: https://github.com/apache/celix/pull/203#discussion_r412448723



##########
File path: misc/experimental/promise/api/celix/Promise.h
##########
@@ -0,0 +1,512 @@
+/**
+ *Licensed to the Apache Software Foundation (ASF) under one
+ *or more contributor license agreements.  See the NOTICE file
+ *distributed with this work for additional information
+ *regarding copyright ownership.  The ASF licenses this file
+ *to you under the Apache License, Version 2.0 (the
+ *"License"); you may not use this file except in compliance
+ *with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *Unless required by applicable law or agreed to in writing,
+ *software distributed under the License is distributed on an
+ *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ *specific language governing permissions and limitations
+ *under the License.
+ */
+
+#pragma once
+
+#include "celix/impl/SharedPromiseState.h"
+
+namespace celix {
+
+    /**
+     * A Promise of a value.
+     * <p>
+     * A Promise represents a future value. It handles the interactions for
+     * asynchronous processing. A {@link Deferred} object can be used to create a
+     * Promise and later resolve the Promise. A Promise is used by the caller of an
+     * asynchronous function to get the result or handle the error. The caller can
+     * either get a callback when the Promise is resolved with a value or an error,
+     * or the Promise can be used in chaining. In chaining, callbacks are provided
+     * that receive the resolved Promise, and a new Promise is generated that
+     * resolves based upon the result of a callback.
+     * <p>
+     * Both {@link #onResolve(Runnable) callbacks} and
+     * {@link #then(Success, Failure) chaining} can be repeated any number of times,
+     * even after the Promise has been resolved.
+     * <p>
+     * Example callback usage:
+     *
+     * <pre>
+     * celix::Promise&lt;std::string&gt; foo{};
+     * foo.onResolve([]{ std::cout << "resolved" << std::endl; });
+     * </pre>
+     *
+     *
+     * @tparam <T> The value type associated with this Promise.
+     * @ThreadSafe
+     */
+    template<typename T>
+    class Promise {
+    public:
+        using type = T;
+
+        explicit Promise(std::shared_ptr<celix::impl::SharedPromiseState<T>>
s);
+
+//        ~Promise() {
+//            //TODO maybe make a special detach call to state if the count is 1
+//            //state->detachIfNeeded(state); //create a callback with ref to self if
share_ptr count is 1
+//        }
+
+        /**
+         * Returns whether this Promise has been resolved.
+         *
+         * <p>
+         * This Promise may be successfully resolved or resolved with a failure.
+         *
+         * @return {@code true} if this Promise was resolved either successfully or
+         *         with a failure; {@code false} if this Promise is unresolved.
+         */
+        bool isDone() const;
+
+        //
+        /**
+         * Returns whether this Promise has been resolved and whether it resolved successfully.
+         * NOTE although not part of the OSGi spec, IMO this is clearer than (isDone() &&
!getFailure())
+         *
+         *
+         * @return {@code true} if this Promise was resolved successfully.
+         *         {@code false} if this Promise is unresolved or resolved with a failure.
+         */
+        bool isSuccessfullyResolved() const;
+
+        /**
+         * Returns the failure of this Promise.
+         *
+         * <p>
+         * If this Promise is not {@link #isDone() resolved}, this method must block
+         * and wait for this Promise to be resolved before completing.
+         *
+         * <p>
+         * If this Promise was resolved with a failure, this method returns with the
+         * failure of this Promise. If this Promise was successfully resolved, this
+         * method must return {@code null}.
+         *
+         * @return The failure of this resolved Promise or {@code null} if this
+         *         Promise was successfully resolved.
+         * @throws InterruptedException If the current thread was interrupted while
+         *         waiting.
+         */
+        std::exception_ptr getFailure() const;
+
+        /**
+         * Returns the value of this Promise.
+         *
+         * <p>
+         * If this Promise is not {@link #isDone() resolved}, this method must block
+         * and wait for this Promise to be resolved before completing.
+         *
+         * <p>
+         * If this Promise was successfully resolved, this method returns with the
+         * value of this Promise. If this Promise was resolved with a failure, this
+         * method must throw an {@code InvocationTargetException} with the
+         * {@link #getFailure() failure exception} as the cause.
+         *
+         * @return The value of this resolved Promise.
+         * @throws InvocationTargetException If this Promise was resolved with a
+         *         failure. The cause of the {@code InvocationTargetException} is
+         *         the failure exception.
+         * @throws InterruptedException If the current thread was interrupted while
+         *         waiting.
+         */
+        const T& getValue() const;
+
+        //TODO is a move value needed? Howto handle this with resolve callbacks
+        T moveValue();
+
+        /**
+         * Wait till the promise is resolved.
+         *
+         * <p>
+         * If this Promise is not {@link #isDone() resolved}, this method must block
+         * and wait for this Promise to be resolved before completing.
+         *
+         */
+        void wait() const; //NOTE not part of the OSGI promise, wait till resolved (used
in testing)

Review comment:
       getValue return a possible value or throws an exception. 
   
   Mainly for testing you just want to wait till a promise is resolved. The actual asserts/checks
are done on success/failure callbacks.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message