Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ Code cleanup #566

Closed
wants to merge 4 commits into from
Closed

C++ Code cleanup #566

wants to merge 4 commits into from

Conversation

TheMagnetar
Copy link
Member

When merged this pull request will:

  • Clean c++ code
  • Const correctness
  • Use of base types (int16_t, int32_t, ...)
  • Get rid of microsoft specific types (DWORD, BYTE,...)
  • Get rid of microsoft "safe" printf variants
  • Get rid of macro codes (DECLARE_MEMBER, ...)
  • Consistent naming of arguments and member types

@TheMagnetar TheMagnetar added this to the 2.7.0 milestone Aug 11, 2018
Copy link
Member

@NouberNou NouberNou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general not a big fan of the naming of function arguments or member variables. Hungarian type notation should really be avoided as it is totally redundant information.

for (int i = 0; i < sampleCount*channels && i < 4095; i++, iter++) {
if (samples[i] > 0) {
currentSample = 20.0f * log10((float)samples[i]);
ACRE_RESULT CAmplitudeAttenuation::process(int16_t *const a_samples, const int32_t ac_sampleCount, const int32_t ac_channels, CPlayer *const a_player) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please not do pseudo hungarian notation? If you want to signify a variable is an argument I've always prefered a trailing _, so player_. Noting the constiness of an argument is also not super helpful in most modern editors/IDEs. It should show a redline.

@TheMagnetar
Copy link
Member Author

Can do

@@ -16,13 +16,13 @@

// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int32_t, int32_t>(std::isspace))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be converted to using a lambda. std::ptr_fun has been deprecated in C++11. and std::not1 has been deprecated in c++17.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is example code for ltrim/rtrim https://github.com/dedmen/armake/blob/cpp/src/utils.cpp#L251
Also supporting tabs.

@@ -210,7 +212,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
char *value = new char[4096];
//DEBUG("Read from pipe [%d]\n", hPipe);
ret = ReadFile(readHandle, (LPVOID)value, 4096, &cbRead, NULL);
if ( ! ret) {
if ( !ret) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( !ret) {
if (!ret) {

?

m_sfreq = 48000.0f;
m_mfreq = 5200.0f;
lpFilter1.setup(4, (float64_t) m_sfreq, (float64_t) m_mfreq*0.3);
lpFilter2.setup(4, (float64_t) m_sfreq, (float64_t) m_mfreq*0.5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use proper c++ static_cast

short maxAmp = 1;
for (int i = 0; i < sampleCount; ++i) {
samples[i] = (short)(((float)tempSamples[i]*1.0f)*cos(((float)i/(sfreq/mfreq))*1.4*M_PI));
int16_t *tempSamples = new int16_t[sampleCount];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use std::vector with resize for a temp array. That way you don't have to worry about cleanup.


g_Log = (Log *)new Log(const_cast<char *>(loggingPath.c_str()));
g_Log = (Log *) new Log(const_cast<char *>(loggingPath.c_str()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the reason of casting Log* to Log* here?

Copy link
Contributor

@dedmen dedmen Feb 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why is it casting a constant string to a non constant char*, That might be undefined behavior.

for (int i = 0; i < n; ++i)
result[i] /= n;
return result;
if (m_inverse == false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== false?

Suggested change
if (m_inverse == false)
if (!m_inverse)

#define TS_SAMPLE_RATE 48000
#define CUTTOFF_FREQ 15000
#define TS_SAMPLE_RATE_Hz 48000
#define CUTTOFF_FREQ_Hz 15000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define CUTTOFF_FREQ_Hz 15000
#define CUTTOFF_FREQ_Hz 15000

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Macros are usually identified by being all upper case. The z is not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is accepted when trying to define units. mHz is not the same as MHz and so on.


DECLARE_MEMBER(int, ChannelCount);
virtual __inline void setChannelCount(const int32_t value) { this->m_channelCount = value; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__inline is MSVC specific, inline is the real keyword.
Also inline doesn't make much sense on virtual functions. If the compiler doesn't know exactly that the class will be that exact type, then it won't be able to inline. And if it knew that it was that type, then it would also inline if you don't put the inline keyword there.

bool getConnected() const { return (this->getConnectedRead() && this->getConnectedWrite()); };
void setConnected (const bool value) { this->setConnectedRead(value); this->setConnectedWrite(value); };

virtual __inline void setPipeHandleRead(const HANDLE value) { this->m_pipeHandleRead = value; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions should be override, not virtual

Suggested change
virtual __inline void setPipeHandleRead(const HANDLE value) { this->m_pipeHandleRead = value; }
void setPipeHandleRead(const HANDLE value) override { this->m_pipeHandleRead = value; }

delete this->buffer;
this->buffer = NULL;
if (this->m_buffer) {
delete this->m_buffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just make it a vector and it will cleanup itself.

@@ -21,11 +21,11 @@ RPC_FUNCTION(ext_remoteStopSpeaking) {
this->getSelf()->getCurveScale()
) */

ACRE_ID id = (ACRE_ID)vMessage->getParameterAsInt(0);
ACRE_ID id = (ACRE_ID) vMessage->getParameterAsInt(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_cast and auto?
Is the cast even necessary? Can't it implictly convert on =?

@@ -5,7 +5,7 @@

Log *g_Log = NULL;

Log::Log(char *logFile) {
Log::Log(char *logFile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const char* ?

ACRE_RESULT updateShouldSwitchTS3Channel(const bool state);
bool shouldSwitchTS3Channel();

virtual __inline void sethadVAD(const bool value) { this->m_hadVAD = value; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setHadVad* ?
Also override.

@@ -6,53 +6,48 @@
#include <string>


typedef struct __WAVEDESCR
typedef struct __waveDescr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheMagnetar TheMagnetar modified the milestones: 2.7.0, Ongoing Apr 3, 2019
@jonpas jonpas modified the milestones: Ongoing, Backlog Feb 24, 2021
@jonpas
Copy link
Member

jonpas commented Sep 14, 2023

No further activity and tons of conflicts, would need to get re-done.

@jonpas jonpas closed this Sep 14, 2023
@jonpas jonpas deleted the CppCleanup branch September 14, 2023 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants