<commit_before>/*
 * Medical Image Registration ToolKit (MIRTK)
 *
 * Copyright 2013-2016 Imperial College London
 * Copyright 2013-2016 <NAME>
 *
 * 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 MIRTK_Object_H
#define MIRTK_Object_H

#include "mirtk/Array.h"
#include "mirtk/Exception.h"
#include "mirtk/Pair.h"
#include "mirtk/String.h"
#include "mirtk/Stream.h"


namespace mirtk {


// =============================================================================
// Basic object interface
// =============================================================================

/// Ordered list of parameter name/value pairs
typedef Array<Pair<string, string> >    ParameterList;
typedef ParameterList::iterator         ParameterIterator;
typedef ParameterList::const_iterator   ParameterConstIterator;


/**
 * Base class for all MIRTK object classes
 *
 * \note This base class must be a virtual interface without any own data
 *       members to not change the type size of subclasses! Derive another
 *       intermediate abstract base class from it to add data members shared
 *       by a more specific class of objects.
 */
class Object
{
public:

  /// Get name of this class type
  static const char *NameOfType();

  /// Get name of class, which this object is an instance of
  virtual const char *NameOfClass() const = 0;

  /// Destructor
  virtual ~Object() = default;

  /// Set parameter value from string
  ///
  /// \param[in] name  Parameter name.
  /// \param[in] value Parameter value.
  ///
  /// \returns Whether the named parameter is valid and a valid value was
  ///          set from the provided string representation of the value.
  virtual bool Set(const char *name, const char *value);

  /// Get parameter name/value pairs
  virtual ParameterList Parameter() const;

  /// Set parameters from name/value pairs
  void Parameter(const ParameterList &);

  // ---------------------------------------------------------------------------
  // Error handling
protected:

  /// Raise error in member function
  ///
  /// The current implementation prints the error message to STDERR and terminates
  /// the program with exit code 1. In future releases, when all library code has
  /// been rewritten to use this function, a suitable runtime exception may be
  /// thrown instead.
  ///
  /// \param[in] err  Error type. Unused at the moment, but may be used in
  ///                 future releases to throw the appropriate exception type.
  /// \param[in] cls  Name of class that defines the member function.
  /// \param[in] func Name of member function which is throwing the error (i.e., __func__).
  /// \param[in] args Error message. The given arguments are converted to strings
  ///                 using the ToString template function. These strings are then
  ///                 concatenated to produce the complete error message.
  template <typename... Args>
  static void Throw(ErrorType err, const char *cls, const char *func, Args... args);

  /// Raise error in member function
  ///
  /// The current implementation prints the error message to STDERR and terminates
  /// the program with exit code 1. In future releases, when all library code has
  /// been rewritten to use this function, a suitable runtime exception may be
  /// thrown instead.
  ///
  /// \param[in] err  Error type. Unused at the moment, but may be used in
  ///                 future releases to throw the appropriate exception type.
  /// \param[in] func Name of member function which is throwing the error (i.e., __func__).
  /// \param[in] args Error message. The given arguments are converted to strings
  ///                 using the ToString template function. These strings are then
  ///                 concatenated to produce the complete error message.
  template <typename... Args>
  void Throw(ErrorType err, const char *func, Args... args) const;
};

// =============================================================================
// Inline definitions
// =============================================================================

// -----------------------------------------------------------------------------
inline const char *Object::NameOfType()
{
  return "mirtk::Object";
}

// -----------------------------------------------------------------------------
inline bool Object::Set(const char *, const char *)
{
  return false;
}

// -----------------------------------------------------------------------------
inline ParameterList Object::Parameter() const
{
  return ParameterList();
}

// -----------------------------------------------------------------------------
inline void Object::Parameter(const ParameterList &param)
{
  for (ParameterConstIterator it = param.begin(); it != param.end(); ++it) {
    this->Set(it->first.c_str(), it->second.c_str());
  }
}

// -----------------------------------------------------------------------------
template <typename... Args>
void Object::Throw(ErrorType err, const char *cls, const char *func, Args... args)
{
  string member(cls);
  member += "::";
  member += func;
  mirtk::Throw(err, member.c_str(), args...);
}

// -----------------------------------------------------------------------------
template <typename... Args>
void Object::Throw(ErrorType err, const char *func, Args... args) const
{
  Throw(err, this->NameOfClass(), func, args...);
}

// =============================================================================
// Auxiliary functions for subclass implementation
// =============================================================================

