This page describes how to implement a custom E\+B\+ML data type using the provided C\+R\+TP templates, along with the helper functions such as {\ttfamily \mbox{\hyperlink{namespaceebml_a44e26fccb32167dfcce2a1a5d2d7b557}{ebml\+::size()}}}, {\ttfamily \mbox{\hyperlink{namespaceebml_a136c352940138f9676eb143e5f199e5f}{ebml\+::pack()}}}, {\ttfamily \mbox{\hyperlink{namespaceebml_a69e04a7c1d5125458fe3183aa9411b3a}{ebml\+::unpack()}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_ab6da0c4788e0bcfb7bad0dbbe275f46c}{ebml\+::repr()}}}. These templates serve as the basis for the concrete type and element instances used in encoding and decoding E\+B\+ML data.\hypertarget{data_type_templates_data_type_overview}{}\section{Overview}\label{data_type_templates_data_type_overview}
The E\+B\+ML library employs the C\+R\+TP (Curiously Recurring Template Pattern) (\mbox{\hyperlink{crtp_usage}{The C\+R\+TP Design Pattern}}) to simplify the process of subclassing {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlElementType}{ebml\+::ebml\+Element\+Type}}} and {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlElement}{ebml\+::ebml\+Element}}}. In particular, the templates\+:


\begin{DoxyItemize}
\item {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$}\+: Inherits from a C\+R\+TP type base ({\ttfamily \mbox{\hyperlink{classebml_1_1ebmlTypeCRTP}{ebml\+::ebml\+Type\+C\+R\+TP}}}) and provides a factory interface and metadata for a given type {\ttfamily T}.
\begin{DoxyItemize}
\item The following members are available\+:
\begin{DoxyItemize}
\item {\ttfamily T \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$\+::defaultval} provides the default value for an {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataElement}{ebml\+::ebml\+Data\+Element}}$<$T$>$} element when none is provided, or an empty element is decoded.
\item {\ttfamily static T \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$\+::defaultdefault} provides a default value for {\ttfamily defaultval} in{\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$} element when none is provided.
\end{DoxyItemize}
\item {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataElement}{ebml\+::ebml\+Data\+Element}}$<$T$>$} instances can be created using these functions\+:
\begin{DoxyItemize}
\item {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$\+::operator()(const T\& data)} constructs an {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataElement}{ebml\+::ebml\+Data\+Element}}$<$T$>$} using the provided data.
\item {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$\+::operator()(\+T\&\& data)} constructs an {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataElement}{ebml\+::ebml\+Data\+Element}}$<$T$>$}, moving the provided data into the element.
\end{DoxyItemize}
\end{DoxyItemize}
\item {\ttfamily \mbox{\hyperlink{classebml_1_1ebmlDataElement}{ebml\+::ebml\+Data\+Element}}$<$T$>$}\+: Inherits from a C\+R\+TP element base ({\ttfamily \mbox{\hyperlink{classebml_1_1ebmlElementCRTP}{ebml\+::ebml\+Element\+C\+R\+TP}}}) and encapsulates the actual value of type {\ttfamily T} along with functions for encoding, decoding, cloning, and representation.
\begin{DoxyItemize}
\item Provides a public data member {\ttfamily T data} representing the payload.
\end{DoxyItemize}
\end{DoxyItemize}

