|
10 | 10 | ///
|
11 | 11 | /// @brief Helper functions for accessing global objects from all threads
|
12 | 12 |
|
13 |
| -threadsafe Function TSDS_Write(string name, [variable var]) |
14 |
| - ASSERT_TS(!ParamIsDefault(var), "Missing var parameter") |
| 13 | +/// @brief Creates/Overwrites a threadstorage and puts a numerical value in |
| 14 | +threadsafe static Function TSDS_Create(string name, variable var) |
| 15 | + |
15 | 16 | ASSERT_TS(!IsEmpty(name), "name can not be empty")
|
16 | 17 |
|
17 |
| - TUFXOP_Init/N=name/Z |
18 |
| - TUFXOP_GetStorage/N=name/Z wv |
| 18 | + TUFXOP_Init/Z/N=name |
| 19 | + TUFXOP_GetStorage/Z/N=name wv |
19 | 20 |
|
20 |
| - Make/FREE/N=(1)/D data = var |
| 21 | + Make/FREE/D data = {var} |
21 | 22 | wv[0] = data
|
22 | 23 | End
|
23 | 24 |
|
| 25 | +/// @brief Reads a numerical value from a threadstorage |
| 26 | +/// |
| 27 | +/// @param name name of threadstorage |
| 28 | +/// @param defValue [optional: default NaN] default value used when storage is created, create flag must be set |
| 29 | +/// @param create [optional: default 0] when set the threadstorage is created if it did not exist or had an incompatible format, defValue must be given |
24 | 30 | threadsafe Function TSDS_ReadVar(string name, [variable defValue, variable create])
|
| 31 | + |
| 32 | + variable argCheck = ParamIsDefault(defValue) + ParamIsDefault(create) |
| 33 | + ASSERT_TS(argCheck == 2 || argCheck == 0, "defaul value and create must be either both set or both default.") |
25 | 34 | ASSERT_TS(!IsEmpty(name), "name can not be empty")
|
26 | 35 |
|
27 |
| - if(ParamIsDefault(defValue)) |
28 |
| - defValue = NaN |
29 |
| - endif |
30 |
| - |
31 |
| - if(ParamIsDefault(create)) |
32 |
| - create = 0 |
33 |
| - else |
34 |
| - create = !!create |
35 |
| - endif |
| 36 | + defValue = ParamIsDefault(defValue) ? NaN : defValue |
| 37 | + create = ParamIsDefault(create) ? 0 : !!create |
36 | 38 |
|
37 | 39 | WAVE/Z data = TSDS_Read(name)
|
| 40 | + if(WaveExists(data) && IsNumericWave(data) && DimSize(data, ROWS) == 1) |
| 41 | + return data[0] |
| 42 | + endif |
38 | 43 |
|
39 |
| - if(!WaveExists(data)) |
40 |
| - if(create) |
41 |
| - TSDS_Write(name, var = defValue) |
42 |
| - endif |
| 44 | + ASSERT_TS(create == 1, "Error reading from threadstorage:" + name) |
43 | 45 |
|
44 |
| - return defValue |
45 |
| - endif |
| 46 | + TSDS_Create(name, defValue) |
46 | 47 |
|
47 |
| - return data[0] |
| 48 | + return defValue |
48 | 49 | End
|
49 | 50 |
|
| 51 | +/// @brief Reads a single wave ref wave from a named threadstorage |
50 | 52 | threadsafe static Function/WAVE TSDS_Read(string name)
|
51 |
| - TUFXOP_GetStorage/Q/N=name/Z wv |
52 | 53 |
|
53 |
| - if(V_flag) |
54 |
| - return $"" |
| 54 | + TUFXOP_GetStorage/Q/N=name/Z wv |
| 55 | + if(!V_flag && WaveExists(wv) && IsWaveRefWave(wv) && DimSize(wv, ROWS) == 1) |
| 56 | + return wv[0] |
55 | 57 | endif
|
56 | 58 |
|
57 |
| - if(!WaveExists(wv) || !IsWaveRefWave(wv) || DimSize(wv, ROWS) != 1) |
58 |
| - return $"" |
59 |
| - endif |
| 59 | + return $"" |
| 60 | +End |
| 61 | + |
| 62 | +/// @brief Writes a numerical value to a threadstorage, if the threadstorage does not exist it is automatically created. |
| 63 | +/// |
| 64 | +/// @param name name of threadstorage |
| 65 | +/// @param var numerical value that should be written |
| 66 | +/// @returns 0 if write was successful, 1 if write was not successful |
| 67 | +threadsafe Function TSDS_WriteVar(string name, variable var) |
| 68 | + |
| 69 | + ASSERT_TS(!IsEmpty(name), "name can not be empty") |
60 | 70 |
|
61 |
| - WAVE/Z data = wv[0] |
| 71 | + WAVE/Z data = TSDS_Read(name) |
| 72 | + if(WaveExists(data) && IsNumericWave(data) && DimSize(data, ROWS) == 1) |
| 73 | + data[0] = var |
62 | 74 |
|
63 |
| - if(!WaveExists(data) || !IsNumericWave(data) || DimSize(wv, ROWS) != 1) |
64 |
| - return $"" |
| 75 | + return NaN |
65 | 76 | endif
|
66 | 77 |
|
67 |
| - return data |
| 78 | + TSDS_Create(name, var) |
68 | 79 | End
|
0 commit comments