Skip to content

Commit

Permalink
was fighting c++ but i emerge victorious!
Browse files Browse the repository at this point in the history
  • Loading branch information
jyothiraditya-n committed Aug 15, 2023
1 parent 76b9954 commit 8b3d73f
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 769 deletions.
5 changes: 4 additions & 1 deletion clang_tidy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ ccflags="$(cat ".config/ccflags_debug.conf")"
for i in "$@"; do
case "$i" in
*.c) (set -x; clang-tidy "$i" -- $cflags);;
*.cpp) (set -x; clang-tidy "$i" -- $cflags);;
*.cpp) (set -x; clang-tidy "$i" -- $ccflags);;

*.h) (set -x; clang-tidy "$i" -- $cflags);;
*.hpp) (set -x; clang-tidy "$i" -- $ccflags);;
esac
done
44 changes: 18 additions & 26 deletions docs/LC_macros.h.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ \section{Annotated Version of the \mintinline{bash}{LC_macros.h} Headerfile.}
You can use these macros in your program by including the \mintinline{bash}{LC_macros.h} header file.\footnotemark This section is an annotated version of this header file. For all of these macros, \monoc{lflag} needs to be a string constant, \monoc{sflag} needs to be a char constant.

\footnotetext{
Needs libClame Version 1.1 or better.
The first in each series of macros require libClame Version 1.1 or better, while the remainedr of the macro overloads with flag setting require libClame Version 1.2 or better.
}

\subsection{Flag to Call a Function}
Expand All @@ -27,49 +27,41 @@ \subsection{Flag to Call a Function}
\footnotetext{See Section \ref{sec:callback-function}.}

\begin{minted}{c}
#define LC_MAKE_CALL(lflag, sflag, func) \
{lflag, sflag, func, NULL, 0, 0, NULL, NULL, 0, 0, 0, 0}
#define LC_MAKE_CALL(lflag, sflag, func) ...
\end{minted}

\subsection{Flag to Set a Boolean to a Given Value}
This macro lets you make a flag to set a boolean to a given value. \monoc{var} needs to be of declared as type \monoc{bool} and \monoc{val} needs to be a boolean constant.

\begin{minted}{c}
#define LC_MAKE_BOOL(lflag, sflag, var, val) \
{lflag, sflag, NULL, &var, LC_BOOL_VAR, val, NULL, NULL, 0, 0, 0, \
false}
#define LC_MAKE_BOOL(lflag, sflag, var, val) ...
#define LC_MAKE_BOOL_F(lflag, sflag, var, val, flag) ...
\end{minted}

\subsection{Flags to Get Config String(s)}
This macro lets you make a flag to set strings. \monoc{var} needs to be declared as type \monoc{char *}, \monoc{arr} needs to be declared as type \monoc{char **}, len needs to be declared as type \monoc{size_t} and \monoc{min_len} and \monoc{max_len} need to be unsigned integer constants.
This macro lets you make a flag to set strings. \monoc{var} needs to be declared as type \monoc{char *}, \monoc{arr} needs to be declared as type \monoc{char **}, len needs to be declared as type \monoc{size_t} and \monoc{min(_len)} and \monoc{max(_len)} need to be unsigned integer constants.

\begin{minted}{c}
#define LC_MAKE_STRING(lflag, sflag, var) \
{lflag, sflag, NULL, &var, LC_STRING_VAR, 0, NULL, NULL, 0, 0, 0, \
false}
#define LC_MAKE_STRING(lflag, sflag, var) ...
#define LC_MAKE_STRING_F(lflag, sflag, var, func) ...

#define LC_MAKE_STRING_ARR(lflag, sflag, arr, len) \
{lflag, sflag, NULL, &arr, LC_STRING_VAR, 0, NULL, &len, 0, 0, \
SIZE_MAX, false}
#define LC_MAKE_STRING_ARR(lflag, sflag, arr, len) ...
#define LC_MAKE_STRING_ARR_F(lflag, sflag, arr, len) ...

#define LC_MAKE_STRING_ARR_BOUNDED(lflag, sflag, arr, len, min_len, max_len) \
{lflag, sflag, NULL, &arr, LC_STRING_VAR, 0, NULL, &len, 0, min_len, \
max_len, false}
#define LC_MAKE_STRING_ARR_BOUNDED(lflag, sflag, arr, len, min_len, max_len) ...
#define LC_MAKE_STRING_ARR_BOUNDED_F(lflag, sflag, arr, len, min, max, func) ...
\end{minted}

