LCOV - code coverage report
Current view: top level - modules/shm - shm_publisher_impl.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 33 34 97.1 %
Date: 2026-09-30 18:30:16 Functions: 12 13 92.3 %
Branches: 14 26 53.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2026 by Thun Lu. All rights reserved.
       3                 :            :  * Author: Thun Lu <thun.lu@zohomail.cn>
       4                 :            :  * Repo:   https://github.com/thun-res/vlink
       5                 :            :  *  _    __   __      _           __
       6                 :            :  * | |  / /  / /     (_) ____    / /__
       7                 :            :  * | | / /  / /     / / / __ \  / //_/
       8                 :            :  * | |/ /  / /___  / / / / / / / ,<
       9                 :            :  * |___/  /_____/ /_/ /_/ /_/ /_/|_|
      10                 :            :  *
      11                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
      12                 :            :  * you may not use this file except in compliance with the License.
      13                 :            :  * You may obtain a copy of the License at
      14                 :            :  *
      15                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
      16                 :            :  *
      17                 :            :  * Unless required by applicable law or agreed to in writing, software
      18                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      19                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      20                 :            :  * See the License for the specific language governing permissions and
      21                 :            :  * limitations under the License.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "./shm_publisher_impl.h"
      25                 :            : 
      26                 :            : #include <utility>
      27                 :            : 
      28                 :            : #include "./base/helpers.h"
      29                 :            : 
      30                 :            : namespace vlink {
      31                 :            : 
      32                 :            : // ShmPublisherImpl
      33         [ +  - ]:         53 : ShmPublisherImpl::ShmPublisherImpl(const ShmConf& conf) : conf_(conf) {}
      34                 :            : 
      35                 :         53 : void ShmPublisherImpl::init() {
      36   [ +  +  +  -  :         53 :   static auto& factory = ShmFactory::get();
             +  -  -  - ]
      37                 :            : 
      38                 :         53 :   conf_.hash_code = Helpers::get_hash_code(conf_.event);
      39                 :            : 
      40         [ +  + ]:         53 :   if (init_impl_type == kSetter) {
      41         [ +  - ]:          1 :     if (conf_.history == 0) {
      42                 :          1 :       conf_.history = 1;
      43                 :            :     }
      44                 :            : 
      45         [ +  - ]:          2 :     object_ = factory.get_object<Object>(
      46         [ +  - ]:          3 :         {kSetter, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait, conf_.event, nullptr});
      47                 :            :   } else {
      48   [ +  -  +  - ]:        104 :     object_ = factory.get_object<Object>(
      49                 :        156 :         {kImplType, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait, std::string{}, nullptr});
      50                 :            :   }
      51                 :            : 
      52                 :         53 :   object_->add_impl(this);
      53                 :            : 
      54   [ +  -  +  - ]:         97 :   object_->register_sub_connect_callback(this, [this](bool) { PublisherImpl::update_subscribers(); });
      55                 :            : 
      56                 :         53 :   PublisherImpl::update_subscribers();
      57                 :         53 : }
      58                 :            : 
      59                 :         53 : void ShmPublisherImpl::deinit() {
      60                 :         53 :   object_->remove_impl(this);
      61                 :            : 
      62                 :         53 :   detach();
      63                 :         53 : }
      64                 :            : 
      65                 :         54 : bool ShmPublisherImpl::is_support_loan() const { return true; }
      66                 :            : 
      67                 :         71 : Bytes ShmPublisherImpl::loan(int64_t size) { return object_->loan(static_cast<uint64_t>(conf_.hash_code), size); }
      68                 :            : 
      69                 :          3 : bool ShmPublisherImpl::return_loan(const Bytes& bytes) { return object_->release(bytes); }
      70                 :            : 
      71                 :          0 : const Conf* ShmPublisherImpl::get_conf() const { return &conf_; }
      72                 :            : 
      73                 :         15 : const AbstractNode* ShmPublisherImpl::get_abstract_node() const { return object_.get(); }
      74                 :            : 
      75                 :       4331 : bool ShmPublisherImpl::has_subscribers() const { return object_->has_subscribers(); }
      76                 :            : 
      77                 :          2 : void ShmPublisherImpl::detect_subscribers(ConnectCallback&& callback) {
      78                 :          2 :   object_->enable_detect_timer();
      79                 :            : 
      80                 :          2 :   PublisherImpl::detect_subscribers(std::move(callback));
      81                 :          2 : }
      82                 :            : 
      83                 :         46 : bool ShmPublisherImpl::wait_for_subscribers(std::chrono::milliseconds timeout) {
      84                 :         46 :   object_->enable_detect_timer();
      85                 :            : 
      86                 :         46 :   return PublisherImpl::wait_for_subscribers(timeout);
      87                 :            : }
      88                 :            : 
      89                 :       4208 : bool ShmPublisherImpl::write(const Bytes& msg_data) {
      90                 :       4208 :   return object_->publish(static_cast<uint64_t>(conf_.hash_code), msg_data);
      91                 :            : }
      92                 :            : 
      93                 :            : }  // namespace vlink

Generated by: LCOV version 1.14