locbased.h revision c73f511526464f8e56c242df80552e9b0d94ae3d
1/*
2**********************************************************************
3* Copyright (c) 2004-2014, International Business Machines
4* Corporation and others.  All Rights Reserved.
5**********************************************************************
6* Author: Alan Liu
7* Created: January 16 2004
8* Since: ICU 2.8
9**********************************************************************
10*/
11#ifndef LOCBASED_H
12#define LOCBASED_H
13
14#include "unicode/locid.h"
15#include "unicode/uobject.h"
16
17/**
18 * Macro to declare a locale LocaleBased wrapper object for the given
19 * object, which must have two members named `validLocale' and
20 * `actualLocale'.
21 */
22#define U_LOCALE_BASED(varname, objname) \
23  LocaleBased varname((objname).validLocale, (objname).actualLocale);
24
25U_NAMESPACE_BEGIN
26
27/**
28 * A utility class that unifies the implementation of getLocale() by
29 * various ICU services.  This class is likely to be removed in the
30 * ICU 3.0 time frame in favor of an integrated approach with the
31 * services framework.
32 * @since ICU 2.8
33 */
34class U_COMMON_API LocaleBased : public UMemory {
35
36 public:
37
38    /**
39     * Construct a LocaleBased wrapper around the two pointers.  These
40     * will be aliased for the lifetime of this object.
41     */
42    inline LocaleBased(char* validAlias, char* actualAlias);
43
44    /**
45     * Construct a LocaleBased wrapper around the two const pointers.
46     * These will be aliased for the lifetime of this object.
47     */
48    inline LocaleBased(const char* validAlias, const char* actualAlias);
49
50    /**
51     * Return locale meta-data for the service object wrapped by this
52     * object.  Either the valid or the actual locale may be
53     * retrieved.
54     * @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE
55     * @param status input-output error code
56     * @return the indicated locale
57     */
58    Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
59
60    /**
61     * Return the locale ID for the service object wrapped by this
62     * object.  Either the valid or the actual locale may be
63     * retrieved.
64     * @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE
65     * @param status input-output error code
66     * @return the indicated locale ID
67     */
68    const char* getLocaleID(ULocDataLocaleType type, UErrorCode& status) const;
69
70    /**
71     * Set the locale meta-data for the service object wrapped by this
72     * object.  If either parameter is zero, it is ignored.
73     * @param valid the ID of the valid locale
74     * @param actual the ID of the actual locale
75     */
76    void setLocaleIDs(const char* valid, const char* actual);
77
78    /**
79     * Set the locale meta-data for the service object wrapped by this
80     * object.
81     * @param valid the ID of the valid locale
82     * @param actual the ID of the actual locale
83     */
84    void setLocaleIDs(const Locale& valid, const Locale& actual);
85
86 private:
87
88    char* valid;
89
90    char* actual;
91};
92
93inline LocaleBased::LocaleBased(char* validAlias, char* actualAlias) :
94    valid(validAlias), actual(actualAlias) {
95}
96
97inline LocaleBased::LocaleBased(const char* validAlias,
98                                const char* actualAlias) :
99    // ugh: cast away const
100    valid((char*)validAlias), actual((char*)actualAlias) {
101}
102
103U_NAMESPACE_END
104
105#endif
106