diff options
Diffstat (limited to 'idlc/gen/cpp_gen_base_cb.h')
-rw-r--r-- | idlc/gen/cpp_gen_base_cb.h | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/idlc/gen/cpp_gen_base_cb.h b/idlc/gen/cpp_gen_base_cb.h new file mode 100644 index 0000000..56216c4 --- /dev/null +++ b/idlc/gen/cpp_gen_base_cb.h @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed 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. + */ + +#ifndef IDLC_CPP_GEN_CPP_GEN_BASE_CB_H_ +#define IDLC_CPP_GEN_CPP_GEN_BASE_CB_H_ + +const char CB_BUNDLE[] = R"__cls_bundle(class Bundle final { + public: + Bundle() { + raw_ = bundle_create(); + } + + Bundle(bundle* b) { + raw_ = b; + } + + ~Bundle() { + if (raw_) + bundle_free(raw_); + } + + Bundle(Bundle&& b) : raw_(b.raw_) { + b.raw_ = nullptr; + } + + Bundle& operator = (Bundle&& b) { + raw_ = b.raw_; + b.raw_ = nullptr; + return *this; + } + + Bundle(const Bundle& b) : raw_(bundle_dup(b.GetHandle())) {} + + Bundle& operator = (const Bundle& b) { + raw_ = bundle_dup(b.GetHandle()); + return *this; + } + + bundle* GetHandle() const { + return raw_; + } + + private: + bundle* raw_; +}; + +)__cls_bundle"; + +const char CB_CALLBACK_BASE[] = +R"__cpp_cb( +std::atomic<int> ##::CallbackBase::seq_num_ { 0 }; + +##::CallbackBase::CallbackBase(int delegate_id, bool once) + : id_(delegate_id), once_(once) { + seq_id_ = seq_num_++; +} + +int ##::CallbackBase::GetId() const { + return id_; +} + +int ##::CallbackBase::GetSeqId() const { + return seq_id_; +} + +bool ##::CallbackBase::IsOnce() const { + return once_; +} + +std::string ##::CallbackBase::GetTag() const { + return std::to_string(id_) + "::" + std::to_string(seq_id_); +} + +rpc_port_parcel_h operator << (rpc_port_parcel_h h, const ##::CallbackBase& cb) { + rpc_port_parcel_write_int32(h, cb.id_); + rpc_port_parcel_write_int32(h, cb.seq_id_); + rpc_port_parcel_write_bool(h, cb.once_); + + return h; +} + +rpc_port_parcel_h operator >> (rpc_port_parcel_h h, ##::CallbackBase& cb) { + rpc_port_parcel_read_int32(h, &cb.id_); + rpc_port_parcel_read_int32(h, &cb.seq_id_); + rpc_port_parcel_read_bool(h, &cb.once_); + + return h; +} +)__cpp_cb"; + +const char CB_VERSION[] = +R"__cpp_cb(/* + * Generated by tidlc $$. + */ +)__cpp_cb"; + +const char CB_CALLBACK_BASE_HEADER_FRONT[] = +R"__cpp_cb( + class CallbackBase { + public: + CallbackBase(int delegate_id, bool once); + virtual ~CallbackBase() = default; +)__cpp_cb"; + +const char CB_CALLBACK_BASE_HEADER_BACK[] = +R"__cpp_cb( + int GetId() const; + int GetSeqId() const; + bool IsOnce() const; + std::string GetTag() const; + + private: + friend rpc_port_parcel_h operator << (rpc_port_parcel_h h, const CallbackBase& cb); + friend rpc_port_parcel_h operator >> (rpc_port_parcel_h h, CallbackBase& cb); + + static std::atomic<int> seq_num_; + int id_; + int seq_id_; + bool once_; + }; +)__cpp_cb"; + +const char CB_CALLBACK_CLASS[] = +R"__cpp_cb( + class $$ : public CallbackBase { + public:$$ +$$ + private:$$ + }; +)__cpp_cb"; + +const char CB_CALLBACK_CTOR_STUB[] = +R"__cpp_cb( + ##(rpc_port_h port, std::weak_ptr<ServiceBase> service) + : CallbackBase(static_cast<int>(DelegateId::##), false) { + port_ = port; + service_ = std::move(service); + } +)__cpp_cb"; + +const char CB_CALLBACK_CTOR_PROXY[] = +R"__cpp_cb( + ##(bool once = false) + : CallbackBase(static_cast<int>(DelegateId::##), once) {} +)__cpp_cb"; + +const char CB_CALLBACK_PRIVATE_PROXY[] = +R"__cpp_cb( + void OnReceivedEvent(rpc_port_parcel_h port) override; +)__cpp_cb"; + +const char CB_CALLBACK_PRIVATE_STUB[] = +R"__cpp_cb( + rpc_port_h port_; + std::weak_ptr<ServiceBase> service_; + bool valid_ = true; +)__cpp_cb"; + +const char CB_CALLBACK_INVOKE_METHOD[] = +R"__cpp_cb( +void $$::$$::Invoke($$) { + if (port_ == nullptr) + throw NotConnectedSocketException(); + if (service_.lock().get() == nullptr) + throw NotConnectedSocketException(); + + if (IsOnce() && !valid_) + throw InvalidCallbackException(); + + rpc_port_parcel_h p; + rpc_port_parcel_create(&p); + rpc_port_parcel_write_int32(p, static_cast<int>(MethodId::__Callback)); + p << *this; +$$ + // Send + set_last_result(rpc_port_parcel_send(p, port_)); + rpc_port_parcel_destroy(p); + valid_ = false; +} +)__cpp_cb"; + +const char CB_CALLBACK_ON_RECEIVED_EVENT_METHOD[] = +R"__cpp_cb( +void $$::$$::OnReceivedEvent(rpc_port_parcel_h parcel) { +$$ +} +)__cpp_cb"; + +const char CB_LOG_TAG[] = +R"__cpp_cb( +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "$$" +)__cpp_cb"; + +const char CB_LOG_DEF[] = +R"__cpp_cb( +#ifdef _E +#undef _E +#endif + +#ifdef _W +#undef _W +#endif + +#ifdef _I +#undef _I +#endif + +#ifdef _D +#undef _D +#endif + +#define _E(fmt, ...) dlog_print(DLOG_ERROR, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define _W(fmt, ...) dlog_print(DLOG_WARN, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define _I(fmt, ...) dlog_print(DLOG_INFO, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define _D(fmt, ...) dlog_print(DLOG_DEBUG, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__) +)__cpp_cb"; + +#endif // IDLC_CPP_GEN_CPP_GEN_BASE_CB_H_ |