AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
masterport.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 #include "masterport.h"
9 #include <iostream>
10 
11 
12 using namespace std;
13 
14 
15 static bool gTerminating (false);
16 static MasterPortPtr gMasterPort; // Singleton
17 
18 
19 static ostream & operator << (ostream & inOutStr, const MasterPort & inObj)
20 {
21  return inOutStr << "MasterPort instance " << hex << &inObj << dec << " portRef " << hex << inObj.GetPortRef () << dec << " " << (inObj.IsOkay () ? "OK" : "Not OK");
22 
23 } // MasterPort ostream operator <<
24 
25 
26 IONotificationPortRef MasterPort::Get (void)
27 {
28  MasterPortPtr masterPortInstance (GetInstance ());
29  IONotificationPortRef masterPortRef (NULL);
30 
31  assert (masterPortInstance && "Must have valid MasterPort instance");
32  if (masterPortInstance)
33  {
34  masterPortRef = *masterPortInstance;
35  assert (masterPortRef && "Must have valid masterPortRef");
36  }
37 
38  return masterPortRef;
39 
40 } // Get
41 
42 
44 {
45  if (!gMasterPort)
46  Create (gMasterPort);
47 
48  return gMasterPort;
49 
50 } // GetInstance
51 
52 
53 bool MasterPort::Create (MasterPortPtr & outObj)
54 {
55  outObj = NULL;
56  try
57  {
58  outObj = gTerminating ? NULL : new MasterPort;
59  if (!outObj->IsOkay ())
60  outObj = NULL;
61  }
62  catch (std::bad_alloc)
63  {
64  }
65  return outObj;
66 
67 } // Create
68 
69 
70 MasterPort::MasterPort ()
71 {
72  //
73  // To set up asynchronous notifications, create a notification port, then add its run loop event source to our run loop.
74  // It is not necessary to call CFRunLoopRemoveSource during teardown because that's implicitly done when the notification
75  // port is destroyed.
76  //
77  mpMasterPort = IONotificationPortCreate (kIOMasterPortDefault);
78  if (!mpMasterPort)
79  {
80  cerr << "IONotificationPortCreate returned NULL" << endl;
81  return;
82  }
83  CFRunLoopSourceRef pRunLoopSource (IONotificationPortGetRunLoopSource (mpMasterPort));
84  if (!pRunLoopSource)
85  {
86  cerr << "IONotificationPortGetRunLoopSource returned NULL" << endl;
87  return;
88  }
89  CFRunLoopAddSource (CFRunLoopGetCurrent (), pRunLoopSource, kCFRunLoopDefaultMode);
90 
91 } // constructor
92 
93 
95 {
96  cerr << *this << " destructor" << endl;
97  if (mpMasterPort)
98  IONotificationPortDestroy (mpMasterPort);
99  mpMasterPort = NULL;
100 
101 } // destructor
102 
103 
108 {
109  public:
110  explicit inline MasterPortDestroyer () {cerr << "MasterPortDestroyer is armed" << endl;}
111  virtual inline ~MasterPortDestroyer ()
112  {
113  cerr << "MasterPortDestroyer triggered" << endl;
114  gMasterPort = NULL;
115  } // destructor
116 
117  private:
118  // Do not copy!
119  inline MasterPortDestroyer (const MasterPortDestroyer & inObj) {if (&inObj != this) assert (false);}
120  inline MasterPortDestroyer & operator = (const MasterPortDestroyer & inRHS) {if (&inRHS != this) assert (false); return *this;}
121 
122 }; // MasterPortDestroyer
123 
MasterPortDestroyer::MasterPortDestroyer
MasterPortDestroyer()
Definition: masterport.cpp:110
MasterPort::GetInstance
static MasterPortPtr GetInstance(void)
Definition: masterport.cpp:43
NULL
#define NULL
Definition: ntv2caption608types.h:19
AJARefPtr
I am a reference-counted pointer template class. I am intended to be a proxy for an underlying object...
Definition: ajarefptr.h:89
gMasterPortDestroyer
static MasterPortDestroyer gMasterPortDestroyer
Definition: masterport.cpp:124
MasterPortDestroyer::~MasterPortDestroyer
virtual ~MasterPortDestroyer()
Definition: masterport.cpp:111
gTerminating
static bool gTerminating(false)
MasterPort::~MasterPort
virtual ~MasterPort()
Definition: masterport.cpp:94
MasterPort::Get
static IONotificationPortRef Get(void)
Definition: masterport.cpp:26
MasterPortDestroyer
I sit around waiting for main() to exit(), whereupon I destroy the gMasterPort singleton.
Definition: masterport.cpp:107
MasterPort::GetPortRef
virtual IONotificationPortRef GetPortRef(void) const
Definition: masterport.h:35
std
Definition: json.hpp:5362
MasterPort
Definition: masterport.h:23
MasterPort::IsOkay
virtual bool IsOkay(void) const
Definition: masterport.h:37
gMasterPort
static MasterPortPtr gMasterPort
Definition: masterport.cpp:16
operator<<
static ostream & operator<<(ostream &inOutStr, const MasterPort &inObj)
Definition: masterport.cpp:19
masterport.h
Declares the MasterPort class.