﻿<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="thpdoc.xsl"?>
<page id="class" suffix=" Keyword">
  <subsection>Defines a class.<code>class ClassName
{
	Class_contents;
}</code>
Classes can contain the following primitives: <kw>typedef</kw>, <kw>class</kw>, <kw>const</kw>, <kw>enum</kw> and <link>functions</link>
  <p>THDL++ classes are similar to VHDL packages. They do not describe objects created and maintained during runtime. Instead they allow combining definitions of types, constants and algorithms inside a single named primitive. Entities templates and functions can accept classes as template arguments. Use the <kw>any</kw> keyword for declaring such arguments. See examples for more details.</p>
	</subsection>
  <section name="Remarks">
	<p>THDL++ allows inheriting classes. Inheritance syntax is similar to C++/C#. Base class contents can be accessed from a child class using the <kw>base</kw> keyword.</p>
	<p>Note that THDL++ classes do not support private/protected/public modifiers. All class contents are always public.</p>
	<p>You can define compile-time lists of classes and use them in generic statements (e.g. <kw>generate</kw> <kw>foreach</kw>). See <link>lists</link> for more details.</p>
	<p>Classes can be declared in the global scope, inside namespaces, other classes and entities.</p>
  </section>
	<examples>
	<example name="Generic minimum finder">
	This example sketches a generic minimum finder. The data type (e.g. int, natural or logic vector) and the comparison function (e.g. "less than") are abstracted from the entity definition. This allows reusing the entity code for any possible combination of data type and function by defining a corresponding class:
		<code>class NormalMin
{
	typedef logic[8] DataType;
	
	bool CompareFunction(DataType a, DataType b)
	{
		return a lt b;
	}
}

template&lt;any _TypeInfo, int _InputCount&gt; entity MinFinder
{
	port in _TypeInfo.DataType[_InputCount] Inputs;
	port out _TypeInfo.DataType Output;
	
	process Compute (any)
	{
		for(...)
		{
			if (_TypeInfo.CompareFunction(x, cur_minimum))
				cur_minimum = x;
		}
	}
}</code>
	When you instantiate a templated entity, specify the template arguments. E.g.:
<code>MinFinder&lt;NormalMin, 4&gt; finder(...);</code>
	This will instantiate a minimum finder with 4 inputs based on the NormalMin definition.
		</example>
		<example name="Inheritance">
	You can easily inherit classes. Let's modify the previous example so that the comparison treats 0x55 as a special "super minimum" value:
		<code>class SuperMin : NormalMin
{
	const int SuperMinValue = 0x55;

	bool CompareFunction(DataType a, DataType b)
	{
		if (a == SuperMinValue)
			return true;
		else if (b == SuperMinValue)
			return false;
		else
			return base.CompareFunction(a, b);
	}
}</code>
The instantiation should be changed accordingly:
<code>MinFinder&lt;SuperMin, 4&gt; finder(...);</code>
Note that the DataType definition was inherited from NormalMin and did not have to be redefined.
		</example>
	</examples>
  <section name="Code generation">
	<p>THDL++ compiler will convert functions defined in the classes to VHDL functions. Every function name will be prefixed by the fully qualified class name. If a class or a function is templated, one instance of VHDL function will be created for every combination of template arguments that is used in the code. See <link>functions</link> page for more details.</p>
  </section>
  <seealso id="entity"/>
  <seealso id="template"/>
  <seealso id="base"/>
  <seealso id="functions"/>
</page>