<commit_before>/*
  @copyright <NAME> 2000-2013
  @author <NAME>
  This file is part of EcoLab

  Open source licensed under the MIT license. See LICENSE for details.
*/

/* a series of TCL commands for accessing members of a C++
object. Accessed by the macro make_model(x), where x is the
name of a C++ object.

*/
/**\file
\brief TCL access descriptor
*/

#ifndef TCL_OBJ_BASE_H
#define TCL_OBJ_BASE_H
#include "tcl++.h"
#include "pack_base.h"
#include "pack_stl.h"
#include "ref.h"
#include "error.h"
#include "accessor.h"

#include "isa_base.h"
#include "function.h"
#include <iostream>
#include <sstream>

/* define this macro to x to track TCL_obj registrations */
#define TCL_OBJ_DBG(x)

// ensure TCL_obj_templates is included, or linktime failure
namespace
{
  int TCL_obj_template_not_included();
  int dummyXXX=TCL_obj_template_not_included();
}

namespace ecolab
{
  using namespace classdesc;
  using classdesc::enable_if;
  using functional::bound_method;
  using functional::Return;
  /* classes for wrapping C++ functions, suitable for passing to
     Tcl_CreateCommand */

  template <class T>
  void ensure_exists(T*& x)
  {if (x==NULL) x=new T;}