\subsection{Flags to Get a Variable or an Array of Other Types}
This macro lets you make a flag to set any variable or array. For this paragraph, let us denote the type of the data you are treating as \monoc{T}. \monoc{var} needs to be declared as type \monoc{T}, \monoc{arr} needs to be declared as type \monoc{T *}, \monoc{fmt} needs to be a string constant, len needs to be declared as type \monoc{size_t} and \monoc{min_len} and \monoc{max_len} need to be unsigned integer constants.
This macro lets you make a flag to set any variable or array. For this paragraph, let us denote the type of the data you are treating as \monoc{T}. \monoc{var} needs to be declared as type \monoc{T}, \monoc{arr} needs to be declared as type \monoc{T *}, \monoc{fmt} needs to be a string constant, len needs to be declared as type \monoc{size_t} and \monoc{min(_len)} and \monoc{max(_len)} need to be unsigned integer constants.

\begin{minted}{c}
#define LC_MAKE_VAR(lflag, sflag, var, fmt) \
{lflag, sflag, NULL, &var, LC_OTHER_VAR, 0, fmt, NULL, sizeof(var), \
0, SIZE_MAX, false}
#define LC_MAKE_VAR(lflag, sflag, var, fmt) ...
#define LC_MAKE_VAR_F(lflag, sflag, var, fmt, func) ...

#define LC_MAKE_ARR(lflag, sflag, arr, fmt, len) \
{lflag, sflag, NULL, &arr, LC_OTHER_VAR, 0, fmt, &len, sizeof(*arr), \
0, SIZE_MAX, false}
#define LC_MAKE_ARR(lflag, sflag, arr, fmt, len) ...
#define LC_MAKE_ARR_F(lflag, sflag, arr, fmt, len, func) ...

#define LC_MAKE_ARR_BOUNDED(lflag, sflag, arr, fmt, len, min_len, max_len) \
{lflag, sflag, NULL, &arr, LC_OTHER_VAR, 0, fmt, &len, sizeof(*arr), \
min_len, max_len, false}
#define LC_MAKE_ARR_BOUNDED(lflag, sflag, arr, fmt, len, min_len, max_len) ...
#define LC_MAKE_ARR_BOUNDED(lflag, sflag, arr, fmt, len, min, max, func) ...
\end{minted}
28 changes: 28 additions & 0 deletions inc/LC_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,59 @@
{lflag, sflag, NULL, &var, LC_BOOL_VAR, val, NULL, NULL, 0, 0, 0, \
false}

#define LC_MAKE_BOOL_F(lflag, sflag, var, val, func) \
{lflag, sflag, func, &var, LC_BOOL_VAR, val, NULL, NULL, 0, 0, 0, \
false}

/* Flags to get config string(s). */
#define LC_MAKE_STRING(lflag, sflag, var) \
{lflag, sflag, NULL, &var, LC_STRING_VAR, 0, NULL, NULL, 0, 0, 0, \
false}

#define LC_MAKE_STRING_F(lflag, sflag, var, func) \
{lflag, sflag, func, &var, LC_STRING_VAR, 0, NULL, NULL, 0, 0, 0, \
false}

#define LC_MAKE_STRING_ARR(lflag, sflag, arr, len) \
{lflag, sflag, NULL, &arr, LC_STRING_VAR, 0, NULL, &len, 0, 0, \
SIZE_MAX, false}

#define LC_MAKE_STRING_ARR_F(lflag, sflag, arr, len, func) \
{lflag, sflag, func, &arr, LC_STRING_VAR, 0, NULL, &len, 0, 0, \
SIZE_MAX, false}

#define LC_MAKE_STRING_ARR_BOUNDED(lflag, sflag, arr, len, min_len, max_len) \
{lflag, sflag, NULL, &arr, LC_STRING_VAR, 0, NULL, &len, 0, min_len, \
max_len, false}

#define LC_MAKE_STRING_ARR_BOUNDED_F(lflag, sflag, arr, len, min, max, func) \
{lflag, sflag, func, &arr, LC_STRING_VAR, 0, NULL, &len, 0, min, \
max, false}

/* Flags to get a variable or an array of other types. */
#define LC_MAKE_VAR(lflag, sflag, var, fmt) \
{lflag, sflag, NULL, &var, LC_OTHER_VAR, 0, fmt, NULL, sizeof(var), \
0, SIZE_MAX, false}

