GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4CommandProtoList.cxx
Go to the documentation of this file.
1 // $Id$
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4CommandProtoList.h"
15 
16 #include <iostream>
17 
18 #include "TMutex.h"
19 #include "TObjArray.h"
20 
21 #include "TGo4LockGuard.h"
22 #include "TGo4Log.h"
23 #include "TGo4RemoteCommand.h"
24 
26  TNamed(name,"This is a TGo4CommandProtoList")
27 {
28  GO4TRACE((14,"TGo4CommandProtoList::TGo4CommandProtoList(const char *) ctor",__LINE__, __FILE__));
29 
30  fxListMutex = new TMutex;
31  fxCommandList = new TObjArray;
33 }
34 
36 {
37  GO4TRACE((14,"TGo4CommandProtoList::~TGo4CommandProtoList() dtor",__LINE__, __FILE__));
38  {
39  TGo4LockGuard listguard(fxListMutex);
40  fxCommandList->Delete();
41  delete fxCommandList;
42  }
43  delete fxListMutex;
44 }
45 
47 {
48  GO4TRACE((12,"TGo4CommandProtoList::ShowCommands()",__LINE__, __FILE__));
49 
50  TGo4LockGuard listguard(fxListMutex);
51  TIter iter(fxCommandList);
52  TGo4Log::Debug(" CommandProtoList Showing the known commands:");
53  std::cout << " Name: \t| Description:"<<std::endl;
54  while(auto com = (TGo4Command *) iter())
55  std::cout << " "<< com->GetName()<<"\t| "<<com->What()<<std::endl;
56 }
57 
59 {
60  GO4TRACE((12,"TGo4CommandProtoList::MakeCommand(const char *)",__LINE__, __FILE__));
61  TGo4Command *rev = nullptr;
62  TGo4LockGuard listguard(fxListMutex);
63  TObject *obj = fxCommandList->FindObject(name);
64  if(!obj) {
65  //no, do nothing
66  GO4TRACE((10,"TGo4CommandProtoList::RemoveCommand(TGo4Command *) command not found in array",__LINE__, __FILE__));
67  } else {
68  // yes, create it it
69  GO4TRACE((10,"TGo4CommandProtoList::RemoveCommand(TGo4Command *) cloning command",__LINE__, __FILE__));
70  rev = (TGo4Command *) obj->Clone();
71  }
72 
73  return rev;
74 }
75 
77 {
78  if(!remcon) return nullptr;
79  TGo4Command *com = MakeCommand(remcon->GetCommandName());
80  if(com)
81  com->Set(remcon); // copy optional parameters from remote command
82  return com;
83 }
84 
85 
87 {
88  GO4TRACE((12,"TGo4CommandProtoList::RemoveCommand(const char *)",__LINE__, __FILE__));
89 
90  TGo4LockGuard listguard(fxListMutex);
91  TObject *obj = fxCommandList->FindObject(name);
92  if(!obj) {
93  //no, do nothing
94  GO4TRACE((10,"TGo4CommandProtoList::RemoveCommand(TGo4Command *) command not found in array",__LINE__, __FILE__));
95  } else {
96  // yes, remove it
97  GO4TRACE((10,"TGo4CommandProtoList::RemoveCommand(TGo4Command *) removing command from array",__LINE__, __FILE__));
98  fxCommandList->Remove(obj);
99  fxCommandList->Compress();
100  fxCommandList->Expand(fxCommandList->GetLast()+1);
101  delete obj;
102  }
103 }
104 
105 
107 {
108  GO4TRACE((12,"TGo4CommandProtoList::AddCommand(TGo4Command *)",__LINE__, __FILE__));
109  TGo4LockGuard listguard(fxListMutex);
110  if(!fxCommandList->FindObject(com)) {
111  //no, add new command
112  GO4TRACE((10,"TGo4CommandProtoList::AddCommand(TGo4Command *) Adding new go4 commandto array",__LINE__, __FILE__));
113  fxCommandList->AddLast(com);
114  } else {
115  // yes, do nothing
116  GO4TRACE((10,"TGo4CommandProtoList::AddCommand(TGo4Command *) command was already in array",__LINE__, __FILE__));
117  }
118 }
119 
121 {
122  if(this != &two) {
123  TGo4LockGuard outerguard(two.fxListMutex);
124  TIter iter(two.fxCommandList);
125  while (auto com = dynamic_cast<TGo4Command *>(iter()))
126  AddCommand(com);
127  std::cout <<"CommandProtoList "<< GetName() <<"used operator += for adding list " << two.GetName() << std::endl;
128  }
129  return *this;
130 }
const char * GetCommandName() const
virtual void Set(TGo4RemoteCommand *remcon)
static void Debug(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:281
void RemoveCommand(const char *name)
TGo4CommandProtoList & operator+=(const TGo4CommandProtoList &two)
void AddCommand(TGo4Command *com)
#define GO4TRACE(X)
Definition: TGo4Log.h:25
TGo4Command * MakeCommand(const char *name)