<commit_before>/*
The MIT License

Copyright (c) 2011 by <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

#ifndef __appares_model_h
#define __appares_model_h

#include <csutil/hash.h>
#include <csutil/stringarray.h>

#include <wx/event.h>

class wxWindow;
class wxTextCtrl;
class wxCheckBox;
class wxListCtrl;
class wxStaticText;
class wxPanel;

namespace Ares
{

class Value;
class BufferValueChangeListener;
class ViewValueChangeListener;
class SelectedValueChangeListener;

/**
 * Listen to changes in a value.
 */
struct ValueChangeListener : public csRefCount
{
  /**
   * Called if the value changes.
   */
  virtual void ValueChanged (Value* value) = 0;
};

/**
 * All possible value types.
 */
enum ValueType
{
  /// No type.
  VALUE_NONE = 0,

  /// The basic types.
  VALUE_STRING,
  VALUE_LONG,
  VALUE_BOOL,
  VALUE_FLOAT,

  /**
   * A collection of values. In this situation the
   * children have no specific name and can only
   * be accessed by iterating over them.
   */
  VALUE_COLLECTION,

  /**
   * A collection of values. In this situation the
   * children can also be accessed by name and
   * it is not possible to delete or add children.
   */
  VALUE_COMPOSITE,
};


/**
 * An interface representing a domain value.
 */
class Value : public csRefCount
{
protected:
  csRefArray<ValueChangeListener> listeners;

  void FireValueChanged ()
  {
    for (size_t i = 0 ; i < listeners.GetSize () ; i++)
      listeners[i]->ValueChanged (this);
  }

public:
  /**
   * Get the type of the value.
   */
  virtual ValueType GetType () const { return VALUE_STRING; }

  /**
   * Get the value if the type is VALUE_STRING.
   * Returns 0 if the value does not represent a string.
   */
  virtual const char* GetStringValue () { return 0; }

  /**
   * Set the value as a string. Does nothing if the value does
   * not represent a string.
   */
  virtual void SetStringValue (const char* str) { }

  /**
   * Get the value if the type is VALUE_LONG.
   * Returns 0 if the value does not represent a long.
   */
  virtual long GetLongValue () { return 0; }

  /**
   * Set the value as a long. Does nothing if the value does
   * not represent a long.
   */
  virtual void SetLongValue (long v) { }

  /**
   * Get the value if the type is VALUE_BOOL.
   * Returns false if the value does not represent a bool.
   */
  virtual bool GetBoolValue () { return false; }

  /**
   * Set the value as a bool. Does nothing if the value does
   * not represent a bool.
   */
  virtual void SetBoolValue (bool v) { }

  /**
   * Get the value if the type is VALUE_FLOAT.
   * Returns 0.0f if the value does not represent a float.
   */
  virtual float GetFloatValue () { return 0.0f; }

  /**
   * Set the value as a float. Does nothing if the value does
   * not represent a float.
   */
  virtual void SetFloatValue (float v) { }

  /**
   * Add a listener for value changes. Note that a value does not
   * have to support this.
   */
  virtual void AddValueChangeListener (ValueChangeListener* listener)
  {
    listeners.Push (listener);
  }
  
  /**
   * Remove a listener.
   */
  virtual void RemoveValueChangeListener (ValueChangeListener* listener)
  {
    listeners.Delete (listener);
  }

  // -----------------------------------------------------------------------------

  /**
   * If the type of this value is VALUE_COLLECTION or VALUE_COMPOSITE,
   * then you can get the children using ResetIterator(), HasNext(),
   * and NextChild().
   */
  virtual void ResetIterator () { }

  /**
   * Check if there are still children to process.
   */
  virtual bool HasNext () { return false; }

  /**
   * Get the next child. If the optional 'name' parameter
   * is given then it will be filled with the name of the component (if
   * it has a name). Only components of type VALUE_COMPOSITE support names
   * for their children.
   */
  virtual Value* NextChild (csString* name = 0) { return 0; }

  /**
   * If the type of this value is VALUE_COMPOSITE or VALUE_COLLECTION then you
   * can sometimes get a child by index here.
   */
  virtual Value* GetChild (size_t idx) { return 0; }

  /**
   * If the type of this value is VALUE_COMPOSITE then you can get
   * a child by name here.
   */
  virtual Value* GetChild (const char* name) { return 0; }

  // -----------------------------------------------------------------------------

