Scippy

SCIP

Solving Constraint Integer Programs

objreader.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (c) 2002-2024 Zuse Institute Berlin (ZIB) */
7 /* */
8 /* Licensed under the Apache License, Version 2.0 (the "License"); */
9 /* you may not use this file except in compliance with the License. */
10 /* You may obtain a copy of the License at */
11 /* */
12 /* http://www.apache.org/licenses/LICENSE-2.0 */
13 /* */
14 /* Unless required by applicable law or agreed to in writing, software */
15 /* distributed under the License is distributed on an "AS IS" BASIS, */
16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17 /* See the License for the specific language governing permissions and */
18 /* limitations under the License. */
19 /* */
20 /* You should have received a copy of the Apache-2.0 license */
21 /* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22 /* */
23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 /**@file objreader.h
26  * @brief C++ wrapper for file readers and writers
27  * @author Tobias Achterberg
28  */
29 
30 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
31 
32 #ifndef __SCIP_OBJREADER_H__
33 #define __SCIP_OBJREADER_H__
34 
35 #include <cstring>
36 #include <utility>
37 
38 #include "scip/scip.h"
39 #include "objscip/objcloneable.h"
40 
41 namespace scip
42 {
43 
44 /** @brief C++ wrapper for file readers and writers
45  *
46  * This class defines the interface for file readers and writers implemented in C++.
47  *
48  * - \ref READER "Instructions for implementing a file reader and writer"
49  * - \ref FILEREADERS "List of available file readers and writers"
50  * - \ref type_reader.h "Corresponding C interface"
51  */
52 class ObjReader : public ObjCloneable
53 {
54 public:
55  /*lint --e{1540}*/
56 
57  /** SCIP data structure */
59 
60  /** name of the file reader */
61  char* scip_name_;
62 
63  /** description of the file reader */
64  char* scip_desc_;
65 
66  /** file extension that reader processes */
68 
69  /** default constructor */
71  SCIP* scip, /**< SCIP data structure */
72  const char* name, /**< name of file reader */
73  const char* desc, /**< description of file reader */
74  const char* extension /**< file extension that reader processes */
75  )
76  : scip_(scip),
77  scip_name_(0),
78  scip_desc_(0),
79  scip_extension_(0)
80  {
81  /* the macro SCIPduplicateMemoryArray does not need the first argument: */
82  SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_name_, name, std::strlen(name)+1) );
83  SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_desc_, desc, std::strlen(desc)+1) );
84  SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_extension_, extension, std::strlen(extension)+1) );
85  }
86 
87  /** copy constructor */
88  ObjReader(const ObjReader& o) : ObjReader(o.scip_, o.scip_name_, o.scip_desc_, o.scip_extension_) {}
89 
90  /** move constructor */
91  ObjReader(ObjReader&& o) : scip_(o.scip_), scip_name_(0), scip_desc_(0), scip_extension_(0)
92  {
93  std::swap(scip_name_, o.scip_name_);
94  std::swap(scip_desc_, o.scip_desc_);
95  std::swap(scip_extension_, o.scip_extension_);
96  }
97 
98  /** destructor */
99  virtual ~ObjReader()
100  {
101  /* the macro SCIPfreeMemoryArray does not need the first argument: */
102  /*lint --e{64}*/
103  SCIPfreeMemoryArray(scip_, &scip_name_);
104  SCIPfreeMemoryArray(scip_, &scip_desc_);
105  SCIPfreeMemoryArray(scip_, &scip_extension_);
106  }
107 
108  /** assignment of polymorphic classes causes slicing and is therefore disabled. */
109  ObjReader& operator=(const ObjReader& o) = delete;
110 
111  /** assignment of polymorphic classes causes slicing and is therefore disabled. */
112  ObjReader& operator=(ObjReader&& o) = delete;
113 
114  /** destructor of file reader to free user data (called when SCIP is exiting)
115  *
116  * @see SCIP_DECL_READERFREE(x) in @ref type_reader.h
117  */
118  virtual SCIP_DECL_READERFREE(scip_free)
119  { /*lint --e{715}*/
120  return SCIP_OKAY;
121  }
122 
123  /** problem reading method of reader
124  *
125  * @see SCIP_DECL_READERREAD(x) in @ref type_reader.h
126  */
127  virtual SCIP_DECL_READERREAD(scip_read)
128  { /*lint --e{715}*/
129 
130  /* set result pointer to indicate that the reading was not performed */
131  assert(result != NULL);
132  (*result) = SCIP_DIDNOTRUN;
133 
134  return SCIP_OKAY;
135  }
136 
137  /** problem writing method of reader; NOTE: if the parameter "genericnames" is TRUE, then
138  * SCIP already set all variable and constraint names to generic names; therefore, this
139  * method should always use SCIPvarGetName() and SCIPconsGetName();
140  *
141  * @see SCIP_DECL_READERWRITE(x) in @ref type_reader.h
142  */
143  virtual SCIP_DECL_READERWRITE(scip_write)
144  { /*lint --e{715}*/
145 
146  /* set result pointer to indicate that the writing was not performed */
147  assert(result != NULL);
148  (*result) = SCIP_DIDNOTRUN;
149 
150  return SCIP_OKAY;
151  }
152 };
153 
154 } /* namespace scip */
155 
156 
157 
158 /** creates the file reader for the given file reader object and includes it in SCIP
159  *
160  * The method should be called in one of the following ways:
161  *
162  * 1. The user is resposible of deleting the object:
163  * SCIP_CALL( SCIPcreate(&scip) );
164  * ...
165  * MyReader* myreader = new MyReader(...);
166  * SCIP_CALL( SCIPincludeObjReader(scip, &myreader, FALSE) );
167  * ...
168  * SCIP_CALL( SCIPfree(&scip) );
169  * delete myreader; // delete reader AFTER SCIPfree() !
170  *
171  * 2. The object pointer is passed to SCIP and deleted by SCIP in the SCIPfree() call:
172  * SCIP_CALL( SCIPcreate(&scip) );
173  * ...
174  * SCIP_CALL( SCIPincludeObjReader(scip, new MyReader(...), TRUE) );
175  * ...
176  * SCIP_CALL( SCIPfree(&scip) ); // destructor of MyReader is called here
177  */
178 SCIP_EXPORT
180  SCIP* scip, /**< SCIP data structure */
181  scip::ObjReader* objreader, /**< file reader object */
182  SCIP_Bool deleteobject /**< should the reader object be deleted when reader is freed? */
183  );
184 
185 /** returns the reader object of the given name, or 0 if not existing */
186 SCIP_EXPORT
188  SCIP* scip, /**< SCIP data structure */
189  const char* name /**< name of file reader */
190  );
191 
192 /** returns the reader object for the given file reader */
193 SCIP_EXPORT
195  SCIP* scip, /**< SCIP data structure */
196  SCIP_READER* reader /**< file reader */
197  );
198 
199 #endif
#define NULL
Definition: def.h:267
SCIP_RETCODE SCIPincludeObjReader(SCIP *scip, scip::ObjReader *objreader, SCIP_Bool deleteobject)
Definition: objreader.cpp:155
#define SCIPduplicateMemoryArray(scip, ptr, source, num)
Definition: scip_mem.h:76
SCIP * scip_
Definition: objreader.h:58
#define SCIPfreeMemoryArray(scip, ptr)
Definition: scip_mem.h:80
ObjReader(ObjReader &&o)
Definition: objreader.h:91
char * scip_desc_
Definition: objreader.h:64
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
char * scip_name_
Definition: objreader.h:61
definition of base class for all clonable classes
virtual SCIP_DECL_READERWRITE(scip_write)
Definition: objreader.h:143
ObjReader(SCIP *scip, const char *name, const char *desc, const char *extension)
Definition: objreader.h:70
ObjReader & operator=(const ObjReader &o)=delete
virtual ~ObjReader()
Definition: objreader.h:99
ObjReader(const ObjReader &o)
Definition: objreader.h:88
#define SCIP_Bool
Definition: def.h:91
scip::ObjReader * SCIPgetObjReader(SCIP *scip, SCIP_READER *reader)
Definition: objreader.cpp:199
char * scip_extension_
Definition: objreader.h:67
scip::ObjReader * SCIPfindObjReader(SCIP *scip, const char *name)
Definition: objreader.cpp:180
C++ wrapper for file readers and writers.
Definition: objreader.h:52
Definition of base class for all clonable classes.
Definition: objcloneable.h:47
virtual SCIP_DECL_READERFREE(scip_free)
Definition: objreader.h:118
virtual SCIP_DECL_READERREAD(scip_read)
Definition: objreader.h:127
#define SCIP_CALL_ABORT(x)
Definition: def.h:359
SCIP callable library.