/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ namespace Opc.Ua.SourceGeneration { /// /// Template strings /// internal static class NodeIdTemplates { /// /// Identifiers file template /// public static readonly TemplateString File = TemplateString.Parse( $$""" {{Tokens.CodeHeader}} namespace {{Tokens.Namespace}} { {{Tokens.ListOfIdentifiers}} {{Tokens.ListOfNodeIds}} } """); /// /// Identifiers per node class /// public static readonly TemplateString IdsPerNodeClass = TemplateString.Parse( $$""" /// /// A class that declares constants for all {{Tokens.NodeClass}} /// symbolic names in the {{Tokens.Namespace}} namespace. /// [global::System.CodeDom.Compiler.GeneratedCodeAttribute("{{Tokens.Tool}}", "{{Tokens.Version}}")] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute()] public static partial class {{Tokens.NodeClass}}s { {{Tokens.ListOfIdentifiers}} {{Tokens.IdentifierReflection}} } """); /// /// NodeIds per node class /// public static readonly TemplateString NodeIdPerNodeClass = TemplateString.Parse( $$""" /// /// A class that declares constants for all {{Tokens.NodeClass}} /// NodeIds in the {{Tokens.Namespace}} namespace. /// [global::System.CodeDom.Compiler.GeneratedCodeAttribute("{{Tokens.Tool}}", "{{Tokens.Version}}")] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute()] public static partial class {{Tokens.NodeClass}}Ids { {{Tokens.ListOfIdentifiers}} {{Tokens.IdentifierReflection}} } """); /// /// Identifier declaration /// public static readonly TemplateString IdDeclaration = TemplateString.Parse( $$""" public {{Tokens.IdModifier}} {{Tokens.IdType}} {{Tokens.SymbolicName}} = {{Tokens.Identifier}}; """); /// /// NodeId declaration with namespace URI /// public static readonly TemplateString NodeIdDeclarationAbsolute = TemplateString.Parse( $$""" public static readonly global::Opc.Ua.ExpandedNodeId {{Tokens.SymbolicName}} = new global::Opc.Ua.ExpandedNodeId(global::{{Tokens.NamespacePrefix}}.{{Tokens.NodeClass}}s.{{Tokens.SymbolicName}}, {{Tokens.NamespaceUri}}); """); /// /// NodeId declaration /// public static readonly TemplateString NodeIdDeclaration = TemplateString.Parse( $$""" public static global::Opc.Ua.NodeId {{Tokens.SymbolicName}} => new global::Opc.Ua.NodeId(global::{{Tokens.NamespacePrefix}}.{{Tokens.NodeClass}}s.{{Tokens.SymbolicName}}); """); /// /// Reflection methods for identifiers /// public static readonly TemplateString Reflection = TemplateString.Parse( $$""" /// /// Returns the browse names for all {{Tokens.ClassName}} /// public static global::System.Collections.Generic.IEnumerable BrowseNames => s_nameToId.Value.Keys; /// /// Returns the ids for all {{Tokens.ClassName}}. /// public static global::System.Collections.Generic.IEnumerable<{{Tokens.IdType}}> Identifiers => s_idToName.Value.Keys; /// /// Returns the browse name for a {{Tokens.ClassName}} id. /// public static string GetBrowseName({{Tokens.IdType}} identifier) { return s_idToName.Value.TryGetValue(identifier, out string name) ? name : string.Empty; } /// /// Returns the id for a {{Tokens.ClassName}} string. /// public static {{Tokens.IdType}} GetIdentifier(string browseName) { return s_nameToId.Value.TryGetValue(browseName, out {{Tokens.IdType}} id) ? id : default; } /// /// Returns the browse name for a {{Tokens.ClassName}} id. /// public static bool TryGetBrowseName( {{Tokens.IdType}} identifier, /*[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]*/ out string? name) { return s_idToName.Value.TryGetValue(identifier, out name); } /// /// Returns the id for a {{Tokens.ClassName}} string. /// public static bool TryGetValue(string browseName, out {{Tokens.IdType}} id) { return s_nameToId.Value.TryGetValue(browseName, out id); } /// /// Lazy id to name lookup. /// private static readonly global::System.Lazy< global::System.Collections.Generic.IReadOnlyDictionary<{{Tokens.IdType}}, string>> s_idToName = new global::System.Lazy< global::System.Collections.Generic.IReadOnlyDictionary<{{Tokens.IdType}}, string>>(() => { var lookup = new global::System.Collections.Generic.Dictionary<{{Tokens.IdType}}, string>(); {{Tokens.ListOfIdentifersToNames}} #if NET8_0_OR_GREATER return global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(lookup); #else return new global::System.Collections.ObjectModel.ReadOnlyDictionary<{{Tokens.IdType}}, string>(lookup); #endif }); /// /// Lazy name to id lookup. /// private static readonly global::System.Lazy< global::System.Collections.Generic.IReadOnlyDictionary> s_nameToId = new global::System.Lazy< global::System.Collections.Generic.IReadOnlyDictionary>(() => { var lookup = new global::System.Collections.Generic.Dictionary(); {{Tokens.ListOfNamesToIdentifiers}} #if NET8_0_OR_GREATER return global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(lookup); #else return new global::System.Collections.ObjectModel.ReadOnlyDictionary(lookup); #endif }); """); } }