Template instances for the following types are already provided\+:
\begin{DoxyItemize}
\item {\ttfamily unsigned long long} (as {\ttfamily \mbox{\hyperlink{group__type__implementations_gad0a323f4412eb95e1a9993f788222ca9}{ebml\+::ebml\+Unsigned\+Integer\+Type}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_a094d15ef60da5dd0e74a4e379ac3e547}{ebml\+::ebml\+Unsigned\+Integer}}})
\item {\ttfamily long long} (as {\ttfamily \mbox{\hyperlink{group__type__implementations_gaf8c32801fed10dc5af1101f6e198d217}{ebml\+::ebml\+Signed\+Integer\+Type}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_a5429f4885c9e4e9eeda85b245ea6166a}{ebml\+::ebml\+Signed\+Integer}}})
\item {\ttfamily double} (as {\ttfamily \mbox{\hyperlink{group__type__implementations_gae03320bc9dfc744ed3ce62a52e785b09}{ebml\+::ebml\+Float\+Type}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_af998fa3b620e1bb7363e1426c9910b9c}{ebml\+::ebml\+Float}}})
\item {\ttfamily std\+::string} (as {\ttfamily \mbox{\hyperlink{group__type__implementations_ga0fd1e99631f8e5caf2d32494618605a6}{ebml\+::ebml\+Binary\+Type}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_a9b2775bff9ce97bc09b955ca1de557ae}{ebml\+::ebml\+Binary}}})
\item {\ttfamily std\+::wstring} (as {\ttfamily \mbox{\hyperlink{group__type__implementations_gaf687038eed4b1b3a2e6ecbe30319c121}{ebml\+::ebml\+Unicode\+Type}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_abd9662588801df32dd1d2853a9f2f9b1}{ebml\+::ebml\+Unicode}}})
\item {\ttfamily \mbox{\hyperlink{namespaceebml_a7e667ec3fe8b51fb5b8f9690734d8638}{ebml\+::timepoint\+\_\+t}} = std\+::chrono\+::time\+\_\+point$<$std\+::chrono\+::system\+\_\+clock, std\+::chrono\+::nanoseconds$>$} (as {\ttfamily \mbox{\hyperlink{group__type__implementations_ga23ddacee2aa5e9960ec3611759c6212f}{ebml\+::ebml\+Date\+Time\+Type}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_a544c5c28e36511ffd20b5907e551754f}{ebml\+::ebml\+Date\+Time}}})
\end{DoxyItemize}

To create a custom data type instance for E\+B\+ML, you must provide implementations for the following template functions\+:


\begin{DoxyItemize}
\item {\ttfamily \mbox{\hyperlink{namespaceebml_a0385005834de08a2401ec491023f9ab3}{ebml\+::size(const T\&)}}} Calculates and returns the number of bytes required to encode a value of type {\ttfamily T}. A macro {\ttfamily \mbox{\hyperlink{struct_8h_abad27775fff46b825f9185ec87c903cd}{D\+E\+F\+\_\+\+S\+I\+Z\+E(\+T)}}} is provided to ensure a correct function signature.
\item {\ttfamily \mbox{\hyperlink{namespaceebml_a902c75e723a22780c779a3fc1531a07f}{ebml\+::pack(const T\&, char$\ast$ dest)}}} or {\ttfamily pack(const T\&, size\+\_\+t, char$\ast$ dest)} Encodes a value of type {\ttfamily T} into the provided destination buffer. The function returns the number of bytes written. Macros {\ttfamily \mbox{\hyperlink{struct_8h_aa95a7202373d55d4529a3a401237865e}{D\+E\+F\+\_\+\+P\+A\+C\+K(\+T)}}} and {\ttfamily \mbox{\hyperlink{struct_8h_a81273eaca61e4a36033f245d59720499}{D\+E\+F\+\_\+\+P\+A\+C\+K\+\_\+\+A\+L\+T(\+T)}}} are provided to ensure correct function signatures.
\item {\ttfamily \mbox{\hyperlink{namespaceebml_a69e04a7c1d5125458fe3183aa9411b3a}{ebml\+::unpack}}$<$T$>$(const char$\ast$, size\+\_\+t)} Decodes the raw byte buffer back into a value of type {\ttfamily T}. A macro {\ttfamily \mbox{\hyperlink{struct_8h_a3f9a8b15c2401ddcf3da0afcd5bf0703}{D\+E\+F\+\_\+\+U\+N\+P\+A\+C\+K(\+T)}}} is provided to ensure a correct function signature.
\item {\ttfamily \mbox{\hyperlink{namespaceebml_a3c5e99350812f2a7182e2de8f1e46b3b}{ebml\+::repr(const T\&)}}} Returns a string representation of the {\ttfamily T} value.
\end{DoxyItemize}

