Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Declaring the AxisCamera as a module level variable. (http://www.chiefdelphi.com/forums/showthread.php?t=105407)

enrique 04-04-2012 13:14

Declaring the AxisCamera as a module level variable.
 
What would be the proper way to declare the AxisCamera class as a module level so that autonomous and teleop can use the same connection and we don't have to wait for a connection twice?

AxisCamera &camera = AxisCamera::GetInstance("10.12.51.11");

bob.wolff68 04-04-2012 15:30

Re: Declaring the AxisCamera as a module level variable.
 
As a member variable to your SimpleRobot class, define AxisCamera& camera; Then in the initializers, initialize it there. For instance...

Code:

#define CAMERA_IP "10.19.67.11"

class RobotDemo : public SimpleRobot
{
  AxisCamera& camera;
  Joystick driver;    // driving Joystick for driver
public:
  RobotDemo(void) :
        driver(1),
        camera(AxisCamera::GetInstance(CAMERA_IP))
  {
        // Robot constructor
  }

// ....


enrique 05-04-2012 00:09

Re: Declaring the AxisCamera as a module level variable.
 
awesome, thanks

rbmj 12-04-2012 10:59

Re: Declaring the AxisCamera as a module level variable.
 
Since AxisCamera is a singleton, you can just call AxisCamera::GetInstance(NULL). It doesn't reconnect. Use the SOURCE :D

See?
Code:

AxisCamera &AxisCamera::GetInstance(const char *cameraIP)
{
        if (NULL == _instance) //_instance is a static variable of type AxisCamera*
        {
                _instance = new AxisCamera(cameraIP);

                _instance->m_videoServer = new PCVideoServer();
        }

        return *_instance;
}

Just make sure you're not the first one to call GetInstance, otherwise you're screwed. Static initialization order fiasco anyone? Yay intermittent segfaults!

Another way is to just have an inline function:

Code:

static inline AxisCamera& camera() {
  static AxisCamera& c = AxisCamera::GetInstance(IP);
  return c;
}

Yes it's nice to have encapsulation, but when the camera is a global singleton *anyway*, what's the point?


All times are GMT -5. The time now is 17:36.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi