summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/any_helper.py
blob: a087ffdbcda050c16bca61dd453d7a5c43c65419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

ANY_NAMESPACE = 'json_schema_compiler::any'
ANY_CLASS = ANY_NAMESPACE + '::Any'

class AnyHelper(object):
  """A util class that generates code that uses
  tools/json_schema_compiler/any.cc.
  """
  def Init(self, any_prop, src, dst):
    """Initialize |dst|.|any_prop| to |src|.

    src: Value*
    dst: Type*
    """
    if any_prop.optional:
      return '%s->%s->Init(*%s)' % (dst, any_prop.unix_name, src)
    else:
      return '%s->%s.Init(*%s)' % (dst, any_prop.unix_name, src)

  def GetValue(self, any_prop, var):
    """Get |var| as a const Value&.

    var: Any* or Any
    """
    if any_prop.optional:
      return '%s->value()' % var
    else:
      return '%s.value()' % var