Program Listing for File service.hpp

Return to documentation for file (include/naoqi_driver/service/service.hpp)

/*
 * Copyright 2015 Aldebaran
 *
 * 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 SERVICE_HPP
#define SERVICE_HPP

#include <string>

#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

#include <rclcpp/rclcpp.hpp>

namespace naoqi
{
namespace service
{


class Service
{

public:

  template<typename T>
  Service( T srv ):
    srvPtr_( boost::make_shared<ServiceModel<T> >(srv) )
  {}

  void reset( rclcpp::Node* node )
  {
    std::cout << name() << " is resetting" << std::endl;
    srvPtr_->reset( node );
    std::cout << name() << " reset" << std::endl;
  }

  std::string name() const
  {
    return srvPtr_->name();
  }

  std::string topic() const
  {
    return srvPtr_->topic();
  }

private:

  struct ServiceConcept
  {
    virtual ~ServiceConcept(){}
    virtual void reset( rclcpp::Node* node ) = 0;
    virtual std::string name() const = 0;
    virtual std::string topic() const = 0;
  };


  template<typename T>
  struct ServiceModel : public ServiceConcept
  {
    ServiceModel( const T& other ):
      service_( other )
    {}

    std::string name() const
    {
      return service_->name();
    }

    std::string topic() const
    {
      return service_->topic();
    }

    bool isInitialized() const
    {
      return service_->isInitialized();
    }

    void reset( rclcpp::Node* node )
    {
      service_->reset( node );
    }

    T service_;
  };

  boost::shared_ptr<ServiceConcept> srvPtr_;

}; // class service

} //service
} //naoqi

#endif