  /**
   * Delete a child value from this value. Returns false if this value could not
   * be deleted for some reason. This only works for VALUE_COLLECTION.
   */
  virtual bool DeleteValue (Value* child) { return false; }

  /**
   * Add a child value to this value. Returns false if this value could
   * not be added for some reason. This only works for VALUE_COLLECTION.
   */
  virtual bool AddValue (Value* child) { return false; }

  /**
   * Update a child. Returns false if this child could not be updated.
   * The default implementation just calls DeleteChild() first and then
   * AddChild() with the new child. This only works for VALUE_COLLECTION.
   */
  virtual bool UpdateValue (Value* oldChild, Value* child)
  {
    if (!DeleteValue (oldChild)) return false;
    if (!AddValue (child))
    {
      AddValue (oldChild);
      return false;
    }
    return true;
  }
};

/**
 * A constant null value.
 */
class NullValue : public Value
{
public:
  virtual ValueType GetType () const { return VALUE_NONE; }
};

/**
 * An abstract composite value which supports iteration and
 * selection of the children by name. This is an abstract
 * class that has to be subclassed in order to provide the
 * name and value of every child by index.
 * Subclasses must also override the standard Value::GetChild(idx).
 */
class AbstractCompositeValue : public Value
{
private:
  size_t idx;

protected:
  /// Override this function to indicate the number of children.
  virtual size_t GetChildCount () = 0;
  /// Override this function to return the right name for a given child.
  virtual const char* GetName (size_t idx) = 0;

public:
  AbstractCompositeValue () { }
  virtual ~AbstractCompositeValue () { }

  virtual ValueType GetType () const { return VALUE_COMPOSITE; }

