|
| 1 | +/* |
| 2 | + * This file is part of OpenModelica. |
| 3 | + * |
| 4 | + * Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC), |
| 5 | + * c/o Linköpings universitet, Department of Computer and Information Science, |
| 6 | + * SE-58183 Linköping, Sweden. |
| 7 | + * |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR |
| 11 | + * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2. |
| 12 | + * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES |
| 13 | + * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, |
| 14 | + * ACCORDING TO RECIPIENTS CHOICE. |
| 15 | + * |
| 16 | + * The OpenModelica software and the Open Source Modelica |
| 17 | + * Consortium (OSMC) Public License (OSMC-PL) are obtained |
| 18 | + * from OSMC, either from the above address, |
| 19 | + * from the URLs: http://www.ida.liu.se/projects/OpenModelica or |
| 20 | + * http://www.openmodelica.org, and in the OpenModelica distribution. |
| 21 | + * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html. |
| 22 | + * |
| 23 | + * This program is distributed WITHOUT ANY WARRANTY; without |
| 24 | + * even the implied warranty of MERCHANTABILITY or FITNESS |
| 25 | + * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH |
| 26 | + * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. |
| 27 | + * |
| 28 | + * See the full OSMC Public License conditions for more details. |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +encapsulated uniontype Pointer<T> |
| 33 | +"Creating shared (sometimes mutable) objects. |
| 34 | +
|
| 35 | +This uniontype contains routines for creating and updating objects, |
| 36 | +similar to array<> structures. Use this uniontype over the Mutable |
| 37 | +package if you need to be able to create constants that are just |
| 38 | +pointers to static, immutable data. Use the Mutable uniontype if you |
| 39 | +do not need to create constants (that package has lower overhead |
| 40 | +since it does no extra checks)." |
| 41 | + |
| 42 | +impure function create |
| 43 | + input T data; |
| 44 | + output Pointer<T> ptr; |
| 45 | +external "C" ptr=pointerCreate(data) annotation(Include=" |
| 46 | +static inline void* pointerCreate(void *data) |
| 47 | +{ |
| 48 | + return mmc_mk_box1(0, data); |
| 49 | +} |
| 50 | +"); |
| 51 | +end create; |
| 52 | + |
| 53 | +function createImmutable |
| 54 | + input T data; |
| 55 | + output Pointer<T> ptr; |
| 56 | +external "builtin" ptr=mmc_mk_some(data); |
| 57 | +end createImmutable; |
| 58 | + |
| 59 | +impure function update |
| 60 | + input Pointer<T> mutable; |
| 61 | + input T data; |
| 62 | +external "C" pointerUpdate(OpenModelica.threadData(), mutable, data) annotation(Include=" |
| 63 | +static inline void pointerUpdate(threadData_t *threadData, void *ptr, void *data) |
| 64 | +{ |
| 65 | + if (valueConstructor(ptr)!=0) { |
| 66 | + MMC_THROW_INTERNAL(); |
| 67 | + } |
| 68 | + MMC_STRUCTDATA(ptr)[0] = data; |
| 69 | +} |
| 70 | +"); |
| 71 | +end update; |
| 72 | + |
| 73 | +impure function access |
| 74 | + input Pointer<T> mutable; |
| 75 | + output T data; |
| 76 | +external "C" data=pointerAccess(mutable) annotation(Include=" |
| 77 | +static inline void* pointerAccess(void *ptr) |
| 78 | +{ |
| 79 | + return MMC_STRUCTDATA(ptr)[0]; |
| 80 | +} |
| 81 | +"); |
| 82 | +end access; |
| 83 | + |
| 84 | +annotation(__OpenModelica_Interface="util"); |
| 85 | +end Pointer; |
0 commit comments