  /* utility macro for declaring references to objects referred to in TCL
     arguments */

#define declare(name, typename, tcl_name)                               \
  typename *name##_entry;                                               \
  if (TCL_obj_properties().count(const_cast<char*>(tcl_name))==0)       \
    throw error("%s does not exist!",tcl_name);                         \
  name##_entry=TCL_obj_properties()[tcl_name]->memberPtrCasted<typename>(); \
  if (!name##_entry)                                                    \
    throw error("Incorrect argument %s assigned to %s",tcl_name,#name); \
  typename& name=*name##_entry;

  int TCL_proc(ClientData cd, Tcl_Interp *interp, int argc, CONST84 char **argv);
  int TCL_oproc(ClientData cd, Tcl_Interp *interp, int argc, 
                Tcl_Obj *const argv[]);  
  void TCL_delete(ClientData cd);

  inline void TCL_cmd_data_delete(ClientData cd)
  {
    delete static_cast<cmd_data*>(cd);
  }

  /* TCL_args - support for TCL object types:
     Use >> notation to extract arguments */

#if (TCL_MAJOR_VERSION<8)
#error TCL 8.x or greater supported. Please upgrade your TCL
#endif

  /* used for testing whether simple or compound type */

#ifdef _CLASSDESC
#pragma omit pack ecolab::TCL_args
#pragma omit unpack ecolab::TCL_args
#pragma omit TCL_obj ecolab::TCL_args
#endif

  /**
     \brief Represent arguments to TCL commands

     Declare a member function taking a single argument of type
     TCL_args, the extract the arguments using operator>>, or assign the
     arguments 1 by 1, or use operator[] to access

     \code
     struct foo
     {
     void bar1(TCL_args args)
     {
     int arg1=args, arg2=args;
     }
     int bar2(TCL_args args)
     {
     int arg1, arg2;
     args>>arg1>>arg2;
     }
     int bar3(TCL_args args)
     {
     int arg1=args[0], arg2=args[1];
     }
     }
     \endcode

     args[-1] refers to the command name.
  */
  class TCL_args
  {
    int nextArg;
    int m_count;
    vector<const Tcl_Obj*> argv;
    const Tcl_Obj *  pop_arg()
    {
      if (nextArg<argv.size()) 
        return argv[nextArg++];} 
      else 
        throw error("too few arguments");
    }
    CLASSDESC_ACCESS(TCL_args);
    
    void pushObj(
  public:
    const int& count;
    TCL_args(): nextArg(0), argv(NULL), count(m_count) {}
    TCL_args(int a, Tcl_Obj *const *v): nextArg(0)m_count(a), argv(v) , count(m_count)
    {if (count) pop_arg();}
    TCL_args(const TCL_args& x): m_count(x.m_count), argv(x.argv), 
                                 count(m_count) {}
    TCL_args operator[](int i) const
    {
      if (count<=i) 
        throw error("too few arguments");
      else
        return TCL_args(2,argv+i-1);
    }

    const char* str(); 

    TCL_args& operator<<

    TCL_args& operator>>(std::string& x) {x=str(); return *this;}
    TCL_args& operator>>(const char*& x) {x=str(); return *this;}
    TCL_args& operator>>(bool& x) {
      int tmp;
      if (Tcl_GetBooleanFromObj(interp(),pop_arg(),&tmp)!=TCL_OK) 
        throw error("argument error");
      x=tmp;
      return *this;
    }
    TCL_args& operator>>(int& x) {
      if (Tcl_GetIntFromObj(interp(),pop_arg(),&x)!=TCL_OK) 
        throw error("argument error");
      return *this;
    }
    TCL_args& operator>>(unsigned& x) {
      int tmp;
      if (Tcl_GetIntFromObj(interp(),pop_arg(),&tmp)!=TCL_OK) 
        throw error("argument error");
      if (tmp>=0) x=tmp;
      else throw error("assigning %d to an unsigned variable",tmp);
      return *this;
    }
    TCL_args& operator>>(long& x) {
      if (Tcl_GetLongFromObj(interp(),pop_arg(),&x)!=TCL_OK) 
        throw error("argument error");<commit_msg>Revert accidental commit of WIP at 8b99b806dcd9412499bb9691a49177a65f5bd66b.<commit_after>/*
  @copyright <NAME> 2000-2013
  @author <NAME>
  This file is part of EcoLab

  Open source licensed under the MIT license. See LICENSE for details.
*/

/* a series of TCL commands for accessing members of a C++
object. Accessed by the macro make_model(x), where x is the
name of a C++ object.

*/
/**\file
\brief TCL access descriptor
*/

#ifndef TCL_OBJ_BASE_H
#define TCL_OBJ_BASE_H
#include "tcl++.h"
#include "pack_base.h"
#include "pack_stl.h"
#include "ref.h"
#include "error.h"
#include "accessor.h"

#include "isa_base.h"
#include "function.h"
#include <iostream>
#include <sstream>

/* define this macro to x to track TCL_obj registrations */
#define TCL_OBJ_DBG(x)

// ensure TCL_obj_templates is included, or linktime failure
namespace
{
  int TCL_obj_template_not_included();
  int dummyXXX=TCL_obj_template_not_included();
}

namespace ecolab
{
  using namespace classdesc;
  using classdesc::enable_if;
  using functional::bound_method;
  using functional::Return;
  /* classes for wrapping C++ functions, suitable for passing to
     Tcl_CreateCommand */

  template <class T>
  void ensure_exists(T*& x)
  {if (x==NULL) x=new T;}

  /* utility macro for declaring references to objects referred to in TCL
     arguments */

#define declare(name, typename, tcl_name)                               \
  typename *name##_entry;                                               \
  if (TCL_obj_properties().count(const_cast<char*>(tcl_name))==0)       \
    throw error("%s does not exist!",tcl_name);                         \
  name##_entry=TCL_obj_properties()[tcl_name]->memberPtrCasted<typename>(); \
  if (!name##_entry)                                                    \
    throw error("Incorrect argument %s assigned to %s",tcl_name,#name); \
  typename& name=*name##_entry;

  int TCL_proc(ClientData cd, Tcl_Interp *interp, int argc, CONST84 char **argv);
  int TCL_oproc(ClientData cd, Tcl_Interp *interp, int argc, 
                Tcl_Obj *const argv[]);  
  void TCL_delete(ClientData cd);

  inline void TCL_cmd_data_delete(ClientData cd)
  {
    delete static_cast<cmd_data*>(cd);
  }

  /* TCL_args - support for TCL object types:
     Use >> notation to extract arguments */

#if (TCL_MAJOR_VERSION<8)
#error TCL 8.x or greater supported. Please upgrade your TCL
#endif

  /* used for testing whether simple or compound type */

#ifdef _CLASSDESC
#pragma omit pack ecolab::TCL_args
#pragma omit unpack ecolab::TCL_args
#pragma omit TCL_obj ecolab::TCL_args
#endif

  /**
     \brief Represent arguments to TCL commands

     Declare a member function taking a single argument of type
     TCL_args, the extract the arguments using operator>>, or assign the
     arguments 1 by 1, or use operator[] to access

     \code
     struct foo
     {
     void bar1(TCL_args args)
     {
     int arg1=args, arg2=args;
     }
     int bar2(TCL_args args)
     {
     int arg1, arg2;
     args>>arg1>>arg2;
     }
     int bar3(TCL_args args)
     {
     int arg1=args[0], arg2=args[1];
     }
     }
     \endcode

     args[-1] refers to the command name.
  */
  class TCL_args
  {
    int m_count;
    Tcl_Obj * const *argv;
    Tcl_Obj *  pop_arg()
    {
      if (m_count>0) 
        {m_count--; return *argv++;} 
      else 
        throw error("too few arguments");
    }
    CLASSDESC_ACCESS(TCL_args);
  public:
    const int& count;
    TCL_args(): m_count(0), argv(NULL), count(m_count) {}
    TCL_args(int a, Tcl_Obj *const *v): m_count(a), argv(v) , count(m_count)
    {if (count) pop_arg();}
    TCL_args(const TCL_args& x): m_count(x.m_count), argv(x.argv), 
                                 count(m_count) {}
    TCL_args operator[](int i) const
    {
      if (count<=i) 
        throw error("too few arguments");
      else
        return TCL_args(2,argv+i-1);
    }

    const char* str(); 

    TCL_args& operator>>(std::string& x) {x=str(); return *this;}
    TCL_args& operator>>(const char*& x) {x=str(); return *this;}
    TCL_args& operator>>(bool& x) {
      int tmp;
      if (Tcl_GetBooleanFromObj(interp(),pop_arg(),&tmp)!=TCL_OK) 
        throw error("argument error");
      x=tmp;
      return *this;
    }
    TCL_args& operator>>(int& x) {
      if (Tcl_GetIntFromObj(interp(),pop_arg(),&x)!=TCL_OK) 
        throw error("argument error");
      return *this;
    }
    TCL_args& operator>>(unsigned& x) {
      int tmp;
      if (Tcl_GetIntFromObj(interp(),pop_arg(),&tmp)!=TCL_OK) 
        throw error("argument error");
      if (tmp>=0) x=tmp;
      else throw error("assigning %d to an unsigned variable",tmp);
      return *this;
    }
    TCL_args& operator>>(long& x) {
      if (Tcl_GetLongFromObj(interp(),pop_arg(),&x)!=TCL_OK) 
        throw error("argument error");
{'fraction_non_alphanumeric': 0.12196633274811525, 'fraction_numerical': 0.009811008984818754, 'mean_word_length': 2.809598741148702, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4588485', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3281, 'n_words': 885}