  virtual void ResetIterator () { idx = 0; }
  virtual bool HasNext () { return idx < GetChildCount (); }
  virtual Value* NextChild (csString* name = 0)
  {
    if (name) *name = GetName (idx);
    idx++;
    return GetChild (idx-1);
  }
  // This function is only here because otherwise the compiler complains
  // in the usage of GetChild (idx-1).
  virtual Value* GetChild (size_t idx)
  {
    return Value::GetChild (idx);<commit_msg>Using 'using' for a proper overloading/inheritance solution.<commit_after>/*
The MIT License

Copyright (c) 2011 by <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

#ifndef __appares_model_h
#define __appares_model_h

#include <csutil/hash.h>
#include <csutil/stringarray.h>

#include <wx/event.h>

class wxWindow;
class wxTextCtrl;
class wxCheckBox;
class wxListCtrl;
class wxStaticText;
class wxPanel;

namespace Ares
{

class Value;
class BufferValueChangeListener;
class ViewValueChangeListener;
class SelectedValueChangeListener;

/**
 * Listen to changes in a value.
 */
struct ValueChangeListener : public csRefCount
{
  /**
   * Called if the value changes.
   */
  virtual void ValueChanged (Value* value) = 0;
};

/**
 * All possible value types.
 */
enum ValueType
{
  /// No type.
  VALUE_NONE = 0,

  /// The basic types.
  VALUE_STRING,
  VALUE_LONG,
  VALUE_BOOL,
  VALUE_FLOAT,

  /**
   * A collection of values. In this situation the
   * children have no specific name and can only
   * be accessed by iterating over them.
   */
  VALUE_COLLECTION,

  /**
   * A collection of values. In this situation the
   * children can also be accessed by name and
   * it is not possible to delete or add children.
   */
  VALUE_COMPOSITE,
};


/**
 * An interface representing a domain value.
 */
class Value : public csRefCount
{
protected:
  csRefArray<ValueChangeListener> listeners;

  void FireValueChanged ()
  {
    for (size_t i = 0 ; i < listeners.GetSize () ; i++)
      listeners[i]->ValueChanged (this);
  }

public:
  /**
   * Get the type of the value.
   */
  virtual ValueType GetType () const { return VALUE_STRING; }

  /**
   * Get the value if the type is VALUE_STRING.
   * Returns 0 if the value does not represent a string.
   */
  virtual const char* GetStringValue () { return 0; }

  /**
   * Set the value as a string. Does nothing if the value does
   * not represent a string.
   */
  virtual void SetStringValue (const char* str) { }

  /**
   * Get the value if the type is VALUE_LONG.
   * Returns 0 if the value does not represent a long.
   */
  virtual long GetLongValue () { return 0; }

  /**
   * Set the value as a long. Does nothing if the value does
   * not represent a long.
   */
  virtual void SetLongValue (long v) { }

  /**
   * Get the value if the type is VALUE_BOOL.
   * Returns false if the value does not represent a bool.
   */
  virtual bool GetBoolValue () { return false; }

  /**
   * Set the value as a bool. Does nothing if the value does
   * not represent a bool.
   */
  virtual void SetBoolValue (bool v) { }

  /**
   * Get the value if the type is VALUE_FLOAT.
   * Returns 0.0f if the value does not represent a float.
   */
  virtual float GetFloatValue () { return 0.0f; }

  /**
   * Set the value as a float. Does nothing if the value does
   * not represent a float.
   */
  virtual void SetFloatValue (float v) { }

  /**
   * Add a listener for value changes. Note that a value does not
   * have to support this.
   */
  virtual void AddValueChangeListener (ValueChangeListener* listener)
  {
    listeners.Push (listener);
  }
  
  /**
   * Remove a listener.
   */
  virtual void RemoveValueChangeListener (ValueChangeListener* listener)
  {
    listeners.Delete (listener);
  }

  // -----------------------------------------------------------------------------

  /**
   * If the type of this value is VALUE_COLLECTION or VALUE_COMPOSITE,
   * then you can get the children using ResetIterator(), HasNext(),
   * and NextChild().
   */
  virtual void ResetIterator () { }

  /**
   * Check if there are still children to process.
   */
  virtual bool HasNext () { return false; }

  /**
   * Get the next child. If the optional 'name' parameter
   * is given then it will be filled with the name of the component (if
   * it has a name). Only components of type VALUE_COMPOSITE support names
   * for their children.
   */
  virtual Value* NextChild (csString* name = 0) { return 0; }

  /**
   * If the type of this value is VALUE_COMPOSITE or VALUE_COLLECTION then you
   * can sometimes get a child by index here.
   */
  virtual Value* GetChild (size_t idx) { return 0; }

  /**
   * If the type of this value is VALUE_COMPOSITE then you can get
   * a child by name here.
   */
  virtual Value* GetChild (const char* name) { return 0; }

  // -----------------------------------------------------------------------------

  /**
   * Delete a child value from this value. Returns false if this value could not
   * be deleted for some reason. This only works for VALUE_COLLECTION.
   */
  virtual bool DeleteValue (Value* child) { return false; }

  /**
   * Add a child value to this value. Returns false if this value could
   * not be added for some reason. This only works for VALUE_COLLECTION.
   */
  virtual bool AddValue (Value* child) { return false; }

  /**
   * Update a child. Returns false if this child could not be updated.
   * The default implementation just calls DeleteChild() first and then
   * AddChild() with the new child. This only works for VALUE_COLLECTION.
   */
  virtual bool UpdateValue (Value* oldChild, Value* child)
  {
    if (!DeleteValue (oldChild)) return false;
    if (!AddValue (child))
    {
      AddValue (oldChild);
      return false;
    }
    return true;
  }
};

/**
 * A constant null value.
 */
class NullValue : public Value
{
public:
  virtual ValueType GetType () const { return VALUE_NONE; }
};

/**
 * An abstract composite value which supports iteration and
 * selection of the children by name. This is an abstract
 * class that has to be subclassed in order to provide the
 * name and value of every child by index.
 * Subclasses must also override the standard Value::GetChild(idx).
 */
class AbstractCompositeValue : public Value
{
private:
  size_t idx;

  using Value::GetChild;

protected:
  /// Override this function to indicate the number of children.
  virtual size_t GetChildCount () = 0;
  /// Override this function to return the right name for a given child.
  virtual const char* GetName (size_t idx) = 0;

public:
  AbstractCompositeValue () { }
  virtual ~AbstractCompositeValue () { }

  virtual ValueType GetType () const { return VALUE_COMPOSITE; }

  virtual void ResetIterator () { idx = 0; }
  virtual bool HasNext () { return idx < GetChildCount (); }
  virtual Value* NextChild (csString* name = 0)
  {
    if (name) *name = GetName (idx);
    idx++;
    return GetChild (idx-1);
{'fraction_non_alphanumeric': 0.1029450959488273, 'fraction_numerical': 0.0032649253731343282, 'mean_word_length': 2.8603395061728394, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5417289', 'n_tokens_mistral': 4339, 'n_tokens_neox': 4200, 'n_words': 1873}