#define LC_MAKE_VAR_F(lflag, sflag, var, fmt, func) \
{lflag, sflag, NULL, &var, LC_OTHER_VAR, 0, fmt, NULL, sizeof(var), \
0, SIZE_MAX, false}

#define LC_MAKE_ARR(lflag, sflag, arr, fmt, len) \
{lflag, sflag, NULL, &arr, LC_OTHER_VAR, 0, fmt, &len, sizeof(*arr), \
0, SIZE_MAX, false}

#define LC_MAKE_ARR_F(lflag, sflag, arr, fmt, len, func) \
{lflag, sflag, func, &arr, LC_OTHER_VAR, 0, fmt, &len, sizeof(*arr), \
0, SIZE_MAX, false}

#define LC_MAKE_ARR_BOUNDED(lflag, sflag, arr, fmt, len, min_len, max_len) \
{lflag, sflag, NULL, &arr, LC_OTHER_VAR, 0, fmt, &len, sizeof(*arr), \
min_len, max_len, false}

#define LC_MAKE_ARR_BOUNDED_F(lflag, sflag, arr, fmt, len, min, max, func) \
{lflag, sflag, func, &arr, LC_OTHER_VAR, 0, fmt, &len, sizeof(*arr), \
min, max, false}

/* End Header Guard */
#endif
11 changes: 7 additions & 4 deletions inc/libClame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ namespace libClame {
/* Program name set via argv[0]. */
extern std::string prog_name;

/* Tables for C/C++ interop. */
/* Tables for C/C++ interop and C++ lambda cache tables. */
extern std::list<std::string> __string_list;
extern std::unordered_map<std::string, callback_t> __call_table;
extern std::list<std::string> __string_table;
extern std::list<char*> __c_string_table;
extern std::list<std::tuple<char**,size_t>> __c_strarr_table;
extern std::unordered_map<std::string, callback_t> __shadow_table;
extern std::unordered_map<std::string, char*> __c_string_table;

extern std::unordered_map<std::string, std::tuple<char**, size_t>>
__c_strarr_table;

/* Function call interceptor. */
extern int __interceptor(LC_flag_t* __c_flag);
Expand Down
Binary file added inc/libClame/.templates.hpp.kate-swp
Binary file not shown.
65 changes: 40 additions & 25 deletions inc/libClame/__templates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* this program. If not, see <https://www.gnu.org/licenses/>. */

/* Included by <libClame/templates.hpp> in the libClame namespace. */
#ifndef LC_TEMPLATES_HPP
#include <libClame/templates.hpp>
#else

/* Helper for getting scanf codes. */
template<typename T>
Expand All @@ -38,29 +41,42 @@ std::string __get_fmt() {

/* Tables for C/C++ interop. */
template<typename T>
std::list<std::tuple<T*,size_t>> __c_arr_table;
std::unordered_map<std::string, std::tuple<T*, size_t>> __c_arr_table;

/* Don't write the same code twice. */
template<template<typename> typename T1, typename T2>
static LC_flag_t __make_arr(
std::string lflag, char sflag, T1<T2>& arr,
std::optional<libClame::limits_t> limits,
std::optional<libClame::callback_t> function,
std::optional<std::string> sscanf_fmt
std::string& lflag, char sflag, T1<T2>* arr_ptr,
std::optional<libClame::limits_t>& limits,
std::optional<libClame::callback_t>& function,
std::optional<std::string>& sscanf_fmt
){
/* Pointer to malloc()'d C arr we'll manage. */
libClame::__c_arr_table<T2>.push_back({NULL, 0});
auto& c_arr_entry = *libClame::__c_arr_table<T2>.rbegin();
/* Make a copy of the flag that won't get mutated. */
libClame::__string_list.push_back(std::move(lflag));
const auto c_lflag = (*libClame::__string_list.rbegin()).c_str();

/* Pointer to malloc()'d C arr we'll manage. */
auto& c_arr_entry = libClame::__c_arr_table<T2>[c_lflag] = {NULL, 0};
auto& c_arr = std::get<0>(c_arr_entry);
auto& c_arr_len = std::get<1>(c_arr_entry);

/* Make a copy of the flag that won't get mutated. */
libClame::__string_table.push_back(std::move(lflag));
const auto c_lflag = (*libClame::__string_table.rbegin()).c_str();
/* Add the function to our shadow table. Empty lambda if nothing was
* provided in the optional. */
libClame::__shadow_table[c_lflag] = function.value_or([](){});;

/* Add our lambda function */
libClame::__call_table[c_lflag] = [c_lflag, arr_ptr](){
/* Dereference the pointer to get a C++ reference. */
auto& arr = *arr_ptr;

/* Pointer to malloc()'d C arr we'll manage. */
auto& c_arr_entry = libClame::__c_arr_table<T2>[c_lflag];
auto& c_arr = std::get<0>(c_arr_entry);
auto& c_arr_len = std::get<1>(c_arr_entry);

/* Get the reference to the function we were provided. */
const auto& function = libClame::__shadow_table[c_lflag];

/* Add the wrapper function to our call table. */
libClame::__call_table[c_lflag] = [&]() {
/* Clear the C++ array. */
arr.clear();

Expand All @@ -74,16 +90,15 @@ static LC_flag_t __make_arr(
c_arr = NULL;

/* Run the callback code if it was specified. */
//function.value_or([](){})();
(void) function; // BUG, needs checking.
function();
};

/* Get the format string and store it away. */
libClame::__string_table.push_back(
libClame::__string_list.push_back(
sscanf_fmt.value_or(libClame::__get_fmt<T2>())
);

const auto fmt = (*libClame::__string_table.rbegin()).c_str();
const auto fmt = (*libClame::__string_list.rbegin()).c_str();

/* Get the limits for the array if they are defined. */
const auto set_limits = limits.value_or(
Expand All @@ -93,12 +108,12 @@ static LC_flag_t __make_arr(
const auto& min = std::get<0>(set_limits);
const auto& max = std::get<1>(set_limits);

/* The variables are: long_flag, short_flag, function, var_ptr,
* var_type, value, fmt_string, arr_length, var_length, min_arr_length,
* max_arr_length, readonly. */

return LC_flag_t{
c_lflag, sflag, libClame::__interceptor, &c_arr, LC_OTHER_VAR,
0, fmt, &c_arr_len, sizeof(T2), min, max, false
};
/* Make the structure. */
return LC_MAKE_ARR_BOUNDED_F(
c_lflag, sflag, c_arr, fmt, c_arr_len, min, max,
libClame::__interceptor
);
}

/* End Header Guard. */
#endif
24 changes: 10 additions & 14 deletions inc/libClame/templates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,25 @@ namespace libClame {
std::optional<std::string> sscanf_fmt
){
/* Make a copy of the flag that won't get mutated. */
libClame::__string_table.push_back(std::move(lflag));
libClame::__string_list.push_back(std::move(lflag));
const auto& c_lflag = (
*libClame::__string_table.rbegin()
*libClame::__string_list.rbegin()
).c_str();

/* Run the callback code or an empty lambda. */
libClame::__call_table[c_lflag] = function.value_or([](){});

/* Get the format string and store it away. */
libClame::__string_table.push_back(
libClame::__string_list.push_back(
sscanf_fmt.value_or(libClame::__get_fmt<T>())
);

const auto fmt = (*libClame::__string_table.rbegin()).c_str();
const auto fmt = (*libClame::__string_list.rbegin()).c_str();

/* The variables are: long_flag, short_flag, function, var_ptr,
* var_type, value, fmt_string, arr_length, var_length,
* min_arr_length, max_arr_length, readonly. */

return LC_flag_t{
c_lflag, sflag, libClame::__interceptor, &var,
LC_OTHER_VAR, 0, fmt, NULL, sizeof(T), 0, 0, false
};
/* Make the structure. */
return LC_MAKE_VAR_F(
c_lflag, sflag, var, fmt, libClame::__interceptor
);
}

template<typename T>
Expand All @@ -66,7 +62,7 @@ namespace libClame {
std::optional<std::string> sscanf_fmt
){
return libClame::__make_arr(
lflag, sflag, arr, limits, function, sscanf_fmt
lflag, sflag, &arr, limits, function, sscanf_fmt
);
}

Expand All @@ -78,7 +74,7 @@ namespace libClame {
std::optional<std::string> sscanf_fmt
){
return libClame::__make_arr(
lflag, sflag, arr, limits, function, sscanf_fmt
lflag, sflag, &arr, limits, function, sscanf_fmt
);
}
}
Expand Down
Loading

0 comments on commit 8b3d73f

Please sign in to comment.