Namespace use or abuse

I Was looking at too much of someone else's code and I thought if I could see all the digitalRead() and digitalWrite() activity (scattered in the program) it might help.

Out on a limb a bit with the namespace feature of C++, I came up with this, which sorta kinda works mostly, but not always.

It may be a completely wrong thing to try. Any way of doing what I want here would be fine with me, the idea is to avoid modifying the original code.

I may be overlooking a totally simpler way. Just want something better than global search and replace, which, trust me, I am not above doing.

Thia code works, to a point, but it does not catch all the calls. Making plausible additions or changes hasn't worked for me, the typical error is

error: call of overloaded 'digitalRead(uint8_t&)' is ambiguous

which comes up if I uncomment my attempt to plug one hole.

/* catch functions here, report and pass them up */

# define USE_ALTO
# ifdef USE_ALTO
namespace ALTO {

void pinMode(int pin, byte val) {
  Serial.print(" pinMode : ");
  Serial.print(pin); Serial.print(" "); Serial.println(val);
  ::pinMode(pin, val);
}

/*
void digitalWrite(uint8_t pin, uint8_t val) {
  Serial.print(" digitalWrite : ");
  Serial.print(pin); Serial.print(" "); Serial.println(val);
  ::digitalWrite(pin, val);
}

*/

void digitalWrite(int pin, int val) {
  Serial.print(" digitalWrite : ");
  Serial.print(pin); Serial.print(" "); Serial.println(val);
  ::digitalWrite(pin, val);
}

byte digitalRead(int pin) {
  byte theValue = ::digitalRead(pin);
  Serial.print(" digitalRead : ");
  Serial.print(pin); Serial.print(" "); Serial.println(theValue);

  return theValue;
}

// attempt to fix missing digitalRead call
/* namesake.ino:74:16: error: call of overloaded 'digitalRead(uint8_t&)' is ambiguous
   digitalRead(x);         // this didn't get caught

byte digitalRead(uint8_t pin) {
  byte theValue = ::digitalRead(pin);
  Serial.print(" digitalRead : ");
  Serial.print(pin); Serial.print(" "); Serial.println(theValue);

  return theValue;
}
*/

}

// hardware/arduino/avr/cores/arduino/Arduino.h.

using namespace ALTO;
# endif

void setup()
{
  Serial.begin(115200);
  Serial.println("namesapce world!\n");

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
 
  Serial.println();

  for (int xx = 2; xx <= 5; xx++)
    pinMode(xx, INPUT_PULLUP);

  Serial.println();


// these don't get caught?
  for (uint8_t xx = 7; xx <= 10; xx++)
    pinMode(xx, INPUT_PULLUP);

  Serial.println();

  digitalWrite(7, 0);
  digitalRead(7);
  Serial.println();

  uint8_t x = 11;
  digitalWrite(x, 1);
  digitalRead(x);         // this didn't get caught
  Serial.println();

  char y = 12;
  digitalWrite(y, 0);
  digitalRead(y);
  Serial.println();

  int z = 13;
  digitalWrite(z, 1);
  digitalRead(z);
  Serial.println();
}

void loop() {}

Play with it here. I assume it is either fixable or unfixable and I would appreciate knowing how to fix it, or why it is unfixable and how to do what I am trying to do in any conventional manner.

TIA

a7

1 Like

This makes everything in the ALTO namespace available in the global namespace, which causes the overload errors.

Maybe you want to omit this line and use ALTO::digitalRead(...) etc.?

[edit]

I am not entirely sure what you want to do, but if you want to "wrap" these function, then perhaps this terrible hack may be be of use.

[deleted wrong suggestion]

[edit2]

I do not think the above is going to work either...

i would use grep in linux or cygwin environments. it's one of the first tools created on unix

I suppose also the issue is that you are overloading a function across namespaces and using various mixtures of argument/parameter datatypes (int/uint8_t) in the function definition and the call of that function causing a problem with the resolution of the target function.

EDIT

Incidentally, I see this warning from the code in the OP (compiled for a Uno under 1.8.19) :

C:\Users\6v6gt\Documents\Arduino\sketch_sep30a\sketch_sep30a.ino: In function 'void setup()':
C:\Users\6v6gt\Documents\Arduino\sketch_sep30a\sketch_sep30a.ino:85:20: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
   digitalWrite(x, 1);
                    ^
C:\Users\6v6gt\Documents\Arduino\sketch_sep30a\sketch_sep30a.ino:22:6: note: candidate 1: void ALTO::digitalWrite(int, int)
 void digitalWrite(int pin, int val) {
      ^~~~~~~~~~~~
In file included from C:\Users\6v6gt\AppData\Local\Temp\arduino_build_406497\sketch\sketch_sep30a.ino.cpp:1:0:
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Arduino.h:135:6: note: candidate 2: void digitalWrite(uint8_t, uint8_t)
 void digitalWrite(uint8_t pin, uint8_t val);
      ^~~~~~~~~~~~
Compiling libraries...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.