// -----------------------------------------------------------------------------
/// Find parameter in parameters list
inline ParameterConstIterator Find(const ParameterList &params, string name)
{
  ParameterConstIterator it = params.begin();
  while (it != params.end() && it->first != name) ++it;<commit_msg>fix: Ambiguity of static/non-static Object::Throw call [Common] (#372)<commit_after>/*
 * Medical Image Registration ToolKit (MIRTK)
 *
 * Copyright 2013-2016 Imperial College London
 * Copyright 2013-2016 <NAME>
 *
 * 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 MIRTK_Object_H
#define MIRTK_Object_H

#include "mirtk/Array.h"
#include "mirtk/Exception.h"
#include "mirtk/Pair.h"
#include "mirtk/String.h"
#include "mirtk/Stream.h"


namespace mirtk {


// =============================================================================
// Basic object interface
// =============================================================================

/// Ordered list of parameter name/value pairs
typedef Array<Pair<string, string> >    ParameterList;
typedef ParameterList::iterator         ParameterIterator;
typedef ParameterList::const_iterator   ParameterConstIterator;


/**
 * Base class for all MIRTK object classes
 *
 * \note This base class must be a virtual interface without any own data
 *       members to not change the type size of subclasses! Derive another
 *       intermediate abstract base class from it to add data members shared
 *       by a more specific class of objects.
 */
class Object
{
public:

  /// Get name of this class type
  static const char *NameOfType();

  /// Get name of class, which this object is an instance of
  virtual const char *NameOfClass() const = 0;

  /// Destructor
  virtual ~Object() = default;

  /// Set parameter value from string
  ///
  /// \param[in] name  Parameter name.
  /// \param[in] value Parameter value.
  ///
  /// \returns Whether the named parameter is valid and a valid value was
  ///          set from the provided string representation of the value.
  virtual bool Set(const char *name, const char *value);

  /// Get parameter name/value pairs
  virtual ParameterList Parameter() const;

  /// Set parameters from name/value pairs
  void Parameter(const ParameterList &);

  // ---------------------------------------------------------------------------
  // Error handling
protected:

  /// Raise error in member function
  ///
  /// The current implementation prints the error message to STDERR and terminates
  /// the program with exit code 1. In future releases, when all library code has
  /// been rewritten to use this function, a suitable runtime exception may be
  /// thrown instead.
  ///
  /// \param[in] err  Error type. Unused at the moment, but may be used in
  ///                 future releases to throw the appropriate exception type.
  /// \param[in] cls  Name of class that defines the member function.
  /// \param[in] func Name of member function which is throwing the error (i.e., __func__).
  /// \param[in] args Error message. The given arguments are converted to strings
  ///                 using the ToString template function. These strings are then
  ///                 concatenated to produce the complete error message.
  template <typename... Args>
  static void ThrowStatic(ErrorType err, const char *cls, const char *func, Args... args);

  /// Raise error in member function
  ///
  /// The current implementation prints the error message to STDERR and terminates
  /// the program with exit code 1. In future releases, when all library code has
  /// been rewritten to use this function, a suitable runtime exception may be
  /// thrown instead.
  ///
  /// \param[in] err  Error type. Unused at the moment, but may be used in
  ///                 future releases to throw the appropriate exception type.
  /// \param[in] func Name of member function which is throwing the error (i.e., __func__).
  /// \param[in] args Error message. The given arguments are converted to strings
  ///                 using the ToString template function. These strings are then
  ///                 concatenated to produce the complete error message.
  template <typename... Args>
  void Throw(ErrorType err, const char *func, Args... args) const;
};

// =============================================================================
// Inline definitions
// =============================================================================

// -----------------------------------------------------------------------------
inline const char *Object::NameOfType()
{
  return "mirtk::Object";
}

// -----------------------------------------------------------------------------
inline bool Object::Set(const char *, const char *)
{
  return false;
}

// -----------------------------------------------------------------------------
inline ParameterList Object::Parameter() const
{
  return ParameterList();
}

// -----------------------------------------------------------------------------
inline void Object::Parameter(const ParameterList &param)
{
  for (ParameterConstIterator it = param.begin(); it != param.end(); ++it) {
    this->Set(it->first.c_str(), it->second.c_str());
  }
}

// -----------------------------------------------------------------------------
template <typename... Args>
void Object::ThrowStatic(ErrorType err, const char *cls, const char *func, Args... args)
{
  string member(cls);
  member += "::";
  member += func;
  mirtk::Throw(err, member.c_str(), args...);
}

// -----------------------------------------------------------------------------
template <typename... Args>
void Object::Throw(ErrorType err, const char *func, Args... args) const
{
  ThrowStatic(err, this->NameOfClass(), func, args...);
}

// =============================================================================
// Auxiliary functions for subclass implementation
// =============================================================================

// -----------------------------------------------------------------------------
/// Find parameter in parameters list
inline ParameterConstIterator Find(const ParameterList &params, string name)
{
  ParameterConstIterator it = params.begin();
  while (it != params.end() && it->first != name) ++it;
{'fraction_non_alphanumeric': 0.25651396126623627, 'fraction_numerical': 0.0038111534572606363, 'mean_word_length': 2.2801020408163266, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5308450', 'n_tokens_mistral': 3021, 'n_tokens_neox': 2899, 'n_words': 1242}