These functions are typically defined in the {\ttfamily \mbox{\hyperlink{struct_8h}{struct.\+h}}} and {\ttfamily \mbox{\hyperlink{repr_8h}{repr.\+h}}} and implemented in {\ttfamily struct.\+tpp} and {\ttfamily repr.\+tpp}.

\begin{DoxyNote}{Note}
When implementing specializations for {\ttfamily \mbox{\hyperlink{namespaceebml_a44e26fccb32167dfcce2a1a5d2d7b557}{ebml\+::size()}}}, {\ttfamily \mbox{\hyperlink{namespaceebml_a136c352940138f9676eb143e5f199e5f}{ebml\+::pack}}} and {\ttfamily \mbox{\hyperlink{namespaceebml_a69e04a7c1d5125458fe3183aa9411b3a}{ebml\+::unpack}}}, it is important that these specializations are declared {\bfseries before} including {\ttfamily struct.\+tpp}.
\end{DoxyNote}
\hypertarget{data_type_templates_data_impl}{}\section{Implementing a Custom E\+B\+M\+L Data Type}\label{data_type_templates_data_impl}
To implement a custom E\+B\+ML data type using these templates, follow these steps\+:


\begin{DoxyEnumerate}
\item {\bfseries Implement Template Functions} ~\newline
 Provide implementations (or overloads) for the functions\+:
\begin{DoxyItemize}
\item {\ttfamily size\+\_\+t \mbox{\hyperlink{namespaceebml_a0385005834de08a2401ec491023f9ab3}{ebml\+::size(const T\& value)}}}\+: Should return the number of bytes needed for the encoded representation of {\ttfamily value}. A macro {\ttfamily \mbox{\hyperlink{struct_8h_abad27775fff46b825f9185ec87c903cd}{D\+E\+F\+\_\+\+S\+I\+Z\+E(\+T)}}} is provided to ensure a correct function signature.
\item Pack functions\+:
\begin{DoxyItemize}
\item {\ttfamily size\+\_\+t \mbox{\hyperlink{namespaceebml_a902c75e723a22780c779a3fc1531a07f}{ebml\+::pack(const T\& value, char$\ast$ dest)}}} {\itshape or}
\item {\ttfamily size\+\_\+t \mbox{\hyperlink{namespaceebml_a2ca9fdcd493e677f02886593a4ae63e0}{ebml\+::pack(const T\& value, size\+\_\+t size, char$\ast$ dest)}}}
\end{DoxyItemize}

Should write the encoded bytes of {\ttfamily value} into the {\ttfamily dest} buffer, returning the number of bytes written into {\ttfamily dest}. Macros {\ttfamily \mbox{\hyperlink{struct_8h_aa95a7202373d55d4529a3a401237865e}{D\+E\+F\+\_\+\+P\+A\+C\+K(\+T)}}} and {\ttfamily \mbox{\hyperlink{struct_8h_a81273eaca61e4a36033f245d59720499}{D\+E\+F\+\_\+\+P\+A\+C\+K\+\_\+\+A\+L\+T(\+T)}}} are provided to ensure correct function signatures.
\item {\ttfamily T \mbox{\hyperlink{namespaceebml_a69e04a7c1d5125458fe3183aa9411b3a}{ebml\+::unpack}}$<$T$>$(const char$\ast$ src, size\+\_\+t size)}\+: Should read raw data from {\ttfamily src} and return the decoded value of type {\ttfamily T}. A macro {\ttfamily \mbox{\hyperlink{struct_8h_a3f9a8b15c2401ddcf3da0afcd5bf0703}{D\+E\+F\+\_\+\+U\+N\+P\+A\+C\+K(\+T)}}} is provided to ensure a correct function signature.
\item {\ttfamily std\+::wstring \mbox{\hyperlink{namespaceebml_a3c5e99350812f2a7182e2de8f1e46b3b}{ebml\+::repr(const T\&)}}}\+: Returns a wide string representation of the {\ttfamily T} value.
\end{DoxyItemize}

