bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bash source question & suggestion?


From: Linda Walsh
Subject: bash source question & suggestion?
Date: Tue, 09 Feb 2016 16:30:45 -0800
User-agent: Thunderbird

At various times I see people contributing code to bash.

I was wondering if it might be possible to use
C++ in some patches or new files?

I know that the kernel, and the glib include files go to
extra pain to declare 'C' functions when using C++ where
those C function need to be called from C++.

It seems like there are various parts and features in bash
that would be ideal for C++, that are more verbose,
and less easy to understand & maintain, and certainly
less easy to extend than similar code written in C.

As an example (I'm sure many here know C++, but for those
that might not knows some of the newer features (or some
old features that weren't as useful when mostly writing in
'C') was a routine to print out Human or SI prefixed numbers.

The header file:

char * Binary_Scale (char buf[], double value);
char * Binary_Scale (char buf[], uint64_t value);
char * Binary_Scale (char buf[], int64_t value);
char * SI_Scale (char buf[], uint64_t value);
string Binary_Scale (double value);
string SI_Scale (uint64_t value);

provides different interfaces to return allow
either standard C-strings or newer C++ strings. At link time, the correct form is chosen by which
args are used and by the type of the result.

All of the above call use only 1 function to calculate
and return the desired value and for each function,
only wrappers over the main function
are used to get the various forms like these 2 wrappers:

 string Binary_Scale (double value) { return Scale(value); }
char * Binary_Scale (char buf[], double value) { return Scale(buf, value); }

that call a "string" and "char *" version of "scale":

 string Scale(double value, const int scale = 1024) {
  char buf[16]; return string(Scale(buf, value, scale)); }

which calls the workhorse:

 static char * Scale(char buf[], double value, const int scale = 1024) {
  ...;     return buf;  }

----------

With "compat functions, the same algorithms can be easily
reused in old and new code allowing for more flexibility in
algorithm design, and, as time progresses, greater reliability.

Not being a veteran C++ programmer, when I started wanting to make
changes in a C program,  but seeing the ways C++ could benefit, I
started out by creating alot of "C" compat functions that didn't
have the C++ alternates, but as I grew more comfortable with the C++
data types and features, I went back and implemented C++ interfaces
around the original 'C' funtions to do the same thing.  Later, I
more often started with C++ funcs and providing backwards compat
only where necessary.

The main benefit I found -- a *large* reduction in code size where
multiple C-files of hundreds to 1000 or more lines, shrank down using
'common functions' to <100 lines for many with at least 30-40%
code savings (and increase in clarity) for scores of files.

Maybe the above is already policy or maybe it's pointless to suggest,
but either way, I think it's a good way to go to be able to extend
bash, while shrinking code and boosting reliability.








one of my favorites -- that helps in transitioning
or providing compatibility going from 'C' to C++.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]