Program Listing for File context.hpp

Return to documentation for file (include/email/context.hpp)

// Copyright 2020-2021 Christophe Bedard
//
// 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 EMAIL__CONTEXT_HPP_
#define EMAIL__CONTEXT_HPP_

#include <memory>
#include <optional>  // NOLINT cpplint mistakes <optional> for a C system header
#include <stdexcept>
#include <string>

#include "email/curl/context.hpp"
#include "email/email/info.hpp"
#include "email/email/polling_manager.hpp"
#include "email/email/receiver.hpp"
#include "email/email/sender.hpp"
#include "email/log.hpp"
#include "email/macros.hpp"
#include "email/options.hpp"
#include "email/service_handler.hpp"
#include "email/subscription_handler.hpp"
#include "email/visibility_control.hpp"

namespace email
{

class ContextError : public std::runtime_error
{
public:
  explicit ContextError(const std::string & msg)
  : std::runtime_error(msg)
  {}
};

class ContextNotInitializedError : public ContextError
{
public:
  ContextNotInitializedError()
  : ContextError("context not initialized")
  {}
};

class ContextAlreadyInitializedError : public ContextError
{
public:
  ContextAlreadyInitializedError()
  : ContextError("context already initialized")
  {}
};

class ContextInitFailedError : public ContextError
{
public:
  explicit ContextInitFailedError(std::optional<std::string> reason = std::nullopt)
  : ContextError("context init failed: " + reason.value_or("(unknown)"))
  {}
};


class Context
{
public:

  Context();

  ~Context();


  void
  init();


  void init(int argc, char const * const argv[]);


  bool shutdown();


  bool
  is_valid() const;


  EMAIL_PUBLIC
  std::shared_ptr<Options>
  get_options() const;


  std::shared_ptr<EmailReceiver>
  get_receiver() const;


  std::shared_ptr<EmailSender>
  get_sender() const;


  std::shared_ptr<PollingManager>
  get_polling_manager() const;


  std::shared_ptr<SubscriptionHandler>
  get_subscription_handler() const;


  std::shared_ptr<ServiceHandler>
  get_service_handler() const;

private:
  EMAIL_DISABLE_COPY(Context)


  void
  init_common();

  bool is_valid_;
  std::shared_ptr<Options> options_;
  std::shared_ptr<Logger> logger_;
  std::shared_ptr<EmailReceiver> receiver_;
  std::shared_ptr<EmailSender> sender_;
  std::shared_ptr<PollingManager> polling_manager_;
  std::shared_ptr<SubscriptionHandler> subscription_handler_;
  std::shared_ptr<ServiceHandler> service_handler_;
};


EMAIL_PUBLIC
std::shared_ptr<Context>
get_global_context();

}  // namespace email

#endif  // EMAIL__CONTEXT_HPP_