For example, the implementation of these functions for {\ttfamily T = long long} is provided in the file {\ttfamily \mbox{\hyperlink{ll_8h}{ll.\+h}}}.
\item {\bfseries Define a default value.} Provide a value for {\ttfamily T \mbox{\hyperlink{classebml_1_1ebmlDataType}{ebml\+::ebml\+Data\+Type}}$<$T$>$\+::defaultdefault}. This can be done using the {\ttfamily \mbox{\hyperlink{ebmlDataElement_8h_aa053f14e6e61bc53a55fc966a76a8909}{I\+N\+S\+T\+\_\+\+E\+B\+M\+L\+\_\+\+C\+L\+S(\+T, defaultval)}}} macro.
\item {\bfseries Define the Data Type and Data Element Subclasses} ~\newline
 Use the provided C\+R\+TP templates to create your custom data type\+:
\begin{DoxyItemize}
\item Define your data type by instantiating\+:

\`{}\`{}\`{}c++ typedef ebml\+::ebml\+Data\+Type$<$long long$>$ ebml\+Signed\+Integer\+Type; \`{}\`{}\`{}
\item Define the corresponding element instance type\+:

\`{}\`{}\`{}c++ typedef ebml\+::ebml\+Data\+Element$<$long long$>$ ebml\+Signed\+Integer; \`{}\`{}\`{}
\end{DoxyItemize}
\end{DoxyEnumerate}
\begin{DoxyEnumerate}
\item {\bfseries Instantiate and Use Your Custom Type} ~\newline
 Once your functions are in place and the template classes are specialized (you can use macros like {\ttfamily \mbox{\hyperlink{ebmlDataElement_8h_a1a86980ed59e146fae938434625cd079}{D\+E\+C\+L\+\_\+\+D\+A\+T\+A\+\_\+\+C\+L\+S(\+T)}}} and {\ttfamily \mbox{\hyperlink{ebmlDataElement_8h_aa053f14e6e61bc53a55fc966a76a8909}{I\+N\+S\+T\+\_\+\+E\+B\+M\+L\+\_\+\+C\+L\+S(\+T, defaultval)}}} to assist with declarations and instantiations), you can create and manipulate E\+B\+ML elements of your custom type.

{\bfseries Example\+:}
\end{DoxyEnumerate}


\begin{DoxyCode}
\{c++\}
   // Create an instance of the EBML type for signed integers.
   ebmlSignedIntegerType myIntegerType(0x01, L"TestSignedInteger", -42);

   // Use the type as a factory to create an element instance.
   auto element = myIntegerType(-100);

   // Encode the element into a raw data string.
   std::string encoded = element->encode();

   // Decode the raw data back into an element instance.
   auto decodedElement = myIntegerType.decode(encoded);

   // Retrieve the long long value from the element.
   long long value = ebml::data<long long>(decodedElement);
\end{DoxyCode}
\hypertarget{data_type_templates_data_type_summary}{}\section{Summary}\label{data_type_templates_data_type_summary}
The approach using C\+R\+TP templates in {\ttfamily ebml\+Data\+Type$<$T$>$} and {\ttfamily ebml\+Data\+Element$<$T$>$} allows you to consistently implement new E\+B\+ML data types. By implementing the helper functions ({\ttfamily \mbox{\hyperlink{namespaceebml_a44e26fccb32167dfcce2a1a5d2d7b557}{size()}}}, {\ttfamily \mbox{\hyperlink{namespaceebml_a136c352940138f9676eb143e5f199e5f}{pack()}}}, and {\ttfamily \mbox{\hyperlink{namespaceebml_a69e04a7c1d5125458fe3183aa9411b3a}{unpack()}}}) for your particular data type, you integrate with the E\+B\+ML framework, which then provides you with standardized methods for element creation, encoding/decoding, cloning, and representation.

This design enables a uniform, type-\/safe interface for working with E\+B\+ML documents and simplifies the development of new E\+B\+ML data types. / 